Skip to content

Commit

Permalink
feat(curation): add tests for set signer
Browse files Browse the repository at this point in the history
  • Loading branch information
robertu7 committed Jun 4, 2024
1 parent 6b41bd6 commit 82f11e9
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 30 deletions.
38 changes: 20 additions & 18 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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)
68 changes: 56 additions & 12 deletions src/test/Curation/Vault.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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));

Expand All @@ -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
*/
Expand All @@ -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);
Expand All @@ -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)))
Expand All @@ -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));

Expand All @@ -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));

Expand All @@ -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));

Expand Down Expand Up @@ -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(
Expand All @@ -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));

Expand All @@ -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));

Expand All @@ -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));

Expand Down Expand Up @@ -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);
}
}

0 comments on commit 82f11e9

Please sign in to comment.