Decentralized Exchange (DEX) arbitrage is a strategy to capitalize on price discrepancies between two or more decentralized exchanges. This method involves buying a token on one DEX where it is undervalued and selling it on another where it is overvalued, thereby profiting from the price difference. This repository provides an implementation of a smart contract and a trading bot to perform DEX arbitrage effectively.
- Price Imbalance: Large trades can create slippage and price imbalances in liquidity pools.
- Restoring Balance: Arbitrage bots exploit these imbalances to restore price equilibrium by transferring liquidity between markets.
- Profitability Check: The smart contract ensures the entire transaction is profitable; otherwise, it reverts the trade to avoid losses.
- Batch Transactions: Combine multiple swaps into a single transaction.
- Profit Validation: Transactions are reverted if no profit is made.
- Customizable Routes: Flexible architecture to support different tokens, routes, and DEXes.
- Supports Dual and Triangular Arbitrage: Includes functionality for two-token and three-token arbitrage strategies.
The getAmountOutMin
function queries the router to get the minimum output for a given input amount:
function getAmountOutMin(address router, address _tokenIn, address _tokenOut, uint256 _amount) public view returns (uint256) {
address[] memory path = new address[](2);
path[0] = _tokenIn;
path[1] = _tokenOut;
uint256[] memory amountOutMins = IUniswapV2Router(router).getAmountsOut(_amount, path);
return amountOutMins[path.length - 1];
}
For dual DEX trades, we estimate profitability with:
function estimateDualDexTrade(address _router1, address _router2, address _token1, address _token2, uint256 _amount) external view returns (uint256) {
uint256 amtBack1 = getAmountOutMin(_router1, _token1, _token2, _amount);
uint256 amtBack2 = getAmountOutMin(_router2, _token2, _token1, amtBack1);
return amtBack2;
}
The dualDexTrade
function batches trades to ensure profitability:
function dualDexTrade(address _router1, address _router2, address _token1, address _token2, uint256 _amount) external onlyOwner {
uint startBalance = IERC20(_token1).balanceOf(address(this));
swap(_router1, _token1, _token2, _amount);
uint tradeableAmount = IERC20(_token2).balanceOf(address(this)) - IERC20(_token2).balanceOf(address(this));
swap(_router2, _token2, _token1, tradeableAmount);
require(IERC20(_token1).balanceOf(address(this)) > startBalance, "Trade Reverted, No Profit Made");
}
- Node.js
- Hardhat
- Solidity compiler
- Aurora testnet/mainnet RPC URL
- A wallet private key
Clone this repository and install dependencies:
git clone https://github.com/jamesbachini/DEX-Arbitrage.git
cd DEX-Arbitrage
npm install
- Add your private key to the
.env
file. - Deploy the smart contract:
npx hardhat run --network aurora ./scripts/deploy.js
- Add the deployed contract address to
.env
.
The bot queries routes and tokens for opportunities and executes trades when profitable.
- Checking Profitability:
const amtBack = await arb.estimateDualDexTrade(router1, router2, token1, token2, amount); if (amtBack.gt(profitTarget)) { await dualTrade(router1, router2, token1, token2, amount); }
- Executing Trades:
const tx = await arb.connect(owner).dualDexTrade(router1, router2, token1, token2, amount); await tx.wait();
- Configure your routes and tokens in the
config/aurora.json
file. - Run the bot:
node ./scripts/trade.js
- Use tools like DeFiLlama to identify active DEXes.
- Query token lists (e.g., Aurora token list) to find tradeable pairs.
- Explore triangular arbitrage with:
function estimateTriDexTrade(address _router1, address _router2, address _router3, address _token1, address _token2, address _token3, uint256 _amount) external view returns (uint256) { uint amtBack1 = getAmountOutMin(_router1, _token1, _token2, _amount); uint amtBack2 = getAmountOutMin(_router2, _token2, _token3, amtBack1); uint amtBack3 = getAmountOutMin(_router3, _token3, _token1, amtBack2); return amtBack3; }
- Small-scale tests with ~$20 capital showed profitable returns:
- wNEAR: 9.66% in under 12 hours.
- USDT: 1.24%.
- Scaling to $300 reduced profitability due to slippage.
- DEX arbitrage on low-volume chains can yield excellent returns with low risk.
- High-volume chains require gas optimization and advanced strategies like MEV (Miner Extractable Value).
This code is for educational purposes only. It is unaudited and not production-ready. Use at your own risk.
Feel free to fork and contribute to the project. Pull requests are welcome for improvements and optimizations.
This project is licensed under the MIT License. See the LICENSE file for details.