SFS - Register a Smart Contract with Hardhat
Hardhat is an Ethereum development framework for creating, testing, and deploying smart contracts. It includes a suite of tools that aid in developing smart contracts and managing the complete development process, allowing developers to write scripts, compile, test, and deploy efficiently.
In this tutorial, you will learn how to register a smart contract to Mode Network’s SFS (Sequencer Fee Sharing) Contract with Hardhat.
Prerequisites
Before you continue with this tutorial you should:
Have an Ethereum wallet preferably Metamask Installed.
Have a basic understanding of Solidity.
Bridge Sepolia Eth with Mode Network Bridging to Testnet
If you want to jump straight to the code, go here
What is Sequencer Fee Sharing (SFS)?
The SFS contract allows developers to earn a portion of the transaction fees from transactions running on their contract when deployed and registered on Mode Network. The SFS contract implements this functionality and permits other contracts to register and earn.
To register, your smart contract has to make a call to the SFS contract, after this is done successfully, you will be issued an NFT (Non-Fungible Token). This NFT allows you to claim your fees accumulated in the SFS contract.
How does the SFS Register function work?
Let’s look at the SFS contract's register function to understand how the registration works.
Note that this is only a portion of the SFS contract, check here to see the complete code.
This function receives "_recipient"
as an input and then mints an NFT to this recipient once your smart contract is registered.
Then, the msg.sender is picked up as the contract to register in the SFS updating the mapping with relevant information.
Here are a few things to note:
The Function is specified public meaning an external smart contract can call it.
The msg.sender is assumed to be a smart contract address, ensuring that a smart contract can register a fee recipient.
The Function mints an ownership NFT that allows the owner to claim fees earned by the recipient smart contract.
Now that we’ve understood how the register function works, let’s set up our project.
1. Setting Up Our Project
Open your terminal to create a new directory for your project.
mkdir hardhat
Navigate into your project
cd hardhat
Initialize an npm project - This creates your package.json file.
npm init -y
Install hardhat
npm install --save-dev hardhat
To create your project run
npx hardhat init
When prompted make these selections:
Choose "Create a TypeScript project".
For the .gitignore prompt, select "Yes" (or y).
For installing the project's dependencies select "Yes" (or y).
2. Writing our smart contract
Head to the contracts folder of your project, for the sake of this tutorial we will be making use of the contract provided by Hardhat.
Rename the file names lock.sol to any name of your choice then add the register contract to your file and the register function to your constructor.
3. Configuring Hardhat for Mode
Head to the hardhat.config.ts file and replace it with this code to set Mode Network Testnet.
4. Compiling our contract
To compile the smart contract run
npx hardhat compile
It would return;
Successfully generated 8 typings!
Compiled 1 Solidity file successfully (evm target: london).
It's important that the EVM version is set to london. You might get a "PUSH0" error if you don't set the evm version correctly. You can read more about this issue here.
5. Deploying
To deploy we will make use of the deploy.ts file in the scripts folder, we won’t be changing anything because we didn’t change our smart contract.
To deploy the smart contract to Mode Testnet run
npx hardhat run scripts/deploy.ts --network mode
It returns
Lock with 0.001ETH and unlock timestamp 1702421602 deployed to 0x6900A6158C1BD0C61F1E6a191B1f822EA000E2e8
Congratulations! You just deployed and registered your contract with Hardhat and Typescript.
Check the Explorer
To view your transaction head to Mode Testnet and paste the contract address.
Your contract should be registered and your NFT minted.
To import the NFT to your wallet, check our Adding the SFS NFT to your wallet tutorial.
To learn more about mode and how to turn your code into a business join our Discord and say hi.
To learn more about mode and how to turn your code into a business join our Discord and say hi.
Last updated