How Does a DEX Aggregator Find the Best Price After Gas?
A DEX aggregator finds the best swap price by simulating your trade across available pools and token paths, then choosing the route that returns the most tokens after gas. Because a large trade moves a pool’s price against you, splitting it across pools can beat using one exchange, as long as the extra execution cost doesn’t outweigh the improvement.
How the quote is built
Start with the token pair and amount. The aggregator checks available pools, their reserves, fees and current prices, then estimates what each route would return. A route can use one pool, several pools in sequence, or split the trade across different pools.
For instance, a same-chain swap feature in Jumper bridge is powered by LI.FI, which compares quotes from aggregators such as 1inch and 0x. Jumper bridge is a cross-chain service that brings together bridge and swap routes for transfers between supported networks.
Next, the aggregator simulates the routes. It tests direct pairs alongside multi-hop paths, such as ETH→WBTC→USDC. An extra hop can help when the intermediate pools offer better liquidity, but it also adds a fee and another pool whose price movement affects the result. 1inch Pathfinder documentation describes finding routes that can split trades across liquidity sources and use intermediate tokens.
Pool math explains why splitting can help. In a constant-product pool such as Uniswap v2, the reserves of the two tokens must keep the same product as a swap changes them. Taking more of one token out leaves less behind, so each additional unit costs more of the other token. Uniswap’s Uniswap v2 Core whitepaper describes this constant-product design; the effect depends on the pool’s reserves and the size of the trade.
Say you’re swapping 100 ETH to USDC. Sending it all through one Uniswap pool pushes further along its pricing curve as the pool’s ETH reserve falls. If a Curve pool also has useful ETH-to-USDC liquidity, the aggregator can simulate sending part of the trade through each. The combined result may return more USDC than either pool alone, after their fees.
Then it subtracts estimated gas from each route’s return and ranks the net results. This matters especially on Ethereum mainnet: extra hops and split transactions can cost enough gas to erase the tokens they gain. Routes there often favor fewer pools than equivalent routes on layer 2 networks, where transaction costs are lower. The tradeoff depends on current gas prices, pool liquidity and amount.
Finally, the aggregator builds a transaction for a router contract. The transaction includes a minimum-output limit: if execution would return less than that floor, it reverts instead of completing the swap at a worse price.
Related questions
Why does my swap quote change before I confirm?
A quote is an estimate based on pool states and gas prices at the time it’s built. Other swaps can change reserves before your transaction executes, and network fees can move too, so the best route or net return may change.
Are DEX aggregators more expensive than swapping on Uniswap directly?
They can be, because comparing and combining routes may add gas or use pools with different fees. A direct Uniswap swap can cost less when its pool already offers the best net return; an aggregator helps when another pool, split or path improves the result enough to cover its added costs.
Comments
Post a Comment