Skip to content

Commit 904d5d8

Browse files
committed
Merged SimpleAllocator
1 parent fe0477a commit 904d5d8

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

src/allocators/SimpleAllocator.sol

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ contract SimpleAllocator is ISimpleAllocator {
3838

3939
/// @inheritdoc ISimpleAllocator
4040
function lock(Compact calldata compact_) external {
41-
bytes32 tokenHash = _checkAllocation(compact_);
41+
bytes32 tokenHash = _checkAllocation(compact_, true);
4242

4343
bytes32 digest = keccak256(
4444
abi.encodePacked(
@@ -67,18 +67,10 @@ contract SimpleAllocator is ISimpleAllocator {
6767
}
6868

6969
/// @inheritdoc IAllocator
70-
function attest(address operator_, address from_, address, uint256 id_, uint256 amount_)
71-
external
72-
view
73-
returns (bytes4)
74-
{
70+
function attest(address, address from_, address, uint256 id_, uint256 amount_) external view returns (bytes4) {
7571
if (msg.sender != COMPACT_CONTRACT) {
7672
revert InvalidCaller(msg.sender, COMPACT_CONTRACT);
7773
}
78-
// For a transfer, the sponsor is the arbiter
79-
if (operator_ != from_) {
80-
revert InvalidCaller(operator_, from_);
81-
}
8274
uint256 balance = ERC6909(COMPACT_CONTRACT).balanceOf(from_, id_);
8375
// Check unlocked balance
8476
bytes32 tokenHash = _getTokenHash(id_, from_);
@@ -94,7 +86,7 @@ contract SimpleAllocator is ISimpleAllocator {
9486
revert InsufficientBalance(from_, id_, balance, fullAmount);
9587
}
9688

97-
return 0x1a808f91;
89+
return this.attest.selector;
9890
}
9991

10092
/// @inheritdoc IERC1271
@@ -165,12 +157,12 @@ contract SimpleAllocator is ISimpleAllocator {
165157
return keccak256(abi.encode(id_, sponsor_));
166158
}
167159

168-
function _checkAllocation(Compact memory compact_) internal view returns (bytes32) {
160+
function _checkAllocation(Compact memory compact_, bool checkSponsor_) internal view returns (bytes32) {
169161
// Check msg.sender is sponsor
170-
if (msg.sender != compact_.sponsor) {
162+
if (checkSponsor_ && msg.sender != compact_.sponsor) {
171163
revert InvalidCaller(msg.sender, compact_.sponsor);
172164
}
173-
bytes32 tokenHash = _getTokenHash(compact_.id, msg.sender);
165+
bytes32 tokenHash = _getTokenHash(compact_.id, compact_.sponsor);
174166
// Check no lock is already active for this sponsor
175167
if (
176168
_claim[tokenHash] > block.timestamp
@@ -204,10 +196,10 @@ contract SimpleAllocator is ISimpleAllocator {
204196
revert NonceAlreadyConsumed(compact_.nonce);
205197
}
206198

207-
uint256 balance = ERC6909(COMPACT_CONTRACT).balanceOf(msg.sender, compact_.id);
199+
uint256 balance = ERC6909(COMPACT_CONTRACT).balanceOf(compact_.sponsor, compact_.id);
208200
// Check balance is enough
209201
if (balance < compact_.amount) {
210-
revert InsufficientBalance(msg.sender, compact_.id, balance, compact_.amount);
202+
revert InsufficientBalance(compact_.sponsor, compact_.id, balance, compact_.amount);
211203
}
212204

213205
return tokenHash;

test/SimpleAllocator.t.sol

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -447,12 +447,6 @@ contract SimpleAllocator_Attest is Deposited {
447447
simpleAllocator.attest(address(user), address(user), address(usdc), usdcId, defaultAmount);
448448
}
449449

450-
function test_revert_InvalidCaller_FromNotOperator() public {
451-
vm.prank(attacker);
452-
vm.expectRevert(abi.encodeWithSelector(ISimpleAllocator.InvalidCaller.selector, attacker, user));
453-
compactContract.transfer(user, attacker, defaultAmount, address(usdc), address(simpleAllocator));
454-
}
455-
456450
function test_revert_InsufficientBalance_NoActiveLock(uint128 falseAmount_) public {
457451
vm.assume(falseAmount_ > defaultAmount);
458452

@@ -492,6 +486,34 @@ contract SimpleAllocator_Attest is Deposited {
492486
compactContract.transfer(user, attacker, defaultAmount, address(usdc), address(simpleAllocator));
493487
}
494488

489+
function test_successfullyAttested_returnsSelector() public {
490+
bytes4 selector = bytes4(0x1a808f91);
491+
492+
uint32 transferAmount = 10;
493+
uint32 lockedAmount = 90;
494+
495+
address otherUser = makeAddr('otherUser');
496+
497+
// Lock tokens
498+
uint256 defaultExpiration_ = vm.getBlockTimestamp() + defaultResetPeriod;
499+
vm.prank(user);
500+
simpleAllocator.lock(
501+
Compact({
502+
arbiter: arbiter,
503+
sponsor: user,
504+
nonce: defaultNonce,
505+
id: usdcId,
506+
expires: defaultExpiration_,
507+
amount: lockedAmount
508+
})
509+
);
510+
compactContract.transfer(user, otherUser, transferAmount, address(usdc), address(simpleAllocator));
511+
512+
vm.prank(address(compactContract));
513+
bytes4 returnedSelector = simpleAllocator.attest(user, user, otherUser, usdcId, transferAmount);
514+
assertEq(returnedSelector, selector);
515+
}
516+
495517
function test_successfullyAttested(uint32 lockedAmount_, uint32 transferAmount_) public {
496518
transferAmount_ = uint32(bound(transferAmount_, 0, defaultAmount));
497519
lockedAmount_ = uint32(bound(lockedAmount_, 0, defaultAmount - transferAmount_));

0 commit comments

Comments
 (0)