Skip to content
Verified contractExactMIT

TriangularArbitrage

Address0x35a6e113a783b38e6ec9e64f026ca833ee31e370
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,367,371
Creation transaction
0x88fc737ed5…2f2cee9a6f

contracts/TriangularArbitrage.sol

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

interface IERC20 {
    function approve(address spender, uint256 amount) external returns (bool);
        function transfer(address recipient, uint256 amount) external returns (bool);
            function balanceOf(address account) external view returns (uint256);
            }

            interface IPool {
                function flashLoanSimple(
                        address receiverAddress,
                                address asset,
                                        uint256 amount,
                                                bytes calldata params,
                                                        uint16 referralCode
                                                            ) external;
                                                            }

                                                            interface IAerodromeRouter {
                                                                struct Route {
                                                                        address from;
                                                                                address to;
                                                                                        bool stable;
                                                                                                address factory;
                                                                                                    }
                                                                                                        function swapExactTokensForTokens(
                                                                                                                uint256 amountIn,
                                                                                                                        uint256 amountOutMin,
                                                                                                                                Route[] calldata routes,
                                                                                                                                        address to,
                                                                                                                                                uint256 deadline
                                                                                                                                                    ) external returns (uint256[] memory amounts);
                                                                                                                                                    }

                                                                                                                                                    interface ISwapRouter {
                                                                                                                                                        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);
                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                contract TriangularArbitrage {
                                                                                                                                                                                                                                    address public immutable owner;

                                                                                                                                                                                                                                        address public constant AAVE_POOL = address(bytes20(hex"a238dd80c259a72e81d7e4664a9801593f98d1c5"));
                                                                                                                                                                                                                                            address public constant UNISWAP_ROUTER = address(bytes20(hex"2626664c2603336e57b271c5c0b26f421741e481"));
                                                                                                                                                                                                                                                address public constant AERODROME_ROUTER = address(bytes20(hex"cf77a3ba9a5ca399b7c97c74d54e5b1beb874e43"));
                                                                                                                                                                                                                                                    address public constant AERODROME_FACTORY = address(bytes20(hex"4200000000000000000000000000000000000006")); // Base Default Factory

                                                                                                                                                                                                                                                        address public constant OKX_WALLET = address(bytes20(hex"3bb6963b5bc7626b930dce5c9612d5a1fdc2cf84"));
                                                                                                                                                                                                                                                            address public constant WETH = address(bytes20(hex"4200000000000000000000000000000000000006"));

                                                                                                                                                                                                                                                                event ArbitrageExecuted(address indexed asset, uint256 amountBorrowed, uint256 profit);

                                                                                                                                                                                                                                                                    constructor() {
                                                                                                                                                                                                                                                                            owner = msg.sender;
                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                    function startArbitrage(address asset, uint256 amount) external {
                                                                                                                                                                                                                                                                                            require(msg.sender == owner, "Only owner");
                                                                                                                                                                                                                                                                                                    IPool(AAVE_POOL).flashLoanSimple(address(this), asset, amount, "", 0);
                                                                                                                                                                                                                                                                                                        }

                                                                                                                                                                                                                                                                                                            function executeOperation(
                                                                                                                                                                                                                                                                                                                    address asset,
                                                                                                                                                                                                                                                                                                                            uint256 amount,
                                                                                                                                                                                                                                                                                                                                    uint256 premium,
                                                                                                                                                                                                                                                                                                                                            address initiator,
                                                                                                                                                                                                                                                                                                                                                    bytes calldata params
                                                                                                                                                                                                                                                                                                                                                        ) external returns (bool) {
                                                                                                                                                                                                                                                                                                                                                                require(msg.sender == AAVE_POOL, "Caller must be Aave Pool");

                                                                                                                                                                                                                                                                                                                                                                        // 1. Approve Aerodrome Router to spend USDC
                                                                                                                                                                                                                                                                                                                                                                                IERC20(asset).approve(AERODROME_ROUTER, amount);

                                                                                                                                                                                                                                                                                                                                                                                        // Build Aerodrome Route: USDC -> WETH (Volatile pool)
                                                                                                                                                                                                                                                                                                                                                                                                IAerodromeRouter.Route[] memory routes = new IAerodromeRouter.Route[](1);
                                                                                                                                                                                                                                                                                                                                                                                                        routes[0] = IAerodromeRouter.Route({
                                                                                                                                                                                                                                                                                                                                                                                                                    from: asset,
                                                                                                                                                                                                                                                                                                                                                                                                                                to: WETH,
                                                                                                                                                                                                                                                                                                                                                                                                                                            stable: false,
                                                                                                                                                                                                                                                                                                                                                                                                                                                        factory: 0x4200000000000000000000000000000000000006
                                                                                                                                                                                                                                                                                                                                                                                                                                                                });

                                                                                                                                                                                                                                                                                                                                                                                                                                                                        // 2. Swap USDC -> WETH on Aerodrome
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                uint256[] memory amountsOut = IAerodromeRouter(AERODROME_ROUTER).swapExactTokensForTokens(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            amount,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        0,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    routes,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                address(this),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            block.timestamp
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    );
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            uint256 wethOut = amountsOut[1];

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    // 3. Approve Uniswap Router to spend WETH
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            IERC20(WETH).approve(UNISWAP_ROUTER, wethOut);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    // 4. Swap WETH -> USDC on Uniswap V3 (0.05% fee pool)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            uint256 usdcReturned = ISwapRouter(UNISWAP_ROUTER).exactInputSingle(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ISwapRouter.ExactInputSingleParams({
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        tokenIn: WETH,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        tokenOut: asset,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        fee: 500,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        recipient: address(this),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        deadline: block.timestamp,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        amountIn: wethOut,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        amountOutMinimum: amount + premium,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        sqrtPriceLimitX96: 0
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    })
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            );

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    // 5. Verify Net Profitability
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            uint256 amountOwed = amount + premium;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    require(usdcReturned > amountOwed, "Cross-DEX Arbitrage not profitable");
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            uint256 profit = usdcReturned - amountOwed;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    // 6. Repay Aave Flash Loan
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            IERC20(asset).approve(AAVE_POOL, amountOwed);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    // 7. Transfer Net Profit directly to OKX Wallet
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            IERC20(asset).transfer(OKX_WALLET, profit);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    emit ArbitrageExecuted(asset, amount, profit);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            return true;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
Compiler settingsShow
{
  "optimizer": {
    "runs": 200,
    "enabled": false
  },
  "remappings": []
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.