Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement batch permit2 deposits #5

Merged
merged 10 commits into from
Oct 30, 2024
2 changes: 1 addition & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ solc = '0.8.28'
evm_version='cancun'
via_ir = true
# optimizer_runs = 4_294_967_295
optimizer_runs = 200
optimizer_runs = 50
bytecode_hash = 'none'
src = "src"
out = "out"
Expand Down
69 changes: 36 additions & 33 deletions snapshots/TheCompactTest.json
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
{
"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": "57278",
"basicWithdrawal": "60320",
"batchClaim": "112360",
"batchClaimRegisteredWithDeposit": "112360",
"batchClaimRegisteredWithDepositWithWitness": "113051",
"batchClaimWithWitness": "113045",
"batchDepositAndRegisterViaPermit2": "221954",
"batchDepositAndRegisterWithWitnessViaPermit2": "221932",
"claim": "57501",
"claimAndWithdraw": "73758",
"claimWithWitness": "59955",
"depositAndRegisterViaPermit2": "124302",
"depositBatchSingleERC20": "67822",
"depositBatchSingleNative": "28086",
"depositBatchViaPermit2NativeAndERC20": "129587",
"depositBatchViaPermit2SingleERC20": "104714",
"depositERC20AndURI": "67145",
"depositERC20Basic": "67152",
"depositERC20ViaPermit2AndURI": "98349",
"depositETHAndURI": "26769",
"depositETHBasic": "28288",
"qualifiedBatchClaim": "113731",
"qualifiedBatchClaimWithWitness": "113174",
"qualifiedClaim": "60773",
"qualifiedClaimWithWitness": "59336",
"qualifiedSplitBatchClaim": "141307",
"qualifiedSplitBatchClaimWithWitness": "141278",
"qualifiedSplitClaim": "87001",
"qualifiedSplitClaimWithWitness": "87324",
"register": "25335",
"splitBatchClaim": "140794",
"splitBatchClaimWithWitness": "140731",
"splitBatchTransfer": "113570",
"splitBatchWithdrawal": "142818",
"splitClaim": "86927",
"splitClaimWithWitness": "86403"
}
882 changes: 478 additions & 404 deletions src/TheCompact.sol

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions src/interfaces/ITheCompact.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface ITheCompact {
event Claim(address indexed sponsor, address indexed allocator, address indexed arbiter, bytes32 claimHash);
event ForcedWithdrawalEnabled(address indexed account, uint256 indexed id, uint256 withdrawableAt);
event ForcedWithdrawalDisabled(address indexed account, uint256 indexed id);
event CompactRegistered(address indexed sponsor, bytes32 claimHash, bytes32 typehash);
event CompactRegistered(address indexed sponsor, bytes32 claimHash, bytes32 typehash, uint256 expires);
event AllocatorRegistered(uint96 allocatorId, address allocator);

error InvalidToken(address token);
Expand All @@ -38,6 +38,8 @@ interface ITheCompact {
error InvalidScope(uint256 id);
error InvalidDepositTokenOrdering();
error InvalidDepositBalanceChange();
error Permit2CallFailed();
error InvalidRegistrationDuration(uint256 duration);

function deposit(address allocator) external payable returns (uint256 id);

Expand All @@ -53,7 +55,7 @@ interface ITheCompact {

function deposit(uint256[2][] calldata idsAndAmounts, address recipient) external payable returns (bool);

function depositAndRegister(uint256[2][] calldata idsAndAmounts, bytes32[2][] calldata claimHashesAndTypehashes) external payable returns (bool);
function depositAndRegister(uint256[2][] calldata idsAndAmounts, bytes32[2][] calldata claimHashesAndTypehashes, uint256 duration) external payable returns (bool);

function deposit(
address token,
Expand Down Expand Up @@ -87,13 +89,15 @@ interface ITheCompact {

function allocatedWithdrawal(BasicTransfer calldata withdrawal) external returns (bool);

/*
function allocatedTransfer(SplitTransfer calldata transfer) external returns (bool);

function allocatedWithdrawal(SplitTransfer calldata withdrawal) external returns (bool);

function allocatedTransfer(BatchTransfer calldata transfer) external returns (bool);

function allocatedWithdrawal(BatchTransfer calldata withdrawal) external returns (bool);
*/

function allocatedTransfer(SplitBatchTransfer calldata transfer) external returns (bool);

Expand All @@ -105,9 +109,9 @@ interface ITheCompact {

function forcedWithdrawal(uint256 id, address recipient, uint256 amount) external returns (bool);

function register(bytes32 claimHash, bytes32 typehash) external returns (bool);
function register(bytes32 claimHash, bytes32 typehash, uint256 duration) external returns (bool);

function register(bytes32[2][] calldata claimHashesAndTypehashes) external returns (bool);
function register(bytes32[2][] calldata claimHashesAndTypehashes, uint256 duration) external returns (bool);

function consume(uint256[] calldata nonces) external returns (bool);

Expand Down
6 changes: 6 additions & 0 deletions src/lib/EfficiencyLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,10 @@ library EfficiencyLib {
b := or(iszero(calldatasize()), a)
}
}

function asStubborn(bool a) internal pure returns (bool b) {
assembly ("memory-safe") {
b := or(iszero(calldatasize()), a)
}
}
}
14 changes: 0 additions & 14 deletions src/lib/HashLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -580,20 +580,6 @@ library HashLib {
}

///// CATEGORY 6: miscellaneous utilities /////

function toPermit2DepositWitnessHash(address allocator, ResetPeriod resetPeriod, Scope scope, address recipient) internal pure returns (bytes32 witnessHash) {
assembly ("memory-safe") {
let m := mload(0x40) // Grab the free memory pointer; memory will be left dirtied.

mstore(m, PERMIT2_DEPOSIT_WITNESS_FRAGMENT_HASH)
mstore(add(m, 0x20), allocator)
mstore(add(m, 0x40), resetPeriod)
mstore(add(m, 0x60), scope)
mstore(add(m, 0x80), recipient)
witnessHash := keccak256(m, 0xa0)
}
}

function toLatest(bytes32 initialDomainSeparator, uint256 initialChainId) external view returns (bytes32 domainSeparator) {
domainSeparator = initialDomainSeparator;

Expand Down
1 change: 1 addition & 0 deletions src/lib/IdLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ library IdLib {
using IdLib for uint96;
using IdLib for uint256;
using IdLib for address;
using IdLib for ResetPeriod;
using MetadataLib for Lock;
using EfficiencyLib for bool;
using EfficiencyLib for uint8;
Expand Down
Loading