1/5
# Writing the Token Code Okay, so now that we've explained what we're going to do, let's go ahead and do it. We need to create a file, which we will call `RebaseToken.sol`. Then, we'll add the SPDX license identifier: ``` // SPDX-License-Identifier: MIT ``` Next, we will add the solidity version. ``` pragma solidity ^0.8.24; ``` Then, we create our contract: ``` contract RebaseToken { } ``` Now, the next thing we're going to do is install OpenZeppelin because we are going to use the ERC20 contract from OpenZeppelin. So we can run the following command to install the dependency: ```bash forge install openzeppelin/openzeppelin-contracts@v5.1.0 --no-commit ``` It is always a good idea to get the exact path to the dependency we are going to install, so let's search in the browser by searching for "openzeppelin github". This will take us to their github page, and we can find the latest release of the library, which is 5.1.0. Once we have the correct version and path, we can add it to our rebase token. Then, we are going to include our named import. ``` import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol"; ``` Now we need to make sure that we also set up our `foundry.toml` file with remappings: ``` remappings = [ "@openzeppelin/=lib/openzeppelin-contracts/" ] ``` So we can now inherit from the ERC20 contract: ``` contract RebaseToken is ERC20 { } ``` Now that we are inheriting from this ERC20 contract, we can create a constructor. ``` constructor() ERC20("Rebase Token", "RBT") { } ``` We are also going to add a few nat spec comments. ``` /* * @title RebaseToken * @dev A simple ERC20 token with a name, symbol, and 18 decimals. */ ``` ``` /* * @notice Mint the user tokens when they deposit into the vault * @param _to The user to mint the tokens to * @param _amount The amount of tokens to mint */ ``` ``` /* * @notice Calculate the interest that has accumulated since the last update * @param _user The user to calculate the balance for * @return The balance of the user including the interest that has accumulated in the time since the balance was last updated. */ ``` ``` /* * @dev The interest rate can only decrease */ ``` Next, we are going to create the function that sets the interest rate. ``` function setInterestRate(uint256 _newInterestRate) external { // Set the interest rate } ``` Now we also want to be able to mint the accrued interest for each user. So we can create an internal function to calculate the interest that has accrued. ``` function _mintAccruedInterest(address _user) internal view { // find their current balance of rebase tokens that have been minted to the user // calculate their current balance including any interest } ``` We will need a function for a balance of. ``` function balanceOf(address user) public view virtual returns (uint256) { return _balances[account]; } ``` We will also create a function to return the linear interest. ``` function _calculateUserAccumulatedInterestSinceLastUpdate(address _user) internal view returns (uint256) { // get the time since the last update // calculate the interest that has accumulated since the last update // this is going to be linear growth with time //1. calculate the time since the last update //2. calculate the amount of linear growth //3. return the amount of linear growth } ``` Finally, we can add the state variable for the user's interest rate, as well as initialize it. ``` uint256 private constant PRECISION_FACTOR = 1e18; uint256 private s_interestRate = 5e10; mapping (address => uint256) private s_userInterestRate; mapping (address => uint256) private s_lastUpdatedTimestamp; ``` At this point in the video, we have the basic setup of our contract, with OpenZeppelin installed, remappings defined, and our first state and view functions. We are now on our way to completing our rebase token.
A complete guide to creating a rebase token - This lesson covers creating a rebase token smart contract by inheriting the ERC20 contract from OpenZeppelin, setting the global interest rate, creating a mint function to mint accrued interest, and creating a balance of function to calculate the balance of the user, including the interest that has accrued since the last time the user balance was updated.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Advanced smart contract development
How to develop a stablecoin
How to develop a DeFi protocol
How to develop a DAO
Advanced smart contracts testing
Fuzz testing
Manual verification
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Security researcher
$49,999 - $120,000 (avg. salary)
Guest lecturers:
Juliette Chevalier
Lead Developer relations at Aragon
Nader Dabit
Director of developer relations at Avara
Ally Haire
Developer relations at Protocol Labs
Harrison
Founder at GasliteGG
Last updated on March 11, 2025
Solidity Developer
Advanced FoundryDuration: 36min
Duration: 3h 06min
Duration: 5h 02min
Duration: 6h 02min
Duration: 2h 47min
Duration: 1h 23min
Duration: 4h 28min
Duration: 1h 19min
Duration: 1h 10min
Course Overview
About the course
Advanced smart contract development
How to develop a stablecoin
How to develop a DeFi protocol
How to develop a DAO
Advanced smart contracts testing
Fuzz testing
Manual verification
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Security researcher
$49,999 - $120,000 (avg. salary)
Guest lecturers:
Juliette Chevalier
Lead Developer relations at Aragon
Nader Dabit
Director of developer relations at Avara
Ally Haire
Developer relations at Protocol Labs
Harrison
Founder at GasliteGG
Last updated on March 11, 2025
Testimonials
Read what our students have to say about this course.
Chainlink
Chainlink
Gustavo Gonzalez
Solutions Engineer at OpenZeppelin
Francesco Andreoli
Lead Devrel at Metamask
Albert Hu
DeForm Founding Engineer
Radek
Senior Developer Advocate at Ceramic
Boidushya
WalletConnect
Idris
Developer Relations Engineer at Axelar