From 82f11e9ab6ba14e3aaf9033b2a39a98c45510d70 Mon Sep 17 00:00:00 2001 From: robertu <4065233+robertu7@users.noreply.github.com> Date: Tue, 4 Jun 2024 15:36:14 +0800 Subject: [PATCH] feat(curation): add tests for set signer --- .gas-snapshot | 38 ++++++++++---------- src/test/Curation/Vault.t.sol | 68 ++++++++++++++++++++++++++++------- 2 files changed, 76 insertions(+), 30 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index c075f2b..4fef166 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -175,21 +175,23 @@ TheSpaceTest:testTotalSupply() (gas: 7613) TheSpaceTest:testUpgradeTo() (gas: 3215197) TheSpaceTest:testWithdrawTreasury() (gas: 352672) TheSpaceTest:testWithdrawUBI() (gas: 375819) -VaultTest:testCannotErc20TokenClaimAlreadyClaimed() (gas: 181263) -VaultTest:testCannotErc20TokenClaimExpired() (gas: 82592) -VaultTest:testCannotErc20TokenClaimInvalidSignature() (gas: 93550) -VaultTest:testCannotErc20TokenClaimZeroBalance() (gas: 22674) -VaultTest:testCannotErc20TokenDepositZeroAmount() (gas: 15938) -VaultTest:testCannotNativeTokenClaimAlreadyClaimed() (gas: 154193) -VaultTest:testCannotNativeTokenClaimExpired() (gas: 49834) -VaultTest:testCannotNativeTokenClaimInvalidSignature() (gas: 60597) -VaultTest:testCannotNativeTokenClaimZeroBalance() (gas: 20179) -VaultTest:testCannotNativeTokenDepositZeroAmount() (gas: 13621) -VaultTest:testCannotSetSignerByAttacker() (gas: 13226) -VaultTest:testErc20TokenClaim() (gas: 150708) -VaultTest:testErc20TokenDeposit() (gas: 92032) -VaultTest:testErc20TokenSweep() (gas: 101388) -VaultTest:testNativeTokenClaim() (gas: 142804) -VaultTest:testNativeTokenDeposit() (gas: 59633) -VaultTest:testNativeTokenSweep() (gas: 93006) -VaultTest:testSetSigner() (gas: 21536) \ No newline at end of file +VaultTest:testCannotErc20TokenClaimIfAlreadyClaimed() (gas: 181194) +VaultTest:testCannotErc20TokenClaimIfExpired() (gas: 82580) +VaultTest:testCannotErc20TokenClaimIfInvalidSignature() (gas: 93495) +VaultTest:testCannotErc20TokenClaimIfZeroBalance() (gas: 22697) +VaultTest:testCannotErc20TokenDepositIfNotApproval() (gas: 22272) +VaultTest:testCannotErc20TokenDepositIfZeroAmount() (gas: 15926) +VaultTest:testCannotNativeTokenClaimIfAlreadyClaimed() (gas: 154239) +VaultTest:testCannotNativeTokenClaimIfExpired() (gas: 49812) +VaultTest:testCannotNativeTokenClaimIfInvalidSignature() (gas: 60576) +VaultTest:testCannotNativeTokenClaimIfZeroBalance() (gas: 20201) +VaultTest:testCannotNativeTokenDepositIfZeroAmount() (gas: 13710) +VaultTest:testCannotSetSignerByAttacker() (gas: 13227) +VaultTest:testCannotSetSignerIfZeroAddress() (gas: 13556) +VaultTest:testErc20TokenClaim() (gas: 150697) +VaultTest:testErc20TokenDeposit() (gas: 92006) +VaultTest:testErc20TokenSweep() (gas: 101400) +VaultTest:testNativeTokenClaim() (gas: 198773) +VaultTest:testNativeTokenDeposit() (gas: 59589) +VaultTest:testNativeTokenSweep() (gas: 93029) +VaultTest:testSetSigner() (gas: 21581) \ No newline at end of file diff --git a/src/test/Curation/Vault.t.sol b/src/test/Curation/Vault.t.sol index 181285b..7f5d87b 100644 --- a/src/test/Curation/Vault.t.sol +++ b/src/test/Curation/Vault.t.sol @@ -82,14 +82,17 @@ contract VaultTest is Test { vm.prank(CURATOR); vault.deposit{value: _amount}(_vaultId); + // will affect ETH balances assertEq(CURATOR.balance, _curatorBalance - _amount); assertEq(vault.balances(_vaultId), _amount); assertEq(vault.claimed(_vaultId), 0); + + // wont affect ERC-20 balances assertEq(vault.erc20Balances(_vaultId, address(0)), 0); assertEq(vault.erc20Claimed(_vaultId, address(0)), 0); } - function testCannotNativeTokenDepositZeroAmount() public { + function testCannotNativeTokenDepositIfZeroAmount() public { uint256 _amount = 0; bytes32 _vaultId = keccak256(abi.encodePacked(CREATOR)); @@ -113,13 +116,15 @@ contract VaultTest is Test { vault.deposit(_vaultId, address(usdt), _amount); assertEq(usdt.balanceOf(CURATOR), _curatorBalance - _amount); - assertEq(vault.balances(_vaultId), 0); - assertEq(vault.claimed(_vaultId), 0); assertEq(vault.erc20Balances(_vaultId, address(usdt)), _amount); assertEq(vault.erc20Claimed(_vaultId, address(usdt)), 0); + + // wont affect ETH balances + assertEq(vault.balances(_vaultId), 0); + assertEq(vault.claimed(_vaultId), 0); } - function testCannotErc20TokenDepositZeroAmount() public { + function testCannotErc20TokenDepositIfZeroAmount() public { uint256 _amount = 0; bytes32 _vaultId = keccak256(abi.encodePacked(CREATOR)); @@ -128,6 +133,15 @@ contract VaultTest is Test { vault.deposit(_vaultId, address(usdt), _amount); } + function testCannotErc20TokenDepositIfNotApproval() public { + uint256 _amount = 100; + bytes32 _vaultId = keccak256(abi.encodePacked(CREATOR)); + + vm.expectRevert("ERC20: insufficient allowance"); + vm.prank(ATTACKER); + vault.deposit(_vaultId, address(usdt), _amount); + } + /** * Claim: Native Token */ @@ -137,6 +151,8 @@ contract VaultTest is Test { uint256 _creatorBalance = CREATOR.balance; uint256 _expiredAt = block.timestamp + 1 days; + + // first claim bytes32 _digest = keccak256(abi.encodePacked(_vaultId, CREATOR, _expiredAt, block.chainid, address(vault))) .toEthSignedMessageHash(); (uint8 _v, bytes32 _r, bytes32 _s) = vm.sign(DEPLOYER_PK, _digest); @@ -153,9 +169,28 @@ contract VaultTest is Test { assertEq(vault.balances(_vaultId), _amount); assertEq(vault.claimed(_vaultId), _amount); assertEq(vault.available(_vaultId), 0); + + // second claim + _expiredAt = block.timestamp + 2 days; + _digest = keccak256(abi.encodePacked(_vaultId, CREATOR, _expiredAt, block.chainid, address(vault))) + .toEthSignedMessageHash(); + (_v, _r, _s) = vm.sign(DEPLOYER_PK, _digest); + + depositETH(_amount, _vaultId); + + vm.expectEmit(true, true, true, true); + emit IVault.Claimed(_vaultId, CREATOR, _amount); + + vm.prank(CREATOR); + vault.claim(_vaultId, CREATOR, _expiredAt, _v, _r, _s); + + assertEq(CREATOR.balance, _creatorBalance + _amount * 2); + assertEq(vault.balances(_vaultId), _amount * 2); + assertEq(vault.claimed(_vaultId), _amount * 2); + assertEq(vault.available(_vaultId), 0); } - function testCannotNativeTokenClaimZeroBalance() public { + function testCannotNativeTokenClaimIfZeroBalance() public { bytes32 _vaultId = keccak256(abi.encodePacked(CREATOR)); uint256 _expiredAt = block.timestamp + 1 days; bytes32 _digest = keccak256(abi.encodePacked(_vaultId, CREATOR, _expiredAt, block.chainid, address(vault))) @@ -167,7 +202,7 @@ contract VaultTest is Test { vault.claim(_vaultId, CREATOR, _expiredAt, _v, _r, _s); } - function testCannotNativeTokenClaimExpired() public { + function testCannotNativeTokenClaimIfExpired() public { uint256 _amount = 1 ether; bytes32 _vaultId = keccak256(abi.encodePacked(CREATOR)); @@ -183,7 +218,7 @@ contract VaultTest is Test { vault.claim(_vaultId, CREATOR, _expiredAt, _v, _r, _s); } - function testCannotNativeTokenClaimInvalidSignature() public { + function testCannotNativeTokenClaimIfInvalidSignature() public { uint256 _amount = 1 ether; bytes32 _vaultId = keccak256(abi.encodePacked(CREATOR)); @@ -199,7 +234,7 @@ contract VaultTest is Test { vault.claim(_vaultId, ATTACKER, _expiredAt, _v, _r, _s); } - function testCannotNativeTokenClaimAlreadyClaimed() public { + function testCannotNativeTokenClaimIfAlreadyClaimed() public { uint256 _amount = 1 ether; bytes32 _vaultId = keccak256(abi.encodePacked(CREATOR)); @@ -251,7 +286,7 @@ contract VaultTest is Test { assertEq(vault.available(_vaultId, address(usdt)), 0); } - function testCannotErc20TokenClaimZeroBalance() public { + function testCannotErc20TokenClaimIfZeroBalance() public { bytes32 _vaultId = keccak256(abi.encodePacked(CREATOR)); uint256 _expiredAt = block.timestamp + 1 days; bytes32 _digest = keccak256( @@ -264,7 +299,7 @@ contract VaultTest is Test { vault.claim(_vaultId, address(usdt), CREATOR, _expiredAt, _v, _r, _s); } - function testCannotErc20TokenClaimExpired() public { + function testCannotErc20TokenClaimIfExpired() public { uint256 _amount = 100; bytes32 _vaultId = keccak256(abi.encodePacked(CREATOR)); @@ -281,7 +316,7 @@ contract VaultTest is Test { vault.claim(_vaultId, address(usdt), CREATOR, _expiredAt, _v, _r, _s); } - function testCannotErc20TokenClaimInvalidSignature() public { + function testCannotErc20TokenClaimIfInvalidSignature() public { uint256 _amount = 100; bytes32 _vaultId = keccak256(abi.encodePacked(CREATOR)); @@ -298,7 +333,7 @@ contract VaultTest is Test { vault.claim(_vaultId, address(usdt), ATTACKER, _expiredAt, _v, _r, _s); } - function testCannotErc20TokenClaimAlreadyClaimed() public { + function testCannotErc20TokenClaimIfAlreadyClaimed() public { uint256 _amount = 100; bytes32 _vaultId = keccak256(abi.encodePacked(CREATOR)); @@ -384,4 +419,13 @@ contract VaultTest is Test { vm.prank(ATTACKER); vault.setSigner(_signer); } + + function testCannotSetSignerIfZeroAddress() public { + address _signer = address(0); + + vm.expectRevert(abi.encodeWithSignature("ZeroAddress()")); + + vm.prank(OWNER); + vault.setSigner(_signer); + } }