0% found this document useful (0 votes)
7 views3 pages

Safe Arbitrage Contract (Solidity 0.8.x)

The document outlines the implementation of a Solidity smart contract for arbitrage trading between two decentralized exchanges (DEXs) using Uniswap V2. It includes functions for executing arbitrage trades, withdrawing tokens and ETH, and ensures security through ownership restrictions and profitability checks. Optional enhancements such as flashloan integration and real-time price feeds are also suggested.

Uploaded by

Abba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Safe Arbitrage Contract (Solidity 0.8.x)

The document outlines the implementation of a Solidity smart contract for arbitrage trading between two decentralized exchanges (DEXs) using Uniswap V2. It includes functions for executing arbitrage trades, withdrawing tokens and ETH, and ensures security through ownership restrictions and profitability checks. Optional enhancements such as flashloan integration and real-time price feeds are also suggested.

Uploaded by

Abba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.20;

import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract BlastArbitrage is Ownable {


IUniswapV2Router02 public dexA;
IUniswapV2Router02 public dexB;

event ArbitrageExecuted(address tokenIn, address tokenOut, uint amountIn, uint


profit);
event FundsWithdrawn(address token, uint amount);

constructor(address _dexA, address _dexB) {


dexA = IUniswapV2Router02(_dexA);
dexB = IUniswapV2Router02(_dexB);
}

receive() external payable {}

function executeArbitrage(
address tokenIn,
address tokenOut,
uint amountIn,
uint minProfit
) external onlyOwner {
IERC20(tokenIn).transferFrom(msg.sender, address(this), amountIn);
IERC20(tokenIn).approve(address(dexA), amountIn);

address ;
path[0] = tokenIn;
path[1] = tokenOut;

// Swap tokenIn for tokenOut on dexA


uint[] memory amountsOutA = dexA.swapExactTokensForTokens(
amountIn,
1,
path,
address(this),
block.timestamp
);

uint tokenOutAmount = amountsOutA[1];

// Approve tokenOut to dexB for swapping back


IERC20(tokenOut).approve(address(dexB), tokenOutAmount);

// Reverse the path for dexB swap


address ;
reversePath[0] = tokenOut;
reversePath[1] = tokenIn;

// Swap tokenOut back to tokenIn on dexB


uint[] memory amountsOutB = dexB.swapExactTokensForTokens(
tokenOutAmount,
1,
reversePath,
address(this),
block.timestamp
);

uint finalAmount = amountsOutB[1];


require(finalAmount > amountIn + minProfit, "No profitable arbitrage");

emit ArbitrageExecuted(tokenIn, tokenOut, amountIn, finalAmount -


amountIn);
}

function withdrawTokens(address token) external onlyOwner {


uint balance = IERC20(token).balanceOf(address(this));
require(balance > 0, "No token balance");
IERC20(token).transfer(msg.sender, balance);
emit FundsWithdrawn(token, balance);
}

function withdrawETH() external onlyOwner {


uint bal = address(this).balance;
require(bal > 0, "No ETH");
payable(msg.sender).transfer(bal);
}
}

⚙️ Key Setup Steps


✅ Requirements:
Blast-compatible Uniswap-style DEXs (Uniswap V2 forks or routers).

Fund your contract with ERC20 tokens or ETH beforehand (if using WETH, wrap it
yourself or extend logic).

Adjust executeArbitrage to select DEXs based on price differences (currently you


pass both manually).

✅ Security Best Practices


Uses Ownable to restrict access.
Includes withdrawTokens() and withdrawETH() for fund retrieval.

No hardcoded addresses or obfuscated logic.

Approves only exact swap amounts (reduces attack surface).

require checks for profitability.

Optional Enhancements
Add flashloan logic (e.g., from Aave or DyDx if available on Blast).

Integrate real-time price feeds (Chainlink oracles).

Add automatic arbitrage path discovery (scan price spreads before executing).

You might also like