Assign a smart contract to an existing SFS NFT with Thirdweb deployment

Thirdweb is a web3 platform that provides developers with tools to build, deploy, launch, and manage their applications and games. Some of these tools include pre-built smart contracts, SDKs, and APIs.

In this tutorial, you’ll learn how to assign a smart contract to Mode Network’s SFS (Sequencer Fee Sharing) Contract by deploying with thirdweb.

Prerequisites

Before you continue with this tutorial you should:

  1. Metamask Account.

  2. Latest version of Node and Yarn

  3. A thirdweb account.

  4. Basic understanding of Solidity.

  5. Bridge Sepolia Eth with Mode Network.

  6. An SFS NFT token Id.

Sequencer Fee Sharing (SFS) stands out as a pivotal feature within the Mode network, empowering developers to earn a percentage of transaction fees originating from their smart contracts’ activities. This section guides you through the process of leveraging SFS for your smart contract within the Mode ecosystem.

Registration and ERC 721 NFT

To kickstart the fee-sharing mechanism, your smart contract needs to be registered with Mode’s SFS contract. This registration is the gateway to unlocking the benefits of fee-sharing. Upon successful registration, you’ll receive an ERC 721 Non-Fungible Token (NFT). This unique token serves as your exclusive claim to transaction fees generated by your contract.

Transferable and Multipurpose SFS NFT

The SFS NFT isn’t limited to a single smart contract. It’s both transferable and versatile, allowing you to utilize it across multiple contracts or share it between existing contracts. Whether you deploy new smart contracts on Mode or manage multiple contracts under a single SFS NFT, this flexibility caters to your specific needs.

Fee Processing and Accumulation Period

As your smart contract operates within the Mode network, an off-chain component takes charge of processing transactions and calculating your share of fees. It’s important to be aware of a two-week accumulation period during which fees accrue before being transferred to the SFS contract.

Claiming Rewards

Once the fees are successfully transferred to the SFS contract, you can easily claim your accumulated rewards. This process not only acknowledges your contribution to the network but also aligns with Mode’s commitment to incentivizing innovation and utilization of smart contracts within the ecosystem.

Explore the SFS Contract Code

For a deeper understanding and exploration of the SFS contract code, please refer to the [complete SFS contract code here](link-to-SFS-contract-code). Sequencer Fee Sharing is designed to streamline and enhance your experience as a developer on Mode Network, ensuring that you’re duly rewarded for your contributions.

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 assignment works.

Note that this is only a portion of the SFS contract, check here to see the complete code.

function assign(uint256 _tokenId) public onlyUnregistered returns (uint256) {        address smartContract = msg.sender;        if (!_exists(_tokenId)) revert InvalidTokenId();        emit Assign(smartContract, _tokenId);        feeRecipient[smartContract] = NftData({            tokenId: _tokenId,            registered: true,            balanceUpdatedBlock: block.number        });        return _tokenId;    }
Enter fullscreen mode
Exit fullscreen mode

This function takes _tokenId as an input parameter. Subsequently, it verifies the validity of the tokenId before emitting an Assign event. Following this event, the msg.sender (our smart contract) gets associated with the tokenId in the SFS. It updates the feeRecipient mapping with relevant information.

Here are a few points to keep in mind:

  • The assign function is declared as public, allowing an external smart contract to invoke it.

  • The assumption is that msg.sender represents a smart contract address, as this function is intended to be called by a smart contract.

  • The assign function does not generate an ownership NFT; instead, it links your smart contract to an existing SFS NFT.

  • The process of assigning a smart contract to an SFS NFT is a one-time operation.

Now that we comprehend the workings of the assign function, let’s proceed to set up our project and construct our smart contract.

To set up our project, we need to set up a few things.

1. Download the ThirdWeb CLI

To install thirdweb globally, open your terminal and run

npm i -g thirdweb

Now try running:

thirdweb --version

If the CLI is installed correctly you will be prompted to go to the installed version

2. Create a Local environment

Create a new project by running

npx thirdweb create contract

You will be prompted with a menu with different options for setting up your project. We will select ‘Empty Contract’ and write our Smart Contract Code from scratch.

Once thirdweb is done building our project, we’ll run

cd project-name && code .

To open with vscode.

3. Writing Our Smart Contract

Navigate to the “contracts” directory within your project. Specifically for this tutorial, we will be working with a straightforward Hello World Contract.

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.

We’ll create a new file and call it: Contract.sol

And paste the below code into it.

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9; //Here we created our SFS contract contract SFS {
   function assign(uint256 _tokenId) public returns (uint256) {}
} contract HelloWorld {
   string public greeting;
   constructor() {
       greeting = "Hello, World!";
       SFS sfsContract = SFS(0xBBd707815a7F7eb6897C7686274AFabd7B579Ff6);
       sfsContract.assign(45);
   }    function getGreeting() public view returns (string memory) {
       return greeting;
   }    function setGreeting(string memory newGreeting) public {
       greeting = newGreeting;    }
}
Enter fullscreen mode
Exit fullscreen mode

Remember to change the ID (45) inside of sfsContract.assign(45) to the ID of your NFT. Save your file.

NOTE:

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

Thirdweb requires an API key to monitor your smart contracts, check out this tutorial on creating a Thirdweb API Key to get it done.

Navigate to the project root and run either of the following commands:

yarn deploy

or

npx thirdweb deploy

You will then be directed to your thirdweb dashboard.

Select Mode Testnet as the network of your choice.

STEP 2

Choose Mode Testnet as the network, click on deploy, confirm and then sign the transaction from Metamask to complete it.

If successful, you’ll see a “Success” message.

Congratulations! You’ve deployed and assigned your contract to an existing NFT with Thirdweb. Feel free to explore your dashboard for code details, contract events, and more.


Discover more from Coursity

Subscribe to get the latest posts to your email.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *