Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wcgcyx committed Feb 13, 2024
1 parent ebeb6e0 commit 6d1bcb2
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 43 deletions.
77 changes: 43 additions & 34 deletions test/fuzz/child/ChildERC20Bridge.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ contract ChildERC20BridgeTest is Test, IChildERC20BridgeEvents {
}

function testFuzz_MapToken(address rootToken, string memory name, string memory symbol, uint8 decimals) public {
vm.assume(rootToken > address(10) && bytes(name).length != 0 && bytes(symbol).length != 0 && decimals > 0);
vm.assume(rootToken != bridge.NATIVE_ETH() && rootToken != bridge.NATIVE_IMX() && rootToken != ROOT_IMX_TOKEN);
assumeValidRootToken(rootToken);
vm.assume(bytes(name).length != 0 && bytes(symbol).length != 0 && decimals > 0);

// Map token on L1 triggers call on child bridge.
bytes memory data = abi.encode(MAP_TOKEN_SIG, rootToken, name, symbol, decimals);
Expand All @@ -78,11 +78,10 @@ contract ChildERC20BridgeTest is Test, IChildERC20BridgeEvents {
}

function testFuzz_DepositIMX(address sender, address recipient, uint256 depositAmt) public {
vm.assume(sender > address(10) && recipient > address(10) && depositAmt > 0);
vm.assume(sender.code.length == 0 && recipient.code.length == 0);
vm.assume(recipient.balance == 0);
vm.deal(address(bridge), depositAmt);
assumeValidUsers(sender, recipient);
vm.assume(depositAmt > 0);

vm.deal(address(bridge), depositAmt);
assertEq(address(bridge).balance, depositAmt, "Bridge should have depositAmt of IMX");

// Deposit IMX on L1 triggers call on child bridge.
Expand All @@ -98,11 +97,9 @@ contract ChildERC20BridgeTest is Test, IChildERC20BridgeEvents {
}

function testFuzz_WithdrawIMX(address user, uint256 balance, uint256 gasAmt, uint256 withdrawAmt) public {
vm.assume(user > address(10));
vm.assume(user.code.length == 0);
vm.assume(balance > 0 && withdrawAmt > 0 && gasAmt > 0);
vm.assume(balance < type(uint256).max - gasAmt);
vm.assume(balance > withdrawAmt && balance - withdrawAmt > gasAmt);
assumeValidUser(user);
vm.assume(withdrawAmt > 0 && gasAmt > 0);
vm.assume(balance > withdrawAmt && balance - withdrawAmt > gasAmt && balance < type(uint256).max - gasAmt);

// Fund user
vm.deal(user, balance);
Expand Down Expand Up @@ -135,11 +132,9 @@ contract ChildERC20BridgeTest is Test, IChildERC20BridgeEvents {
}

function testFuzz_WithdrawWIMX(address user, uint256 balance, uint256 gasAmt, uint256 withdrawAmt) public {
vm.assume(user > address(10));
vm.assume(user.code.length == 0);
vm.assume(balance > 0 && withdrawAmt > 0 && gasAmt > 0);
vm.assume(balance < type(uint256).max);
vm.assume(balance > withdrawAmt && balance - withdrawAmt > gasAmt);
assumeValidUser(user);
vm.assume(withdrawAmt > 0 && gasAmt > 0);
vm.assume(balance > withdrawAmt && balance - withdrawAmt > gasAmt && balance < type(uint256).max - gasAmt);

// Fund user
vm.deal(user, balance);
Expand Down Expand Up @@ -184,9 +179,8 @@ contract ChildERC20BridgeTest is Test, IChildERC20BridgeEvents {
}

function testFuzz_DepositETH(address sender, address recipient, uint256 depositAmt) public {
vm.assume(sender > address(10) && recipient > address(10) && depositAmt > 0);
vm.assume(sender.code.length == 0 && recipient.code.length == 0);
assertEq(IChildERC20(bridge.childETHToken()).balanceOf(recipient), 0, "Recipient should have 0 ETH");
assumeValidUsers(sender, recipient);
vm.assume(depositAmt > 0);

// Deposit ETH on L1 triggers call on child bridge.
bytes memory data = abi.encode(DEPOSIT_SIG, bridge.NATIVE_ETH(), sender, recipient, depositAmt);
Expand All @@ -204,11 +198,9 @@ contract ChildERC20BridgeTest is Test, IChildERC20BridgeEvents {
}

function testFuzz_WithdrawETH(address user, uint256 balance, uint256 gasAmt, uint256 withdrawAmt) public {
vm.assume(user > address(10));
vm.assume(user.code.length == 0);
vm.assume(balance > 0 && withdrawAmt > 0 && gasAmt > 0);
vm.assume(balance < type(uint256).max);
vm.assume(balance > withdrawAmt);
assumeValidUser(user);
vm.assume(withdrawAmt > 0 && gasAmt > 0);
vm.assume(balance > withdrawAmt && balance - withdrawAmt > gasAmt && balance < type(uint256).max - gasAmt);

// Fund user
vm.deal(user, gasAmt);
Expand Down Expand Up @@ -246,11 +238,9 @@ contract ChildERC20BridgeTest is Test, IChildERC20BridgeEvents {
}

function testFuzz_DepositERC20(address rootToken, address sender, address recipient, uint256 depositAmt) public {
vm.assume(
rootToken > address(10) && rootToken != bridge.NATIVE_ETH() && rootToken != bridge.NATIVE_IMX()
&& rootToken != ROOT_IMX_TOKEN
);
vm.assume(sender > address(10) && recipient > address(10) && depositAmt > 0);
assumeValidRootToken(rootToken);
assumeValidUsers(sender, recipient);
vm.assume(depositAmt > 0);

// Map
bytes memory data = abi.encode(MAP_TOKEN_SIG, rootToken, "Test token", "Test", 18);
Expand Down Expand Up @@ -280,11 +270,10 @@ contract ChildERC20BridgeTest is Test, IChildERC20BridgeEvents {
uint256 gasAmt,
uint256 withdrawAmt
) public {
vm.assume(rootToken != bridge.NATIVE_ETH() && rootToken != bridge.NATIVE_IMX() && rootToken != ROOT_IMX_TOKEN);
vm.assume(rootToken > address(10) && user > address(10));
vm.assume(balance > 0 && withdrawAmt > 0 && gasAmt > 0);
vm.assume(balance < type(uint256).max);
vm.assume(balance > withdrawAmt);
assumeValidRootToken(rootToken);
assumeValidUser(user);
vm.assume(withdrawAmt > 0 && gasAmt > 0);
vm.assume(balance > withdrawAmt && balance - withdrawAmt > gasAmt && balance < type(uint256).max - gasAmt);

// Map
bytes memory data = abi.encode(MAP_TOKEN_SIG, rootToken, "Test token", "Test", 18);
Expand Down Expand Up @@ -325,4 +314,24 @@ contract ChildERC20BridgeTest is Test, IChildERC20BridgeEvents {

vm.stopPrank();
}

function assumeValidRootToken(address rootToken) internal view {
vm.assume(rootToken > address(10));
vm.assume(rootToken != bridge.NATIVE_ETH() && rootToken != bridge.NATIVE_IMX() && rootToken != ROOT_IMX_TOKEN);
}

function assumeValidUsers(address user1, address user2) internal view {
vm.assume(user1 != user2);
assumeValidUser(user1);
assumeValidUser(user2);
}

function assumeValidUser(address user) internal view {
vm.assume(user > address(10));
vm.assume(user.balance == 0);
vm.assume(user.code.length == 0);
vm.assume(childTokenTemplate.balanceOf(user) == 0);
vm.assume(wIMX.balanceOf(user) == 0);
vm.assume(IChildERC20(bridge.childETHToken()).balanceOf(user) == 0);
}
}
35 changes: 26 additions & 9 deletions test/fuzz/root/RootERC20Bridge.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ contract RootERC20BridgeTest is Test, IRootERC20BridgeEvents {
function testFuzz_MapToken(address user, uint256 gasAmt, string memory name, string memory symbol, uint8 decimals)
public
{
vm.assume(user != address(0));
assumeValidUser(user);
vm.assume(gasAmt > 0);
vm.assume(bytes(name).length != 0 && bytes(symbol).length != 0 && decimals > 0);

Expand Down Expand Up @@ -92,7 +92,7 @@ contract RootERC20BridgeTest is Test, IRootERC20BridgeEvents {
function testFuzz_DepositIMX(address sender, address recipient, uint256 balance, uint256 gasAmt, uint256 depositAmt)
public
{
vm.assume(sender != address(0) && recipient != address(0));
assumeValidUsers(sender, recipient);
vm.assume(balance > 0 && depositAmt > 0 && gasAmt > 0);
vm.assume(balance > depositAmt && balance < type(uint256).max);
vm.assume(depositAmt <= IMX_DEPOSITS_LIMIT);
Expand Down Expand Up @@ -138,7 +138,8 @@ contract RootERC20BridgeTest is Test, IRootERC20BridgeEvents {
}

function testFuzz_WithdrawIMX(address sender, address recipient, uint256 withdrawAmt) public {
vm.assume(sender != address(0) && recipient != address(0) && withdrawAmt > 0);
assumeValidUsers(sender, recipient);
vm.assume(withdrawAmt > 0);

imxToken.mint(address(bridge), withdrawAmt);

Expand All @@ -163,7 +164,7 @@ contract RootERC20BridgeTest is Test, IRootERC20BridgeEvents {
function testFuzz_DepositETH(address sender, address recipient, uint256 balance, uint256 gasAmt, uint256 depositAmt)
public
{
vm.assume(sender != address(0) && recipient != address(0));
assumeValidUsers(sender, recipient);
vm.assume(balance > 0 && depositAmt > 0 && gasAmt > 0);
vm.assume(balance > depositAmt && balance < type(uint256).max - gasAmt && balance - depositAmt > gasAmt);

Expand All @@ -172,7 +173,6 @@ contract RootERC20BridgeTest is Test, IRootERC20BridgeEvents {
vm.startPrank(sender);

// Before deposit
assertEq(sender.balance, balance, "Sender should have given balance");
assertEq(address(bridge).balance, 0, "Bridge should have 0 balance");

// Deposit out of balance should fail
Expand Down Expand Up @@ -204,7 +204,7 @@ contract RootERC20BridgeTest is Test, IRootERC20BridgeEvents {
uint256 gasAmt,
uint256 depositAmt
) public {
vm.assume(sender != address(0) && recipient != address(0));
assumeValidUsers(sender, recipient);
vm.assume(balance > 0 && depositAmt > 0 && gasAmt > 0);
vm.assume(balance > depositAmt && balance < type(uint256).max - gasAmt && balance - depositAmt > gasAmt);

Expand Down Expand Up @@ -250,7 +250,8 @@ contract RootERC20BridgeTest is Test, IRootERC20BridgeEvents {
}

function testFuzz_WithdrawETH(address sender, address recipient, uint256 withdrawAmt) public {
vm.assume(sender != address(0) && recipient != address(0) && withdrawAmt > 0);
assumeValidUsers(sender, recipient);
vm.assume(withdrawAmt > 0);

vm.deal(address(bridge), withdrawAmt);

Expand Down Expand Up @@ -279,7 +280,7 @@ contract RootERC20BridgeTest is Test, IRootERC20BridgeEvents {
uint256 gasAmt,
uint256 depositAmt
) public {
vm.assume(sender != address(0) && recipient != address(0));
assumeValidUsers(sender, recipient);
vm.assume(balance > 0 && depositAmt > 0 && gasAmt > 0);
vm.assume(balance > depositAmt && balance < type(uint256).max);
vm.assume(gasAmt < 100);
Expand Down Expand Up @@ -331,7 +332,8 @@ contract RootERC20BridgeTest is Test, IRootERC20BridgeEvents {
}

function testFuzz_WithdrawERC20(address sender, address recipient, uint256 withdrawAmt) public {
vm.assume(sender != address(0) && recipient != address(0) && withdrawAmt > 0);
assumeValidUsers(sender, recipient);
vm.assume(withdrawAmt > 0);

// Map token
ChildERC20 rootToken = new ChildERC20();
Expand Down Expand Up @@ -360,4 +362,19 @@ contract RootERC20BridgeTest is Test, IRootERC20BridgeEvents {

vm.stopPrank();
}

function assumeValidUsers(address user1, address user2) internal view {
vm.assume(user1 != user2);
assumeValidUser(user1);
assumeValidUser(user2);
}

function assumeValidUser(address user) internal view {
vm.assume(user > address(10));
vm.assume(user.balance == 0);
vm.assume(user.code.length == 0);
vm.assume(childTokenTemplate.balanceOf(user) == 0);
vm.assume(imxToken.balanceOf(user) == 0);
vm.assume(wETH.balanceOf(user) == 0);
}
}

0 comments on commit 6d1bcb2

Please sign in to comment.