Test
1.Manager User
- At first we create a Struct "Users" and the same name variable to store the data
struct Users {
address payable admin;
address payable alice;
address payable broker;
//...
}
Users internal users;
- In the setUp() function we initialize the necessary roles
function setUp() public {
users = Users({
admin:createUser("admin");
alice:CreateUser("alice");
...
})
}
/// @dev Generates a user, labels its address, and funds it with test assets.
function createUser(string memory name) internal returns (address payable) {
address payable user = payable(makeAddr(name));
vm.deal({ account: user, newBalance: 100 ether });
deal({ token: address(dai), to: user, give: 1_000_000e18 });
deal({ token: address(usdt), to: user, give: 1_000_000e18 });
return user;
}
Refer: https://twitter.com/RightNowIn/status/1761094201071002000
https://rareskills.io/post/invariant-testing-solidity https://twitter.com/DevDacian/status/1732269870014992761 https://twitter.com/DevDacian/status/1758068540756881845 https://twitter.com/DevDacian/status/1732645103867773236 https://twitter.com/DevDacian/status/1747565664506909178 https://twitter.com/DevDacian/status/1733009929508917499 https://paco0x.org/foundry-invariant-test/
2.Tests Proxy Contracts
https://twitter.com/RightNowIn/status/1748048778467140053
3. Fork Test
How to fork
First Go to Alchemy or QucikNode or whaterver you like, get your api
Open the terminal:
anvil --fork-url YOUR_ENDPOINT_URL --fork-block-number 19000000
then you got everything you need
4.Display amounts in exponential form.
function testDisplay() public {
uint256 balances = 566645678676;
console.log("My Balance is :", balances);
console.log("My Balance is %e", balances);
}
Output:
[PASS] testDisplay() (gas: 4103)
Logs:
My Balance is : 566645678676
My Balance is 5.66645678676e11
5.Colorful Logging
function testColor() public {
console.log("Color:", StdStyle.yellow("Hello world"));
console.log("Color:", StdStyle.blue("Hello world"));
console.log("Color:", StdStyle.green("Hello world"));
console.log("Color:", StdStyle.red("Hello world"));
}
6.Using Debug and Inspect understand what EVM do during Code execute
forge inspect 0.sol:ContractName ir > out.yul
or use Debug
forge test --match-test test_XXX --debug