No history yet

AMM Mathematical Foundations

The Constant Product Formula

Automated Market Makers (AMMs) do away with traditional order books. Instead of matching individual buy and sell orders, they use a simple, elegant mathematical rule to facilitate trades. The most fundamental of these is the constant product formula, popularized by platforms like Uniswap V2.

x * y = k

In this formula, x is the quantity of one token in a liquidity pool, and y is the quantity of the other. The variable k is the constant product, or liquidity invariant. This value must remain constant before and after any trade, ignoring fees for the moment. The formula ensures that as the supply of one asset decreases, the supply of the other must increase to maintain the balance defined by k.

Let's imagine a liquidity pool for Wrapped Ether (WETH) and a stablecoin, USDC. Assume the pool contains 10 WETH and 20,000 USDC.

xWETH=10yUSDC=20,000x_{WETH} = 10 \\ y_{USDC} = 20,000

We can now calculate the constant product, k, for this specific pool.

k=xWETH×yUSDCk=10×20,000=200,000k = x_{WETH} \times y_{USDC} \\ k = 10 \times 20,000 = 200,000

Calculating a Trade

Now, suppose a trader wants to swap 1 WETH for USDC. They will add their 1 WETH to the pool. The pool's total WETH reserve changes, and the AMM's formula must solve for the new amount of USDC that keeps k at 200,000. The difference between the old USDC reserve and the new one is what the trader receives. Let's call the amount of WETH the trader adds Δx.

(x+Δx)×(yΔy)=k(x + \Delta x) \times (y - \Delta y) = k

With our numbers, the pool's new WETH balance (x') becomes 11. We can now solve for the new USDC balance (y').

11×y=200,000y=200,0001118,181.82 USDC11 \times y' = 200,000 \\ y' = \frac{200,000}{11} \approx 18,181.82 \text{ USDC}

The amount of USDC the trader receives (Amount Out or Δy) is the difference between the original USDC balance and the new one.

Δy=yyΔy=20,00018,181.82=1,818.18 USDC\Delta y = y - y' \\ \Delta y = 20,000 - 18,181.82 = 1,818.18 \text{ USDC}

Price, Impact, and Slippage

In an AMM, the price of an asset is simply the ratio of the reserves. Before the trade, the price of WETH in terms of USDC was:

PriceWETH=yUSDCxWETH=20,00010=2,000 USDC per WETHPrice_{WETH} = \frac{y_{USDC}}{x_{WETH}} = \frac{20,000}{10} = 2,000 \text{ USDC per WETH}

Notice the trader didn't get 2,000 USDC for their WETH. They received 1,818.18 USDC. The effective price they paid was lower. This difference is known as price impact or slippage. It's the effect a trade has on the price within the pool. The larger the trade relative to the pool's size (its k value), the larger the price impact.

After the trade, the reserves are 11 WETH and 18,181.82 USDC. The new instantaneous price of WETH is:

PriceWETH=18,181.82111,652.89 USDC per WETHPrice'_{WETH} = \frac{18,181.82}{11} \approx 1,652.89 \text{ USDC per WETH}

This automatic price adjustment is the core of how AMMs work. They don't need external price feeds; the price is determined entirely by the actions of traders interacting with the liquidity pool. Every trade rebalances the reserves and, in doing so, sets a new price. This mechanism allows for continuous, decentralized trading without relying on traditional financial intermediaries.

Let's test your understanding of these calculations.

Quiz Questions 1/5

What is the fundamental mathematical rule used by many Automated Market Makers (AMMs) like Uniswap V2?

Quiz Questions 2/5

A liquidity pool contains 20 WETH and 40,000 USDC. Using the constant product formula, what is the value of the constant product, k?