Skip to content

Commit

Permalink
clean up a few bits and bobs
Browse files Browse the repository at this point in the history
  • Loading branch information
0age committed Oct 9, 2024
1 parent 294f46e commit d129674
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 73 deletions.
141 changes: 69 additions & 72 deletions src/TheCompact.sol
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ contract TheCompact is ITheCompact, ERC6909 {
payable
returns (uint256 id)
{
Lock memory lock = address(0).toLock(allocator, resetPeriod, scope);
id = lock.toId();
id = address(0).toIdIfRegistered(scope, resetPeriod, allocator);

_deposit(msg.sender, recipient, id, msg.value);
}
Expand All @@ -118,8 +117,7 @@ contract TheCompact is ITheCompact, ERC6909 {
revert InvalidToken(token);
}

Lock memory lock = token.toLock(allocator, resetPeriod, scope);
id = lock.toId();
id = token.toIdIfRegistered(scope, resetPeriod, allocator);

token.safeTransferFrom(msg.sender, address(this), amount);

Expand Down Expand Up @@ -204,8 +202,7 @@ contract TheCompact is ITheCompact, ERC6909 {
revert InvalidToken(token);
}

Lock memory lock = token.toLock(allocator, resetPeriod, scope);
id = lock.toId();
id = token.toIdIfRegistered(scope, resetPeriod, allocator);

ISignatureTransfer.SignatureTransferDetails memory signatureTransferDetails =
ISignatureTransfer.SignatureTransferDetails({ to: address(this), requestedAmount: amount });
Expand Down Expand Up @@ -272,7 +269,7 @@ contract TheCompact is ITheCompact, ERC6909 {
}
}

uint256 initialId = address(0).toLock(allocator, resetPeriod, scope).toId();
uint256 initialId = address(0).toIdIfRegistered(scope, resetPeriod, allocator);

bytes32 witness = keccak256(
abi.encode(
Expand All @@ -294,71 +291,6 @@ contract TheCompact is ITheCompact, ERC6909 {
);
}

function _processBatchPermit2Deposits(
bool firstUnderlyingTokenIsNative,
address recipient,
uint256 initialId,
uint256 totalTokens,
ISignatureTransfer.TokenPermissions[] calldata permitted,
address depositor,
uint256 nonce,
uint256 deadline,
bytes32 witness,
bytes calldata signature
) internal returns (uint256[] memory ids) {
ids = new uint256[](totalTokens);

uint256 totalTokensLessInitialNative;
unchecked {
totalTokensLessInitialNative = totalTokens - firstUnderlyingTokenIsNative.asUint256();
}

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

unchecked {
ISignatureTransfer.SignatureTransferDetails[] memory details =
new ISignatureTransfer.SignatureTransferDetails[](totalTokensLessInitialNative);

ISignatureTransfer.TokenPermissions[] memory permittedTokens =
new ISignatureTransfer.TokenPermissions[](totalTokensLessInitialNative);

for (uint256 i = 0; i < totalTokensLessInitialNative; ++i) {
ISignatureTransfer.TokenPermissions calldata permittedToken =
permitted[i + firstUnderlyingTokenIsNative.asUint256()];

permittedTokens[i] = permittedToken;
details[i] = ISignatureTransfer.SignatureTransferDetails({
to: address(this),
requestedAmount: permittedToken.amount
});

uint256 id = initialId.withReplacedToken(permittedToken.token);
ids[i + firstUnderlyingTokenIsNative.asUint256()] = id;

_deposit(depositor, recipient, id, permittedToken.amount);
}

ISignatureTransfer.PermitBatchTransferFrom memory permitTransferFrom =
ISignatureTransfer.PermitBatchTransferFrom({
permitted: permittedTokens,
nonce: nonce,
deadline: deadline
});

_PERMIT2.permitWitnessTransferFrom(
permitTransferFrom,
details,
depositor,
witness,
"CompactDeposit witness)CompactDeposit(address depositor,address allocator,uint8 resetPeriod,uint8 scope,address recipient)TokenPermissions(address token,uint256 amount)",
signature
);
}
}

function claim(
Allocation calldata allocation,
AllocationAuthorization calldata allocationAuthorization,
Expand Down Expand Up @@ -792,6 +724,71 @@ contract TheCompact is ITheCompact, ERC6909 {
);
}

function _processBatchPermit2Deposits(
bool firstUnderlyingTokenIsNative,
address recipient,
uint256 initialId,
uint256 totalTokens,
ISignatureTransfer.TokenPermissions[] calldata permitted,
address depositor,
uint256 nonce,
uint256 deadline,
bytes32 witness,
bytes calldata signature
) internal returns (uint256[] memory ids) {
ids = new uint256[](totalTokens);

uint256 totalTokensLessInitialNative;
unchecked {
totalTokensLessInitialNative = totalTokens - firstUnderlyingTokenIsNative.asUint256();
}

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

unchecked {
ISignatureTransfer.SignatureTransferDetails[] memory details =
new ISignatureTransfer.SignatureTransferDetails[](totalTokensLessInitialNative);

ISignatureTransfer.TokenPermissions[] memory permittedTokens =
new ISignatureTransfer.TokenPermissions[](totalTokensLessInitialNative);

for (uint256 i = 0; i < totalTokensLessInitialNative; ++i) {
ISignatureTransfer.TokenPermissions calldata permittedToken =
permitted[i + firstUnderlyingTokenIsNative.asUint256()];

permittedTokens[i] = permittedToken;
details[i] = ISignatureTransfer.SignatureTransferDetails({
to: address(this),
requestedAmount: permittedToken.amount
});

uint256 id = initialId.withReplacedToken(permittedToken.token);
ids[i + firstUnderlyingTokenIsNative.asUint256()] = id;

_deposit(depositor, recipient, id, permittedToken.amount);
}

ISignatureTransfer.PermitBatchTransferFrom memory permitTransferFrom =
ISignatureTransfer.PermitBatchTransferFrom({
permitted: permittedTokens,
nonce: nonce,
deadline: deadline
});

_PERMIT2.permitWitnessTransferFrom(
permitTransferFrom,
details,
depositor,
witness,
"CompactDeposit witness)CompactDeposit(address depositor,address allocator,uint8 resetPeriod,uint8 scope,address recipient)TokenPermissions(address token,uint256 amount)",
signature
);
}
}

function _deriveClaimAmountsAndEmitEvents(
BatchAllocation calldata batchAllocation,
BatchAllocationAuthorization calldata batchAllocationAuthorization,
Expand Down
13 changes: 13 additions & 0 deletions src/interfaces/ITheCompact.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
BatchAllocation,
BatchAllocationAuthorization
} from "../types/EIP712Types.sol";
import { ISignatureTransfer } from "permit2/src/interfaces/ISignatureTransfer.sol";

/**
* @title The Compact
Expand Down Expand Up @@ -88,6 +89,18 @@ interface ITheCompact {
bytes calldata signature
) external returns (uint256 id);

function deposit(
address depositor,
ISignatureTransfer.TokenPermissions[] calldata permitted,
address allocator,
ResetPeriod resetPeriod,
Scope scope,
address recipient,
uint256 nonce,
uint256 deadline,
bytes calldata signature
) external payable returns (uint256[] memory ids);

function claim(
Allocation calldata allocation,
AllocationAuthorization calldata allocationAuthorization,
Expand Down
6 changes: 6 additions & 0 deletions src/lib/EfficiencyLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ library EfficiencyLib {
}
}

function asUint256(uint96 a) internal pure returns (uint256 b) {
assembly {
b := a
}
}

function asUint256(Scope a) internal pure returns (uint256 b) {
assembly {
b := a
Expand Down
12 changes: 11 additions & 1 deletion src/lib/IdLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ library IdLib {
using IdLib for address;
using MetadataLib for Lock;
using EfficiencyLib for uint8;
using EfficiencyLib for uint96;
using EfficiencyLib for uint256;
using EfficiencyLib for address;
using EfficiencyLib for ResetPeriod;
Expand Down Expand Up @@ -159,7 +160,16 @@ library IdLib {
function toId(Lock memory lock) internal pure returns (uint256 id) {
id = (
(lock.scope.asUint256() << 255) | (lock.resetPeriod.asUint256() << 252)
| (uint256(lock.allocator.usingAllocatorId()) << 160) | lock.token.asUint256()
| (lock.allocator.usingAllocatorId().asUint256() << 160) | lock.token.asUint256()
);
}

function toIdIfRegistered(address token, Scope scope, ResetPeriod resetPeriod, address allocator) internal view returns (uint256 id) {
uint96 allocatorId = allocator.usingAllocatorId();
allocatorId.mustHaveARegisteredAllocator();
id = (
(scope.asUint256() << 255) | (resetPeriod.asUint256() << 252)
| (allocatorId.asUint256() << 160) | token.asUint256()
);
}

Expand Down

0 comments on commit d129674

Please sign in to comment.