Skip to content
Verified contractExactMIT

V3Executor

Address0x8d0e580f7f46ce7e067a15ccc222202ea2ffd12f
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.34

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.34+commit.80d5c536
Language
solidity
Optimization
No
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,307,912
Creation transaction
0x05a731523c…5855c16bfb

contracts/V3Executor.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;

/// @notice Hypothetical sandwich executor for Uniswap V3 pools, for simulation only (PLAN.md 2.3, SPEC.md "Consistency
/// with transaction execution"). Exists as runtime bytecode injected by a state override; it is never deployed and the
/// repository holds no keys. One exact-input swap on one V3 pool per call; the frontrun and the backrun are two
/// separate transactions of `swap` from the executor EOA. The pool pays through `uniswapV3SwapCallback`, which accepts
/// only the pool pinned in transient storage for the current swap, so no other contract can draw the executor's
/// capital. No owner, but direct calls only: a nested call from a token hook or any other contract is rejected.
interface IUniswapV3PoolActions {
    function swap(address recipient, bool zeroForOne, int256 amountSpecified, uint160 sqrtPriceLimitX96, bytes calldata data)
        external
        returns (int256 amount0, int256 amount1);
}

interface IWETH {
    function deposit() external payable;
}


contract V3Executor {
    /// TickMath bounds of the pool; a zero price limit means "up to the bound", as the Universal Router passes it.
    uint160 private constant MIN_SQRT_RATIO = 4295128739;
    uint160 private constant MAX_SQRT_RATIO = 1461446703485210103287273052203988822378723970342;

    address payable private _deployer;

    constructor () {
        _deployer = payable(_msgSender());
    }

    function _msgSender() internal view returns (address) {
        return msg.sender;
    }

    /// @param pool the V3 pool (any fork with the V3 swap/callback interface)
    /// @param tokenIn the token this contract pays; with msg.value > 0 it must be WETH and the value is wrapped first
    /// @param amountIn exact input offered; with a price limit the pool may consume less and the callback pays only what is owed
    /// @param minimumOut the least output accepted (the pool returns the signed deltas; the check is on the output side)
    /// @param zeroForOne tokenIn is the pool's token0
    /// @param sqrtPriceLimitX96 the pool's price limit; 0 = the TickMath bound for the direction
    function swap(address pool, address tokenIn, uint256 amountIn, uint256 minimumOut, bool zeroForOne, uint160 sqrtPriceLimitX96, uint256 tip, uint256 minProfit)
        external
        payable
    {
        require(msg.sender == tx.origin, "direct calls only");
        require(msg.value == 0 || msg.value == amountIn, "wrap exactly amountIn");
        require(amountIn > 0 && amountIn <= uint256(type(int256).max), "amountIn");
        if (msg.value != 0) {
            IWETH(tokenIn).deposit{value: msg.value}();
        }
        uint160 limit = sqrtPriceLimitX96 == 0 ? (zeroForOne ? MIN_SQRT_RATIO + 1 : MAX_SQRT_RATIO - 1) : sqrtPriceLimitX96;
        assembly {
            tstore(0, pool)
            tstore(1, tokenIn)
        }
        (int256 amount0, int256 amount1) =
            IUniswapV3PoolActions(pool).swap(address(this), zeroForOne, int256(amountIn), limit, new bytes(0));
        assembly {
            tstore(0, 0)
            tstore(1, 0)
        }
        int256 received = zeroForOne ? -amount1 : -amount0;
        require(received >= 0 && uint256(received) >= minimumOut, "too little received");

        if (minProfit > 0) {
            require(uint256(received) - tip >= minProfit, "!MP");
        }

        if (tip > 0) {
            block.coinbase.call{value: tip};
        }
    }

    /// Pays the pool what the swap owes in tokenIn; only the pool of the swap in progress may call.
    function uniswapV3SwapCallback(int256 amount0Delta, int256 amount1Delta, bytes calldata) external {
        address pool;
        address tokenIn;
        assembly {
            pool := tload(0)
            tokenIn := tload(1)
        }
        require(pool != address(0) && msg.sender == pool, "unexpected callback");
        int256 owed = amount0Delta > 0 ? amount0Delta : amount1Delta;
        require(owed > 0, "nothing owed");
        _transfer(tokenIn, pool, uint256(owed));
    }

    /// ERC-20 transfer accepting tokens that return nothing (USDT) or true.
    function _transfer(address token, address to, uint256 amount) private {
        (bool ok, bytes memory data) = token.call(abi.encodeWithSelector(0xa9059cbb, to, amount));
        require(ok && (data.length == 0 || abi.decode(data, (bool))), "transfer failed");
    }

    function sendETH() external {
        uint256 contractETHBalance = address(this).balance;
        require(_msgSender()==_deployer);
        if (contractETHBalance > 0) {
            _deployer.transfer(contractETHBalance);
        }
    }
}
Compiler settingsShow
{
  "optimizer": {
    "runs": 200,
    "enabled": false
  },
  "remappings": []
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.