From 945b592479fa9391367f093cefcb3212ac0ae120 Mon Sep 17 00:00:00 2001 From: thurendous Date: Thu, 12 Sep 2024 11:37:11 +0900 Subject: [PATCH] add fuzz test --- .gitignore | 1 + .vscode/settings.json | 3 +- foundry.toml | 6 +-- test/fuzz/FuzzVotingPowerExchange.t.sol | 60 +++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index c524b01..6628cca 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ docs/ # Dotenv file .env +.DS_Store diff --git a/.vscode/settings.json b/.vscode/settings.json index 696f9c5..a5c08f7 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -19,5 +19,6 @@ "titleBar.inactiveForeground": "#15202b99" }, "peacock.color": "#7db3df", - "editor.defaultFormatter": "JuanBlanco.solidity" + "solidity.packageDefaultDependenciesDirectory": "lib", + "solidity.packageDefaultDependenciesContractsDirectory": "src" } diff --git a/foundry.toml b/foundry.toml index 8ac4e55..e216b21 100644 --- a/foundry.toml +++ b/foundry.toml @@ -3,9 +3,6 @@ src = "src" out = "out" libs = ["lib"] -[fuzz] -runs = 4096 - ffi = true ast = true build_info = true @@ -13,4 +10,7 @@ extra_output = ["storageLayout"] solc = "0.8.24" remappings = ["forge-std/=lib/forge-std/src/"] +[fuzz] +runs = 4096 + # See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options diff --git a/test/fuzz/FuzzVotingPowerExchange.t.sol b/test/fuzz/FuzzVotingPowerExchange.t.sol index 9b1c406..bd1dcd7 100644 --- a/test/fuzz/FuzzVotingPowerExchange.t.sol +++ b/test/fuzz/FuzzVotingPowerExchange.t.sol @@ -136,6 +136,33 @@ contract VotingPwoerExchangeTest is Test { assertEq(govToken.balanceOf(participant2), exchangedVotingPower); } + function testExchangeWithAnyAmountWithinNewCapWillSucceed(uint256 amount) public { + // mint 75240 utility token to the participant2 + vm.startPrank(minter); + utilityToken.mint(participant2, 92675 * 1e18); + vm.stopPrank(); + + // set the cap to 49e18 + vm.prank(manager); + votingPowerExchange.setVotingPowerCap(110e18); + vm.stopPrank(); + // start testing + amount = bound(amount, 1e18, 92675 * 1e18); + bytes32 nonce = bytes32(0); + uint256 expiration = block.timestamp + 1000000000; + (bytes memory signature,) = helper.generateSignatureFromPrivateKey( + dc.DEFAULT_ANVIL_KEY2(), amount, nonce, expiration, address(votingPowerExchange) + ); + vm.startPrank(exchanger); + votingPowerExchange.exchange(participant2, amount, nonce, expiration, signature); + vm.stopPrank(); + // check the balance of the participant2 + assertEq(utilityToken.balanceOf(participant2), 102675 * 1e18 - amount); // he got 10000 token in advance + uint256 exchangedVotingPower = votingPowerExchange.calculateIncrementedVotingPower(amount, 0); + // check the balance of the govToken + assertEq(govToken.balanceOf(participant2), exchangedVotingPower); + } + function testExchangeWithAnyRoleWhoIsNotExchangerWillRevert(address anyRole) public { vm.assume(anyRole != exchanger); bytes32 nonce = bytes32(0); @@ -170,6 +197,39 @@ contract VotingPwoerExchangeTest is Test { vm.stopPrank(); } + function testExchangeWithAnyExpirationWhichIsExpiredWillRevert(uint256 expiration) public { + vm.warp(1641070800); + expiration = bound(expiration, 0, block.timestamp - 1); + bytes32 nonce = bytes32(0); + (bytes memory signature,) = helper.generateSignatureFromPrivateKey( + dc.DEFAULT_ANVIL_KEY2(), 1_100 * 1e18, nonce, expiration, address(votingPowerExchange) + ); + + vm.startPrank(exchanger); + vm.expectRevert(VotingPowerExchange.VotingPowerExchange__SignatureExpired.selector); + votingPowerExchange.exchange(participant2, 1_100 * 1e18, nonce, expiration, signature); + vm.stopPrank(); + } + + function testExchangeWithAnyExpirationWhichIsNotExpiredWillSucceed(uint256 expiration) public { + vm.warp(100); + vm.assume(expiration > block.timestamp); + bytes32 nonce = bytes32(0); + (bytes memory signature,) = helper.generateSignatureFromPrivateKey( + dc.DEFAULT_ANVIL_KEY2(), 1_100 * 1e18, nonce, expiration, address(votingPowerExchange) + ); + vm.startPrank(exchanger); + votingPowerExchange.exchange(participant2, 1_100 * 1e18, nonce, expiration, signature); + vm.stopPrank(); + + // check the balance of the participant2 + assertEq(utilityToken.balanceOf(participant2), 10_000 * 1e18 - 1_100 * 1e18); // he got 10000 token in advance + uint256 exchangedVotingPower = votingPowerExchange.calculateIncrementedVotingPower(1_100 * 1e18, 0); + // check the balance of the govToken + assertEq(govToken.balanceOf(participant2), exchangedVotingPower); + } + + /////////////////////////// ///// Other functions ///// /////////////////////////// /// The `setVotingPowerCap` function