Skip to content
Finality

Crypto protocol and market news

Automated Market Makers: How Constant-Product Pricing Works

Learn how automated market makers work for beginners: a pool’s reserve ratio sets the spot rate, while trade size and fees determine the output you receive.

The Finality Desk5 min read#56e0c8

Automated Market Makers: How Constant-Product Pricing Works

An automated market maker sets a swap’s price from the balances in its liquidity pool: the reserve ratio gives the spot price, and each trade changes that ratio. A constant-product pool keeps the product of its reserves fixed for pricing, so larger trades move the rate further.

The pool’s reserves set the rate

A constant-product automated market maker prices a trade by using the incoming token to change the pool’s reserves, then calculating how much of the other token can leave while preserving the product. The trade has three parts:

  • In: the amount of one token sent to the pool.
  • Process: the pool applies its fee and calculates the output under x × y = k.
  • Out: the amount of the other token the pool returns.

Here, x and y are the reserves of the two tokens, and k is their product. The spot price is the ratio between reserves: for a pool holding x units of A and y units of B, one A is priced at y ÷ x B. That is a reference rate for a tiny trade. The actual amount received depends on how much the trade shifts the reserves.

This is the counterparty mechanism: there is no order book matching a buyer with a seller. The pool’s tokens and pricing rule determine the available output. SunSwap is a TRON exchange for swapping TRC-20 tokens through liquidity pools. Its pool versions make it a concrete example of how SunSwap prices and routes swaps; the article explains how those versions affect the path and pricing of a trade.

A larger trade moves the price further

To estimate output before fees, use the constant-product relationship. Let x be the pool’s starting reserve of the token you send, y the starting reserve of the token you receive, and q the input amount. Then output = y − (x × y)/(x + q). The quantities for each token must use consistent units. With a fee, replace q with the input amount after the fee is deducted.

Take a pool with 1,000 A and 1,000 B. Its spot price is 1 B per A. Sending 100 B means x = 1,000 B, y = 1,000 A, and q = 100 B. The calculation gives 1,000 − 1,000,000 ÷ 1,100 = about 90.9 A before fees. The trade adds 100 B and removes about 90.9 A, leaving the product approximately unchanged.

At the starting spot rate, 100 B appears to be worth 100 A. The pool returns about 9.1 A less, a shortfall of roughly 9% compared with that spot-price estimate. This difference is price impact: it comes from the trade changing the reserve ratio as it executes. It is not a separate fee.

The reusable calculation assumes a two-token constant-product pool and ignores other effects such as routing through multiple pools. A fee reduces the amount used in the output calculation; the pool retains that fee for liquidity providers. The execution rate is the actual output divided by the input. The spot price is the pool’s reserve ratio before the trade. Mixing them up makes a quote look better than the amount the pool can deliver.

Trade size matters relative to pool depth. The same input generally causes more price impact in a smaller pool because it is a larger share of the reserves. A small trade against deep reserves stays closer to the spot rate; a large trade against shallow reserves pushes the rate further. Compare both the quoted output and the pool’s reserves when judging a rate.

Fees and arbitrage shape the pool price

A pool fee is taken from the input amount used for the swap, and the fee stays in the pool for liquidity providers. The fee and price impact affect a trade in different ways: the fee reduces the amount that enters the pricing calculation, while price impact follows from the reserve change. A quote that accounts for only one can misstate the output.

Arbitrageurs help pull a pool’s price toward prices available in other markets. When the pool’s reserve ratio drifts, a trader can swap against it at a rate that differs from elsewhere, then trade in the other market to capture the gap. Those trades change the pool’s reserves and move its price back toward the outside price. The pool price can still move as trades and outside markets change.

SunSwap V2 uses constant-product pricing. SunSwap V3 adds concentrated liquidity: liquidity providers can place funds within chosen price ranges, creating more depth near the current price than V2 when liquidity is concentrated there. As the price moves beyond a range, that liquidity no longer contributes in the same way. This changes how much depth is available at a given price, while the reserve ratio remains the useful starting point for understanding a V2-style pool.

Related questions

What is a liquidity pool?

A liquidity pool is a smart-contract-held balance of tokens that traders swap against. Its reserves provide the inventory used by the pricing rule.

What is the difference between an AMM and an order book?

An AMM calculates output from pool reserves and a formula. An order book displays offers from buyers and sellers, and a trade executes against available orders. The visible rate in an AMM comes from the pool’s changing balances.

What does an LP token represent?

An LP token represents a liquidity provider’s share in a pool under that pool’s rules. The provider supplies tokens to the pool and may receive a portion of its fees; the token is distinct from the pool’s swap price.