Verified contractPartialMIT
Deliver
Address0xad8a3c3745633280fafb0f44d0c2cc2c48475673
- 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, 1,000,000 runs
- EVM version
- paris
- License
- MIT
- Match
- Partialcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 25,877,948
- Creation transaction
- 0x3797ea16bd…9322d194f0
9 files
- src/Deliver.solmain13.05 KB
- lib/openzeppelin-contracts/contracts/interfaces/IERC1363.sol4.45 KB
- lib/openzeppelin-contracts/contracts/interfaces/IERC165.sol193 B
- lib/openzeppelin-contracts/contracts/interfaces/IERC20.sol182 B
- lib/openzeppelin-contracts/contracts/interfaces/IERC20Metadata.sol216 B
- lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol2.78 KB
- lib/openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol684 B
- lib/openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol13.96 KB
- lib/openzeppelin-contracts/contracts/utils/introspection/IERC165.sol871 B
src/Deliver.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
/// @title Deliver
/// @notice An outcome verifier: it forwards the entire balance it is *already holding* of a given
/// asset to a recipient, and reverts unless that balance meets a caller-supplied floor.
///
/// @dev ## Why this exists
///
/// This is the **last step of a swap route**. A route is a chain of components nobody here
/// wrote — an aggregator, a bridge, an AMM hop, an RFQ filler — each with its own notion of
/// slippage, its own `minOut` semantics, and sometimes no guarantee at all. Rather than trust
/// each provider's promise, every route ends by depositing its output into this contract, and
/// the floor is enforced once, at the end, on the amount that actually arrived.
///
/// That is why it knows nothing about swaps: the guarantee has to hold *regardless* of what
/// ran in front of it. Adding or reordering providers changes nothing about how delivery is
/// enforced. And because the check happens once on the real output, the intermediate hops'
/// own slippage settings stop being a safety property — if the route under-delivers, this
/// step reverts and the user keeps their input.
///
/// **The guarantee, stated exactly:** if the call does not revert, the recipient was sent the
/// entire balance this contract held of that asset, and that balance was at least `min`. The
/// two WARNINGs below are what that does not cover.
///
/// ## What this contract is
///
/// This is a pass-through, not a vault, not a router, not a swapper. The intended flow is:
///
/// 1. Some upstream system (a bridge, a filler, a swap, a plain transfer) moves funds *into*
/// this contract.
/// 2. Someone calls {deliverToken} / {deliverNative} with `(asset, recipient, min)`.
/// 3. The contract reads its own balance, asserts `balance >= min`, and sends the whole
/// balance to `recipient`.
///
/// Verification is therefore *post-hoc*: the contract never pulls funds in, never calls
/// `transferFrom`, and knows nothing about how the balance got there. It only asserts a lower
/// bound on what arrived and forwards it.
///
/// ## Sweep, not amount
///
/// There is deliberately no `amount` parameter. Whatever arrived is what gets delivered — the
/// full balance, including any dust left over from a prior flow. That is the point of the
/// primitive, not an oversight. A consequence worth stating plainly: **any balance sitting in
/// this contract is unconditionally claimable by the next caller.** Never park funds here.
///
/// ## `min` is a floor on what arrived
///
/// This contract performs no swap and cannot tell slippage from a bridge fee; `min` is purely
/// a lower bound on the balance held at call time. In the route, though, `min` *is* the
/// route's final `minOut` — enforced once, at the end, on the real output.
///
/// ## Permissionless by design
///
/// No owner, no storage, no allowlist, no pause. Anyone may call either function. Safety comes
/// entirely from the *caller* binding `(asset, recipient, min)` — typically an intent or
/// settlement system upstream that commits to those three values before funding this contract.
/// Access control here would not add safety; it would only move the trust assumption. If a
/// caller passes a wrong `recipient`, that is the caller's bug, and this contract will happily
/// execute it.
///
/// **Consequence — fund and deliver in ONE transaction.** A balance sitting here between
/// transactions does not merely sit at risk; it *belongs* to whoever calls next, for a
/// recipient of their choosing, and calling `deliverToken(token, self, 0)` against it is a
/// valid in-spec use of this contract by anyone. If a route deposits output in one
/// transaction and delivers in another, the funds are gone in between. Pinned by
/// `test_AnyoneCanDivertAStrandedBalance`. See `docs/INTEGRATING.md`.
///
/// ## WARNING — fee-on-transfer, rebasing, and hook tokens can under-deliver
///
/// `min` is checked against the **held balance before the transfer**, not against the amount
/// the recipient actually ends up with. This is the cheap form and it is a deliberate choice.
///
/// For a token that takes a fee on transfer, rebases downward mid-transfer, or otherwise moves
/// less than the requested amount, **the check can pass while the recipient receives strictly
/// less than `min`.** The call will succeed. Nothing here detects it. Integrators who need a
/// hard guarantee on *received* amount for such tokens must enforce it themselves — measure the
/// recipient's balance delta in the calling contract — or must not route those tokens through
/// this contract at all.
///
/// The mirror image of that case **reverts** rather than under-delivering. A token that debits
/// `amount + fee` from the *sender* — charging the fee on top so the recipient receives the
/// full `amount` — can never be swept: this contract holds exactly its balance and the transfer
/// demands more. **Topping the contract up does not help**, because the sweep then attempts the
/// new, larger balance and comes up short by the fee again. Such a token is simply
/// undeliverable here and must be filtered upstream. It is broken well beyond this contract —
/// no holder of one can ever transfer their entire balance — which is why the failure is left
/// to revert rather than worked around. Pinned by
/// `test_SenderSurchargeTokenCannotBeSweptAndToppingUpDoesNotHelp`.
///
/// ## WARNING — `address(0)` is a native-ETH sentinel, not a rejected input
///
/// Both `address(0)` and `0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE` are accepted by
/// {deliverToken} as sentinels meaning "native ETH", and route to the native path.
///
/// **Why `address(0)` is accepted.** It is a compatibility choice, not an oversight.
/// `0xEeee...EEeE` is the more common convention, but a number of protocols encode native ETH
/// as the zero address instead. Rejecting it would mean a caller arriving from one of those
/// conventions falls through to the ERC-20 path with a token address that has no code — a
/// confusing revert, or worse, a caller that special-cases around it. Accepting both means the
/// tuple `(token, recipient, min)` carries the same meaning whichever convention produced it.
///
/// Accepting `address(0)` has a specific, accepted hazard: the classic uninitialized-variable
/// caller bug — a caller that forgets to set its `token` field and passes a zero address —
/// normally reverts harmlessly. **Here it does not revert. It sweeps this contract's entire
/// native ETH balance to the caller-supplied `recipient`.** A bug that would otherwise be a
/// revert becomes an ETH transfer. This was chosen knowingly; callers must treat a zero token
/// address as a live, funds-moving input and validate it upstream.
///
/// ## Reentrancy
///
/// There is no state to corrupt and the transfer is the last action. A token transfer hook or a
/// native recipient that re-enters will find a zero balance, so a re-entrant call either
/// no-ops (when its `min` is 0) or reverts (when its `min` is greater than 0). Neither can
/// double-deliver. A token whose hook fires *before* its own balance update can force the outer
/// transfer to fail; the whole transaction then reverts atomically and no funds move.
///
/// ## Events
///
/// None are emitted. On the **ERC-20 path** that costs nothing: the token's own `Transfer` log
/// already carries `(recipient, amount)`, which is the outcome an indexer needs, so a second
/// event would be redundant and cost gas.
///
/// That rationale does **not** extend to the native path. `recipient.call{value: balance}("")`
/// emits no log at all, so a native delivery leaves no event an indexer can read: there is no
/// on-chain record that the floor was cleared, for whom, or for how much. Native deliveries
/// are observable only through transaction traces. If you need to index them, index the traces
/// or watch the recipient's balance — do not expect a log. The SVM side has the same shape;
/// `deliver_sol` emits nothing either.
contract Deliver {
using SafeERC20 for IERC20;
/// @notice Sentinel token address meaning "native ETH", the widely used `0xEeee...EEeE` form.
/// @dev See the contract-level WARNING: `address(0)` is *also* treated as this sentinel.
address public constant NATIVE_SENTINEL = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
/// @notice The balance held at call time was below the caller-supplied floor. Nothing was moved.
/// @param balance The balance this contract held when the check ran.
/// @param min The floor the caller required.
error BalanceBelowMin(uint256 balance, uint256 min);
/// @notice The native ETH send failed; the delivery reverts rather than stranding value.
/// @param recipient The address the send was attempted to.
/// @param amount The amount that was attempted.
error NativeTransferFailed(address recipient, uint256 amount);
/// @notice Deliver this contract's entire balance of `token` to `recipient`, requiring at least
/// `min`.
/// @dev Reverts with {BalanceBelowMin} if the held balance is below `min`. A held balance of 0
/// with `min == 0` succeeds as a no-op transfer of 0 — that is intentional and tested.
///
/// If `token` is `address(0)` or {NATIVE_SENTINEL}, this dispatches to the native path and
/// sweeps ETH instead. Read the contract-level `address(0)` WARNING before relying on that.
///
/// The `min` check is on the *held* balance, pre-transfer. See the fee-on-transfer WARNING:
/// a fee-taking or rebasing token can pass this check and still deliver less than `min`.
/// @param token The ERC-20 to sweep, or a native sentinel to sweep ETH.
/// @param recipient The address that receives the entire balance. Not validated — the caller
/// owns this choice, including passing `address(0)` and burning the funds.
/// @param min The minimum balance that must be held for the delivery to be considered valid.
function deliverToken(
IERC20 token,
address recipient,
uint256 min
) external {
if (address(token) == address(0) || address(token) == NATIVE_SENTINEL) {
_deliverNative(recipient, min);
return;
}
uint256 balance = token.balanceOf(address(this));
if (balance < min) revert BalanceBelowMin(balance, min);
token.safeTransfer(recipient, balance);
}
/// @notice Deliver this contract's entire native ETH balance to `recipient`, requiring at least
/// `min`.
/// @dev The direct native entry point; {deliverToken} routes here for the two native sentinels.
/// Reverts with {BalanceBelowMin} if the held balance is below `min`, and with
/// {NativeTransferFailed} if the send fails — it fails closed, it never swallows a failed
/// send. A balance of 0 with `min == 0` succeeds as a no-op 0-value call, mirroring the
/// ERC-20 path.
/// @param recipient The address that receives the entire ETH balance. All remaining gas is
/// forwarded, so a contract recipient may execute arbitrary logic, including re-entering
/// this contract — where it will find a zero balance.
/// @param min The minimum ETH balance that must be held for the delivery to be considered valid.
function deliverNative(
address recipient,
uint256 min
) external {
_deliverNative(recipient, min);
}
/// @dev Shared native sweep. Checks the floor against `address(this).balance`, then forwards the
/// whole balance with all available gas. Reverting on a failed send is deliberate: a silent
/// failure would report a delivery that did not happen.
function _deliverNative(
address recipient,
uint256 min
) internal {
uint256 balance = address(this).balance;
if (balance < min) revert BalanceBelowMin(balance, min);
(bool ok,) = recipient.call{value: balance}("");
if (!ok) revert NativeTransferFailed(recipient, balance);
}
/// @notice Accepts native ETH so the contract can be funded ahead of a delivery.
/// @dev Empty on purpose: funding is step one of the flow, delivery is a separate explicit call.
receive() external payable {}
}
lib/openzeppelin-contracts/contracts/interfaces/IERC1363.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.4.0) (interfaces/IERC1363.sol)
pragma solidity >=0.6.2;
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);
}
lib/openzeppelin-contracts/contracts/interfaces/IERC165.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.4.0) (interfaces/IERC165.sol)
pragma solidity >=0.4.16;
import {IERC165} from "../utils/introspection/IERC165.sol";
lib/openzeppelin-contracts/contracts/interfaces/IERC20.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.4.0) (interfaces/IERC20.sol)
pragma solidity >=0.4.16;
import {IERC20} from "../token/ERC20/IERC20.sol";
lib/openzeppelin-contracts/contracts/interfaces/IERC20Metadata.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.4.0) (interfaces/IERC20Metadata.sol)
pragma solidity >=0.6.2;
import {IERC20Metadata} from "../token/ERC20/extensions/IERC20Metadata.sol";
lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.4.0) (token/ERC20/IERC20.sol)
pragma solidity >=0.4.16;
/**
* @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);
}
lib/openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.4.0) (token/ERC20/extensions/IERC20Metadata.sol)
pragma solidity >=0.6.2;
import {IERC20} from "../IERC20.sol";
/**
* @dev Interface for the optional metadata functions from the ERC-20 standard.
*/
interface IERC20Metadata is IERC20 {
/**
* @dev Returns the name of the token.
*/
function name() external view returns (string memory);
/**
* @dev Returns the symbol of the token.
*/
function symbol() external view returns (string memory);
/**
* @dev Returns the decimals places of the token.
*/
function decimals() external view returns (uint8);
}
lib/openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.7.0) (token/ERC20/utils/SafeERC20.sol)
pragma solidity ^0.8.20;
import {IERC20} from "../IERC20.sol";
import {IERC1363} from "../../../interfaces/IERC1363.sol";
import {IERC20Metadata} from "../../../interfaces/IERC20Metadata.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 {
if (!_safeTransfer(token, to, value, true)) {
revert SafeERC20FailedOperation(address(token));
}
}
/**
* @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 {
if (!_safeTransferFrom(token, from, to, value, true)) {
revert SafeERC20FailedOperation(address(token));
}
}
/**
* @dev Variant of {safeTransfer} that returns a bool instead of reverting if the operation is not successful.
*/
function trySafeTransfer(IERC20 token, address to, uint256 value) internal returns (bool) {
return _safeTransfer(token, to, value, false);
}
/**
* @dev Variant of {safeTransferFrom} that returns a bool instead of reverting if the operation is not successful.
*/
function trySafeTransferFrom(IERC20 token, address from, address to, uint256 value) internal returns (bool) {
return _safeTransferFrom(token, from, to, value, false);
}
/**
* @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 {
if (!_safeApprove(token, spender, value, false)) {
if (!_safeApprove(token, spender, 0, true)) revert SafeERC20FailedOperation(address(token));
if (!_safeApprove(token, spender, value, true)) revert SafeERC20FailedOperation(address(token));
}
}
/**
* @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 relies 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 relies 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}.
* Oppositely, 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 Attempts to fetch the token decimals. A return value of false indicates that the attempt failed in some way.
function tryGetDecimals(IERC20 token) internal view returns (bool success, uint8 decimals) {
bytes4 selector = IERC20Metadata.decimals.selector;
assembly ("memory-safe") {
mstore(0x00, selector)
success := staticcall(gas(), token, 0x00, 4, 0x00, 0x20)
success := and(and(success, gt(returndatasize(), 0x1f)), lt(mload(0x00), 0x100))
decimals := mul(success, mload(0x00))
}
}
/**
* @dev Imitates a Solidity `token.transfer(to, value)` call, 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 to The recipient of the tokens
* @param value The amount of token to transfer
* @param bubble Behavior switch if the transfer call reverts: bubble the revert reason or return a false boolean.
*/
function _safeTransfer(IERC20 token, address to, uint256 value, bool bubble) private returns (bool success) {
bytes4 selector = IERC20.transfer.selector;
assembly ("memory-safe") {
let fmp := mload(0x40)
mstore(0x00, selector)
mstore(0x04, and(to, shr(96, not(0))))
mstore(0x24, value)
success := call(gas(), token, 0, 0x00, 0x44, 0x00, 0x20)
// if call success and return is true, all is good.
// otherwise (not success or return is not true), we need to perform further checks
if iszero(and(success, eq(mload(0x00), 1))) {
// if the call was a failure and bubble is enabled, bubble the error
if and(iszero(success), bubble) {
returndatacopy(fmp, 0x00, returndatasize())
revert(fmp, returndatasize())
}
// if the return value is not true, then the call is only successful if:
// - the token address has code
// - the returndata is empty
success := and(success, and(iszero(returndatasize()), gt(extcodesize(token), 0)))
}
mstore(0x40, fmp)
}
}
/**
* @dev Imitates a Solidity `token.transferFrom(from, to, value)` call, 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 from The sender of the tokens
* @param to The recipient of the tokens
* @param value The amount of token to transfer
* @param bubble Behavior switch if the transfer call reverts: bubble the revert reason or return a false boolean.
*/
function _safeTransferFrom(
IERC20 token,
address from,
address to,
uint256 value,
bool bubble
) private returns (bool success) {
bytes4 selector = IERC20.transferFrom.selector;
assembly ("memory-safe") {
let fmp := mload(0x40)
mstore(0x00, selector)
mstore(0x04, and(from, shr(96, not(0))))
mstore(0x24, and(to, shr(96, not(0))))
mstore(0x44, value)
success := call(gas(), token, 0, 0x00, 0x64, 0x00, 0x20)
// if call success and return is true, all is good.
// otherwise (not success or return is not true), we need to perform further checks
if iszero(and(success, eq(mload(0x00), 1))) {
// if the call was a failure and bubble is enabled, bubble the error
if and(iszero(success), bubble) {
returndatacopy(fmp, 0x00, returndatasize())
revert(fmp, returndatasize())
}
// if the return value is not true, then the call is only successful if:
// - the token address has code
// - the returndata is empty
success := and(success, and(iszero(returndatasize()), gt(extcodesize(token), 0)))
}
mstore(0x40, fmp)
mstore(0x60, 0)
}
}
/**
* @dev Imitates a Solidity `token.approve(spender, value)` call, 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 spender The spender of the tokens
* @param value The amount of token to approve
* @param bubble Behavior switch if the approve call reverts: bubble the revert reason or return a false boolean.
*/
function _safeApprove(IERC20 token, address spender, uint256 value, bool bubble) private returns (bool success) {
bytes4 selector = IERC20.approve.selector;
assembly ("memory-safe") {
let fmp := mload(0x40)
mstore(0x00, selector)
mstore(0x04, and(spender, shr(96, not(0))))
mstore(0x24, value)
success := call(gas(), token, 0, 0x00, 0x44, 0x00, 0x20)
// if call success and return is true, all is good.
// otherwise (not success or return is not true), we need to perform further checks
if iszero(and(success, eq(mload(0x00), 1))) {
// if the call was a failure and bubble is enabled, bubble the error
if and(iszero(success), bubble) {
returndatacopy(fmp, 0x00, returndatasize())
revert(fmp, returndatasize())
}
// if the return value is not true, then the call is only successful if:
// - the token address has code
// - the returndata is empty
success := and(success, and(iszero(returndatasize()), gt(extcodesize(token), 0)))
}
mstore(0x40, fmp)
}
}
}
lib/openzeppelin-contracts/contracts/utils/introspection/IERC165.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.4.0) (utils/introspection/IERC165.sol)
pragma solidity >=0.4.16;
/**
* @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);
}
Compiler settingsShow
{
"viaIR": false,
"metadata": {
"appendCBOR": true,
"bytecodeHash": "none",
"useLiteralContent": false
},
"libraries": {},
"optimizer": {
"runs": 1000000,
"enabled": true
},
"evmVersion": "paris",
"remappings": [
"forge-std/=lib/forge-std/src/",
"@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
"erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
"halmos-cheatcodes/=lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/",
"openzeppelin-contracts/=lib/openzeppelin-contracts/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.