Description:
ERC20 token contract with Factory capabilities. Standard implementation for fungible tokens on Ethereum.
Blockchain: Ethereum
Source Code: View Code On The Blockchain
Solidity Source Code:
{"ArbitrageController.sol":{"content":"// SPDX-License-Identifier: MIT\r
pragma solidity ^0.8.30;\r
\r
import {IArbitrageStrategy} from "./IArbitrageStrategy.sol";\r
import {SafeTransferLib} from "./SafeTransferLib.sol";\r
import { IERC20 } from "./IERC20.sol";\r
import {IPunks} from "./IPunks.sol";\r
\r
contract ArbitrageController{\r
IPunks public immutable punks;\r
IArbitrageStrategy public strategy;\r
address public owner;\r
\r
modifier onlyOwner(){\r
require(msg.sender == owner, "Not owner you scum ;)");\r
_;\r
}\r
\r
constructor() {\r
owner = msg.sender;\r
punks = IPunks(0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB);\r
}\r
\r
fallback() external payable {}\r
receive() external payable {}\r
\r
function changeOwner(address _newOnwer) external onlyOwner {\r
owner = _newOnwer;\r
}\r
\r
function setStrategy(address _strategy) external onlyOwner {\r
strategy = IArbitrageStrategy(_strategy);\r
}\r
\r
function emergencyWithdrawETH() external{\r
SafeTransferLib.safeTransferETH(owner, address(this).balance);\r
}\r
\r
function emergencyWithdrawToken(address _token) external{\r
IERC20(_token).transfer(owner, IERC20(_token).balanceOf(address(this)));\r
}\r
\r
function emergencyWithdrawPunk(uint256 punkId) external {\r
punks.transferPunk(owner, punkId);\r
}\r
\r
function currentPunkInAuctionPriceInWai() external view returns (uint256){\r
return strategy.currentPunkInAuctionPriceInWai();\r
}\r
\r
function buyPunkForEth() external payable{\r
strategy.buyPunkForEth{value: msg.value}();\r
}\r
\r
function profit() external view returns (uint256){\r
return strategy.profit();\r
}\r
function executeArbitrage() external{\r
strategy.executeArbitrage();\r
}\r
} "},"IArbitrageStrategy.sol":{"content":"// SPDX-License-Identifier: MIT\r
pragma solidity ^0.8.30;\r
\r
interface IArbitrageStrategy {\r
function currentPunkInAuctionPriceInWai() external view returns (uint256);\r
function buyPunkForEth() external payable;\r
function profit() external view returns (uint256);\r
function executeArbitrage() external;\r
}"},"IERC20.sol":{"content":"// SPDX-License-Identifier: MIT\r
// OpenZeppelin Contracts (last updated v5.4.0) (token/ERC20/IERC20.sol)\r
\r
pragma solidity \u003e=0.4.16;\r
\r
/**\r
* @dev Interface of the ERC-20 standard as defined in the ERC.\r
*/\r
interface IERC20 {\r
/**\r
* @dev Emitted when `value` tokens are moved from one account (`from`) to\r
* another (`to`).\r
*\r
* Note that `value` may be zero.\r
*/\r
event Transfer(address indexed from, address indexed to, uint256 value);\r
\r
/**\r
* @dev Emitted when the allowance of a `spender` for an `owner` is set by\r
* a call to {approve}. `value` is the new allowance.\r
*/\r
event Approval(address indexed owner, address indexed spender, uint256 value);\r
\r
/**\r
* @dev Returns the value of tokens in existence.\r
*/\r
function totalSupply() external view returns (uint256);\r
\r
/**\r
* @dev Returns the value of tokens owned by `account`.\r
*/\r
function balanceOf(address account) external view returns (uint256);\r
\r
/**\r
* @dev Moves a `value` amount of tokens from the caller\u0027s account to `to`.\r
*\r
* Returns a boolean value indicating whether the operation succeeded.\r
*\r
* Emits a {Transfer} event.\r
*/\r
function transfer(address to, uint256 value) external returns (bool);\r
\r
/**\r
* @dev Returns the remaining number of tokens that `spender` will be\r
* allowed to spend on behalf of `owner` through {transferFrom}. This is\r
* zero by default.\r
*\r
* This value changes when {approve} or {transferFrom} are called.\r
*/\r
function allowance(address owner, address spender) external view returns (uint256);\r
\r
/**\r
* @dev Sets a `value` amount of tokens as the allowance of `spender` over the\r
* caller\u0027s tokens.\r
*\r
* Returns a boolean value indicating whether the operation succeeded.\r
*\r
* IMPORTANT: Beware that changing an allowance with this method brings the risk\r
* that someone may use both the old and the new allowance by unfortunate\r
* transaction ordering. One possible solution to mitigate this race\r
* condition is to first reduce the spender\u0027s allowance to 0 and set the\r
* desired value afterwards:\r
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729\r
*\r
* Emits an {Approval} event.\r
*/\r
function approve(address spender, uint256 value) external returns (bool);\r
\r
/**\r
* @dev Moves a `value` amount of tokens from `from` to `to` using the\r
* allowance mechanism. `value` is then deducted from the caller\u0027s\r
* allowance.\r
*\r
* Returns a boolean value indicating whether the operation succeeded.\r
*\r
* Emits a {Transfer} event.\r
*/\r
function transferFrom(address from, address to, uint256 value) external returns (bool);\r
}"},"IPunks.sol":{"content":"// SPDX-License-Identifier: MIT\r
pragma solidity ^0.8.30;\r
\r
interface IPunks {\r
function buyPunk(uint256 punkIndex) external payable;\r
function transferPunk(address to, uint256 punkIndex) external;\r
function offerPunkForSale(uint256 punkIndex, uint256 minSalePriceInWei) external;\r
function punksOfferedForSale(uint256 punkId)\r
external\r
view\r
returns (\r
bool isForSale,\r
uint256 punkIndex,\r
address seller,\r
uint256 minValue,\r
address onlySellTo\r
);\r
function balanceOf(address owner) external view returns (uint256);\r
function punkIndexToAddress(uint256 punkIndex) external view returns (address);\r
function pendingWithdrawals(address) external view returns (uint256);\r
function withdraw() external;\r
}"},"SafeTransferLib.sol":{"content":"// SPDX-License-Identifier: MIT\r
pragma solidity ^0.8.4;\r
\r
/// @notice Safe ETH and ERC20 transfer library that gracefully handles missing return values.\r
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/SafeTransferLib.sol)\r
/// @author Modified from Solmate (https://github.com/transmissions11/solmate/blob/main/src/utils/SafeTransferLib.sol)\r
/// @author Permit2 operations from (https://github.com/Uniswap/permit2/blob/main/src/libraries/Permit2Lib.sol)\r
///\r
/// @dev Note:\r
/// - For ETH transfers, please use `forceSafeTransferETH` for DoS protection.\r
library SafeTransferLib {\r
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/\r
/* CUSTOM ERRORS */\r
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/\r
\r
/// @dev The ETH transfer has failed.\r
error ETHTransferFailed();\r
\r
/// @dev The ERC20 `transferFrom` has failed.\r
error TransferFromFailed();\r
\r
/// @dev The ERC20 `transfer` has failed.\r
error TransferFailed();\r
\r
/// @dev The ERC20 `approve` has failed.\r
error ApproveFailed();\r
\r
/// @dev The ERC20 `totalSupply` query has failed.\r
error TotalSupplyQueryFailed();\r
\r
/// @dev The Permit2 operation has failed.\r
error Permit2Failed();\r
\r
/// @dev The Permit2 amount must be less than `2**160 - 1`.\r
error Permit2AmountOverflow();\r
\r
/// @dev The Permit2 approve operation has failed.\r
error Permit2ApproveFailed();\r
\r
/// @dev The Permit2 lockdown operation has failed.\r
error Permit2LockdownFailed();\r
\r
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/\r
/* CONSTANTS */\r
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/\r
\r
/// @dev Suggested gas stipend for contract receiving ETH that disallows any storage writes.\r
uint256 internal constant GAS_STIPEND_NO_STORAGE_WRITES = 2300;\r
\r
/// @dev Suggested gas stipend for contract receiving ETH to perform a few\r
/// storage reads and writes, but low enough to prevent griefing.\r
uint256 internal constant GAS_STIPEND_NO_GRIEF = 100000;\r
\r
/// @dev The unique EIP-712 domain separator for the DAI token contract.\r
bytes32 internal constant DAI_DOMAIN_SEPARATOR =\r
0xdbb8cf42e1ecb028be3f3dbc922e1d878b963f411dc388ced501601c60f7c6f7;\r
\r
/// @dev The address for the WETH9 contract on Ethereum mainnet.\r
address internal constant WETH9 = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;\r
\r
/// @dev The canonical Permit2 address.\r
/// [Github](https://github.com/Uniswap/permit2)\r
/// [Etherscan](https://etherscan.io/address/0x000000000022D473030F116dDEE9F6B43aC78BA3)\r
address internal constant PERMIT2 = 0x000000000022D473030F116dDEE9F6B43aC78BA3;\r
\r
/// @dev The canonical address of the `SELFDESTRUCT` ETH mover.\r
/// See: https://gist.github.com/Vectorized/1cb8ad4cf393b1378e08f23f79bd99fa\r
/// [Etherscan](https://etherscan.io/address/0x00000000000073c48c8055bD43D1A53799176f0D)\r
address internal constant ETH_MOVER = 0x00000000000073c48c8055bD43D1A53799176f0D;\r
\r
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/\r
/* ETH OPERATIONS */\r
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/\r
\r
// If the ETH transfer MUST succeed with a reasonable gas budget, use the force variants.\r
//\r
// The regular variants:\r
// - Forwards all remaining gas to the target.\r
// - Reverts if the target reverts.\r
// - Reverts if the current contract has insufficient balance.\r
//\r
// The force variants:\r
// - Forwards with an optional gas stipend\r
// (defaults to `GAS_STIPEND_NO_GRIEF`, which is sufficient for most cases).\r
// - If the target reverts, or if the gas stipend is exhausted,\r
// creates a temporary contract to force send the ETH via `SELFDESTRUCT`.\r
// Future compatible with `SENDALL`: https://eips.ethereum.org/EIPS/eip-4758.\r
// - Reverts if the current contract has insufficient balance.\r
//\r
// The try variants:\r
// - Forwards with a mandatory gas stipend.\r
// - Instead of reverting, returns whether the transfer succeeded.\r
\r
/// @dev Sends `amount` (in wei) ETH to `to`.\r
function safeTransferETH(address to, uint256 amount) internal {\r
/// @solidity memory-safe-assembly\r
assembly {\r
if iszero(call(gas(), to, amount, codesize(), 0x00, codesize(), 0x00)) {\r
mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.\r
revert(0x1c, 0x04)\r
}\r
}\r
}\r
\r
/// @dev Sends all the ETH in the current contract to `to`.\r
function safeTransferAllETH(address to) internal {\r
/// @solidity memory-safe-assembly\r
assembly {\r
// Transfer all the ETH and check if it succeeded or not.\r
if iszero(call(gas(), to, selfbalance(), codesize(), 0x00, codesize(), 0x00)) {\r
mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.\r
revert(0x1c, 0x04)\r
}\r
}\r
}\r
\r
/// @dev Force sends `amount` (in wei) ETH to `to`, with a `gasStipend`.\r
function forceSafeTransferETH(address to, uint256 amount, uint256 gasStipend) internal {\r
/// @solidity memory-safe-assembly\r
assembly {\r
if lt(selfbalance(), amount) {\r
mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.\r
revert(0x1c, 0x04)\r
}\r
if iszero(call(gasStipend, to, amount, codesize(), 0x00, codesize(), 0x00)) {\r
mstore(0x00, to) // Store the address in scratch space.\r
mstore8(0x0b, 0x73) // Opcode `PUSH20`.\r
mstore8(0x20, 0xff) // Opcode `SELFDESTRUCT`.\r
if iszero(create(amount, 0x0b, 0x16)) { revert(codesize(), codesize()) } // For gas estimation.\r
}\r
}\r
}\r
\r
/// @dev Force sends all the ETH in the current contract to `to`, with a `gasStipend`.\r
function forceSafeTransferAllETH(address to, uint256 gasStipend) internal {\r
/// @solidity memory-safe-assembly\r
assembly {\r
if iszero(call(gasStipend, to, selfbalance(), codesize(), 0x00, codesize(), 0x00)) {\r
mstore(0x00, to) // Store the address in scratch space.\r
mstore8(0x0b, 0x73) // Opcode `PUSH20`.\r
mstore8(0x20, 0xff) // Opcode `SELFDESTRUCT`.\r
if iszero(create(selfbalance(), 0x0b, 0x16)) { revert(codesize(), codesize()) } // For gas estimation.\r
}\r
}\r
}\r
\r
/// @dev Force sends `amount` (in wei) ETH to `to`, with `GAS_STIPEND_NO_GRIEF`.\r
function forceSafeTransferETH(address to, uint256 amount) internal {\r
/// @solidity memory-safe-assembly\r
assembly {\r
if lt(selfbalance(), amount) {\r
mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.\r
revert(0x1c, 0x04)\r
}\r
if iszero(call(GAS_STIPEND_NO_GRIEF, to, amount, codesize(), 0x00, codesize(), 0x00)) {\r
mstore(0x00, to) // Store the address in scratch space.\r
mstore8(0x0b, 0x73) // Opcode `PUSH20`.\r
mstore8(0x20, 0xff) // Opcode `SELFDESTRUCT`.\r
if iszero(create(amount, 0x0b, 0x16)) { revert(codesize(), codesize()) } // For gas estimation.\r
}\r
}\r
}\r
\r
/// @dev Force sends all the ETH in the current contract to `to`, with `GAS_STIPEND_NO_GRIEF`.\r
function forceSafeTransferAllETH(address to) internal {\r
/// @solidity memory-safe-assembly\r
assembly {\r
// forgefmt: disable-next-item\r
if iszero(call(GAS_STIPEND_NO_GRIEF, to, selfbalance(), codesize(), 0x00, codesize(), 0x00)) {\r
mstore(0x00, to) // Store the address in scratch space.\r
mstore8(0x0b, 0x73) // Opcode `PUSH20`.\r
mstore8(0x20, 0xff) // Opcode `SELFDESTRUCT`.\r
if iszero(create(selfbalance(), 0x0b, 0x16)) { revert(codesize(), codesize()) } // For gas estimation.\r
}\r
}\r
}\r
\r
/// @dev Sends `amount` (in wei) ETH to `to`, with a `gasStipend`.\r
function trySafeTransferETH(address to, uint256 amount, uint256 gasStipend)\r
internal\r
returns (bool success)\r
{\r
/// @solidity memory-safe-assembly\r
assembly {\r
success := call(gasStipend, to, amount, codesize(), 0x00, codesize(), 0x00)\r
}\r
}\r
\r
/// @dev Sends all the ETH in the current contract to `to`, with a `gasStipend`.\r
function trySafeTransferAllETH(address to, uint256 gasStipend)\r
internal\r
returns (bool success)\r
{\r
/// @solidity memory-safe-assembly\r
assembly {\r
success := call(gasStipend, to, selfbalance(), codesize(), 0x00, codesize(), 0x00)\r
}\r
}\r
\r
/// @dev Force transfers ETH to `to`, without triggering the fallback (if any).\r
/// This method attempts to use a separate contract to send via `SELFDESTRUCT`,\r
/// and upon failure, deploys a minimal vault to accrue the ETH.\r
function safeMoveETH(address to, uint256 amount) internal returns (address vault) {\r
/// @solidity memory-safe-assembly\r
assembly {\r
to := shr(96, shl(96, to)) // Clean upper 96 bits.\r
for { let mover := ETH_MOVER } iszero(eq(to, address())) {} {\r
let selfBalanceBefore := selfbalance()\r
if or(lt(selfBalanceBefore, amount), eq(to, mover)) {\r
mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.\r
revert(0x1c, 0x04)\r
}\r
if extcodesize(mover) {\r
let balanceBefore := balance(to) // Check via delta, in case `SELFDESTRUCT` is bricked.\r
mstore(0x00, to)\r
pop(call(gas(), mover, amount, 0x00, 0x20, codesize(), 0x00))\r
// If `address(to).balance \u003e= amount + balanceBefore`, skip vault workflow.\r
if iszero(lt(balance(to), add(amount, balanceBefore))) { break }\r
// Just in case `SELFDESTRUCT` is changed to not revert and do nothing.\r
if lt(selfBalanceBefore, selfbalance()) { invalid() }\r
}\r
let m := mload(0x40)\r
// If the mover is missing or bricked, deploy a minimal vault\r
// that withdraws all ETH to `to` when being called only by `to`.\r
// forgefmt: disable-next-item\r
mstore(add(m, 0x20), 0x33146025575b600160005260206000f35b3d3d3d3d47335af1601a5760003dfd)\r
mstore(m, or(to, shl(160, 0x6035600b3d3960353df3fe73)))\r
// Compute and store the bytecode hash.\r
mstore8(0x00, 0xff) // Write the prefix.\r
mstore(0x35, keccak256(m, 0x40))\r
mstore(0x01, shl(96, address())) // Deployer.\r
mstore(0x15, 0) // Salt.\r
vault := keccak256(0x00, 0x55)\r
pop(call(gas(), vault, amount, codesize(), 0x00, codesize(), 0x00))\r
// The vault returns a single word on success. Failure reverts with empty data.\r
if iszero(returndatasize()) {\r
if iszero(create2(0, m, 0x40, 0)) { revert(codesize(), codesize()) } // For gas estimation.\r
}\r
mstore(0x40, m) // Restore the free memory pointer.\r
break\r
}\r
}\r
}\r
\r
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/\r
/* ERC20 OPERATIONS */\r
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/\r
\r
/// @dev Sends `amount` of ERC20 `token` from `from` to `to`.\r
/// Reverts upon failure.\r
///\r
/// The `from` account must have at least `amount` approved for\r
/// the current contract to manage.\r
function safeTransferFrom(address token, address from, address to, uint256 amount) internal {\r
/// @solidity memory-safe-assembly\r
assembly {\r
let m := mload(0x40) // Cache the free memory pointer.\r
mstore(0x60, amount) // Store the `amount` argument.\r
mstore(0x40, to) // Store the `to` argument.\r
mstore(0x2c, shl(96, from)) // Store the `from` argument.\r
mstore(0x0c, 0x23b872dd000000000000000000000000) // `transferFrom(address,address,uint256)`.\r
let success := call(gas(), token, 0, 0x1c, 0x64, 0x00, 0x20)\r
if iszero(and(eq(mload(0x00), 1), success)) {\r
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {\r
mstore(0x00, 0x7939f424) // `TransferFromFailed()`.\r
revert(0x1c, 0x04)\r
}\r
}\r
mstore(0x60, 0) // Restore the zero slot to zero.\r
mstore(0x40, m) // Restore the free memory pointer.\r
}\r
}\r
\r
/// @dev Sends `amount` of ERC20 `token` from `from` to `to`.\r
///\r
/// The `from` account must have at least `amount` approved for the current contract to manage.\r
function trySafeTransferFrom(address token, address from, address to, uint256 amount)\r
internal\r
returns (bool success)\r
{\r
/// @solidity memory-safe-assembly\r
assembly {\r
let m := mload(0x40) // Cache the free memory pointer.\r
mstore(0x60, amount) // Store the `amount` argument.\r
mstore(0x40, to) // Store the `to` argument.\r
mstore(0x2c, shl(96, from)) // Store the `from` argument.\r
mstore(0x0c, 0x23b872dd000000000000000000000000) // `transferFrom(address,address,uint256)`.\r
success := call(gas(), token, 0, 0x1c, 0x64, 0x00, 0x20)\r
if iszero(and(eq(mload(0x00), 1), success)) {\r
success := lt(or(iszero(extcodesize(token)), returndatasize()), success)\r
}\r
mstore(0x60, 0) // Restore the zero slot to zero.\r
mstore(0x40, m) // Restore the free memory pointer.\r
}\r
}\r
\r
/// @dev Sends all of ERC20 `token` from `from` to `to`.\r
/// Reverts upon failure.\r
///\r
/// The `from` account must have their entire balance approved for the current contract to manage.\r
function safeTransferAllFrom(address token, address from, address to)\r
internal\r
returns (uint256 amount)\r
{\r
/// @solidity memory-safe-assembly\r
assembly {\r
let m := mload(0x40) // Cache the free memory pointer.\r
mstore(0x40, to) // Store the `to` argument.\r
mstore(0x2c, shl(96, from)) // Store the `from` argument.\r
mstore(0x0c, 0x70a08231000000000000000000000000) // `balanceOf(address)`.\r
// Read the balance, reverting upon failure.\r
if iszero(\r
and( // The arguments of `and` are evaluated from right to left.\r
gt(returndatasize(), 0x1f), // At least 32 bytes returned.\r
staticcall(gas(), token, 0x1c, 0x24, 0x60, 0x20)\r
)\r
) {\r
mstore(0x00, 0x7939f424) // `TransferFromFailed()`.\r
revert(0x1c, 0x04)\r
}\r
mstore(0x00, 0x23b872dd) // `transferFrom(address,address,uint256)`.\r
amount := mload(0x60) // The `amount` is already at 0x60. We\u0027ll need to return it.\r
// Perform the transfer, reverting upon failure.\r
let success := call(gas(), token, 0, 0x1c, 0x64, 0x00, 0x20)\r
if iszero(and(eq(mload(0x00), 1), success)) {\r
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {\r
mstore(0x00, 0x7939f424) // `TransferFromFailed()`.\r
revert(0x1c, 0x04)\r
}\r
}\r
mstore(0x60, 0) // Restore the zero slot to zero.\r
mstore(0x40, m) // Restore the free memory pointer.\r
}\r
}\r
\r
/// @dev Sends `amount` of ERC20 `token` from the current contract to `to`.\r
/// Reverts upon failure.\r
function safeTransfer(address token, address to, uint256 amount) internal {\r
/// @solidity memory-safe-assembly\r
assembly {\r
mstore(0x14, to) // Store the `to` argument.\r
mstore(0x34, amount) // Store the `amount` argument.\r
mstore(0x00, 0xa9059cbb000000000000000000000000) // `transfer(address,uint256)`.\r
// Perform the transfer, reverting upon failure.\r
let success := call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)\r
if iszero(and(eq(mload(0x00), 1), success)) {\r
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {\r
mstore(0x00, 0x90b8ec18) // `TransferFailed()`.\r
revert(0x1c, 0x04)\r
}\r
}\r
mstore(0x34, 0) // Restore the part of the free memory pointer that was overwritten.\r
}\r
}\r
\r
/// @dev Sends all of ERC20 `token` from the current contract to `to`.\r
/// Reverts upon failure.\r
function safeTransferAll(address token, address to) internal returns (uint256 amount) {\r
/// @solidity memory-safe-assembly\r
assembly {\r
mstore(0x00, 0x70a08231) // Store the function selector of `balanceOf(address)`.\r
mstore(0x20, address()) // Store the address of the current contract.\r
// Read the balance, reverting upon failure.\r
if iszero(\r
and( // The arguments of `and` are evaluated from right to left.\r
gt(returndatasize(), 0x1f), // At least 32 bytes returned.\r
staticcall(gas(), token, 0x1c, 0x24, 0x34, 0x20)\r
)\r
) {\r
mstore(0x00, 0x90b8ec18) // `TransferFailed()`.\r
revert(0x1c, 0x04)\r
}\r
mstore(0x14, to) // Store the `to` argument.\r
amount := mload(0x34) // The `amount` is already at 0x34. We\u0027ll need to return it.\r
mstore(0x00, 0xa9059cbb000000000000000000000000) // `transfer(address,uint256)`.\r
// Perform the transfer, reverting upon failure.\r
let success := call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)\r
if iszero(and(eq(mload(0x00), 1), success)) {\r
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {\r
mstore(0x00, 0x90b8ec18) // `TransferFailed()`.\r
revert(0x1c, 0x04)\r
}\r
}\r
mstore(0x34, 0) // Restore the part of the free memory pointer that was overwritten.\r
}\r
}\r
\r
/// @dev Sets `amount` of ERC20 `token` for `to` to manage on behalf of the current contract.\r
/// Reverts upon failure.\r
function safeApprove(address token, address to, uint256 amount) internal {\r
/// @solidity memory-safe-assembly\r
assembly {\r
mstore(0x14, to) // Store the `to` argument.\r
mstore(0x34, amount) // Store the `amount` argument.\r
mstore(0x00, 0x095ea7b3000000000000000000000000) // `approve(address,uint256)`.\r
let success := call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)\r
if iszero(and(eq(mload(0x00), 1), success)) {\r
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {\r
mstore(0x00, 0x3e3f8f73) // `ApproveFailed()`.\r
revert(0x1c, 0x04)\r
}\r
}\r
mstore(0x34, 0) // Restore the part of the free memory pointer that was overwritten.\r
}\r
}\r
\r
/// @dev Sets `amount` of ERC20 `token` for `to` to manage on behalf of the current contract.\r
/// If the initial attempt to approve fails, attempts to reset the approved amount to zero,\r
/// then retries the approval again (some tokens, e.g. USDT, requires this).\r
/// Reverts upon failure.\r
function safeApproveWithRetry(address token, address to, uint256 amount) internal {\r
/// @solidity memory-safe-assembly\r
assembly {\r
mstore(0x14, to) // Store the `to` argument.\r
mstore(0x34, amount) // Store the `amount` argument.\r
mstore(0x00, 0x095ea7b3000000000000000000000000) // `approve(address,uint256)`.\r
// Perform the approval, retrying upon failure.\r
let success := call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)\r
if iszero(and(eq(mload(0x00), 1), success)) {\r
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {\r
mstore(0x34, 0) // Store 0 for the `amount`.\r
mstore(0x00, 0x095ea7b3000000000000000000000000) // `approve(address,uint256)`.\r
pop(call(gas(), token, 0, 0x10, 0x44, codesize(), 0x00)) // Reset the approval.\r
mstore(0x34, amount) // Store back the original `amount`.\r
// Retry the approval, reverting upon failure.\r
success := call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)\r
if iszero(and(eq(mload(0x00), 1), success)) {\r
// Check the `extcodesize` again just in case the token selfdestructs lol.\r
if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) {\r
mstore(0x00, 0x3e3f8f73) // `ApproveFailed()`.\r
revert(0x1c, 0x04)\r
}\r
}\r
}\r
}\r
mstore(0x34, 0) // Restore the part of the free memory pointer that was overwritten.\r
}\r
}\r
\r
/// @dev Returns the amount of ERC20 `token` owned by `account`.\r
/// Returns zero if the `token` does not exist.\r
function balanceOf(address token, address account) internal view returns (uint256 amount) {\r
/// @solidity memory-safe-assembly\r
assembly {\r
mstore(0x14, account) // Store the `account` argument.\r
mstore(0x00, 0x70a08231000000000000000000000000) // `balanceOf(address)`.\r
amount :=\r
mul( // The arguments of `mul` are evaluated from right to left.\r
mload(0x20),\r
and( // The arguments of `and` are evaluated from right to left.\r
gt(returndatasize(), 0x1f), // At least 32 bytes returned.\r
staticcall(gas(), token, 0x10, 0x24, 0x20, 0x20)\r
)\r
)\r
}\r
}\r
\r
/// @dev Performs a `token.balanceOf(account)` check.\r
/// `implemented` denotes whether the `token` does not implement `balanceOf`.\r
/// `amount` is zero if the `token` does not implement `balanceOf`.\r
function checkBalanceOf(address token, address account)\r
internal\r
view\r
returns (bool implemented, uint256 amount)\r
{\r
/// @solidity memory-safe-assembly\r
assembly {\r
mstore(0x14, account) // Store the `account` argument.\r
mstore(0x00, 0x70a08231000000000000000000000000) // `balanceOf(address)`.\r
implemented :=\r
and( // The arguments of `and` are evaluated from right to left.\r
gt(returndatasize(), 0x1f), // At least 32 bytes returned.\r
staticcall(gas(), token, 0x10, 0x24, 0x20, 0x20)\r
)\r
amount := mul(mload(0x20), implemented)\r
}\r
}\r
\r
/// @dev Returns the total supply of the `token`.\r
/// Reverts if the token does not exist or does not implement `totalSupply()`.\r
function totalSupply(address token) internal view returns (uint256 result) {\r
/// @solidity memory-safe-assembly\r
assembly {\r
mstore(0x00, 0x18160ddd) // `totalSupply()`.\r
if iszero(\r
and(gt(returndatasize(), 0x1f), staticcall(gas(), token, 0x1c, 0x04, 0x00, 0x20))\r
) {\r
mstore(0x00, 0x54cd9435) // `TotalSupplyQueryFailed()`.\r
revert(0x1c, 0x04)\r
}\r
result := mload(0x00)\r
}\r
}\r
\r
/// @dev Sends `amount` of ERC20 `token` from `from` to `to`.\r
/// If the initial attempt fails, try to use Permit2 to transfer the token.\r
/// Reverts upon failure.\r
///\r
/// The `from` account must have at least `amount` approved for the current contract to manage.\r
function safeTransferFrom2(address token, address from, address to, uint256 amount) internal {\r
if (!trySafeTransferFrom(token, from, to, amount)) {\r
permit2TransferFrom(token, from, to, amount);\r
}\r
}\r
\r
/// @dev Sends `amount` of ERC20 `token` from `from` to `to` via Permit2.\r
/// Reverts upon failure.\r
function permit2TransferFrom(address token, address from, address to, uint256 amount)\r
internal\r
{\r
/// @solidity memory-safe-assembly\r
assembly {\r
let m := mload(0x40)\r
mstore(add(m, 0x74), shr(96, shl(96, token)))\r
mstore(add(m, 0x54), amount)\r
mstore(add(m, 0x34), to)\r
mstore(add(m, 0x20), shl(96, from))\r
// `transferFrom(address,address,uint160,address)`.\r
mstore(m, 0x36c78516000000000000000000000000)\r
let p := PERMIT2\r
let exists := eq(chainid(), 1)\r
if iszero(exists) { exists := iszero(iszero(extcodesize(p))) }\r
if iszero(\r
and(\r
call(gas(), p, 0, add(m, 0x10), 0x84, codesize(), 0x00),\r
lt(iszero(extcodesize(token)), exists) // Token has code and Permit2 exists.\r
)\r
) {\r
mstore(0x00, 0x7939f4248757f0fd) // `TransferFromFailed()` or `Permit2AmountOverflow()`.\r
revert(add(0x18, shl(2, iszero(iszero(shr(160, amount))))), 0x04)\r
}\r
}\r
}\r
\r
/// @dev Permit a user to spend a given amount of\r
/// another user\u0027s tokens via native EIP-2612 permit if possible, falling\r
/// back to Permit2 if native permit fails or is not implemented on the token.\r
function permit2(\r
address token,\r
address owner,\r
address spender,\r
uint256 amount,\r
uint256 deadline,\r
uint8 v,\r
bytes32 r,\r
bytes32 s\r
) internal {\r
bool success;\r
/// @solidity memory-safe-assembly\r
assembly {\r
for {} shl(96, xor(token, WETH9)) {} {\r
mstore(0x00, 0x3644e515) // `DOMAIN_SEPARATOR()`.\r
if iszero(\r
and( // The arguments of `and` are evaluated from right to left.\r
lt(iszero(mload(0x00)), eq(returndatasize(), 0x20)), // Returns 1 non-zero word.\r
// Gas stipend to limit gas burn for tokens that don\u0027t refund gas when\r
// an non-existing function is called. 5K should be enough for a SLOAD.\r
staticcall(5000, token, 0x1c, 0x04, 0x00, 0x20)\r
)\r
) { break }\r
// After here, we can be sure that token is a contract.\r
let m := mload(0x40)\r
mstore(add(m, 0x34), spender)\r
mstore(add(m, 0x20), shl(96, owner))\r
mstore(add(m, 0x74), deadline)\r
if eq(mload(0x00), DAI_DOMAIN_SEPARATOR) {\r
mstore(0x14, owner)\r
mstore(0x00, 0x7ecebe00000000000000000000000000) // `nonces(address)`.\r
mstore(\r
add(m, 0x94),\r
lt(iszero(amount), staticcall(gas(), token, 0x10, 0x24, add(m, 0x54), 0x20))\r
)\r
mstore(m, 0x8fcbaf0c000000000000000000000000) // `IDAIPermit.permit`.\r
// `nonces` is already at `add(m, 0x54)`.\r
// `amount != 0` is already stored at `add(m, 0x94)`.\r
mstore(add(m, 0xb4), and(0xff, v))\r
mstore(add(m, 0xd4), r)\r
mstore(add(m, 0xf4), s)\r
success := call(gas(), token, 0, add(m, 0x10), 0x104, codesize(), 0x00)\r
break\r
}\r
mstore(m, 0xd505accf000000000000000000000000) // `IERC20Permit.permit`.\r
mstore(add(m, 0x54), amount)\r
mstore(add(m, 0x94), and(0xff, v))\r
mstore(add(m, 0xb4), r)\r
mstore(add(m, 0xd4), s)\r
success := call(gas(), token, 0, add(m, 0x10), 0xe4, codesize(), 0x00)\r
break\r
}\r
}\r
if (!success) simplePermit2(token, owner, spender, amount, deadline, v, r, s);\r
}\r
\r
/// @dev Simple permit on the Permit2 contract.\r
function simplePermit2(\r
address token,\r
address owner,\r
address spender,\r
uint256 amount,\r
uint256 deadline,\r
uint8 v,\r
bytes32 r,\r
bytes32 s\r
) internal {\r
/// @solidity memory-safe-assembly\r
assembly {\r
let m := mload(0x40)\r
mstore(m, 0x927da105) // `allowance(address,address,address)`.\r
{\r
let addressMask := shr(96, not(0))\r
mstore(add(m, 0x20), and(addressMask, owner))\r
mstore(add(m, 0x40), and(addressMask, token))\r
mstore(add(m, 0x60), and(addressMask, spender))\r
mstore(add(m, 0xc0), and(addressMask, spender))\r
}\r
let p := mul(PERMIT2, iszero(shr(160, amount)))\r
if iszero(\r
and( // The arguments of `and` are evaluated from right to left.\r
gt(returndatasize(), 0x5f), // Returns 3 words: `amount`, `expiration`, `nonce`.\r
staticcall(gas(), p, add(m, 0x1c), 0x64, add(m, 0x60), 0x60)\r
)\r
) {\r
mstore(0x00, 0x6b836e6b8757f0fd) // `Permit2Failed()` or `Permit2AmountOverflow()`.\r
revert(add(0x18, shl(2, iszero(p))), 0x04)\r
}\r
mstore(m, 0x2b67b570) // `Permit2.permit` (PermitSingle variant).\r
// `owner` is already `add(m, 0x20)`.\r
// `token` is already at `add(m, 0x40)`.\r
mstore(add(m, 0x60), amount)\r
mstore(add(m, 0x80), 0xffffffffffff) // `expiration = type(uint48).max`.\r
// `nonce` is already at `add(m, 0xa0)`.\r
// `spender` is already at `add(m, 0xc0)`.\r
mstore(add(m, 0xe0), deadline)\r
mstore(add(m, 0x100), 0x100) // `signature` offset.\r
mstore(add(m, 0x120), 0x41) // `signature` length.\r
mstore(add(m, 0x140), r)\r
mstore(add(m, 0x160), s)\r
mstore(add(m, 0x180), shl(248, v))\r
if iszero( // Revert if token does not have code, or if the call fails.\r
mul(extcodesize(token), call(gas(), p, 0, add(m, 0x1c), 0x184, codesize(), 0x00))) {\r
mstore(0x00, 0x6b836e6b) // `Permit2Failed()`.\r
revert(0x1c, 0x04)\r
}\r
}\r
}\r
\r
/// @dev Approves `spender` to spend `amount` of `token` for `address(this)`.\r
function permit2Approve(address token, address spender, uint160 amount, uint48 expiration)\r
internal\r
{\r
/// @solidity memory-safe-assembly\r
assembly {\r
let addressMask := shr(96, not(0))\r
let m := mload(0x40)\r
mstore(m, 0x87517c45) // `approve(address,address,uint160,uint48)`.\r
mstore(add(m, 0x20), and(addressMask, token))\r
mstore(add(m, 0x40), and(addressMask, spender))\r
mstore(add(m, 0x60), and(addressMask, amount))\r
mstore(add(m, 0x80), and(0xffffffffffff, expiration))\r
if iszero(call(gas(), PERMIT2, 0, add(m, 0x1c), 0xa0, codesize(), 0x00)) {\r
mstore(0x00, 0x324f14ae) // `Permit2ApproveFailed()`.\r
revert(0x1c, 0x04)\r
}\r
}\r
}\r
\r
/// @dev Revokes an approval for `token` and `spender` for `address(this)`.\r
function permit2Lockdown(address token, address spender) internal {\r
/// @solidity memory-safe-assembly\r
assembly {\r
let m := mload(0x40)\r
mstore(m, 0xcc53287f) // `Permit2.lockdown`.\r
mstore(add(m, 0x20), 0x20) // Offset of the `approvals`.\r
mstore(add(m, 0x40), 1) // `approvals.length`.\r
mstore(add(m, 0x60), shr(96, shl(96, token)))\r
mstore(add(m, 0x80), shr(96, shl(96, spender)))\r
if iszero(call(gas(), PERMIT2, 0, add(m, 0x1c), 0xa0, codesize(), 0x00)) {\r
mstore(0x00, 0x96b3de23) // `Permit2LockdownFailed()`.\r
revert(0x1c, 0x04)\r
}\r
}\r
}\r
}"}}
Submitted on: 2025-10-17 17:27:58
Comments
Log in to comment.
No comments yet.