Developing DEX Aggregator API
Project comes with simple request: "we need our own aggregator, like 1inch". Behind this lies not just proxy to several DEXs—this is full routing engine, which in 200–400 ms must find optimal swap route across dozens of pools, calculate slippage, price impact and return quote executable without surprises. Most teams underestimate task and get API giving good quotes on tests but losing user money on mainnet at high volatility.
Where Routing Logic Breaks Down in Practice
Stale Quotes and Race Condition on Execution
Most common problem source—gap between quote receipt moment and transaction send moment. Over 15–30 seconds while user confirms swap in wallet, pool state changes. If API doesn't account for this and returns amountOutMin without adequate slippage tolerance, transaction either reverts (user pays gas for nothing) or executes at worse price.
Specific pattern: aggregator requests reserves via getReserves() from Uniswap v2 pair, computes price by xy=k formula. Between request and tx deployment to mempool—3 blocks, each with large swap. amountOut diverges from reality by 1.5%. At slippageTolerance = 0.5% transaction reverts. Solution—short quote TTL (5–10 seconds) and dynamic slippage based on pair historical volatility.
Unaccounted Fee Tiers in Uniswap v3
Uniswap v3 has pools with different fee tiers: 0.01%, 0.05%, 0.3%, 1%. For USDC/USDT pair liquidity concentrated in 0.01% pool. If router by default takes 0.3% pool—user gets worse price and pays 30x more commission. Routing engine must check liquidity in all tiers via PoolAddress.computeAddress and select pool with best depth relative to swap size.
Gas vs Price: Multi-Hop Not Always Beneficial
Route A→B→C may give 0.3% better price than direct A→C, but cost 80k gas more. At 30 gwei gas and $500 swap, extra $2.4 gas turns benefit into loss. API should calculate net output accounting for gas cost and return route optimal by final amount, not just by swap price.
How We Build DEX Aggregator API
Routing Engine Architecture
System core—liquidity graph. Node—token, edge—pool with weight as effective exchange rate (with fee). Path finding algorithm: modified Bellman-Ford for maximum output path (not shortest). For multi-hop up to 3 hops works acceptable time; for 4+ use heuristic with beam search.
Supported liquidity sources:
| Protocol | Versions | Integration Features |
|---|---|---|
| Uniswap | v2, v3, v4 | v3: all fee tiers; v4: hooks |
| Curve | StableSwap, CryptoSwap | Nonlinear AMM formula |
| Balancer | v2 WeightedPool, StablePool | Multi-token pools |
| PancakeSwap | v2, v3 | BSC + Ethereum |
| SushiSwap | v2 | Multi-chain |
Pool data synced via combination of The Graph subgraph (for historical data) and direct on-chain calls via eth_call batch (for current reserves). Graph gives ~500 ms latency—too slow for real-time quotes. So critical data (reserves, sqrtPriceX96 for v3) cached locally and updated via WebSocket subscription to Swap, Mint, Burn events.
Contract Part: Smart Order Router
API returns not just route but calldata for execution. For multi-hop swaps across protocols—custom router contract batching calls and ensuring atomicity. If one chain step reverts—all reverts.
Contract written in Solidity 0.8.x using delegatecall for adapter calls for each protocol. Adapters isolated—adding new DEX doesn't touch main router. Tests via Foundry with fork mainnet: real pools, real reserves, edge cases at low liquidity.
MEV and Sandwich Attack Protection
Aggregator quote is tasty target for MEV-bots. If amountOutMin too soft, sandwich inevitable: bot sees tx in mempool, pushes price ahead, your swap executes at worse price, bot reverts. Countermeasures: amountOutMin by default = 99% of quote (1% slippage), optional Flashbots Protect or MEV Blocker integration for private mempool routing.
Process and Timelines
Analytics (1 day): target DEX list, chains (Ethereum, Arbitrum, Base, BSC), latency requirements.
Routing Engine Development (2–3 days): liquidity graph, path-finding algorithm, reserves cache.
Router Contract Development (1–2 days): multi-hop adapters, fork tests.
REST/RPC API (1 day): /quote, /swap endpoints, rate limiting, documentation.
Testing and Optimization (1 day): load tests, latency profiling.
Total: 3–5 days for basic version with 3–5 DEXs on single chain. Multi-chain with 10+ liquidity sources—2–3 weeks. Cost calculated after clarifying protocol list and chains.







