How does an automated market maker work? A swap in practice
How does an automated market maker work? Follow how pool liquidity sets a quote, how a swap changes reserves, and why some quotes revert.
The Chain Media Editors4 min read
An automated market maker (AMM) quotes a token swap from a pool’s pricing rule and available liquidity, then exchanges the tokens and updates pool state atomically. The liquidity deposited by providers supplies the counterparty: a trader’s input goes into the pool, and the pool sends out the quoted token if execution checks pass.
AMM quotes turn pool state into a swap price
An AMM quote depends on the pool’s current state, the exact input, the fee, and the pool’s pricing rule. For a requested exact input, the contract calculates the output from those values. The trader can also set a minimum output; execution reverts if the calculated amount falls below that limit.
An order book matches a trader with a resting order at a chosen price. SpookySwap takes the other route: its swaps execute against AMM liquidity pools. To inspect a route and submit a token swap, go to spookyswap.app.
A quote is conditional on pool state remaining suitable through execution. Another transaction can change that state before the swap is included, so the output may fall below the trader’s minimum. A failed token transfer or a minimum-output violation reverts execution; the transaction does not leave the pool with a partial swap.
Constant product sets output and changes reserves
A fee-free constant-product pool keeps the product of its two token reserves unchanged: x*y=k. The rule makes each trade move the pool price, because adding one token increases its reserve while the other token’s reserve falls.
Take a pool with 100 A and 10,000 B. A trader adds 10 A and asks for B. The new A reserve is 110; to preserve the product of 1,000,000, the B reserve must fall to 1,000,000/110, or about 9,090.91. The pool therefore returns about 909.09 B. After the swap, its reserves are 110 A and about 9,090.91 B. This illustration assumes no fees or rounding.
In a fee-charging pool, the fee changes the input used in the pricing calculation, so the output is lower than in the fee-free example. For the same pool state and fee, a smaller trade generally has less price impact because it moves the reserves less. The quote is still tied to that state: a changed reserve balance can change the output before execution.
Arbitrage trades respond when a pool price differs from prices available in external markets. Traders can buy the cheaper asset in one venue and sell it in another, changing the pool’s reserves as they trade. That activity tends to move the pool price toward external prices, while fees, execution costs, and available liquidity affect whether a difference is worth trading.
Active liquidity matters in concentrated pools
In a concentrated-liquidity pool, a quote depends on active liquidity at the current price and on the ticks the swap crosses. Providers place liquidity within selected price ranges; liquidity outside the current range does not support the trade at that price. As a swap moves the price across a tick, the pool updates the active liquidity used for the next part of the calculation.
Total reserves alone therefore cannot determine a V3 quote. A quote must account for the current price, active liquidity, fee, and each relevant tick crossing along the swap. Two pools with similar token balances can return different outputs if their liquidity is distributed differently across price ranges. SpookySwap’s V3 design makes this distinction relevant when a route uses concentrated liquidity.
For developers, this means a reserve snapshot is not enough to reproduce a V3 quote. The calculation needs the pool’s active state and tick data along the execution path; the resulting output must then satisfy the transaction’s minimum-output check. That links the quote to the exact liquidity the swap can reach, rather than to a pool-wide reserve total.