Modules

Separating your smart contract's logic into different modules can enhance the modularity and maintainability of your code. The @module annotation is used to define classes that encapsulate specific functionalities or sets of functionalities within your contract. These modules can then be extended by the main contract class, which is annotated with @contract. This approach allows you to compose your contract from modular, reusable components.

Here’s how you can structure your contract using modules:

Module Definition (module.ts)

Your module file, module.ts, will contain the module class. This class is marked with @module and extends ContractBase, which provides the foundational functionalities of a contract module:

import { ContractBase } from '@gfusee/mx-sdk-as'; // Ensure to import ContractBase from the correct library/path

@module
export abstract class MyModule extends ContractBase {
    myEndpoint(): void {
        // Implementation of your endpoint logic
    }
}

In this example, MyModule is an abstract class that extends ContractBase. It includes a method myEndpoint() which you can implement with the desired functionality.

Main Contract (index.ts)

The main contract file, index.ts, will extend your module. This root contract is where you define the entry point of your smart contract:

import { MyModule } from './module'; // Import the module you've created

@contract
abstract class MyContract extends MyModule {
    // Your contract's specific logic can be added here
}

MyContract is annotated with @contract, indicating that it's the main contract class. It extends MyModule, thereby inheriting its properties and methods, such as myEndpoint. You can add additional logic, state variables, and methods to MyContract as needed, or keep it lean if all the required logic is encapsulated within the modules.

Working with Multiple Modules

You can create multiple modules to separate different aspects of your contract’s logic. Each module should extend ContractBase and can be extended by either the main contract or other modules. However, the final contract class (annotated with @contract) should be a coherent composition of these modules, extending directly from one of them or chaining multiple modules together.

Last updated