SFS - Assign a contract to an existing NFT with Hardhat
Hardhat is an Ethereum development environment offering tools for smart contract development, and deployment. It supports Solidity, features a testing framework, and allows task automation. With a modular architecture and extensive plugin support,
In this tutorial, youâll learn how to register our smart contract to the Mode SFS contract by assigning our contract to an existing SFS NFT.
Prerequisites
Before you continue with this tutorial you should have:
Ethereum wallet preferably Metamask Installed.
Basic understanding of Solidity.
Sepolia Eth with Mode Network.
An SFS NFT token Id.
What is SFS(Sequencer fee sharing)
Modeâs SFS is one of the primary features of the Mode network, it lets developers get some percentage of the transaction fees from their contracts' activities on the Mode network. For this to work, your smart contract has to be registered to Modeâs SFS contract. Once registered, you get an ERC 721 NFT (Non-Fungible Token) which allows you to claim your share of the transaction fees generated from your contract.
The SFS NFT is transferable and can be used to claim rewards for more than one contract, so you can either get a new NFT for multiple smart contracts deployed on Mode, or you can assign multiple smart contracts to an already existing SFS NFT; which is what weâll be learning about in this tutorial.
You can explore the full SFS contract code here.
Assigning smart contract to an existing SFS NFT
Letâs take a look at the assign function in the SFS contract to understand how the assigning works.
Note that this is only a portion of the SFS contract, check here to see the complete code.
This function receives _tokenId as an input param. It then checks if the tokenId is valid before emitting an Assign event, after which the msg.sender (our smart contract) will be assigned to the tokenId in the SFS, updating the feeRecipient mapping with relevant information.
Here are a few things to note;
The assign function is specified public, meaning an external smart contract can call it.
The msg.sender is assumed to be a smart contract address because this function is expected to be called by a smart contract.
The assign function does not mint an ownership NFT, rather, it assigns your smart contract to an existing SFS NFT.
Assigning a smart contract to an SFS NFT can only be done once.
Now that weâve understood how the assign function works, letâs set up our project and build our smart contract.
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).
Select "Yes" (or y) to install the project's dependencies.
2. Writing our smart contract
Head over to the contracts folder of your project, for the sake of this tutorial we will be making use of the Lock contract provided by Hardhat, and weâll be making some modifications to it.
First, we added the SFS contract to our file with a register function. Then in the constructor, we invoked the assign function by initializing a new instance of our SFS contract and calling the assign method on it.
Mode SFS contact has a different address for Mainnet and Testnet and for this tutorial, weâre deploying to Testnet so weâre going to register to the Testnetâs SFS contract.
Mainnet SFS Address: 0x8680CEaBcb9b56913c519c069Add6Bc3494B7020
Testnet SFS Address: 0xBBd707815a7F7eb6897C7686274AFabd7B579Ff6
3. Configuring harhat for Mode
Head to the hardhat.config.ts file and replace it with this code to set Mode Network Testnet. Remember to change the account's value to your Metamask private key.
For deploying on Mainnet replace the url and chainId to:
url: https://mainnet.mode.network/
chainId: 34443
4. Compiling our smart 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).
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 0x6900B6158C1BD0C61G1E6a191B1f822EA000E2e8
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 assigned to the NFT tokenId. To verify if you assigned your contract correctly, you can check our âCheck the registryâ guide. To import the NFT to your wallet, check our Adding the SFS NFT to your wallet tutorial.
Last updated