Skip to content

Commit 0fbcc2d

Browse files
committed
add AllocatorLogic and RegistrationLogic
1 parent efd14f4 commit 0fbcc2d

File tree

6 files changed

+272
-144
lines changed

6 files changed

+272
-144
lines changed

snapshots/TheCompactTest.json

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
{
2-
"basicTransfer": "56861",
3-
"basicWithdrawal": "59819",
4-
"batchClaim": "111831",
5-
"batchClaimRegisteredWithDeposit": "111831",
6-
"batchClaimRegisteredWithDepositWithWitness": "112593",
7-
"batchClaimWithWitness": "112587",
2+
"basicTransfer": "56858",
3+
"basicWithdrawal": "59816",
4+
"batchClaim": "111828",
5+
"batchClaimRegisteredWithDeposit": "111828",
6+
"batchClaimRegisteredWithDepositWithWitness": "112590",
7+
"batchClaimWithWitness": "112584",
88
"batchDepositAndRegisterViaPermit2": "221900",
99
"batchDepositAndRegisterWithWitnessViaPermit2": "221878",
10-
"batchTransfer": "81539",
11-
"batchWithdrawal": "99954",
12-
"claim": "56978",
13-
"claimAndWithdraw": "73269",
14-
"claimWithWitness": "59443",
10+
"batchTransfer": "81536",
11+
"batchWithdrawal": "99951",
12+
"claim": "56975",
13+
"claimAndWithdraw": "73266",
14+
"claimWithWitness": "59440",
1515
"depositAndRegisterViaPermit2": "124270",
1616
"depositBatchSingleERC20": "67868",
1717
"depositBatchSingleNative": "28171",
@@ -22,21 +22,21 @@
2222
"depositERC20ViaPermit2AndURI": "98312",
2323
"depositETHAndURI": "26777",
2424
"depositETHBasic": "28384",
25-
"qualifiedBatchClaim": "113249",
26-
"qualifiedBatchClaimWithWitness": "112713",
27-
"qualifiedClaim": "60237",
28-
"qualifiedClaimWithWitness": "58777",
29-
"qualifiedSplitBatchClaim": "140937",
30-
"qualifiedSplitBatchClaimWithWitness": "140929",
31-
"qualifiedSplitClaim": "86524",
32-
"qualifiedSplitClaimWithWitness": "86890",
25+
"qualifiedBatchClaim": "113246",
26+
"qualifiedBatchClaimWithWitness": "112710",
27+
"qualifiedClaim": "60234",
28+
"qualifiedClaimWithWitness": "58774",
29+
"qualifiedSplitBatchClaim": "140934",
30+
"qualifiedSplitBatchClaimWithWitness": "140926",
31+
"qualifiedSplitClaim": "86521",
32+
"qualifiedSplitClaimWithWitness": "86887",
3333
"register": "25379",
34-
"splitBatchClaim": "140399",
35-
"splitBatchClaimWithWitness": "140363",
36-
"splitBatchTransfer": "110614",
37-
"splitBatchWithdrawal": "139822",
38-
"splitClaim": "86447",
39-
"splitClaimWithWitness": "85928",
40-
"splitTransfer": "82751",
41-
"splitWithdrawal": "93705"
34+
"splitBatchClaim": "140396",
35+
"splitBatchClaimWithWitness": "140360",
36+
"splitBatchTransfer": "110611",
37+
"splitBatchWithdrawal": "139819",
38+
"splitClaim": "86444",
39+
"splitClaimWithWitness": "85925",
40+
"splitTransfer": "82748",
41+
"splitWithdrawal": "93702"
4242
}

src/TheCompact.sol

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { Scope } from "./types/Scope.sol";
1111
import { ResetPeriod } from "./types/ResetPeriod.sol";
1212
import { ForcedWithdrawalStatus } from "./types/ForcedWithdrawalStatus.sol";
1313

14+
import { AllocatorLogic } from "./lib/AllocatorLogic.sol";
1415
import { ClaimProcessor } from "./lib/ClaimProcessor.sol";
1516
import { Extsload } from "./lib/Extsload.sol";
1617

@@ -25,7 +26,7 @@ import { ISignatureTransfer } from "permit2/src/interfaces/ISignatureTransfer.so
2526
* formation and mediation of reusable "resource locks."
2627
* This contract has not yet been properly tested, audited, or reviewed.
2728
*/
28-
contract TheCompact is ITheCompact, ClaimProcessor, ERC6909, Extsload {
29+
contract TheCompact is ITheCompact, AllocatorLogic, ClaimProcessor, ERC6909, Extsload {
2930
function deposit(address allocator) external payable returns (uint256) {
3031
return _performBasicNativeTokenDeposit(allocator);
3132
}

src/lib/AllocatorLogic.sol

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.27;
3+
4+
import { BatchTransfer, SplitBatchTransfer } from "../types/BatchClaims.sol";
5+
import { BasicTransfer, SplitTransfer } from "../types/Claims.sol";
6+
import { CompactCategory } from "../types/CompactCategory.sol";
7+
import { SplitComponent, TransferComponent, SplitByIdComponent } from "../types/Components.sol";
8+
import { ForcedWithdrawalStatus } from "../types/ForcedWithdrawalStatus.sol";
9+
import { Lock } from "../types/Lock.sol";
10+
import { ResetPeriod } from "../types/ResetPeriod.sol";
11+
import { Scope } from "../types/Scope.sol";
12+
13+
import { ConsumerLib } from "./ConsumerLib.sol";
14+
import { EfficiencyLib } from "./EfficiencyLib.sol";
15+
import { FunctionCastLib } from "./FunctionCastLib.sol";
16+
import { HashLib } from "./HashLib.sol";
17+
import { IdLib } from "./IdLib.sol";
18+
import { MetadataRenderer } from "./MetadataRenderer.sol";
19+
import { ValidityLib } from "./ValidityLib.sol";
20+
21+
import { SafeTransferLib } from "solady/utils/SafeTransferLib.sol";
22+
import { Tstorish } from "tstorish/Tstorish.sol";
23+
import { ISignatureTransfer } from "permit2/src/interfaces/ISignatureTransfer.sol";
24+
25+
contract AllocatorLogic {
26+
using HashLib for address;
27+
using HashLib for bytes32;
28+
using HashLib for uint256;
29+
using HashLib for BasicTransfer;
30+
using HashLib for SplitTransfer;
31+
using HashLib for BatchTransfer;
32+
using HashLib for SplitBatchTransfer;
33+
using IdLib for uint96;
34+
using IdLib for uint256;
35+
using IdLib for address;
36+
using IdLib for Lock;
37+
using IdLib for ResetPeriod;
38+
using SafeTransferLib for address;
39+
using ConsumerLib for uint256;
40+
using EfficiencyLib for bool;
41+
using EfficiencyLib for bytes32;
42+
using EfficiencyLib for uint256;
43+
using ValidityLib for address;
44+
using ValidityLib for uint96;
45+
using ValidityLib for uint256;
46+
using ValidityLib for bytes32;
47+
using FunctionCastLib for function(bytes32, address, BasicTransfer calldata) internal;
48+
49+
function _consume(uint256[] calldata nonces) internal returns (bool) {
50+
// NOTE: this may not be necessary, consider removing
51+
msg.sender.usingAllocatorId().mustHaveARegisteredAllocator();
52+
53+
unchecked {
54+
uint256 i;
55+
56+
assembly ("memory-safe") {
57+
i := nonces.offset
58+
}
59+
60+
uint256 end = i + (nonces.length << 5);
61+
uint256 nonce;
62+
for (; i < end; i += 0x20) {
63+
assembly ("memory-safe") {
64+
nonce := calldataload(i)
65+
}
66+
nonce.consumeNonceAsAllocator(msg.sender);
67+
}
68+
}
69+
70+
return true;
71+
}
72+
73+
function _registerAllocator(address allocator, bytes calldata proof) internal returns (uint96 allocatorId) {
74+
allocator = uint256(uint160(allocator)).asSanitizedAddress();
75+
if (!allocator.canBeRegistered(proof)) {
76+
assembly ("memory-safe") {
77+
// revert InvalidRegistrationProof(allocator)
78+
mstore(0, 0x4e7f492b)
79+
mstore(0x20, allocator)
80+
revert(0x1c, 0x24)
81+
}
82+
}
83+
84+
allocatorId = allocator.register();
85+
}
86+
87+
function _hasConsumedAllocatorNonce(uint256 nonce, address allocator) internal view returns (bool) {
88+
return allocator.hasConsumedAllocatorNonce(nonce);
89+
}
90+
91+
function _getLockDetails(uint256 id) internal view returns (address token, address allocator, ResetPeriod resetPeriod, Scope scope) {
92+
token = id.toToken();
93+
allocator = id.toAllocatorId().toRegisteredAllocator();
94+
resetPeriod = id.toResetPeriod();
95+
scope = id.toScope();
96+
}
97+
}

src/lib/InternalLogic.sol

Lines changed: 0 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,6 @@ contract InternalLogic is Tstorish {
8484
_PERMIT2_INITIALLY_DEPLOYED = _checkPermit2Deployment();
8585
}
8686

87-
function _emitClaim(address sponsor, bytes32 messageHash, address allocator) internal {
88-
assembly ("memory-safe") {
89-
mstore(0, messageHash)
90-
log4(0, 0x20, _CLAIM_EVENT_SIGNATURE, shr(0x60, shl(0x60, sponsor)), shr(0x60, shl(0x60, allocator)), caller())
91-
}
92-
}
93-
94-
function _notExpiredAndSignedByAllocator(bytes32 messageHash, address allocator, BasicTransfer calldata transferPayload) internal {
95-
transferPayload.expires.later();
96-
97-
messageHash.signedBy(allocator, transferPayload.allocatorSignature, _INITIAL_DOMAIN_SEPARATOR.toLatest(_INITIAL_CHAIN_ID));
98-
99-
_emitClaim(msg.sender, messageHash, allocator);
100-
}
101-
10287
function _setReentrancyGuard() internal {
10388
_setTstorish(_REENTRANCY_GUARD_SLOT, 1);
10489
}
@@ -107,105 +92,6 @@ contract InternalLogic is Tstorish {
10792
_clearTstorish(_REENTRANCY_GUARD_SLOT);
10893
}
10994

110-
function _register(address sponsor, bytes32 claimHash, bytes32 typehash, uint256 duration) internal {
111-
assembly ("memory-safe") {
112-
let m := mload(0x40)
113-
mstore(add(m, 0x14), sponsor)
114-
mstore(m, _ACTIVE_REGISTRATIONS_SCOPE)
115-
mstore(add(m, 0x34), claimHash)
116-
mstore(add(m, 0x54), typehash)
117-
let cutoffSlot := keccak256(add(m, 0x1c), 0x58)
118-
119-
let expires := add(timestamp(), duration)
120-
if or(lt(expires, sload(cutoffSlot)), gt(duration, 0x278d00)) {
121-
// revert InvalidRegistrationDuration(uint256 duration)
122-
mstore(0, 0x1f9a96f4)
123-
mstore(0x20, duration)
124-
revert(0x1c, 0x24)
125-
}
126-
127-
sstore(cutoffSlot, expires)
128-
mstore(add(m, 0x74), expires)
129-
log2(add(m, 0x34), 0x60, _COMPACT_REGISTERED_SIGNATURE, shr(0x60, shl(0x60, sponsor)))
130-
}
131-
}
132-
133-
function _registerWithDefaults(bytes32 claimHash, bytes32 typehash) internal {
134-
_register(msg.sender, claimHash, typehash, uint256(0x258).asStubborn());
135-
}
136-
137-
function _registerBatch(bytes32[2][] calldata claimHashesAndTypehashes, uint256 duration) internal returns (bool) {
138-
unchecked {
139-
uint256 totalClaimHashes = claimHashesAndTypehashes.length;
140-
for (uint256 i = 0; i < totalClaimHashes; ++i) {
141-
bytes32[2] calldata claimHashAndTypehash = claimHashesAndTypehashes[i];
142-
_register(msg.sender, claimHashAndTypehash[0], claimHashAndTypehash[1], duration);
143-
}
144-
}
145-
146-
return true;
147-
}
148-
149-
function _consume(uint256[] calldata nonces) internal returns (bool) {
150-
// NOTE: this may not be necessary, consider removing
151-
msg.sender.usingAllocatorId().mustHaveARegisteredAllocator();
152-
153-
unchecked {
154-
uint256 i;
155-
156-
assembly ("memory-safe") {
157-
i := nonces.offset
158-
}
159-
160-
uint256 end = i + (nonces.length << 5);
161-
uint256 nonce;
162-
for (; i < end; i += 0x20) {
163-
assembly ("memory-safe") {
164-
nonce := calldataload(i)
165-
}
166-
nonce.consumeNonceAsAllocator(msg.sender);
167-
}
168-
}
169-
170-
return true;
171-
}
172-
173-
function _registerAllocator(address allocator, bytes calldata proof) internal returns (uint96 allocatorId) {
174-
allocator = uint256(uint160(allocator)).asSanitizedAddress();
175-
if (!allocator.canBeRegistered(proof)) {
176-
assembly ("memory-safe") {
177-
// revert InvalidRegistrationProof(allocator)
178-
mstore(0, 0x4e7f492b)
179-
mstore(0x20, allocator)
180-
revert(0x1c, 0x24)
181-
}
182-
}
183-
184-
allocatorId = allocator.register();
185-
}
186-
187-
function _getLockDetails(uint256 id) internal view returns (address token, address allocator, ResetPeriod resetPeriod, Scope scope) {
188-
token = id.toToken();
189-
allocator = id.toAllocatorId().toRegisteredAllocator();
190-
resetPeriod = id.toResetPeriod();
191-
scope = id.toScope();
192-
}
193-
194-
function _hasConsumedAllocatorNonce(uint256 nonce, address allocator) internal view returns (bool) {
195-
return allocator.hasConsumedAllocatorNonce(nonce);
196-
}
197-
198-
function _getRegistrationStatus(address sponsor, bytes32 claimHash, bytes32 typehash) internal view returns (uint256 expires) {
199-
assembly ("memory-safe") {
200-
let m := mload(0x40)
201-
mstore(add(m, 0x14), sponsor)
202-
mstore(m, _ACTIVE_REGISTRATIONS_SCOPE)
203-
mstore(add(m, 0x34), claimHash)
204-
mstore(add(m, 0x54), typehash)
205-
expires := sload(keccak256(add(m, 0x1c), 0x58))
206-
}
207-
}
208-
20995
function _isPermit2Deployed() internal view returns (bool) {
21096
if (_PERMIT2_INITIALLY_DEPLOYED) {
21197
return true;

0 commit comments

Comments
 (0)