# 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:

```typescript
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:

```typescript
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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://fusee.gitbook.io/mx-sdk-as/how-to/modules.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
