Skip to content
Verified contractPartialMIT

PropAMMSwapHelpers

Address0xdb841f6dc2ab60e06ba20d4bb7d7d6ed6e94ac70
Balance

0 ETH

 

Transactions

0

 

Compiler

solc 0.8.28

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.28+commit.7893614a
Language
solidity
Optimization
Yes, 200 runs
EVM version
cancun
Via IR
yes
License
MIT
Match
Partialcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,435,291
Creation transaction
0x0a4729d94b…c5b16d77fa

src/helpers/PropAMMSwapHelpers.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

import { IPropAMM } from "../interfaces/IPropAMM.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import { ReentrancyGuardTransient } from "@openzeppelin/contracts/utils/ReentrancyGuardTransient.sol";

/// @title PropAMMSwapHelpers
/// @notice Swaps on a proprietary AMM (pAMM), falling back to a prebuilt swap when the pAMM can't fill.
/// @dev The fallback uses the `SwapHelpers` encoding. Never leave an allowance on this contract: the fallback is
/// an arbitrary call. Native ETH, fee-on-transfer and rebasing tokens are not supported.
contract PropAMMSwapHelpers is ReentrancyGuardTransient {
    using SafeERC20 for IERC20;

    enum Outcome {
        PropAMMFilled,
        // No quote, or a quote below `minAmountOut`.
        PropAMMSkipped,
        // Reverted or paid less than its quote.
        PropAMMFailed
    }

    // Length word + selector.
    uint256 private constant _DATA_ARGS_OFFSET = 36;

    event PropAMMSwap(
        address indexed pamm,
        address indexed tokenIn,
        address indexed tokenOut,
        uint256 amountIn,
        uint256 amountOut,
        address receiver,
        Outcome outcome,
        address fallbackTarget
    );

    error IdenticalTokens();
    error ZeroAmountIn();
    error InsufficientOutputAmount(uint256 amountOut, uint256 minAmountOut);
    error InvalidReceiver();
    error InvalidFallbackTarget();
    error InvalidPointer(uint256 pointer);
    error AmountInMismatch(uint256 spent, uint256 amountIn);
    error NotSelf();

    /// @notice Swaps on `pamm`, or through the fallback if the pAMM is skipped or fails.
    /// @param pamm The pAMM to try first.
    /// @param tokenIn The token sold, pulled from `msg.sender`.
    /// @param tokenOut The token bought.
    /// @param amountIn The amount of `tokenIn` to sell.
    /// @param minAmountOut Minimum `tokenOut` for `receiver`, whichever venue fills.
    /// @param receiver The address that receives `tokenOut`.
    /// @param fallbackTarget The contract the fallback calls, such as a DEX router (`primary` in `SwapHelpers`).
    /// @param fallbackOperator The spender approved for `amountIn`, often the target itself (`operator` in
    /// `SwapHelpers`).
    /// @param fallbackData The fallback calldata, selector included. Its own minimum output can be zero.
    /// @param fallbackPointers Byte offsets in `fallbackData`, after the selector, where `amountIn` is written.
    /// @return amountOut The `tokenOut` `receiver` received.
    function swap(
        address pamm,
        address tokenIn,
        address tokenOut,
        uint256 amountIn,
        uint256 minAmountOut,
        address receiver,
        address fallbackTarget,
        address fallbackOperator,
        bytes memory fallbackData,
        uint256[] memory fallbackPointers
    )
        external
        nonReentrant
        returns (uint256 amountOut)
    {
        if (tokenIn == tokenOut) {
            revert IdenticalTokens();
        }
        if (amountIn == 0) {
            revert ZeroAmountIn();
        }
        if (receiver == address(0) || receiver == address(this)) {
            revert InvalidReceiver();
        }
        if (fallbackTarget == address(this)) {
            revert InvalidFallbackTarget();
        }

        uint256 outBalanceBefore = IERC20(tokenOut).balanceOf(receiver);

        Outcome outcome = _tryPropAMM(pamm, tokenIn, tokenOut, amountIn, minAmountOut, receiver);
        if (outcome != Outcome.PropAMMFilled) {
            IERC20(tokenIn).safeTransferFrom(msg.sender, address(this), amountIn);
            _executeFallback(
                tokenIn, tokenOut, amountIn, receiver, fallbackTarget, fallbackOperator, fallbackData, fallbackPointers
            );
        }

        amountOut = IERC20(tokenOut).balanceOf(receiver) - outBalanceBefore;
        if (amountOut < minAmountOut) {
            revert InsufficientOutputAmount(amountOut, minAmountOut);
        }

        // forge-lint: disable-next-line(reentrancy-events) `swap` is nonReentrant.
        emit PropAMMSwap(pamm, tokenIn, tokenOut, amountIn, amountOut, receiver, outcome, fallbackTarget);
    }

    /// @notice Pays and runs the pAMM. Self-only, so `swap` can roll it back with try/catch.
    /// @dev Reverts if `receiver` gets less than `minAmountOut`. Other parameters are as in `swap`.
    /// @param payer The account `tokenIn` is pulled from: `swap`'s caller.
    /// @param minAmountOut The pAMM's quote, which `receiver` must get.
    function executePropAMM(
        address payer,
        address pamm,
        address tokenIn,
        address tokenOut,
        uint256 amountIn,
        uint256 minAmountOut,
        address receiver
    )
        external
    {
        if (msg.sender != address(this)) {
            revert NotSelf();
        }

        uint256 balanceBefore = IERC20(tokenOut).balanceOf(receiver);
        // forge-lint: disable-next-line(arbitrary-send-erc20) `payer` is always `swap`'s caller.
        IERC20(tokenIn).safeTransferFrom(payer, pamm, amountIn);
        // forge-lint: disable-next-line(unused-return) Measured at `receiver` instead.
        IPropAMM(pamm).swap(tokenIn, tokenOut, amountIn, minAmountOut, receiver, block.timestamp);

        uint256 received = IERC20(tokenOut).balanceOf(receiver) - balanceBefore;
        if (received < minAmountOut) {
            revert InsufficientOutputAmount(received, minAmountOut);
        }
    }

    /// @notice Writes `amount` into `data` at each pointer, like `SwapHelpers`, but bounds-checked.
    /// @param data Calldata, selector included. Modified in place.
    /// @param pointers Byte offsets in `data`, after the selector, of the words to overwrite.
    /// @param amount The value written at each pointer.
    /// @return The updated `data`.
    function insertAmount(
        bytes memory data,
        uint256[] memory pointers,
        uint256 amount
    )
        public
        pure
        returns (bytes memory)
    {
        uint256 length = pointers.length;
        for (uint256 i = 0; i < length; ++i) {
            uint256 pointer = pointers[i];
            if (data.length < _DATA_ARGS_OFFSET || pointer > data.length - _DATA_ARGS_OFFSET) {
                // forge-lint: disable-next-line(require-revert-in-loop)
                revert InvalidPointer(pointer);
            }
            assembly ("memory-safe") {
                mstore(add(data, add(_DATA_ARGS_OFFSET, pointer)), amount)
            }
        }
        return data;
    }

    function _tryPropAMM(
        address pamm,
        address tokenIn,
        address tokenOut,
        uint256 amountIn,
        uint256 minAmountOut,
        address receiver
    )
        internal
        returns (Outcome)
    {
        uint256 pammQuote = _quotePropAMM(pamm, tokenIn, tokenOut, amountIn);
        if (pammQuote == 0 || pammQuote < minAmountOut) {
            return Outcome.PropAMMSkipped;
        }
        // The pAMM must deliver its quote.
        try this.executePropAMM(msg.sender, pamm, tokenIn, tokenOut, amountIn, pammQuote, receiver) {
            return Outcome.PropAMMFilled;
        } catch {
            return Outcome.PropAMMFailed;
        }
    }

    /// @dev Zero if the call fails or returns less than a word. Copies one word at most.
    function _quotePropAMM(
        address pamm,
        address tokenIn,
        address tokenOut,
        uint256 amountIn
    )
        internal
        returns (uint256 amountOut)
    {
        bytes memory callData = abi.encodeCall(IPropAMM.quote, (tokenIn, tokenOut, amountIn));
        assembly ("memory-safe") {
            let success := call(gas(), pamm, 0, add(callData, 0x20), mload(callData), 0, 0)
            if and(success, gt(returndatasize(), 31)) {
                returndatacopy(0, 0, 0x20)
                amountOut := mload(0)
            }
        }
    }

    /// @dev Must spend exactly `amountIn`. Forwards any `tokenOut` it receives to `receiver`.
    function _executeFallback(
        address tokenIn,
        address tokenOut,
        uint256 amountIn,
        address receiver,
        address target,
        address operator,
        bytes memory data,
        uint256[] memory pointers
    )
        internal
    {
        if (pointers.length != 0) {
            insertAmount(data, pointers, amountIn);
        }
        uint256 inBalanceBefore = IERC20(tokenIn).balanceOf(address(this));
        uint256 outBalanceBefore = IERC20(tokenOut).balanceOf(address(this));

        IERC20(tokenIn).forceApprove(operator, amountIn);
        (bool success, bytes memory response) = target.call(data);
        if (!success) {
            assembly ("memory-safe") {
                revert(add(response, 0x20), mload(response))
            }
        }
        IERC20(tokenIn).forceApprove(operator, 0);

        uint256 inBalanceAfter = IERC20(tokenIn).balanceOf(address(this));
        uint256 spent = inBalanceBefore > inBalanceAfter ? inBalanceBefore - inBalanceAfter : 0;
        if (spent != amountIn) {
            revert AmountInMismatch(spent, amountIn);
        }

        uint256 received = IERC20(tokenOut).balanceOf(address(this)) - outBalanceBefore;
        if (received != 0) {
            IERC20(tokenOut).safeTransfer(receiver, received);
        }
    }
}

dependencies/@openzeppelin-contracts-5.2.0/contracts/interfaces/IERC1363.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (interfaces/IERC1363.sol)

pragma solidity ^0.8.20;

import {IERC20} from "./IERC20.sol";
import {IERC165} from "./IERC165.sol";

/**
 * @title IERC1363
 * @dev Interface of the ERC-1363 standard as defined in the https://eips.ethereum.org/EIPS/eip-1363[ERC-1363].
 *
 * Defines an extension interface for ERC-20 tokens that supports executing code on a recipient contract
 * after `transfer` or `transferFrom`, or code on a spender contract after `approve`, in a single transaction.
 */
interface IERC1363 is IERC20, IERC165 {
    /*
     * Note: the ERC-165 identifier for this interface is 0xb0202a11.
     * 0xb0202a11 ===
     *   bytes4(keccak256('transferAndCall(address,uint256)')) ^
     *   bytes4(keccak256('transferAndCall(address,uint256,bytes)')) ^
     *   bytes4(keccak256('transferFromAndCall(address,address,uint256)')) ^
     *   bytes4(keccak256('transferFromAndCall(address,address,uint256,bytes)')) ^
     *   bytes4(keccak256('approveAndCall(address,uint256)')) ^
     *   bytes4(keccak256('approveAndCall(address,uint256,bytes)'))
     */

    /**
     * @dev Moves a `value` amount of tokens from the caller's account to `to`
     * and then calls {IERC1363Receiver-onTransferReceived} on `to`.
     * @param to The address which you want to transfer to.
     * @param value The amount of tokens to be transferred.
     * @return A boolean value indicating whether the operation succeeded unless throwing.
     */
    function transferAndCall(address to, uint256 value) external returns (bool);

    /**
     * @dev Moves a `value` amount of tokens from the caller's account to `to`
     * and then calls {IERC1363Receiver-onTransferReceived} on `to`.
     * @param to The address which you want to transfer to.
     * @param value The amount of tokens to be transferred.
     * @param data Additional data with no specified format, sent in call to `to`.
     * @return A boolean value indicating whether the operation succeeded unless throwing.
     */
    function transferAndCall(address to, uint256 value, bytes calldata data) external returns (bool);

    /**
     * @dev Moves a `value` amount of tokens from `from` to `to` using the allowance mechanism
     * and then calls {IERC1363Receiver-onTransferReceived} on `to`.
     * @param from The address which you want to send tokens from.
     * @param to The address which you want to transfer to.
     * @param value The amount of tokens to be transferred.
     * @return A boolean value indicating whether the operation succeeded unless throwing.
     */
    function transferFromAndCall(address from, address to, uint256 value) external returns (bool);

    /**
     * @dev Moves a `value` amount of tokens from `from` to `to` using the allowance mechanism
     * and then calls {IERC1363Receiver-onTransferReceived} on `to`.
     * @param from The address which you want to send tokens from.
     * @param to The address which you want to transfer to.
     * @param value The amount of tokens to be transferred.
     * @param data Additional data with no specified format, sent in call to `to`.
     * @return A boolean value indicating whether the operation succeeded unless throwing.
     */
    function transferFromAndCall(address from, address to, uint256 value, bytes calldata data) external returns (bool);

    /**
     * @dev Sets a `value` amount of tokens as the allowance of `spender` over the
     * caller's tokens and then calls {IERC1363Spender-onApprovalReceived} on `spender`.
     * @param spender The address which will spend the funds.
     * @param value The amount of tokens to be spent.
     * @return A boolean value indicating whether the operation succeeded unless throwing.
     */
    function approveAndCall(address spender, uint256 value) external returns (bool);

    /**
     * @dev Sets a `value` amount of tokens as the allowance of `spender` over the
     * caller's tokens and then calls {IERC1363Spender-onApprovalReceived} on `spender`.
     * @param spender The address which will spend the funds.
     * @param value The amount of tokens to be spent.
     * @param data Additional data with no specified format, sent in call to `spender`.
     * @return A boolean value indicating whether the operation succeeded unless throwing.
     */
    function approveAndCall(address spender, uint256 value, bytes calldata data) external returns (bool);
}

dependencies/@openzeppelin-contracts-5.2.0/contracts/interfaces/IERC165.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (interfaces/IERC165.sol)

pragma solidity ^0.8.20;

import {IERC165} from "../utils/introspection/IERC165.sol";

dependencies/@openzeppelin-contracts-5.2.0/contracts/interfaces/IERC20.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (interfaces/IERC20.sol)

pragma solidity ^0.8.20;

import {IERC20} from "../token/ERC20/IERC20.sol";

dependencies/@openzeppelin-contracts-5.2.0/contracts/token/ERC20/IERC20.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (token/ERC20/IERC20.sol)

pragma solidity ^0.8.20;

/**
 * @dev Interface of the ERC-20 standard as defined in the ERC.
 */
interface IERC20 {
    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);

    /**
     * @dev Returns the value of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the value of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves a `value` amount of tokens from the caller's account to `to`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address to, uint256 value) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets a `value` amount of tokens as the allowance of `spender` over the
     * caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 value) external returns (bool);

    /**
     * @dev Moves a `value` amount of tokens from `from` to `to` using the
     * allowance mechanism. `value` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(address from, address to, uint256 value) external returns (bool);
}

dependencies/@openzeppelin-contracts-5.2.0/contracts/token/ERC20/utils/SafeERC20.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.2.0) (token/ERC20/utils/SafeERC20.sol)

pragma solidity ^0.8.20;

import {IERC20} from "../IERC20.sol";
import {IERC1363} from "../../../interfaces/IERC1363.sol";

/**
 * @title SafeERC20
 * @dev Wrappers around ERC-20 operations that throw on failure (when the token
 * contract returns false). Tokens that return no value (and instead revert or
 * throw on failure) are also supported, non-reverting calls are assumed to be
 * successful.
 * To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
 * which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
 */
library SafeERC20 {
    /**
     * @dev An operation with an ERC-20 token failed.
     */
    error SafeERC20FailedOperation(address token);

    /**
     * @dev Indicates a failed `decreaseAllowance` request.
     */
    error SafeERC20FailedDecreaseAllowance(address spender, uint256 currentAllowance, uint256 requestedDecrease);

    /**
     * @dev Transfer `value` amount of `token` from the calling contract to `to`. If `token` returns no value,
     * non-reverting calls are assumed to be successful.
     */
    function safeTransfer(IERC20 token, address to, uint256 value) internal {
        _callOptionalReturn(token, abi.encodeCall(token.transfer, (to, value)));
    }

    /**
     * @dev Transfer `value` amount of `token` from `from` to `to`, spending the approval given by `from` to the
     * calling contract. If `token` returns no value, non-reverting calls are assumed to be successful.
     */
    function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
        _callOptionalReturn(token, abi.encodeCall(token.transferFrom, (from, to, value)));
    }

    /**
     * @dev Increase the calling contract's allowance toward `spender` by `value`. If `token` returns no value,
     * non-reverting calls are assumed to be successful.
     *
     * IMPORTANT: If the token implements ERC-7674 (ERC-20 with temporary allowance), and if the "client"
     * smart contract uses ERC-7674 to set temporary allowances, then the "client" smart contract should avoid using
     * this function. Performing a {safeIncreaseAllowance} or {safeDecreaseAllowance} operation on a token contract
     * that has a non-zero temporary allowance (for that particular owner-spender) will result in unexpected behavior.
     */
    function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
        uint256 oldAllowance = token.allowance(address(this), spender);
        forceApprove(token, spender, oldAllowance + value);
    }

    /**
     * @dev Decrease the calling contract's allowance toward `spender` by `requestedDecrease`. If `token` returns no
     * value, non-reverting calls are assumed to be successful.
     *
     * IMPORTANT: If the token implements ERC-7674 (ERC-20 with temporary allowance), and if the "client"
     * smart contract uses ERC-7674 to set temporary allowances, then the "client" smart contract should avoid using
     * this function. Performing a {safeIncreaseAllowance} or {safeDecreaseAllowance} operation on a token contract
     * that has a non-zero temporary allowance (for that particular owner-spender) will result in unexpected behavior.
     */
    function safeDecreaseAllowance(IERC20 token, address spender, uint256 requestedDecrease) internal {
        unchecked {
            uint256 currentAllowance = token.allowance(address(this), spender);
            if (currentAllowance < requestedDecrease) {
                revert SafeERC20FailedDecreaseAllowance(spender, currentAllowance, requestedDecrease);
            }
            forceApprove(token, spender, currentAllowance - requestedDecrease);
        }
    }

    /**
     * @dev Set the calling contract's allowance toward `spender` to `value`. If `token` returns no value,
     * non-reverting calls are assumed to be successful. Meant to be used with tokens that require the approval
     * to be set to zero before setting it to a non-zero value, such as USDT.
     *
     * NOTE: If the token implements ERC-7674, this function will not modify any temporary allowance. This function
     * only sets the "standard" allowance. Any temporary allowance will remain active, in addition to the value being
     * set here.
     */
    function forceApprove(IERC20 token, address spender, uint256 value) internal {
        bytes memory approvalCall = abi.encodeCall(token.approve, (spender, value));

        if (!_callOptionalReturnBool(token, approvalCall)) {
            _callOptionalReturn(token, abi.encodeCall(token.approve, (spender, 0)));
            _callOptionalReturn(token, approvalCall);
        }
    }

    /**
     * @dev Performs an {ERC1363} transferAndCall, with a fallback to the simple {ERC20} transfer if the target has no
     * code. This can be used to implement an {ERC721}-like safe transfer that rely on {ERC1363} checks when
     * targeting contracts.
     *
     * Reverts if the returned value is other than `true`.
     */
    function transferAndCallRelaxed(IERC1363 token, address to, uint256 value, bytes memory data) internal {
        if (to.code.length == 0) {
            safeTransfer(token, to, value);
        } else if (!token.transferAndCall(to, value, data)) {
            revert SafeERC20FailedOperation(address(token));
        }
    }

    /**
     * @dev Performs an {ERC1363} transferFromAndCall, with a fallback to the simple {ERC20} transferFrom if the target
     * has no code. This can be used to implement an {ERC721}-like safe transfer that rely on {ERC1363} checks when
     * targeting contracts.
     *
     * Reverts if the returned value is other than `true`.
     */
    function transferFromAndCallRelaxed(
        IERC1363 token,
        address from,
        address to,
        uint256 value,
        bytes memory data
    ) internal {
        if (to.code.length == 0) {
            safeTransferFrom(token, from, to, value);
        } else if (!token.transferFromAndCall(from, to, value, data)) {
            revert SafeERC20FailedOperation(address(token));
        }
    }

    /**
     * @dev Performs an {ERC1363} approveAndCall, with a fallback to the simple {ERC20} approve if the target has no
     * code. This can be used to implement an {ERC721}-like safe transfer that rely on {ERC1363} checks when
     * targeting contracts.
     *
     * NOTE: When the recipient address (`to`) has no code (i.e. is an EOA), this function behaves as {forceApprove}.
     * Opposedly, when the recipient address (`to`) has code, this function only attempts to call {ERC1363-approveAndCall}
     * once without retrying, and relies on the returned value to be true.
     *
     * Reverts if the returned value is other than `true`.
     */
    function approveAndCallRelaxed(IERC1363 token, address to, uint256 value, bytes memory data) internal {
        if (to.code.length == 0) {
            forceApprove(token, to, value);
        } else if (!token.approveAndCall(to, value, data)) {
            revert SafeERC20FailedOperation(address(token));
        }
    }

    /**
     * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
     * on the return value: the return value is optional (but if data is returned, it must not be false).
     * @param token The token targeted by the call.
     * @param data The call data (encoded using abi.encode or one of its variants).
     *
     * This is a variant of {_callOptionalReturnBool} that reverts if call fails to meet the requirements.
     */
    function _callOptionalReturn(IERC20 token, bytes memory data) private {
        uint256 returnSize;
        uint256 returnValue;
        assembly ("memory-safe") {
            let success := call(gas(), token, 0, add(data, 0x20), mload(data), 0, 0x20)
            // bubble errors
            if iszero(success) {
                let ptr := mload(0x40)
                returndatacopy(ptr, 0, returndatasize())
                revert(ptr, returndatasize())
            }
            returnSize := returndatasize()
            returnValue := mload(0)
        }

        if (returnSize == 0 ? address(token).code.length == 0 : returnValue != 1) {
            revert SafeERC20FailedOperation(address(token));
        }
    }

    /**
     * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
     * on the return value: the return value is optional (but if data is returned, it must not be false).
     * @param token The token targeted by the call.
     * @param data The call data (encoded using abi.encode or one of its variants).
     *
     * This is a variant of {_callOptionalReturn} that silently catches all reverts and returns a bool instead.
     */
    function _callOptionalReturnBool(IERC20 token, bytes memory data) private returns (bool) {
        bool success;
        uint256 returnSize;
        uint256 returnValue;
        assembly ("memory-safe") {
            success := call(gas(), token, 0, add(data, 0x20), mload(data), 0, 0x20)
            returnSize := returndatasize()
            returnValue := mload(0)
        }
        return success && (returnSize == 0 ? address(token).code.length > 0 : returnValue == 1);
    }
}

dependencies/@openzeppelin-contracts-5.2.0/contracts/utils/ReentrancyGuardTransient.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/ReentrancyGuardTransient.sol)

pragma solidity ^0.8.24;

import {TransientSlot} from "./TransientSlot.sol";

/**
 * @dev Variant of {ReentrancyGuard} that uses transient storage.
 *
 * NOTE: This variant only works on networks where EIP-1153 is available.
 *
 * _Available since v5.1._
 */
abstract contract ReentrancyGuardTransient {
    using TransientSlot for *;

    // keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.ReentrancyGuard")) - 1)) & ~bytes32(uint256(0xff))
    bytes32 private constant REENTRANCY_GUARD_STORAGE =
        0x9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f00;

    /**
     * @dev Unauthorized reentrant call.
     */
    error ReentrancyGuardReentrantCall();

    /**
     * @dev Prevents a contract from calling itself, directly or indirectly.
     * Calling a `nonReentrant` function from another `nonReentrant`
     * function is not supported. It is possible to prevent this from happening
     * by making the `nonReentrant` function external, and making it call a
     * `private` function that does the actual work.
     */
    modifier nonReentrant() {
        _nonReentrantBefore();
        _;
        _nonReentrantAfter();
    }

    function _nonReentrantBefore() private {
        // On the first call to nonReentrant, _status will be NOT_ENTERED
        if (_reentrancyGuardEntered()) {
            revert ReentrancyGuardReentrantCall();
        }

        // Any calls to nonReentrant after this point will fail
        REENTRANCY_GUARD_STORAGE.asBoolean().tstore(true);
    }

    function _nonReentrantAfter() private {
        REENTRANCY_GUARD_STORAGE.asBoolean().tstore(false);
    }

    /**
     * @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
     * `nonReentrant` function in the call stack.
     */
    function _reentrancyGuardEntered() internal view returns (bool) {
        return REENTRANCY_GUARD_STORAGE.asBoolean().tload();
    }
}

dependencies/@openzeppelin-contracts-5.2.0/contracts/utils/TransientSlot.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/TransientSlot.sol)
// This file was procedurally generated from scripts/generate/templates/TransientSlot.js.

pragma solidity ^0.8.24;

/**
 * @dev Library for reading and writing value-types to specific transient storage slots.
 *
 * Transient slots are often used to store temporary values that are removed after the current transaction.
 * This library helps with reading and writing to such slots without the need for inline assembly.
 *
 *  * Example reading and writing values using transient storage:
 * ```solidity
 * contract Lock {
 *     using TransientSlot for *;
 *
 *     // Define the slot. Alternatively, use the SlotDerivation library to derive the slot.
 *     bytes32 internal constant _LOCK_SLOT = 0xf4678858b2b588224636b8522b729e7722d32fc491da849ed75b3fdf3c84f542;
 *
 *     modifier locked() {
 *         require(!_LOCK_SLOT.asBoolean().tload());
 *
 *         _LOCK_SLOT.asBoolean().tstore(true);
 *         _;
 *         _LOCK_SLOT.asBoolean().tstore(false);
 *     }
 * }
 * ```
 *
 * TIP: Consider using this library along with {SlotDerivation}.
 */
library TransientSlot {
    /**
     * @dev UDVT that represent a slot holding a address.
     */
    type AddressSlot is bytes32;

    /**
     * @dev Cast an arbitrary slot to a AddressSlot.
     */
    function asAddress(bytes32 slot) internal pure returns (AddressSlot) {
        return AddressSlot.wrap(slot);
    }

    /**
     * @dev UDVT that represent a slot holding a bool.
     */
    type BooleanSlot is bytes32;

    /**
     * @dev Cast an arbitrary slot to a BooleanSlot.
     */
    function asBoolean(bytes32 slot) internal pure returns (BooleanSlot) {
        return BooleanSlot.wrap(slot);
    }

    /**
     * @dev UDVT that represent a slot holding a bytes32.
     */
    type Bytes32Slot is bytes32;

    /**
     * @dev Cast an arbitrary slot to a Bytes32Slot.
     */
    function asBytes32(bytes32 slot) internal pure returns (Bytes32Slot) {
        return Bytes32Slot.wrap(slot);
    }

    /**
     * @dev UDVT that represent a slot holding a uint256.
     */
    type Uint256Slot is bytes32;

    /**
     * @dev Cast an arbitrary slot to a Uint256Slot.
     */
    function asUint256(bytes32 slot) internal pure returns (Uint256Slot) {
        return Uint256Slot.wrap(slot);
    }

    /**
     * @dev UDVT that represent a slot holding a int256.
     */
    type Int256Slot is bytes32;

    /**
     * @dev Cast an arbitrary slot to a Int256Slot.
     */
    function asInt256(bytes32 slot) internal pure returns (Int256Slot) {
        return Int256Slot.wrap(slot);
    }

    /**
     * @dev Load the value held at location `slot` in transient storage.
     */
    function tload(AddressSlot slot) internal view returns (address value) {
        assembly ("memory-safe") {
            value := tload(slot)
        }
    }

    /**
     * @dev Store `value` at location `slot` in transient storage.
     */
    function tstore(AddressSlot slot, address value) internal {
        assembly ("memory-safe") {
            tstore(slot, value)
        }
    }

    /**
     * @dev Load the value held at location `slot` in transient storage.
     */
    function tload(BooleanSlot slot) internal view returns (bool value) {
        assembly ("memory-safe") {
            value := tload(slot)
        }
    }

    /**
     * @dev Store `value` at location `slot` in transient storage.
     */
    function tstore(BooleanSlot slot, bool value) internal {
        assembly ("memory-safe") {
            tstore(slot, value)
        }
    }

    /**
     * @dev Load the value held at location `slot` in transient storage.
     */
    function tload(Bytes32Slot slot) internal view returns (bytes32 value) {
        assembly ("memory-safe") {
            value := tload(slot)
        }
    }

    /**
     * @dev Store `value` at location `slot` in transient storage.
     */
    function tstore(Bytes32Slot slot, bytes32 value) internal {
        assembly ("memory-safe") {
            tstore(slot, value)
        }
    }

    /**
     * @dev Load the value held at location `slot` in transient storage.
     */
    function tload(Uint256Slot slot) internal view returns (uint256 value) {
        assembly ("memory-safe") {
            value := tload(slot)
        }
    }

    /**
     * @dev Store `value` at location `slot` in transient storage.
     */
    function tstore(Uint256Slot slot, uint256 value) internal {
        assembly ("memory-safe") {
            tstore(slot, value)
        }
    }

    /**
     * @dev Load the value held at location `slot` in transient storage.
     */
    function tload(Int256Slot slot) internal view returns (int256 value) {
        assembly ("memory-safe") {
            value := tload(slot)
        }
    }

    /**
     * @dev Store `value` at location `slot` in transient storage.
     */
    function tstore(Int256Slot slot, int256 value) internal {
        assembly ("memory-safe") {
            tstore(slot, value)
        }
    }
}

dependencies/@openzeppelin-contracts-5.2.0/contracts/utils/introspection/IERC165.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/introspection/IERC165.sol)

pragma solidity ^0.8.20;

/**
 * @dev Interface of the ERC-165 standard, as defined in the
 * https://eips.ethereum.org/EIPS/eip-165[ERC].
 *
 * Implementers can declare support of contract interfaces, which can then be
 * queried by others ({ERC165Checker}).
 *
 * For an implementation, see {ERC165}.
 */
interface IERC165 {
    /**
     * @dev Returns true if this contract implements the interface defined by
     * `interfaceId`. See the corresponding
     * https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[ERC section]
     * to learn more about how these ids are created.
     *
     * This function call must use less than 30 000 gas.
     */
    function supportsInterface(bytes4 interfaceId) external view returns (bool);
}

src/interfaces/IPropAMM.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

/// @title IPropAMM
/// @notice The standard interface proprietary AMMs (pAMMs) expose to be routable, as published by LambdaClass
/// (https://github.com/lambdaclass/propamm-router-contracts, MIT) and used by Titan Builder's pAMM venues.
/// @dev `swap` is push-payment: the caller transfers `amountIn` of `tokenIn` to the pAMM before calling it.
interface IPropAMM {
    struct TokenPair {
        address token0;
        address token1;
    }

    event Swapped(
        address indexed sender,
        address indexed tokenIn,
        address indexed tokenOut,
        uint256 amountIn,
        uint256 amountOut,
        address recipient
    );

    /// @notice Whether a swap of `tokenIn` for `tokenOut` would succeed in the current block.
    function isActive(address tokenIn, address tokenOut) external view returns (bool active);

    /// @notice Every pair the pAMM supports, active or not.
    function getPairs() external view returns (TokenPair[] memory pairs);

    /// @notice The `tokenOut` a swap of `amountIn` would deliver. Reverts when the pair is inactive and needs no
    /// balance or allowance from the caller.
    function quote(address tokenIn, address tokenOut, uint256 amountIn) external returns (uint256 amountOut);

    /// @notice Swaps the `amountIn` of `tokenIn` already transferred to the pAMM, delivering at least
    /// `minAmountOut` of `tokenOut` to `recipient`.
    function swap(
        address tokenIn,
        address tokenOut,
        uint256 amountIn,
        uint256 minAmountOut,
        address recipient,
        uint256 deadline
    )
        external
        returns (uint256 amountOut);
}
Compiler settingsShow
{
  "viaIR": true,
  "metadata": {
    "appendCBOR": false,
    "bytecodeHash": "none",
    "useLiteralContent": false
  },
  "libraries": {},
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "cancun",
  "remappings": [
    "@layerzerolabs/oapp-evm/=dependencies/devtools-0.0.1/packages/oapp-evm/",
    "@layerzerolabs/lz-evm-protocol-v2/=dependencies/layerzero-v2-2.0.2/packages/layerzero-v2/evm/protocol/",
    "@layerzerolabs/lz-evm-oapp-v2/=dependencies/layerzero-v2-2.0.2/packages/layerzero-v2/evm/oapp/",
    "@uniswap/v4-core/=dependencies/v4-core-4.0.0/",
    "@uniswap/v4-periphery/=dependencies/v4-periphery-4.0.0/",
    "@uniswap/universal-router/=dependencies/universal-router-4.0.0/",
    "@uniswap/permit2/=dependencies/permit2-4.0.0/",
    "devtools/=dependencies/devtools-0.0.1/packages/toolbox-foundry/src/",
    "ds-test/=dependencies/forge-std-1.9.7/dependencies/ds-test/src/",
    "enso-weiroll/=dependencies/enso-weiroll-1.4.1/contracts/",
    "erc4626-tests/=dependencies/@openzeppelin-contracts-5.2.0/lib/erc4626-tests/",
    "forge-std/=dependencies/forge-std-1.9.7/src/",
    "halmos-cheatcodes/=dependencies/@openzeppelin-contracts-5.2.0/lib/halmos-cheatcodes/src/",
    "layerzero-v2/=dependencies/layerzero-v2-2.0.2/",
    "openzeppelin-contracts/=dependencies/@openzeppelin-contracts-5.2.0/contracts/",
    "safe-contracts/=dependencies/safe-tools-0.2.0/lib/safe-contracts/contracts/",
    "safe-tools/=dependencies/safe-tools-0.2.0/src/",
    "solady/=dependencies/solady-0.1.22/src/",
    "solmate/=dependencies/solady-0.1.22/lib/solmate/src/",
    "@ensdomains/=dependencies/v4-core-4.0.0/node_modules/@ensdomains/",
    "forge-gas-snapshot/=dependencies/v4-periphery-4.0.0/lib/permit2/lib/forge-gas-snapshot/src/",
    "hardhat/=dependencies/v4-core-4.0.0/node_modules/hardhat/",
    "permit2/=dependencies/v4-periphery-4.0.0/lib/permit2/",
    "v4-core/=dependencies/v4-core-4.0.0/src/",
    "v4-periphery/=dependencies/v4-periphery-4.0.0/",
    "account-abstraction-v7/=dependencies/account-abstraction-v7-0.7.0/contracts/",
    "@openzeppelin-contracts-5.2.0/=dependencies/@openzeppelin-contracts-5.2.0/",
    "account-abstraction-0.7.0/=dependencies/account-abstraction-v7-0.7.0/contracts/",
    "devtools-0.0.1/=dependencies/devtools-0.0.1/",
    "enso-weiroll-1.4.1/=dependencies/enso-weiroll-1.4.1/contracts/",
    "forge-std-1.9.7/=dependencies/forge-std-1.9.7/src/",
    "layerzero-v2-2.0.2/=dependencies/layerzero-v2-2.0.2/",
    "safe-smart-account-1.5.0/=dependencies/safe-smart-account-1.5.0/contracts/",
    "safe-tools-0.2.0/=dependencies/safe-tools-0.2.0/src/",
    "solady-0.1.22/=dependencies/solady-0.1.22/src/",
    "v4-core-4.0.0/=dependencies/v4-core-4.0.0/src/",
    "v4-periphery-4.0.0/=dependencies/v4-periphery-4.0.0/src/",
    "chainlink-ccip/=dependencies/chainlink-ccip-1.6.2/chains/evm/contracts/",
    "chainlink-ccip-1.6.2/=dependencies/chainlink-ccip-1.6.2/chains/evm/contracts/",
    "chainlink-evm/=dependencies/chainlink-evm-1.5.0/contracts/src/v0.8/",
    "chainlink-evm-1.5.0/=dependencies/chainlink-evm-1.5.0/contracts/src/v0.8/",
    "@chainlink/=dependencies/chainlink-evm-1.5.0/",
    "@openzeppelin/[email protected]/utils/introspection/IERC165.sol=dependencies/@openzeppelin-contracts-5.2.0/contracts/utils/introspection/IERC165.sol",
    "@openzeppelin/[email protected]/utils/introspection/ERC165Checker.sol=dependencies/@openzeppelin-contracts-5.2.0/contracts/utils/introspection/ERC165Checker.sol",
    "@openzeppelin/[email protected]/token/ERC20/utils/SafeERC20.sol=dependencies/@openzeppelin-contracts-5.2.0/contracts/token/ERC20/utils/SafeERC20.sol",
    "@openzeppelin/[email protected]/token/ERC20/IERC20.sol=dependencies/@openzeppelin-contracts-5.2.0/contracts/token/ERC20/IERC20.sol",
    "@openzeppelin/contracts/=dependencies/@openzeppelin-contracts-5.2.0/contracts/",
    "universal-router-4.0.0/=dependencies/universal-router-4.0.0/",
    "@memview-sol/=dependencies/evm-cctp-contracts-20251001.0.0/lib/memview-sol/",
    "@openzeppelin/=dependencies/evm-cctp-contracts-20251001.0.0/lib/openzeppelin-contracts/",
    "@uniswap/v2-core/=dependencies/universal-router-4.0.0/node_modules/@uniswap/v2-core/",
    "@uniswap/v3-core/=dependencies/universal-router-4.0.0/node_modules/@uniswap/v3-core/",
    "@uniswap/v3-periphery/=dependencies/universal-router-4.0.0/lib/v3-periphery/",
    "account-abstraction-v7-0.7.0/=dependencies/account-abstraction-v7-0.7.0/contracts/",
    "centre-tokens.git/=dependencies/evm-cctp-contracts-20251001.0.0/lib/centre-tokens.git/",
    "evm-cctp-contracts-20251001.0.0/=dependencies/evm-cctp-contracts-20251001.0.0/",
    "memview-sol/=dependencies/evm-cctp-contracts-20251001.0.0/lib/memview-sol/contracts/",
    "permit2-4.0.0/=dependencies/permit2-4.0.0/",
    "v3-periphery/=dependencies/universal-router-4.0.0/lib/v3-periphery/contracts/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.