Skip to content

Commit

Permalink
test: add batch claim fuzz test
Browse files Browse the repository at this point in the history
  • Loading branch information
maybeast committed Dec 2, 2024
1 parent b74b2db commit d7d7e90
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions contracts/test/EtherSwapFuzzTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import "./SigUtils.sol";
import "../EtherSwap.sol";

contract EtherSwapFuzzTest is Test {
event Claim(bytes32 indexed preimageHash, bytes32 preimage);

EtherSwap internal swap = new EtherSwap();

receive() external payable {}
Expand Down Expand Up @@ -152,4 +154,46 @@ contract EtherSwapFuzzTest is Test {

assertTrue(swap.swaps(swap.hashValues(preimageHash, amount, claimAddress, address(this), timelock)));
}

function testClaimBatch(uint256 batchSize) external {
batchSize = bound(batchSize, 1, 100);
uint256 lockupAmount = 1 ether;
address claimAddress = vm.addr(0xA11CE);
uint256 balanceBeforeClaim = claimAddress.balance;

bytes32[] memory preimages = new bytes32[](batchSize);
bytes32[] memory preimageHashes = new bytes32[](batchSize);
uint256[] memory amounts = new uint256[](batchSize);
address[] memory refundAddresses = new address[](batchSize);
uint256[] memory timelocks = new uint256[](batchSize);

uint256 totalLockupAmount = 0;

for (uint256 i = 0; i < batchSize; i++) {
preimages[i] = sha256(abi.encodePacked(bytes32(i + 1)));
preimageHashes[i] = sha256(abi.encodePacked(preimages[i]));
amounts[i] = (i == 0) ? lockupAmount : (100 + i * 100);
refundAddresses[i] = address(this);
timelocks[i] = block.number + (i * 21);

swap.lock{value: amounts[i]}(preimageHashes[i], claimAddress, timelocks[i]);
totalLockupAmount += amounts[i];
}

vm.prank(claimAddress);

for (uint256 i = 0; i < batchSize; i++) {
vm.expectEmit(true, false, false, true, address(swap));
emit Claim(preimageHashes[i], preimages[i]);
}

swap.claimBatch(preimages, amounts, refundAddresses, timelocks);

for (uint256 i = 0; i < batchSize; i++) {
assertFalse(swap.swaps(swap.hashValues(preimageHashes[i], amounts[i], claimAddress, refundAddresses[i], timelocks[i])));
}

assertEq(address(swap).balance, 0);
assertEq(claimAddress.balance - balanceBeforeClaim, totalLockupAmount);
}
}

0 comments on commit d7d7e90

Please sign in to comment.