From 1ffd78df763f6eff6b1b7d4ecdad027ac841f04b Mon Sep 17 00:00:00 2001 From: 0age <37939117+0age@users.noreply.github.com> Date: Wed, 9 Oct 2024 15:03:17 -0700 Subject: [PATCH] add some basic deposit methods --- src/TheCompact.sol | 21 ++++++++ src/lib/IdLib.sol | 7 ++- test/TheCompact.t.sol | 119 ++++++++++++++++++++++++++++++++++++------ 3 files changed, 130 insertions(+), 17 deletions(-) diff --git a/src/TheCompact.sol b/src/TheCompact.sol index c3e73bb..075bc6c 100644 --- a/src/TheCompact.sol +++ b/src/TheCompact.sol @@ -95,6 +95,27 @@ contract TheCompact is ITheCompact, ERC6909 { _METADATA_RENDERER = new MetadataRenderer(); } + function deposit(address allocator) external payable returns (uint256 id) { + id = address(0).toIdIfRegistered(Scope.Multichain, ResetPeriod.TenMinutes, allocator); + + _deposit(msg.sender, msg.sender, id, msg.value); + } + + function deposit(address token, address allocator, uint256 amount) + external + returns (uint256 id) + { + if (token == address(0)) { + revert InvalidToken(token); + } + + id = token.toIdIfRegistered(Scope.Multichain, ResetPeriod.TenMinutes, allocator); + + token.safeTransferFrom(msg.sender, address(this), amount); + + _deposit(msg.sender, msg.sender, id, amount); + } + function deposit(address allocator, ResetPeriod resetPeriod, Scope scope, address recipient) external payable diff --git a/src/lib/IdLib.sol b/src/lib/IdLib.sol index f324eac..f7bb174 100644 --- a/src/lib/IdLib.sol +++ b/src/lib/IdLib.sol @@ -164,7 +164,12 @@ library IdLib { ); } - function toIdIfRegistered(address token, Scope scope, ResetPeriod resetPeriod, address allocator) internal view returns (uint256 id) { + function toIdIfRegistered( + address token, + Scope scope, + ResetPeriod resetPeriod, + address allocator + ) internal view returns (uint256 id) { uint96 allocatorId = allocator.usingAllocatorId(); allocatorId.mustHaveARegisteredAllocator(); id = ( diff --git a/test/TheCompact.t.sol b/test/TheCompact.t.sol index cc10c79..ea6e086 100644 --- a/test/TheCompact.t.sol +++ b/test/TheCompact.t.sol @@ -137,6 +137,39 @@ contract TheCompactTest is Test { assertEq(block.chainid, currentChainId); } + function test_depositETHBasic() public { + address recipient = swapper; + ResetPeriod resetPeriod = ResetPeriod.TenMinutes; + Scope scope = Scope.Multichain; + uint256 amount = 1e18; + + vm.prank(allocator); + uint96 allocatorId = theCompact.__register(allocator, ""); + + vm.prank(swapper); + uint256 id = theCompact.deposit{ value: amount }(allocator); + + ( + address derivedToken, + address derivedAllocator, + ResetPeriod derivedResetPeriod, + Scope derivedScope + ) = theCompact.getLockDetails(id); + assertEq(derivedToken, address(0)); + assertEq(derivedAllocator, allocator); + assertEq(uint256(derivedResetPeriod), uint256(resetPeriod)); + assertEq(uint256(derivedScope), uint256(scope)); + assertEq( + id, + (uint256(scope) << 255) | (uint256(resetPeriod) << 252) | (uint256(allocatorId) << 160) + | uint256(uint160(address(0))) + ); + + assertEq(address(theCompact).balance, amount); + assertEq(theCompact.balanceOf(recipient, id), amount); + assert(bytes(theCompact.tokenURI(id)).length > 0); + } + function test_depositETHAndURI() public { address recipient = 0x1111111111111111111111111111111111111111; ResetPeriod resetPeriod = ResetPeriod.TenMinutes; @@ -144,7 +177,7 @@ contract TheCompactTest is Test { uint256 amount = 1e18; vm.prank(allocator); - theCompact.__register(allocator, ""); + uint96 allocatorId = theCompact.__register(allocator, ""); vm.prank(swapper); uint256 id = theCompact.deposit{ value: amount }(allocator, resetPeriod, scope, recipient); @@ -159,13 +192,50 @@ contract TheCompactTest is Test { assertEq(derivedAllocator, allocator); assertEq(uint256(derivedResetPeriod), uint256(resetPeriod)); assertEq(uint256(derivedScope), uint256(scope)); - // TODO: make sure ID matches expectations + assertEq( + id, + (uint256(scope) << 255) | (uint256(resetPeriod) << 252) | (uint256(allocatorId) << 160) + | uint256(uint160(address(0))) + ); assertEq(address(theCompact).balance, amount); assertEq(theCompact.balanceOf(recipient, id), amount); assert(bytes(theCompact.tokenURI(id)).length > 0); } + function test_depositERC20Basic() public { + address recipient = swapper; + ResetPeriod resetPeriod = ResetPeriod.TenMinutes; + Scope scope = Scope.Multichain; + uint256 amount = 1e18; + + vm.prank(allocator); + uint96 allocatorId = theCompact.__register(allocator, ""); + + vm.prank(swapper); + uint256 id = theCompact.deposit(address(token), allocator, amount); + + ( + address derivedToken, + address derivedAllocator, + ResetPeriod derivedResetPeriod, + Scope derivedScope + ) = theCompact.getLockDetails(id); + assertEq(derivedToken, address(token)); + assertEq(derivedAllocator, allocator); + assertEq(uint256(derivedResetPeriod), uint256(resetPeriod)); + assertEq(uint256(derivedScope), uint256(scope)); + assertEq( + id, + (uint256(scope) << 255) | (uint256(resetPeriod) << 252) | (uint256(allocatorId) << 160) + | uint256(uint160(address(token))) + ); + + assertEq(token.balanceOf(address(theCompact)), amount); + assertEq(theCompact.balanceOf(recipient, id), amount); + assert(bytes(theCompact.tokenURI(id)).length > 0); + } + function test_depositERC20AndURI() public { address recipient = 0x1111111111111111111111111111111111111111; ResetPeriod resetPeriod = ResetPeriod.TenMinutes; @@ -173,7 +243,7 @@ contract TheCompactTest is Test { uint256 amount = 1e18; vm.prank(allocator); - theCompact.__register(allocator, ""); + uint96 allocatorId = theCompact.__register(allocator, ""); vm.prank(swapper); uint256 id = @@ -189,7 +259,11 @@ contract TheCompactTest is Test { assertEq(derivedAllocator, allocator); assertEq(uint256(derivedResetPeriod), uint256(resetPeriod)); assertEq(uint256(derivedScope), uint256(scope)); - // TODO: make sure ID matches expectations + assertEq( + id, + (uint256(scope) << 255) | (uint256(resetPeriod) << 252) | (uint256(allocatorId) << 160) + | uint256(uint160(address(token))) + ); assertEq(token.balanceOf(address(theCompact)), amount); assertEq(theCompact.balanceOf(recipient, id), amount); @@ -227,7 +301,12 @@ contract TheCompactTest is Test { assertEq(derivedAllocator, allocator); assertEq(uint256(derivedResetPeriod), uint256(resetPeriod)); assertEq(uint256(derivedScope), uint256(scope)); - // TODO: make sure ID matches expectations + + assertEq( + id, + (uint256(scope) << 255) | (uint256(resetPeriod) << 252) | (uint256(allocatorId) << 160) + | uint256(uint160(address(token))) + ); assertEq(token.balanceOf(address(theCompact)), amount); assertEq(theCompact.balanceOf(recipient, id), amount); @@ -293,7 +372,7 @@ contract TheCompactTest is Test { bytes memory signature = abi.encodePacked(r, vs); vm.prank(allocator); - theCompact.__register(allocator, ""); + uint96 allocatorId = theCompact.__register(allocator, ""); uint256 id = theCompact.deposit( swapper, @@ -318,7 +397,11 @@ contract TheCompactTest is Test { assertEq(derivedAllocator, allocator); assertEq(uint256(derivedResetPeriod), uint256(resetPeriod)); assertEq(uint256(derivedScope), uint256(scope)); - // TODO: make sure ID matches expectations + assertEq( + id, + (uint256(scope) << 255) | (uint256(resetPeriod) << 252) | (uint256(allocatorId) << 160) + | uint256(uint160(address(token))) + ); assertEq(token.balanceOf(address(theCompact)), amount); assertEq(theCompact.balanceOf(recipient, id), amount); @@ -336,7 +419,7 @@ contract TheCompactTest is Test { uint256 pledge = 0; vm.prank(allocator); - theCompact.__register(allocator, ""); + uint96 allocatorId = theCompact.__register(allocator, ""); vm.prank(swapper); uint256 id = theCompact.deposit{ value: amount }(allocator, resetPeriod, scope, swapper); @@ -398,7 +481,7 @@ contract TheCompactTest is Test { address recipient = 0x1111111111111111111111111111111111111111; vm.prank(allocator); - theCompact.__register(allocator, ""); + uint96 allocatorId = theCompact.__register(allocator, ""); vm.prank(swapper); uint256 id = @@ -450,7 +533,7 @@ contract TheCompactTest is Test { uint256 pledge = 0; vm.prank(allocator); - theCompact.__register(allocator, ""); + uint96 allocatorId = theCompact.__register(allocator, ""); vm.prank(swapper); uint256 id = theCompact.deposit{ value: amount }(allocator, resetPeriod, scope, swapper); @@ -512,7 +595,7 @@ contract TheCompactTest is Test { address recipient = 0x1111111111111111111111111111111111111111; vm.prank(allocator); - theCompact.__register(allocator, ""); + uint96 allocatorId = theCompact.__register(allocator, ""); vm.prank(swapper); uint256 id = @@ -569,7 +652,7 @@ contract TheCompactTest is Test { uint256 amountReduction = 0; vm.prank(allocator); - theCompact.__register(allocator, ""); + uint96 allocatorId = theCompact.__register(allocator, ""); vm.prank(swapper); uint256 id = theCompact.deposit{ value: amount }(allocator, resetPeriod, scope, swapper); @@ -660,7 +743,7 @@ contract TheCompactTest is Test { uint256 amountReduction = 0; vm.prank(allocator); - theCompact.__register(allocator, ""); + uint96 allocatorId = theCompact.__register(allocator, ""); vm.prank(swapper); uint256 id = theCompact.deposit{ value: amount }(allocator, resetPeriod, scope, swapper); @@ -755,7 +838,7 @@ contract TheCompactTest is Test { uint256 aThirdAmountReduction = 0; vm.prank(allocator); - theCompact.__register(allocator, ""); + uint96 allocatorId = theCompact.__register(allocator, ""); vm.startPrank(swapper); uint256 id = theCompact.deposit{ value: amount }( @@ -940,7 +1023,7 @@ contract TheCompactTest is Test { bytes memory signature = abi.encodePacked(r, vs); vm.prank(allocator); - theCompact.__register(allocator, ""); + uint96 allocatorId = theCompact.__register(allocator, ""); ISignatureTransfer.TokenPermissions[] memory tokenPermissions = new ISignatureTransfer.TokenPermissions[](1); @@ -971,7 +1054,11 @@ contract TheCompactTest is Test { assertEq(derivedAllocator, allocator); assertEq(uint256(derivedResetPeriod), uint256(resetPeriod)); assertEq(uint256(derivedScope), uint256(scope)); - // TODO: make sure ID matches expectations + assertEq( + ids[0], + (uint256(scope) << 255) | (uint256(resetPeriod) << 252) | (uint256(allocatorId) << 160) + | uint256(uint160(address(token))) + ); assertEq(token.balanceOf(address(theCompact)), amount); assertEq(theCompact.balanceOf(recipient, ids[0]), amount);