1/5
## Rebase Token Tests pt. 1 We will start by creating a new file called `RebaseToken.t.sol`: ```javascript SPDX-License-Identifier: MIT pragma solidity ^0.8.24; import "forge-std/Test.sol"; import "forge-std/console.sol"; import "./src/RebaseToken.sol"; import "./src/Vault.sol"; import "./src/interfaces/IRebaseToken.sol"; contract RebaseTokenTest is Test { RebaseToken private rebaseToken; Vault private vault; function setup() public { vm.startPrank(owner); rebaseToken = new RebaseToken(); vault = new Vault(IRebaseToken(address(rebaseToken))); rebaseToken.grantMintAndBurnRole(address(vault)); bool success = payable(address(vault)).call{value: 1e18}(""); vm.stopPrank(); } address public owner = makeAddr("owner"); address public user = makeAddr("user"); function testDepositLinear() public { // 1. deposit vm.startPrank(user); uint256 amount = bound(1e18, 1e18 * 5, type(uint256).max); vm.deal(user, amount); payable(address(vault)).call{value: amount}(""); // 2. check our rebase token balance // 3. warp the time and check the balance again // 4. warp the time again by the same amount and check the balance again vm.stopPrank(); } } ``` We will then create a function called `testDepositLinear`. We will use `vm.startPrank` to run the function in the context of the user. We will then define the `amount` and use `vm.deal` to give ETH to the user. Then we will call the `call` function with the `amount` of ETH. The next step is to warp the time and check the user's rebase token balance again. We will also warp the time again and check the balance a third time. We will then check the difference between the balances each time. The goal is to ensure that the interest is linear and that the amount of interest accrued is constant. We will use the `bound` function in our tests to ensure that the `amount` we use is not too large. Finally, we will wrap our tests with `vm.stopPrank`. ```javascript function testDepositLinear() public { // 1. deposit vm.startPrank(user); uint256 amount = bound(1e18, 1e18 * 5, type(uint256).max); vm.deal(user, amount); payable(address(vault)).call{value: amount}(""); // 2. check our rebase token balance // 3. warp the time and check the balance again // 4. warp the time again by the same amount and check the balance again vm.stopPrank(); } ``` It is good practice to also run fuzz tests and integration tests, in addition to unit tests.
A comprehensive guide to creating a rebase token test in Solidity using Foundry. The lesson covers the process of creating a new test file, importing dependencies, writing a basic setup function, and finally deploying the test. It then demonstrates how to interact with the rebase token to create a deposit and redeem. This lesson also introduces fuzz testing to ensure that the rebase token functions correctly under a wide range of inputs.
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