Create Module Contract
Prerequisite: Read “Create Core Contract” before creating a Module.
- For assistance, refer to the Foundry installation guide. - Add the Thirdweb modular contracts to - foundry.tomlunder- remappings:
- Create a new file called - CounterModule.solin the- srcfolder and start with the following code:- Note The - Modulecontract is the base contract that needs to be inherited for this contract to be recognized as a Module Contract, and we need to implement the- getModuleConfigfunction to prevent the contract to be marked as abstract.
- Create a library called - CounterStorageresponsible for holding the state of the Module Contract:- Note The library - CounterStorageuses the ERC-7201: Namespace storage layout to store the data. Learn more about ERC-7201.
- Set up the function - _counterStorageto access the storage from the- CounterStoragelibrary:
- Set up fallback functions that act as the setters and getters for - step:- Note Fallback functions are extra functionalities that a core contract can use via the Solidity - fallbackfunction.
- Set up a callback function - beforeIncrementthat increases the given count by- step:- Note Callback functions are hook-like functionalities that can be used before or after the main functionality of a core contract. In this snippet, the - beforeIncrementcallback is used before the main increment functionality.
- Lastly, set up the - getModuleConfigfunctionallity as this is the one which is responsible for communicating to the core contract:
In the next tutorial, learn how to deploy this modular contract and attach it to the Core contract.