Skip to content

Commit

Permalink
add fuzz test
Browse files Browse the repository at this point in the history
  • Loading branch information
thurendous committed Sep 12, 2024
1 parent 3e21aa7 commit 945b592
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ docs/

# Dotenv file
.env
.DS_Store
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
"titleBar.inactiveForeground": "#15202b99"
},
"peacock.color": "#7db3df",
"editor.defaultFormatter": "JuanBlanco.solidity"
"solidity.packageDefaultDependenciesDirectory": "lib",
"solidity.packageDefaultDependenciesContractsDirectory": "src"
}
6 changes: 3 additions & 3 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ src = "src"
out = "out"
libs = ["lib"]

[fuzz]
runs = 4096

ffi = true
ast = true
build_info = true
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
60 changes: 60 additions & 0 deletions test/fuzz/FuzzVotingPowerExchange.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 945b592

Please sign in to comment.