Skip to content

Commit

Permalink
fix native token detection on batch permit deposit
Browse files Browse the repository at this point in the history
  • Loading branch information
0age committed Oct 28, 2024
1 parent acdd79e commit ddf3f6e
Show file tree
Hide file tree
Showing 3 changed files with 277 additions and 53 deletions.
70 changes: 37 additions & 33 deletions snapshots/TheCompactTest.json
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
{
"basicTransfer": "57253",
"basicWithdrawal": "60321",
"batchClaim": "112336",
"batchClaimWithWitness": "113021",
"batchTransfer": "82862",
"batchWithdrawal": "101233",
"claim": "57450",
"claimAndWithdraw": "73734",
"claimWithWitness": "59909",
"depositAndRegisterViaPermit2": "123640",
"depositBatchSingleERC20": "67750",
"depositERC20AndURI": "67021",
"depositERC20Basic": "67026",
"depositERC20ViaPermit2AndURI": "98015",
"depositETHAndURI": "26694",
"depositETHBasic": "28230",
"qualifiedBatchClaim": "113685",
"qualifiedBatchClaimWithWitness": "113150",
"qualifiedClaim": "60727",
"qualifiedClaimWithWitness": "59312",
"qualifiedSplitBatchClaim": "141231",
"qualifiedSplitBatchClaimWithWitness": "141202",
"qualifiedSplitClaim": "86977",
"qualifiedSplitClaimWithWitness": "87278",
"register": "24890",
"splitBatchClaim": "140740",
"splitBatchClaimWithWitness": "140677",
"splitBatchTransfer": "113519",
"splitBatchWithdrawal": "142771",
"splitClaim": "86903",
"splitClaimWithWitness": "86379",
"splitTransfer": "83187",
"splitWithdrawal": "94097"
"basicTransfer": "57258",
"basicWithdrawal": "60326",
"batchClaim": "112341",
"batchClaimWithWitness": "113026",
"batchDepositAndRegisterViaPermit2": "226505",
"batchTransfer": "82924",
"batchWithdrawal": "101295",
"claim": "57455",
"claimAndWithdraw": "73739",
"claimWithWitness": "59914",
"depositAndRegisterViaPermit2": "123645",
"depositBatchSingleERC20": "67755",
"depositBatchSingleNative": "28100",
"depositBatchViaPermit2NativeAndERC20": "132812",
"depositBatchViaPermit2SingleERC20": "107954",
"depositERC20AndURI": "67026",
"depositERC20Basic": "67031",
"depositERC20ViaPermit2AndURI": "98020",
"depositETHAndURI": "26738",
"depositETHBasic": "28281",
"qualifiedBatchClaim": "113690",
"qualifiedBatchClaimWithWitness": "113155",
"qualifiedClaim": "60732",
"qualifiedClaimWithWitness": "59317",
"qualifiedSplitBatchClaim": "141287",
"qualifiedSplitBatchClaimWithWitness": "141258",
"qualifiedSplitClaim": "86982",
"qualifiedSplitClaimWithWitness": "87334",
"register": "24895",
"splitBatchClaim": "140745",
"splitBatchClaimWithWitness": "140682",
"splitBatchTransfer": "113635",
"splitBatchWithdrawal": "142887",
"splitClaim": "86908",
"splitClaimWithWitness": "86384",
"splitTransfer": "83192",
"splitWithdrawal": "94152"
}
37 changes: 19 additions & 18 deletions src/TheCompact.sol
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,10 @@ contract TheCompact is ITheCompact, ITheCompactClaims, ERC6909, Tstorish {
address recipient,
bytes calldata signature
) external returns (uint256 id) {
_setTstorish(_REENTRANCY_GUARD_SLOT, 1);
id = token.excludingNative().toIdIfRegistered(scope, resetPeriod, allocator);

_setTstorish(_REENTRANCY_GUARD_SLOT, 1);

address permit2 = address(_PERMIT2);

uint256 initialBalance = token.balanceOf(address(this));
Expand Down Expand Up @@ -356,8 +357,9 @@ contract TheCompact is ITheCompact, ITheCompactClaims, ERC6909, Tstorish {
revert(0, returndatasize())
}

// TODO: add proper revert on no data
revert(0, 0)
// revert Permit2CallFailed();
mstore(0, 0x7f28c61e)
revert(0x1c, 0x04)
}
}

Expand Down Expand Up @@ -516,8 +518,9 @@ contract TheCompact is ITheCompact, ITheCompactClaims, ERC6909, Tstorish {
revert(0, returndatasize())
}

// TODO: add proper revert on no data
revert(0, 0)
// revert Permit2CallFailed();
mstore(0, 0x7f28c61e)
revert(0x1c, 0x04)
}
}

Expand All @@ -540,7 +543,6 @@ contract TheCompact is ITheCompact, ITheCompactClaims, ERC6909, Tstorish {
_clearTstorish(_REENTRANCY_GUARD_SLOT);
}

/* TODO: put these two batch deposit methods back in after finding some room for them
function deposit(
address depositor,
ISignatureTransfer.TokenPermissions[] calldata permitted,
Expand All @@ -556,14 +558,14 @@ contract TheCompact is ITheCompact, ITheCompactClaims, ERC6909, Tstorish {
bool firstUnderlyingTokenIsNative;
assembly ("memory-safe") {
let permittedOffset := permitted.offset
firstUnderlyingTokenIsNative := iszero(shr(96, shl(96, add(permittedOffset, 0x20))))
firstUnderlyingTokenIsNative := iszero(shr(96, shl(96, calldataload(permittedOffset))))

// Revert if:
// * the array is empty
// * the callvalue is zero but the first token is native
// * the callvalue is nonzero but the first token is non-native
// * the first token is non-native and the callvalue doesn't equal the first amount
if or(iszero(totalTokens), or(eq(firstUnderlyingTokenIsNative, iszero(callvalue())), and(firstUnderlyingTokenIsNative, iszero(eq(callvalue(), calldataload(add(permittedOffset, 0x40)))))))
if or(iszero(totalTokens), or(eq(firstUnderlyingTokenIsNative, iszero(callvalue())), and(firstUnderlyingTokenIsNative, iszero(eq(callvalue(), calldataload(add(permittedOffset, 0x20)))))))
{
// revert InvalidBatchDepositStructure()
mstore(0, 0xca0fc08e)
Expand Down Expand Up @@ -594,7 +596,6 @@ contract TheCompact is ITheCompact, ITheCompactClaims, ERC6909, Tstorish {
address allocator,
ResetPeriod resetPeriod,
Scope scope,
address recipient,
uint256 nonce,
uint256 deadline,
bytes32 claimHash,
Expand All @@ -608,14 +609,15 @@ contract TheCompact is ITheCompact, ITheCompactClaims, ERC6909, Tstorish {
bool firstUnderlyingTokenIsNative;
assembly ("memory-safe") {
let permittedOffset := permitted.offset
firstUnderlyingTokenIsNative := iszero(shr(96, shl(96, add(permittedOffset, 0x20))))

firstUnderlyingTokenIsNative := iszero(shr(96, shl(96, calldataload(permittedOffset))))

// Revert if:
// * the array is empty
// * the callvalue is zero but the first token is native
// * the callvalue is nonzero but the first token is non-native
// * the first token is non-native and the callvalue doesn't equal the first amount
if or(iszero(totalTokens), or(eq(firstUnderlyingTokenIsNative, iszero(callvalue())), and(firstUnderlyingTokenIsNative, iszero(eq(callvalue(), calldataload(add(permittedOffset, 0x40)))))))
if or(iszero(totalTokens), or(eq(firstUnderlyingTokenIsNative, iszero(callvalue())), and(firstUnderlyingTokenIsNative, iszero(eq(callvalue(), calldataload(add(permittedOffset, 0x20)))))))
{
// revert InvalidBatchDepositStructure()
mstore(0, 0xca0fc08e)
Expand Down Expand Up @@ -652,20 +654,20 @@ contract TheCompact is ITheCompact, ITheCompactClaims, ERC6909, Tstorish {
}

if (firstUnderlyingTokenIsNative) {
_deposit(recipient, initialId, msg.value);
_deposit(depositor, initialId, msg.value);
ids[0] = initialId;
}

(ISignatureTransfer.SignatureTransferDetails[] memory details, ISignatureTransfer.TokenPermissions[] memory permittedTokens, uint256[] memory initialTokenBalances) =
_preparePermit2ArraysAndGetBalances(ids, totalTokensLessInitialNative, firstUnderlyingTokenIsNative, permitted, initialId);

ISignatureTransfer.PermitBatchTransferFrom memory permitTransferFrom = ISignatureTransfer.PermitBatchTransferFrom({ permitted: permittedTokens, nonce: nonce, deadline: deadline });
string memory witnessTypestring = string.concat("BatchActivation witness)", activationTypestring, "TokenPermissions(address token,uint256 amount)");

bytes32 witnessHash = keccak256(abi.encodePacked(keccak256(bytes(activationTypestring)), keccak256(abi.encodePacked(ids)), claimHash));

_PERMIT2.permitWitnessTransferFrom(permitTransferFrom, details, depositor, witnessHash, witnessTypestring, signature);
_PERMIT2.permitWitnessTransferFrom(
ISignatureTransfer.PermitBatchTransferFrom({ permitted: permittedTokens, nonce: nonce, deadline: deadline }), details, depositor, witnessHash, witnessTypestring, signature
);

uint256 tokenBalance;
uint256 initialBalance;
Expand All @@ -676,7 +678,7 @@ contract TheCompact is ITheCompact, ITheCompactClaims, ERC6909, Tstorish {
initialBalance = initialTokenBalances[i];
errorBuffer |= (initialBalance >= tokenBalance).asUint256();

_deposit(recipient, ids[i + firstUnderlyingTokenIsNative.asUint256()], tokenBalance - initialBalance);
_deposit(depositor, ids[i + firstUnderlyingTokenIsNative.asUint256()], tokenBalance - initialBalance);
}
}

Expand All @@ -688,11 +690,10 @@ contract TheCompact is ITheCompact, ITheCompactClaims, ERC6909, Tstorish {
}
}

_register(depositor, claimHash], keccak256(bytes(compactTypestring));
_register(depositor, claimHash, keccak256(bytes(compactTypestring)));

_clearTstorish(_REENTRANCY_GUARD_SLOT);
}
*/

function allocatedTransfer(BasicTransfer calldata transfer) external returns (bool) {
return _processBasicTransfer(transfer, _release);
Expand Down
Loading

0 comments on commit ddf3f6e

Please sign in to comment.