Skip to content
Verified contractExactMIT

FlashloanArbitrage

Address0x9003e00a94ebc072580d5ade12e5a5372355604c
Balance

0 ETH

 

Transactions

2,073

 

Compiler

solc 0.8.20

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.20+commit.a1b79de6
Language
solidity
Optimization
No
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
23,985,397
Creation transaction
0x78d0b2df38…82f3c201e1

contracts/FlashloanArbitrage.sol

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

interface IPancakeRouter {
    function swapExactTokensForTokens(
        uint amountIn,
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external returns (uint[] memory amounts);
    
    function getAmountsOut(uint amountIn, address[] calldata path) 
        external view returns (uint[] memory amounts);
}

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

interface IPancakeFlashLoan {
    function flashLoan(
        address receiver,
        address token,
        uint256 amount,
        bytes calldata data
    ) external;
}

contract FlashloanArbitrage {
    address public owner;
    address public constant PANCAKE_ROUTER = 0x10ED43C718714eb63d5aA57B78B54704E256024E;
    address public constant PANCAKE_FLASHLOAN = 0x1b81D678ffb9C0263b24A97847620C99d213eB14;
    
    constructor() {
        owner = msg.sender;
    }
    
    modifier onlyOwner() {
        require(msg.sender == owner, "Not owner");
        _;
    }
    
    // Main entry point - called by your bot
    function executeArbitrage(
        address[] calldata path,
        address buyRouter,
        address sellRouter,
        uint256 loanAmount
    ) external onlyOwner {
        require(path.length >= 2, "Invalid path");
        
        // Encode arbitrage data
        bytes memory data = abi.encode(path, buyRouter, sellRouter);
        
        // Request flashloan
        IPancakeFlashLoan(PANCAKE_FLASHLOAN).flashLoan(
            address(this),
            path[0], // Borrow first token in path
            loanAmount,
            data
        );
    }
    
    // Callback from PancakeSwap flashloan
    function pancakeCall(
        address sender,
        uint amount0,
        uint amount1,
        bytes calldata data
    ) external {
        require(msg.sender == PANCAKE_FLASHLOAN, "Not flashloan pool");
        require(sender == address(this), "Not this contract");
        
        // Decode arbitrage parameters
        (address[] memory path, address buyRouter, address sellRouter) = 
            abi.decode(data, (address[], address, address));
        
        uint loanAmount = amount0 > 0 ? amount0 : amount1;
        address loanToken = path[0];
        
        // Step 1: Buy on cheap DEX
        IERC20(loanToken).approve(buyRouter, loanAmount);
        
        uint[] memory amounts = IPancakeRouter(buyRouter).swapExactTokensForTokens(
            loanAmount,
            0, // Accept any amount (risky but needed for testing)
            path,
            address(this),
            block.timestamp + 300
        );
        
        uint tokensReceived = amounts[amounts.length - 1];
        
        // Step 2: Sell on expensive DEX
        address[] memory reversePath = new address[](path.length);
        for (uint i = 0; i < path.length; i++) {
            reversePath[i] = path[path.length - 1 - i];
        }
        
        IERC20(reversePath[0]).approve(sellRouter, tokensReceived);
        
        IPancakeRouter(sellRouter).swapExactTokensForTokens(
            tokensReceived,
            loanAmount, // Must get back at least loan amount
            reversePath,
            address(this),
            block.timestamp + 300
        );
        
        // Step 3: Repay flashloan (0.25% fee)
        uint fee = (loanAmount * 25) / 10000;
        uint repayAmount = loanAmount + fee;
        
        IERC20(loanToken).transfer(msg.sender, repayAmount);
        
        // Keep profit
        uint profit = IERC20(loanToken).balanceOf(address(this));
        if (profit > 0) {
            IERC20(loanToken).transfer(owner, profit);
        }
    }
    
    // Emergency withdrawal
    function withdrawToken(address token, uint amount) external onlyOwner {
        IERC20(token).transfer(owner, amount);
    }
    
    // Receive ETH
    receive() external payable {}
}
Compiler settingsShow
{
  "optimizer": {
    "runs": 200,
    "enabled": false
  },
  "remappings": []
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.