Skip to content

Instantly share code, notes, and snippets.

@0ex-d
Created December 9, 2024 22:43
Show Gist options
  • Save 0ex-d/5536ec9be8a71e03c0378c912df333a3 to your computer and use it in GitHub Desktop.
Save 0ex-d/5536ec9be8a71e03c0378c912df333a3 to your computer and use it in GitHub Desktop.
Solidity Pseudocode for DEX-2-DEX automated, fast execution Arbitrage trader.
// DEX-2-DEX only
// A pseudocode of my automated solidity code to execute fast Arb trade.
// I would be using Uniswap and PancakeSwap DEXs
// 1 ETH = 3,234 USDT (Uniswap)
// 1 ETH = 3,334 USDT (PancakeSwap)
function executeArbitrage( address tokenA, address tokenB, uint amountIn, address dex1, address dex2) external {
uint256 feeDex1 = Dex1(dex1).getTradingFee(tokenA, tokenB);
uint256 feeDex2 = Dex2(dex2).getTradingFee(tokenB, tokenA);
// bid 1 ETH = 3,234 USDT (Uniswap)
uint256 amountOutDex1 = Dex1(dex1).swap(tokenA, tokenB, amountIn);
uint256 amountOutAfterFeeDex1 = amountOutDex1 - (amountOutDex1 * feeDex1 / 10000);
// ask 1 ETH = 3,334 USDT (PancakeSwap)
uint256 amountOutDex2 = Dex2(dex2).swap(tokenB, tokenA, amountOutAfterFeeDex1);
uint256 amountOutAfterFeeDex2 = amountOutDex2 - (amountOutDex2 * feeDex2 / 10000);
uint256 gasCost = tx.gasprice * gasleft();
require(amountOutAfterFeeDex2 > (amountIn + gasCost), "no realized profit");
uint256 profitForCaller = amountOutAfterFeeDex2 - (amountIn + gasCost);
ERC20(tokenA).transfer(msg.sender, profitForCaller);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment