DEX Trading Bot Development (Uniswap, PancakeSwap)
Each Ethereum block contains arbitrage opportunities worth tens of thousands dollars. MEV bots capture them in milliseconds. Retail trader with manual interface competes with these systems and loses by definition.
DEX bot — not script "buy on signal". This is competitive system where 100 millisecond delay costs money, where mempool is battlefield, and where wrong logic puts your transactions under sandwich attack.
Architecture: three types of DEX bots
Arbitrage bot
Finds price discrepancies between DEX and extracts profit. For example: ETH costs $2000 on Uniswap V3 and $2003 on Curve — buy on Uniswap, sell on Curve, $3 profit minus gas.
Problem: on mainnet hundreds of professional MEV bots do this with direct connections to builder nodes via Flashbots. Arbitrage on ETH/USDC on Ethereum mainnet — busy niche with hard competition.
More realistic niches: new tokens in first hours of trading (low competition), multi-hop routes through 3+ pools (harder to find automatically), L2 networks (fewer competitors, cheaper gas), exotic pairs on less popular DEX.
Sniper bot (new listing)
Tracks creation of new pools on Uniswap (PoolCreated event in Factory) or liquidity addition to new pool. On detection — instantly buys token betting on price discovery pump.
Technical: WebSocket connection to Ethereum node (Infura, Alchemy, or own node), listen to pending transactions in mempool and PoolCreated events. On detection — assemble and send transaction with high gas priority fee for block priority.
Risks: honeypot tokens (impossible to sell due to contract code), rugpull (liquidity removed immediately after snipe), MEV competition (other bots snipe same opportunity).
Basic protections: simulate sell transaction before buy (via eth_call), check contract for verified source code, check team wallet doesn't hold 90%+ supply.
Market making bot
Places bid/ask orders around mid-price, earns on spread. On Uniswap V3 implemented through range position management — bot opens narrow range position, rebalances on price exit.
Uniswap V3 SDK provides all tools: calculate optimal range via tickToPrice, simulate fees earned via Pool.computeSwapStep.
Technical implementation
Working with Uniswap V3
Uniswap V3 — most common DEX for bots. Key contracts:
-
UniswapV3Factory— pool creation -
SwapRouter02— swap execution (V3 + backward compatible V2) -
Quoter V2— off-chain quotes without gas -
UniversalRouter— universal router (supports V2, V3, and other protocols)
import { ethers } from "ethers";
import { Pool, Route, Trade, SwapRouter } from "@uniswap/v3-sdk";
import { CurrencyAmount, TradeType, Percent } from "@uniswap/sdk-core";
// Get quote via Quoter V2
const quoter = new ethers.Contract(QUOTER_V2_ADDRESS, QuoterV2ABI, provider);
const amountOut = await quoter.callStatic.quoteExactInputSingle({
tokenIn: WETH_ADDRESS,
tokenOut: USDC_ADDRESS,
fee: 3000, // 0.3%
amountIn: ethers.utils.parseEther("1"),
sqrtPriceLimitX96: 0
});
For production: replace callStatic quotes with own mathematical calculation from on-chain state — faster and doesn't depend on Quoter availability.
Speed: how to hit right block
Delay — is money. Optimization levels:
RPC level: Infura/Alchemy add 50-200ms latency. Own Ethereum node (geth or erigon) — 1-5ms. For competitive arbitrage own node mandatory.
Mempool monitoring: via eth_subscribe("newPendingTransactions") get hashes of pending transactions. For full payload — eth_getTransactionByHash or txpool_content (if node supports). Flashbots Protect API gives access to private mempool.
Gas strategy: EIP-1559 transactions. maxFeePerGas should be sufficient for block inclusion. For urgent transactions — maxPriorityFeePerGas above median of current block. Use eth_feeHistory for statistics of recent blocks.
Bundle via Flashbots: for arb transactions that need atomic inclusion — Flashbots MEV-Boost. Bundle of multiple transactions, either all included or none. Protection from frontrunning.
Protection from MEV attacks on own bot
Your transactions also visible in mempool and can be sandwiched. Bot sees your $10K SHIB buy, inserts own buy before (frontrun) and sell after (backrun). Your slippage — their profit.
Protections:
- Private RPC: Flashbots Protect, MEV Blocker — transactions not in public mempool
- Tight slippage: 0.1-0.3% slippage for liquid pairs makes sandwich unprofitable
- TWAP execution: split large order into parts, execute TWAP-style
PancakeSwap and multichain
PancakeSwap V3 (BNB Chain) — similar architecture to Uniswap V3, same SDK concepts. BNB Chain: block every 3 seconds (faster than Ethereum), cheaper gas. PancakeSwap also on Ethereum and Arbitrum.
Multichain bot works with multiple RPC providers. viem preferable to ethers.js for TypeScript projects — better typing, treeshaking, built-in multicall.
import { createPublicClient, http } from "viem";
import { mainnet, bsc, arbitrum } from "viem/chains";
const clients = {
ethereum: createPublicClient({ chain: mainnet, transport: http(ETH_RPC) }),
bsc: createPublicClient({ chain: bsc, transport: http(BSC_RPC) }),
arbitrum: createPublicClient({ chain: arbitrum, transport: http(ARB_RPC) })
};
Stack and infrastructure
TypeScript + viem/ethers.js. Node.js worker threads for parallel processing of multiple pairs. Redis for caching pool state. PostgreSQL for trade history and P&L analytics.
Deploy on VPS with low latency to Ethereum nodes (Hetzner Frankfurt or AWS eu-west for European nodes). PM2 for process management + Telegram alerts on errors.
Timeline estimates
Basic arbitrage bot for one DEX pair — 3-5 days. With multi-DEX routing, mempool monitoring and Flashbots integration — 1-2 weeks. Sniper bot with sell simulation — 3-5 days. Market making bot with Uniswap V3 LP management — 1-1.5 weeks. Cost calculated after clarifying strategy and target networks.







