Skip to content
Verified contractExactMIT

AaveV3Curve3poolAutoArb

Address0xe067763e42e3dd76c176ebf5ccad0e69639e43d4
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.38

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.38+commit.1835d247
Language
solidity
Optimization
Yes, 200 runs
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,327,000
Creation transaction
0xef8548304f…9b13490647

AaveV3Curve3poolAutoArb.sol

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

/// @notice Minimal Aave V3 single-asset flash-loan interface.
interface IAaveV3SimplePool {
    function flashLoanSimple(
        address receiverAddress,
        address asset,
        uint256 amount,
        bytes calldata params,
        uint16 referralCode
    ) external;
}

interface IERC20Limited {
    function balanceOf(address account) external view returns (uint256);
    function allowance(address owner, address spender) external view returns (uint256);
}

interface IUniswapV3RouterLimited {
    struct ExactInputSingleParams {
        address tokenIn;
        address tokenOut;
        uint24 fee;
        address recipient;
        uint256 deadline;
        uint256 amountIn;
        uint256 amountOutMinimum;
        uint160 sqrtPriceLimitX96;
    }

    function exactInputSingle(
        ExactInputSingleParams calldata params
    ) external payable returns (uint256 amountOut);
}

interface ICurve3PoolLimited {
    function coins(uint256 index) external view returns (address);

    function exchange(
        int128 i,
        int128 j,
        uint256 dx,
        uint256 minDy
    ) external returns (uint256);
}

/// @title Limited-scope Aave V3 / Uniswap V3 / Curve 3pool automated arbitrage
/// @notice Ethereum mainnet only. Starts paused, with no authorized bot.
/// @dev Experimental and NOT externally audited.
///      Supports WETH -> USDC -> USDT -> WETH exclusively.
///      Gas profitability uses a conservative budget, not measured gas.
///      Only the bot may start arbitrage. The owner retains emergency controls.
contract AaveV3Curve3poolAutoArb {
    address public constant AAVE_POOL =
        0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2;

    address public constant UNISWAP_V3_ROUTER =
        0xE592427A0AEce92De3Edee1F18E0157C05861564;

    address public constant CURVE_3POOL =
        0xbEbc44782C7dB0a1A60Cb6fe97d0b483032FF1C7;

    address public constant WETH =
        0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;

    address public constant USDC =
        0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;

    address public constant USDT =
        0xdAC17F958D2ee523a2206206994597C13D831ec7;

    uint256 public constant MAX_LOAN_WETH = 1 ether;

    // Profit protection assumes successful execution uses no more
    // than this gas budget. The sender must also enforce a gas limit.
    uint256 public constant GAS_BUDGET_UNITS = 1_500_000;

    address public immutable owner;

    // An independently funded hot wallet used ONLY to submit trades.
    // The owner must authorize this address before trading can be enabled.
    address public bot;

    bool public paused = true;
    bool private inFlashLoan;

    uint256 private startingWethBalance;

    struct Trade {
        uint256 loanAmountWeth;
        uint24 firstV3Fee;
        uint24 lastV3Fee;
        uint256 minUsdcOut;
        uint256 minUsdtOut;
        uint256 minWethOut;
        uint256 minProfitAfterGasWeth;
        uint256 maxGasPriceWei;
        uint256 deadline;
    }

    event PauseChanged(bool paused);

    event BotUpdated(address indexed previousBot, address indexed newBot);

    event ArbitrageCompleted(
        uint256 loanWeth,
        uint256 premiumWeth,
        uint256 usdcReceived,
        uint256 usdtReceived,
        uint256 wethReceived,
        uint256 profitAfterAaveWeth,
        uint256 protectedGasCostWeth
    );

    event TokenWithdrawn(
        address token,
        address recipient,
        uint256 amount
    );

    modifier onlyOwner() {
        require(msg.sender == owner, "NOT_OWNER");
        _;
    }

    modifier onlyBot() {
        require(msg.sender == bot && bot != address(0), "NOT_BOT");
        _;
    }

    constructor() {
        require(block.chainid == 1, "MAINNET_ONLY");

        owner = msg.sender;

        require(
            ICurve3PoolLimited(CURVE_3POOL).coins(1) == USDC,
            "WRONG_CURVE_USDC"
        );

        require(
            ICurve3PoolLimited(CURVE_3POOL).coins(2) == USDT,
            "WRONG_CURVE_USDT"
        );
    }

    /// @notice Assign a new bot while paused; revoke a bot at any time.
    /// @dev Never hand the bot wallet the owner's private key.
    function setBot(address newBot) external onlyOwner {
        require(!inFlashLoan, "FLASH_ACTIVE");
        require(paused || newBot == address(0), "PAUSE_TO_CHANGE_BOT");
        require(newBot != owner, "BOT_IS_OWNER");
        require(newBot != address(this), "BOT_IS_CONTRACT");

        address previousBot = bot;
        bot = newBot;
        emit BotUpdated(previousBot, newBot);
    }

    function setPaused(bool newPaused) external onlyOwner {
        require(!inFlashLoan, "FLASH_ACTIVE");
        require(newPaused || bot != address(0), "BOT_UNSET");

        paused = newPaused;

        emit PauseChanged(newPaused);
    }

    function startArbitrage(
        Trade calldata trade
    ) external onlyBot {
        require(!paused, "PAUSED");
        require(!inFlashLoan, "FLASH_ACTIVE");

        require(
            trade.loanAmountWeth > 0 &&
            trade.loanAmountWeth <= MAX_LOAN_WETH,
            "INVALID_LOAN"
        );

        require(
            trade.firstV3Fee == 100 ||
            trade.firstV3Fee == 500,
            "INVALID_FIRST_FEE"
        );

        require(
            trade.lastV3Fee == 100 ||
            trade.lastV3Fee == 500,
            "INVALID_LAST_FEE"
        );

        require(
            trade.firstV3Fee != trade.lastV3Fee,
            "SAME_FEE"
        );

        require(
            trade.minUsdcOut > 0 &&
            trade.minUsdtOut > 0 &&
            trade.minWethOut > 0,
            "ZERO_MIN_OUTPUT"
        );

        require(
            trade.minProfitAfterGasWeth > 0,
            "ZERO_MIN_PROFIT"
        );

        require(
            trade.maxGasPriceWei > 0 &&
            tx.gasprice <= trade.maxGasPriceWei,
            "GAS_PRICE_EXCEEDED"
        );

        require(
            trade.deadline >= block.timestamp &&
            trade.deadline <= block.timestamp + 300,
            "INVALID_DEADLINE"
        );

        startingWethBalance =
            IERC20Limited(WETH).balanceOf(address(this));

        inFlashLoan = true;

        IAaveV3SimplePool(AAVE_POOL).flashLoanSimple(
            address(this),
            WETH,
            trade.loanAmountWeth,
            abi.encode(trade),
            0
        );

        inFlashLoan = false;
        startingWethBalance = 0;
    }

    function executeOperation(
        address asset,
        uint256 amount,
        uint256 premium,
        address initiator,
        bytes calldata params
    ) external returns (bool) {
        require(msg.sender == AAVE_POOL, "NOT_AAVE");
        require(
            initiator == address(this) && inFlashLoan,
            "UNEXPECTED_CALLBACK"
        );

        require(asset == WETH, "WRONG_ASSET");

        Trade memory trade = abi.decode(params, (Trade));

        require(
            amount == trade.loanAmountWeth,
            "WRONG_AMOUNT"
        );

        require(
            block.timestamp <= trade.deadline,
            "EXPIRED"
        );

        (
            uint256 usdcOut,
            uint256 usdtOut,
            uint256 wethOut
        ) = _executeSwaps(trade, amount);

        uint256 debt = amount + premium;

        uint256 balance =
            IERC20Limited(WETH).balanceOf(address(this));

        uint256 requiredBalance =
            startingWethBalance + debt;

        require(
            balance >= requiredBalance,
            "LOAN_NOT_COVERED"
        );

        uint256 profitAfterAave =
            balance - requiredBalance;

        uint256 protectedGasCost =
            tx.gasprice * GAS_BUDGET_UNITS;

        require(
            profitAfterAave >=
                protectedGasCost + trade.minProfitAfterGasWeth,
            "INSUFFICIENT_NET_PROFIT"
        );

        _approveExact(WETH, AAVE_POOL, debt);

        emit ArbitrageCompleted(
            amount,
            premium,
            usdcOut,
            usdtOut,
            wethOut,
            profitAfterAave,
            protectedGasCost
        );

        return true;
    }

    function _executeSwaps(
        Trade memory trade,
        uint256 amount
    ) private returns (
        uint256 usdcOut,
        uint256 usdtOut,
        uint256 wethOut
    ) {
        // Leg 1: WETH -> USDC on Uniswap V3.
        _approveExact(
            WETH,
            UNISWAP_V3_ROUTER,
            amount
        );

        usdcOut = IUniswapV3RouterLimited(
            UNISWAP_V3_ROUTER
        ).exactInputSingle(
            IUniswapV3RouterLimited.ExactInputSingleParams({
                tokenIn: WETH,
                tokenOut: USDC,
                fee: trade.firstV3Fee,
                recipient: address(this),
                deadline: trade.deadline,
                amountIn: amount,
                amountOutMinimum: trade.minUsdcOut,
                sqrtPriceLimitX96: 0
            })
        );

        // Leg 2: USDC -> USDT on Curve 3pool.
        // Curve indices: DAI=0, USDC=1, USDT=2.
        _approveExact(
            USDC,
            CURVE_3POOL,
            usdcOut
        );

        usdtOut = ICurve3PoolLimited(
            CURVE_3POOL
        ).exchange(
            1,
            2,
            usdcOut,
            trade.minUsdtOut
        );

        // Leg 3: USDT -> WETH on Uniswap V3.
        _approveExact(
            USDT,
            UNISWAP_V3_ROUTER,
            usdtOut
        );

        wethOut = IUniswapV3RouterLimited(
            UNISWAP_V3_ROUTER
        ).exactInputSingle(
            IUniswapV3RouterLimited.ExactInputSingleParams({
                tokenIn: USDT,
                tokenOut: WETH,
                fee: trade.lastV3Fee,
                recipient: address(this),
                deadline: trade.deadline,
                amountIn: usdtOut,
                amountOutMinimum: trade.minWethOut,
                sqrtPriceLimitX96: 0
            })
        );
    }

    function withdrawToken(
        address token,
        address recipient,
        uint256 amount
    ) external onlyOwner {
        require(!inFlashLoan, "FLASH_ACTIVE");
        require(
            token != address(0) &&
            recipient != address(0),
            "ZERO_ADDRESS"
        );

        _safeCall(
            token,
            abi.encodeWithSignature(
                "transfer(address,uint256)",
                recipient,
                amount
            )
        );

        emit TokenWithdrawn(token, recipient, amount);
    }

    function _approveExact(
        address token,
        address spender,
        uint256 amount
    ) private {
        if (
            IERC20Limited(token).allowance(
                address(this),
                spender
            ) != 0
        ) {
            _safeCall(
                token,
                abi.encodeWithSignature(
                    "approve(address,uint256)",
                    spender,
                    0
                )
            );
        }

        _safeCall(
            token,
            abi.encodeWithSignature(
                "approve(address,uint256)",
                spender,
                amount
            )
        );
    }

    function _safeCall(
        address token,
        bytes memory data
    ) private {
        (bool success, bytes memory result) =
            token.call(data);

        require(
            success &&
            (
                result.length == 0 ||
                (
                    result.length == 32 &&
                    abi.decode(result, (bool))
                )
            ),
            "TOKEN_CALL_FAILED"
        );
    }
}

Compiler settingsShow
{
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "remappings": []
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.