Skip to content

Commit

Permalink
return error and assert the permit error
Browse files Browse the repository at this point in the history
  • Loading branch information
saucepoint committed Aug 29, 2024
1 parent 0d0ada1 commit 4efa23f
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
154534
154533
Original file line number Diff line number Diff line change
@@ -1 +1 @@
144550
144548
Original file line number Diff line number Diff line change
@@ -1 +1 @@
349846
349845
Original file line number Diff line number Diff line change
@@ -1 +1 @@
349149
349147
Original file line number Diff line number Diff line change
@@ -1 +1 @@
375379
375377
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_withSettlePair.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
375794
375793
14 changes: 12 additions & 2 deletions src/base/Permit2Forwarder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {IAllowanceTransfer} from "permit2/src/interfaces/IAllowanceTransfer.sol"
contract Permit2Forwarder {
IAllowanceTransfer public immutable permit2;

error Wrap__Permit2Reverted(address _permit2, bytes reason);

constructor(IAllowanceTransfer _permit2) {
permit2 = _permit2;
}
Expand All @@ -17,18 +19,26 @@ contract Permit2Forwarder {
function permit(address owner, IAllowanceTransfer.PermitSingle calldata permitSingle, bytes calldata signature)
external
payable
returns (bytes memory err)
{
// use try/catch in case an actor front-runs the permit, which would DOS multicalls
try permit2.permit(owner, permitSingle, signature) {} catch {}
try permit2.permit(owner, permitSingle, signature) {}
catch (bytes memory reason) {
err = abi.encodeWithSelector(Wrap__Permit2Reverted.selector, address(permit2), reason);
}
}

/// @notice allows forwarding batch permits to permit2
/// @dev this function is payable to allow multicall with NATIVE based actions
function permitBatch(address owner, IAllowanceTransfer.PermitBatch calldata _permitBatch, bytes calldata signature)
external
payable
returns (bytes memory err)
{
// use try/catch in case an actor front-runs the permit, which would DOS multicalls
try permit2.permit(owner, _permitBatch, signature) {} catch {}
try permit2.permit(owner, _permitBatch, signature) {}
catch (bytes memory reason) {
err = abi.encodeWithSelector(Wrap__Permit2Reverted.selector, address(permit2), reason);
}
}
}
37 changes: 35 additions & 2 deletions test/position-managers/PositionManager.multicall.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ contract PositionManagerMulticallTest is Test, Permit2SignatureHelpers, PosmTest
uint48 permitExpiration = uint48(block.timestamp + 10e18);
uint48 permitNonce = 0;

// redefine error from permit2/src/PermitErrors.sol since its hard-pinned to a solidity version
error InvalidNonce();

bytes32 PERMIT2_DOMAIN_SEPARATOR;

PositionConfig config;
Expand Down Expand Up @@ -379,7 +382,27 @@ contract PositionManagerMulticallTest is Test, Permit2SignatureHelpers, PosmTest
vm.expectRevert();
lpm.ownerOf(tokenId); // token does not exist

lpm.multicall(calls);
bytes[] memory results = lpm.multicall(calls);
assertEq(
results[0],
abi.encode(
abi.encodeWithSelector(
Permit2Forwarder.Wrap__Permit2Reverted.selector,
address(permit2),
abi.encodeWithSelector(InvalidNonce.selector)
)
)
);
assertEq(
results[1],
abi.encode(
abi.encodeWithSelector(
Permit2Forwarder.Wrap__Permit2Reverted.selector,
address(permit2),
abi.encodeWithSelector(InvalidNonce.selector)
)
)
);

assertEq(lpm.ownerOf(tokenId), charlie);
}
Expand Down Expand Up @@ -428,7 +451,17 @@ contract PositionManagerMulticallTest is Test, Permit2SignatureHelpers, PosmTest
vm.expectRevert();
lpm.ownerOf(tokenId); // token does not exist

lpm.multicall(calls);
bytes[] memory results = lpm.multicall(calls);
assertEq(
results[0],
abi.encode(
abi.encodeWithSelector(
Permit2Forwarder.Wrap__Permit2Reverted.selector,
address(permit2),
abi.encodeWithSelector(InvalidNonce.selector)
)
)
);

assertEq(lpm.ownerOf(tokenId), charlie);
}
Expand Down

0 comments on commit 4efa23f

Please sign in to comment.