Last updated
Last updated
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.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:
In this example, MyModule
is an abstract class that extends ContractBase
. It includes a method myEndpoint()
which you can implement with the desired functionality.
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:
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.
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.