💻
mx-sdk-as
  • MultiversX WASM AssemblyScript (Proof of concept)
  • The Crowdfunding Smart Contract
    • The Crowdfunding Smart Contract (part 1)
    • The Crowdfunding Smart Contract (part 2)
  • The Flip Smart Contract
    • The Flip Smart Contract : Introduction
    • The Flip Smart Contract part 1 : Setting up the development environment
    • The Flip Smart Contract part 2 : Think about the contract and the game mechanisms
    • The Flip Smart Contract part 3 : Setting up the project
    • The Flip Smart Contract part 4 : Writing the contract storage
    • The Flip Smart Contract part 5 : Contract administration
    • The Flip Smart Contract part 6 : Bet logic
    • The Flip Smart Contract part 7 : Make the flip
    • The Flip Smart Contract part 8 : Bounty endpoint
    • The Flip Smart Contract part 9 : Testing the contract
  • 💡How to
    • Model classes
    • Enums
    • Modules
Powered by GitBook
On this page
  • Module Definition (module.ts)
  • Main Contract (index.ts)
  • Working with Multiple Modules
  1. How to

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.

PreviousEnums

Last updated 1 year ago

💡