Prerequisite: Read “Create Core Contract” before creating a Module.
For assistance, refer to the Foundry installation guide.
Add the Thirdweb modular contracts to
foundry.tomlunderremappings:Create a new file called
CounterModule.solin thesrcfolder and start with the following code:Note
TheModulecontract is the base contract that needs to be inherited for this contract to be recognized as a Module Contract, and we need to implement thegetModuleConfigfunction to prevent the contract to be marked as abstract.Create a library called
CounterStorageresponsible for holding the state of the Module Contract:Note
The libraryCounterStorageuses the ERC-7201: Namespace storage layout to store the data. Learn more about ERC-7201.Set up the function
_counterStorageto access the storage from theCounterStoragelibrary: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 Solidityfallbackfunction.Set up a callback function
beforeIncrementthat increases the given count bystep:Note
Callback functions are hook-like functionalities that can be used before or after the main functionality of a core contract. In this snippet, thebeforeIncrementcallback 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.