Skip to content

Commit

Permalink
add some basic deposit methods
Browse files Browse the repository at this point in the history
  • Loading branch information
0age committed Oct 9, 2024
1 parent d129674 commit 1ffd78d
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 17 deletions.
21 changes: 21 additions & 0 deletions src/TheCompact.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/lib/IdLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down
119 changes: 103 additions & 16 deletions test/TheCompact.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,47 @@ 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;
Scope scope = Scope.Multichain;
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);
Expand All @@ -159,21 +192,58 @@ 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;
Scope scope = Scope.Multichain;
uint256 amount = 1e18;

vm.prank(allocator);
theCompact.__register(allocator, "");
uint96 allocatorId = theCompact.__register(allocator, "");

vm.prank(swapper);
uint256 id =
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 }(
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 1ffd78d

Please sign in to comment.