4th Street Bar Hive-Bar

Hive-Bar Powered by Hive beta-fdb5b5b

Community post

Smart Contract for Arbitrage Opportunities on DEXs

![Smart Contract for Arbitrage Opportunities on DEXs](https://images.ecency.com/DQmeFjZJUP1iXbC5uXcCL3TfxuCpdyLjDq5s84k7dZgq9KM/smart_contracts.jpg)[source](https://cryptoadventure.com/wp-content/uploads/2019/11/Smart-Contracts.jpg)

Below is a simple Solidity smart contract that checks for arbitrage opportunities on decentralized exchanges (DEXs) and executes the arbitrage if found. I'll also explain each part of the code line by line.

Smart Contract Code

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

import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";
import "@uniswap/v2-core/contracts/interfaces/IUniswapV2Pair.sol";
import "@uniswap/v2-core/contracts/interfaces/IERC20.sol";

contract Arbitrage {
    address public owner;
    IUniswapV2Router02 public uniswapRouter;
    IUniswapV2Router02 public sushiswapRouter;

    constructor(address _uniswapRouter, address _sushiswapRouter) {
        owner = msg.sender;
        uniswapRouter = IUniswapV2Router02(_uniswapRouter);
        sushiswapRouter = IUniswapV2Router02(_sushiswapRouter);
    }

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

    function executeArbitrage(
        address token0,
        address token1,
        uint amount
    ) external onlyOwner {
        uint startBalance = IERC20(token0).balanceOf(address(this));
        
        // Buy token1 on Uniswap
        IERC20(token0).approve(address(uniswapRouter), amount);
        address[] memory path = new address[](2);
        path[0] = token0;
        path[1] = token1;
        uniswapRouter.swapExactTokensForTokens(
            amount,
            1,
            path,
            address(this),
            block.timestamp
        );

        uint token1Amount = IERC20(token1).balanceOf(address(this));

        // Sell token1 on Sushiswap
        IERC20(token1).approve(address(sushiswapRouter), token1Amount);
        path[0] = token1;
        path[1] = token0;
        sushiswapRouter.swapExactTokensForTokens(
            token1Amount,
            1,
            path,
            address(this),
            block.timestamp
        );

        uint endBalance = IERC20(token0).balanceOf(address(this));
        require(endBalance > startBalance, "Arbitrage not profitable");
    }
}

Explanation

  1. License and Pragma:

    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;
    
    • Specifies the license type and the Solidity compiler version.
  2. Imports:

    import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";
    import "@uniswap/v2-core/contracts/interfaces/IUniswapV2Pair.sol";
    import "@uniswap/v2-core/contracts/interfaces/IERC20.sol";
    
    • Imports necessary interfaces for interacting with Uniswap and ERC20 tokens.
  3. Contract Definition:

    contract Arbitrage {
        address public owner;
        IUniswapV2Router02 public uniswapRouter;
        IUniswapV2Router02 public sushiswapRouter;
    
    • Defines the Arbitrage contract with state variables for the owner and the routers for Uniswap and Sushiswap.
  4. Constructor:

    constructor(address _uniswapRouter, address _sushiswapRouter) {
        owner = msg.sender;
        uniswapRouter = IUniswapV2Router02(_uniswapRouter);
        sushiswapRouter = IUniswapV2Router02(_sushiswapRouter);
    }
    
    • Initializes the contract with the addresses of the Uniswap and Sushiswap routers and sets the contract owner.
  5. Modifier:

    modifier onlyOwner() {
        require(msg.sender == owner, "Not the contract owner");
        _;
    }
    
    • Restricts access to certain functions to only the contract owner.
  6. Arbitrage Execution Function:

    function executeArbitrage(
        address token0,
        address token1,
        uint amount
    ) external onlyOwner {
        uint startBalance = IERC20(token0).balanceOf(address(this));
    
    • Defines the executeArbitrage function which takes two token addresses and an amount to trade. It checks the initial balance of token0.
  7. Buying on Uniswap:

    IERC20(token0).approve(address(uniswapRouter), amount);
    address[] memory path = new address[](2);
    path[0] = token0;
    path[1] = token1;
    uniswapRouter.swapExactTokensForTokens(
        amount,
        1,
        path,
        address(this),
        block.timestamp
    );
    
    • Approves the Uniswap router to spend token0, sets up the swap path, and executes the swap from token0 to token1.
  8. Selling on Sushiswap:

    uint token1Amount = IERC20(token1).balanceOf(address(this));
    IERC20(token1).approve(address(sushiswapRouter), token1Amount);
    path[0] = token1;
    path[1] = token0;
    sushiswapRouter.swapExactTokensForTokens(
        token1Amount,
        1,
        path,
        address(this),
        block.timestamp
    );
    
    • Approves the Sushiswap router to spend token1, sets up the swap path, and executes the swap from token1 back to token0.
  9. Profit Check:

    uint endBalance = IERC20(token0).balanceOf(address(this));
    require(endBalance > startBalance, "Arbitrage not profitable");
    
    • Checks the final balance of token0 to ensure the arbitrage was profitable.
43 upvotes 2 downvotes $0.02

Replies (2)

Review before signing

Posting as . Signing with . Keychain permission: Posting. Hive Keychain will ask you to approve this action next.


  
Technical details

Operation fingerprint: