Skip to main content

Contract inheritance

In Massa smart contracts, you can leverage AssemblyScript's standard import/export structure to inherit functions and properties from other contracts or modules. This enables the creation of modular and reusable code by defining common functionalities in base contracts, which can be extended by specialized contracts.

By importing functions and classes from other files, you can easily reuse code across different contracts. This approach is especially useful for building complex contracts that share similar logic or features, such as tokens, where common functionalities can be inherited and extended.

Example: Token contract with inheritance

This example showcases a token contract that follows the Massa Fungible Token (FT) standard, leveraging the base functionalities provided by the FT standard library. The full FT standard implementation is available in the Massa Standards Repository

By using inheritance, you can easily create a new token with customized properties while reusing the FT standard functions.

import { Args } from '@massalabs/as-types';
import { u256 } from 'as-bignum/assembly';
export * as FT from '@massalabs/sc-standards/assembly/contracts/FT/token';

export function constructor(_: StaticArray<u8>): StaticArray<u8> {
const token_name = "My Token";
const token_symbol = "MTK";
const decimals: u8 = 18;
const totalSupply = u256.fromU64(123456);

FT.constructor(
new Args()
.add(token_name)
.add(token_symbol)
.add(decimals)
.add(totalSupply)
.serialize(),
);
}

Why use inheritance with the FT standard?

Using the FT standard as a base allows you to inherit its well-defined functionalities without needing to reimplement core token mechanics. This approach ensures that your token will be compatible with existing Massa applications and services that follow the FT standard, facilitating interoperability and reliability.

Customization with Inheritance

Extending the FT standard allows you to add new functions or modify existing ones, tailoring the token contract to your specific needs. For example, you could implement custom transfer logic, introduce new events, or include access control mechanisms for minting and burning tokens.