Skip to content

Commit

Permalink
add test for register + claim
Browse files Browse the repository at this point in the history
  • Loading branch information
0age committed Oct 28, 2024
1 parent 47c4802 commit 888e55f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 15 deletions.
19 changes: 10 additions & 9 deletions snapshots/TheCompactTest.json
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
{
"basicTransfer": "57231",
"basicWithdrawal": "60321",
"batchClaim": "112342",
"batchClaim": "112336",
"batchClaimWithWitness": "112999",
"batchTransfer": "82862",
"batchWithdrawal": "101233",
"claim": "60835",
"claimAndWithdraw": "73718",
"claim": "57450",
"claimAndWithdraw": "73712",
"claimWithWitness": "59909",
"depositBatchSingleERC20": "67728",
"depositERC20AndURI": "67021",
"depositERC20Basic": "67026",
"depositERC20ViaPermit2AndURI": "98015",
"depositETHAndURI": "26694",
"depositETHBasic": "28186",
"qualifiedBatchClaim": "113669",
"qualifiedBatchClaim": "113663",
"qualifiedBatchClaimWithWitness": "113128",
"qualifiedClaim": "60733",
"qualifiedClaim": "60727",
"qualifiedClaimWithWitness": "59312",
"qualifiedSplitBatchClaim": "141215",
"qualifiedSplitBatchClaim": "141209",
"qualifiedSplitBatchClaimWithWitness": "141180",
"qualifiedSplitClaim": "86961",
"qualifiedSplitClaim": "86955",
"qualifiedSplitClaimWithWitness": "87256",
"splitBatchClaim": "140724",
"register": "24890",
"splitBatchClaim": "140718",
"splitBatchClaimWithWitness": "140655",
"splitBatchTransfer": "113519",
"splitBatchWithdrawal": "142771",
"splitClaim": "86887",
"splitClaim": "86881",
"splitClaimWithWitness": "86379",
"splitTransfer": "83187",
"splitWithdrawal": "94053"
Expand Down
21 changes: 15 additions & 6 deletions src/TheCompact.sol
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ contract TheCompact is ITheCompact, ERC6909, Tstorish {
_deposit(depositor, id, tokenBalance - initialBalance);
}

_registeredClaimHashes[depositor][claimHash] = compactTypehash;
_register(depositor, claimHash, compactTypehash);

_clearTstorish(_REENTRANCY_GUARD_SLOT);
}
Expand Down Expand Up @@ -672,7 +672,7 @@ contract TheCompact is ITheCompact, ERC6909, Tstorish {
}
}
_registeredClaimHashes[depositor][claimHash] = keccak256(bytes(compactTypestring));
_register(depositor, claimHash], keccak256(bytes(compactTypestring));
_clearTstorish(_REENTRANCY_GUARD_SLOT);
}
Expand Down Expand Up @@ -1139,10 +1139,15 @@ contract TheCompact is ITheCompact, ERC6909, Tstorish {
}

function register(bytes32 claimHash, bytes32 typehash) external returns (bool) {
_registeredClaimHashes[msg.sender][claimHash] = typehash;
_register(msg.sender, claimHash, typehash);
return true;
}

function _register(address sponsor, bytes32 claimHash, bytes32 typehash) internal {
_registeredClaimHashes[sponsor][claimHash] = typehash;
emit CompactRegistered(sponsor, claimHash, typehash);
}

function register(bytes32[2][] calldata claimHashesAndTypehashes) external returns (bool) {
return _registerFor(msg.sender, claimHashesAndTypehashes);
}
Expand All @@ -1152,7 +1157,7 @@ contract TheCompact is ITheCompact, ERC6909, Tstorish {
uint256 totalClaimHashes = claimHashesAndTypehashes.length;
for (uint256 i = 0; i < totalClaimHashes; ++i) {
bytes32[2] calldata claimHashAndTypehash = claimHashesAndTypehashes[i];
_registeredClaimHashes[sponsor][claimHashAndTypehash[0]] = claimHashAndTypehash[1];
_register(sponsor, claimHashAndTypehash[0], claimHashAndTypehash[1]);
}
}

Expand Down Expand Up @@ -1867,8 +1872,12 @@ contract TheCompact is ITheCompact, ERC6909, Tstorish {

function _typehashes(uint256 i) internal pure returns (bytes32 typehash) {
assembly ("memory-safe") {
let j := sub(i, 1)
typehash := add(mul(iszero(i), COMPACT_TYPEHASH), add(mul(iszero(j), BATCH_COMPACT_TYPEHASH), mul(iszero(iszero(j)), MULTICHAIN_COMPACT_TYPEHASH)))
let m := mload(0x40)
mstore(0, COMPACT_TYPEHASH)
mstore(0x20, BATCH_COMPACT_TYPEHASH)
mstore(0x40, MULTICHAIN_COMPACT_TYPEHASH)
typehash := mload(shl(5, i))
mstore(0x40, m)
}
}

Expand Down
1 change: 1 addition & 0 deletions src/interfaces/ITheCompact.sol
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ interface ITheCompact {
event ForcedWithdrawalEnabled(address indexed account, uint256 indexed id, uint256 withdrawableAt);
event ForcedWithdrawalDisabled(address indexed account, uint256 indexed id);
event AllocatorRegistered(uint96 allocatorId, address allocator);
event CompactRegistered(address indexed sponsor, bytes32 claimHash, bytes32 typehash);

error InvalidToken(address token);
error Expired(uint256 expiration);
Expand Down
45 changes: 45 additions & 0 deletions test/TheCompact.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,51 @@ contract TheCompactTest is Test {
assertEq(theCompact.balanceOf(claimant, id), amount);
}

function test_registerAndClaim() public {
ResetPeriod resetPeriod = ResetPeriod.TenMinutes;
Scope scope = Scope.Multichain;
uint256 amount = 1e18;
uint256 nonce = 0;
uint256 expires = block.timestamp + 1000;
address claimant = 0x1111111111111111111111111111111111111111;
address arbiter = 0x2222222222222222222222222222222222222222;

vm.prank(allocator);
theCompact.__registerAllocator(allocator, "");

vm.prank(swapper);
uint256 id = theCompact.deposit{ value: amount }(allocator, resetPeriod, scope, swapper);
assertEq(theCompact.balanceOf(swapper, id), amount);

bytes32 typehash = keccak256("Compact(address arbiter,address sponsor,uint256 nonce,uint256 expires,uint256 id,uint256 amount)");

bytes32 claimHash = keccak256(abi.encode(typehash, arbiter, swapper, nonce, expires, id, amount));

bytes32 digest = keccak256(abi.encodePacked(bytes2(0x1901), theCompact.DOMAIN_SEPARATOR(), claimHash));

vm.prank(swapper);
(bool status) = theCompact.register(claimHash, typehash);
vm.snapshotGasLastCall("register");
assert(status);

bytes memory sponsorSignature = "";

(bytes32 r, bytes32 vs) = vm.signCompact(allocatorPrivateKey, digest);
bytes memory allocatorSignature = abi.encodePacked(r, vs);

BasicClaim memory claim = BasicClaim(allocatorSignature, sponsorSignature, swapper, nonce, expires, id, amount, claimant, amount);

vm.prank(arbiter);
(status) = theCompact.claim(claim);
vm.snapshotGasLastCall("claim");
assert(status);

assertEq(address(theCompact).balance, amount);
assertEq(claimant.balance, 0);
assertEq(theCompact.balanceOf(swapper, id), 0);
assertEq(theCompact.balanceOf(claimant, id), amount);
}

function test_claimAndWithdraw() public {
ResetPeriod resetPeriod = ResetPeriod.TenMinutes;
Scope scope = Scope.Multichain;
Expand Down

0 comments on commit 888e55f

Please sign in to comment.