Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions snapshots/inputSettler.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
{
"BasePurchaseOrder": "138441",
"CompactFinaliseFor": "162750",
"CompactFinaliseSelf": "153910",
"CompactFinaliseTo": "153910",
"EscrowFinalise": "76401",
"IntegrationCoinFill": "90063",
"IntegrationCompactFinaliseSelf": "150493",
"IntegrationWormholeReceiveMessage": "73509",
"IntegrationWormholeSubmit": "42285",
"escrowFinaliseWithSignature": "85134",
"escrowOpen": "82779",
"escrowOpenFor3009Single": "113429",
"escrowOpenFor3009SingleArray": "119273",
"escrowOpenFor3009Two": "173808",
"escrowOpenForMsgSender": "83727",
"escrowOpenForPermit2": "125252",
"maxTimestamp1": "461",
"minTimestamp1": "483",
"validate2Fills": "9838",
"validate2FillsMultipleSolvers": "9838"
"BasePurchaseOrder": "147473",
"CompactFinaliseFor": "188158",
"CompactFinaliseSelf": "178213",
"CompactFinaliseTo": "178213",
"EscrowFinalise": "87161",
"IntegrationCoinFill": "97211",
"IntegrationCompactFinaliseSelf": "175393",
"IntegrationWormholeReceiveMessage": "78726",
"IntegrationWormholeSubmit": "48017",
"escrowFinaliseWithSignature": "96962",
"escrowOpen": "91061",
"escrowOpenFor3009Single": "128845",
"escrowOpenFor3009SingleArray": "139528",
"escrowOpenFor3009Two": "197035",
"escrowOpenForMsgSender": "92444",
"escrowOpenForPermit2": "136315",
"maxTimestamp1": "1075",
"minTimestamp1": "1097",
"validate2Fills": "15260",
"validate2FillsMultipleSolvers": "15260"
}
32 changes: 16 additions & 16 deletions snapshots/oracle.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"bitcoinFinaliseDispute": "63461",
"bitcoinOPVerify": "88119",
"bitcoinOutputClaim": "99976",
"bitcoinOutputDispute": "71910",
"bitcoinVerify": "135821",
"bitcoinVerifyWithEmbed": "138439",
"citreaFinaliseDispute": "63461",
"citreaOPVerify": "88119",
"citreaOutputClaim": "99976",
"citreaOutputDispute": "71910",
"citreaVerify": "131077",
"citreaVerifyWithEmbed": "133695",
"hyperlaneOracleHandle": "25901",
"hyperlaneOracleSubmit": "37425",
"hyperlaneOracleSubmitCustomHook": "37421",
"wormholeOracleSubmit": "13837"
"bitcoinFinaliseDispute": "70161",
"bitcoinOPVerify": "97861",
"bitcoinOutputClaim": "105069",
"bitcoinOutputDispute": "77535",
"bitcoinVerify": "175520",
"bitcoinVerifyWithEmbed": "178027",
"citreaFinaliseDispute": "70161",
"citreaOPVerify": "97861",
"citreaOutputClaim": "105069",
"citreaOutputDispute": "77535",
"citreaVerify": "170275",
"citreaVerifyWithEmbed": "172782",
"hyperlaneOracleHandle": "27527",
"hyperlaneOracleSubmit": "44824",
"hyperlaneOracleSubmitCustomHook": "45003",
"wormholeOracleSubmit": "19569"
}
14 changes: 7 additions & 7 deletions snapshots/outputSettler.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"outputSettlerCoinFill": "83493",
"outputSettlerCoinFillDutchAuction": "84756",
"outputSettlerCoinFillExclusive": "84889",
"outputSettlerCoinFillExclusiveDutchAuction": "85636",
"outputSettlerCoinFillNative": "89524",
"outputSettlerCoinFillOrderOutputsNative": "129225",
"outputSettlerCoinfillOrderOutputs": "120238"
"outputSettlerCoinFill": "90768",
"outputSettlerCoinFillDutchAuction": "92738",
"outputSettlerCoinFillExclusive": "92379",
"outputSettlerCoinFillExclusiveDutchAuction": "93716",
"outputSettlerCoinFillNative": "95195",
"outputSettlerCoinFillOrderOutputsNative": "140522",
"outputSettlerCoinfillOrderOutputs": "134744"
}
78 changes: 65 additions & 13 deletions src/input/escrow/InputSettlerEscrow.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ contract InputSettlerEscrow is InputSettlerPurchase, IInputSettlerEscrow {
* Signature type not supported.
*/
error SignatureNotSupported(bytes1);
/**
* @dev An ERC-3009 collection did not increase this contract's token balance by exactly the input amount.
*/
error InvalidBalanceDelta(uint256 expectedBalance, uint256 actualBalance);

/**
* @notice Emitted when an order is opened.
Expand Down Expand Up @@ -289,6 +293,11 @@ contract InputSettlerEscrow is InputSettlerPurchase, IInputSettlerEscrow {
* @notice Helper function for using ERC-3009 to collect assets represented by a StandardOrder.
* @dev For the `receiveWithAuthorization` call, the nonce is set as the orderId to select the order associated with
* the authorization.
*
* Every collection is strictly balance-checked: this contract's token balance must increase by exactly the input
* amount or the call reverts with {InvalidBalanceDelta}. This is required on the ERC-3009 path in particular
* because the single-input branch performs a raw `call` whose success would otherwise be trusted without any
* evidence that tokens moved.
* @param inputs Order inputs to be collected.
* @param fillDeadline Deadline for calling the open function.
* @param signer Provider of the ERC-3009 funds and signer of the intent.
Expand Down Expand Up @@ -322,30 +331,73 @@ contract InputSettlerEscrow is InputSettlerPurchase, IInputSettlerEscrow {
// })
address token = input[0].validatedCleanAddress();
IsContractLib.validateContainsCode(token); // Ensure called contract has code.
uint256 balanceBefore = IERC20(token).balanceOf(address(this));
(bool success,) = token.call(callData);
if (success) return;
// If the call succeeded, require the exact balance increase.
if (success) {
_validateBalanceIncrease(token, balanceBefore, input[1]);
return;
}
// Otherwise it could be because of a lot of reasons. One being the signature is abi.encoded as bytes[].
}
{
uint256 numSignatures = BytesLib.getLengthOfBytesArray(_signature_);
if (numInputs != numSignatures) revert SignatureAndInputsNotEqual();
}
for (uint256 i; i < numInputs; ++i) {
uint256[2] calldata input = inputs[i];
bytes calldata signature = BytesLib.getBytesOfArray(_signature_, i);
// forgefmt: disable-next-line
IERC3009(input[0].validatedCleanAddress()).receiveWithAuthorization({
from: signer,
to: address(this),
value: input[1],
validAfter: 0,
validBefore: fillDeadline,
nonce: orderId,
signature: signature
});
_receiveWithAuthorization(
inputs[i], fillDeadline, signer, BytesLib.getBytesOfArray(_signature_, i), orderId
);
}
}

/**
* @notice Collects a single ERC-3009 input and validates the exact balance increase.
* @param input Order input to be collected.
* @param fillDeadline Deadline for calling the open function.
* @param signer Provider of the ERC-3009 funds and signer of the intent.
* @param signature ERC-3009 signature authorizing the collection of `input`.
* @param orderId The order identifier, used as the authorization nonce.
*/
function _receiveWithAuthorization(
uint256[2] calldata input,
uint32 fillDeadline,
address signer,
bytes calldata signature,
bytes32 orderId
) internal {
address token = input[0].validatedCleanAddress();
uint256 balanceBefore = IERC20(token).balanceOf(address(this));
// forgefmt: disable-next-line
IERC3009(token).receiveWithAuthorization({
from: signer,
to: address(this),
value: input[1],
validAfter: 0,
validBefore: fillDeadline,
nonce: orderId,
signature: signature
});
_validateBalanceIncrease(token, balanceBefore, input[1]);
}

/**
* @notice Validates that this contract's balance of `token` increased by exactly `amount` over `balanceBefore`.
* @dev The expected balance is computed with checked arithmetic because `amount` is attacker-controlled.
* @param token The collected token.
* @param balanceBefore This contract's balance of `token` before the collection.
* @param amount The exact increase required.
*/
function _validateBalanceIncrease(
address token,
uint256 balanceBefore,
uint256 amount
) internal view {
uint256 expectedBalance = balanceBefore + amount;
uint256 actualBalance = IERC20(token).balanceOf(address(this));
if (actualBalance != expectedBalance) revert InvalidBalanceDelta(expectedBalance, actualBalance);
}

// --- Refund --- //

/**
Expand Down
65 changes: 65 additions & 0 deletions test/input/escrow/InputSettlerEscrow.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { MandateOutputEncodingLib } from "../../../src/libs/MandateOutputEncodin

import { InputSettlerBase } from "../../../src/input/InputSettlerBase.sol";
import { InputSettlerPurchase } from "../../../src/input/InputSettlerPurchase.sol";
import { MockERC20Fallback } from "../../mocks/MockERC20Fallback.sol";
import { InputSettlerEscrowTestBase } from "./InputSettlerEscrow.base.t.sol";

contract InputSettlerEscrowTest is InputSettlerEscrowTestBase {
Expand Down Expand Up @@ -346,6 +347,70 @@ contract InputSettlerEscrowTest is InputSettlerEscrowTestBase {
assertEq(token.balanceOf(inputSettlerEscrow), amount);
}

function test_open_for_3009_single_fallback_reverts() external {
MockERC20Fallback fallbackToken = new MockERC20Fallback();
uint256 amount = 10 ** 18;

MandateOutput[] memory outputs = new MandateOutput[](0);

uint256[2][] memory inputs = new uint256[2][](1);
inputs[0] = [uint256(uint160(address(fallbackToken))), amount];

StandardOrder memory order = StandardOrder({
user: swapper,
nonce: 0,
originChainId: block.chainid,
expires: type(uint32).max,
fillDeadline: type(uint32).max,
inputOracle: address(0),
inputs: inputs,
outputs: outputs
});

vm.prank(swapper);
vm.expectRevert(abi.encodeWithSelector(InputSettlerEscrow.InvalidBalanceDelta.selector, amount, 0));
IInputSettlerEscrow(inputSettlerEscrow)
.openFor(order, order.user, abi.encodePacked(bytes1(0x01), new bytes(65)));

bytes32 orderId = InputSettlerEscrow(inputSettlerEscrow).orderIdentifier(order);
assertEq(uint8(InputSettlerEscrow(inputSettlerEscrow).orderStatus(orderId)), 0);
}

function test_open_for_3009_two_fallback_as_array_reverts() external {
MockERC20Fallback fallbackToken = new MockERC20Fallback();
MockERC20Fallback anotherFallbackToken = new MockERC20Fallback();
uint256 amount = 10 ** 18;

MandateOutput[] memory outputs = new MandateOutput[](0);

uint256[2][] memory inputs = new uint256[2][](2);
inputs[0] = [uint256(uint160(address(fallbackToken))), amount];
inputs[1] = [uint256(uint160(address(anotherFallbackToken))), amount];

StandardOrder memory order = StandardOrder({
user: swapper,
nonce: 0,
originChainId: block.chainid,
expires: type(uint32).max,
fillDeadline: type(uint32).max,
inputOracle: address(0),
inputs: inputs,
outputs: outputs
});

bytes[] memory signatures = new bytes[](2);
signatures[0] = new bytes(65);
signatures[1] = new bytes(65);

vm.prank(swapper);
vm.expectRevert(abi.encodeWithSelector(InputSettlerEscrow.InvalidBalanceDelta.selector, amount, 0));
IInputSettlerEscrow(inputSettlerEscrow)
.openFor(order, order.user, abi.encodePacked(bytes1(0x01), abi.encode(signatures)));

bytes32 orderId = InputSettlerEscrow(inputSettlerEscrow).orderIdentifier(order);
assertEq(uint8(InputSettlerEscrow(inputSettlerEscrow).orderStatus(orderId)), 0);
}

/// forge-config: default.isolate = true
function test_open_for_3009_two_as_array() external {
test_open_for_3009_two_as_array(10 ** 18, 251251);
Expand Down
10 changes: 10 additions & 0 deletions test/mocks/MockERC20Fallback.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;

contract MockERC20Fallback {
mapping(address => uint256) public balanceOf;

receive() external payable { }

fallback() external payable { }
}
Loading