Register contract for SFS with Thirdweb
Introduction to Thirdweb and Sequencer Fee Sharing (SFS)
Thirdweb is a web3 platform that offers developers tools to build, deploy, launch, and manage their apps and games. These tools include pre-built smart contracts, SDKs, and APIs.
In this tutorial, you'll learn how to register a smart contract with AppChain’s SFS (Sequencer Fee Sharing) Contract while deploying with Thirdweb.
Prerequisites
Before starting this tutorial, make sure you:
- Have a Thirdweb account.
- Understand the basics of Solidity.
- Bridge Sepolia ETH to AppChain Testnet.
- Have Node.js and Yarn installed and updated.
If you want to skip to the code, click here.
What is Sequencer Fee Sharing (SFS)?
Imagine this: When you deploy your smart contract on AppChain, you earn a percentage of the transaction fees from your contract. This means you'll get a share of the fees from transactions on your contract, rewarding your contribution to the network and encouraging more innovation and use of your contract.
How It Works
To get your share of the fees, you need to register your contract with AppChain's Registry/SFS contract. When you do this, the Registry contract gives you an NFT, which lets you claim your earned fees. The registration call must come from your contract.
An off-chain component handles the transactions and calculates your fee share. Your NFT allows you to claim your accumulated fees from the SFS contract after a two-week period.
How the SFS Register Function Works
The call to register with the SFS must come directly from the smart contract you want to register. Here's the register function:
function register(address _recipient) public onlyUnregistered returns (uint256 tokenId) {
address smartContract = msg.sender;
if (_recipient == address(0)) revert InvalidRecipient();
tokenId = _tokenIdTracker.current();
_mint(_recipient, tokenId);
_tokenIdTracker.increment();
emit Register(smartContract, _recipient, tokenId);
feeRecipient[smartContract] = NftData({
tokenId: tokenId,
registered: true,
balanceUpdatedBlock: block.number
});
}
This function takes "_recipient" as input and mints an NFT to this recipient once your smart contract is registered. The msg.sender
is the contract being registered, and the mapping is updated with relevant details.
Key Points
- The function is public, so external contracts can call it.
msg.sender
is assumed to be a smart contract address.- The function mints an NFT allowing the owner to claim fees earned by the registered contract.
The Register Function in Practice
Here’s an example AppToken
contract to help understand it better:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@thirdweb-dev/contracts/base/ERC20Base.sol";
contract Register {
function register(address _recipient) public returns (uint256 tokenId) {}
}
contract AppToken is ERC20Base {
constructor(
address _defaultAdmin,
string memory _name,
string memory _symbol
)
ERC20Base(
_defaultAdmin,
_name,
_symbol
)
{
Register sfsContract = Register(0xBBd707815a7F7eb6897C7686274AFabd7B579Ff6);
sfsContract.register(msg.sender);
}
}
In this example, we used a simple ERC20 Base contract provided by Thirdweb. Our contract inherits from the ERC20 Base contract and calls the register function with the sender’s address msg.sender
from the constructor.
Key Points
- The code defines two contracts: Register and Contract.
- Contract inherits from Thirdweb’s ERC20 Base contract.
- An instance of the Register contract calls its register function with the sender’s address.
Deploying Our Contract to Thirdweb
Let's go through the process of installing the CLI and deploying your contract. If you run into issues, visit the official documentation.
1. Download the ThirdWeb CLI
To install Thirdweb globally, open your terminal and run:
npm i -g thirdweb
Check the installation with
thirdweb --version
2. Create local environment
Create a new project by running:
npx thirdweb create contract
Choose from the menu options to set up your project. For this tutorial, we'll use an ERC20 contract, but you can choose an empty contract and write your own code.
3. Deploy
Before deploying, Thirdweb requires an API key to monitor your smart contracts. Check out the tutorial on creating a Thirdweb API Key to get started.
Step 1: Deploy Your Contract
Navigate to your project's root directory and run:
npx thirdweb deploy
You'll be directed to your Thirdweb dashboard. Fill in the parameters for your token's name and symbol, and select AppChain Testnet as your network.
Step 2: Complete Deployment
Click "deploy" and sign the transaction in Metamask. If everything goes well, you'll see a "Success" message.
Congratulations! You've deployed and registered your contract. From your dashboard, you can view your code, monitor contract events, and much more. Your contract should now be registered, and the recipient address should have the minted NFT.