Elementary Math in DeFi: Automated Market Maker Curves

Decentralized finance (DeFi) protocols have revolutionized digital asset trading by replacing traditional buyers-and-sellers order books with Automated Market Makers (AMMs). This article explores how these protocols rely on elementary mathematical equations, specifically the constant product formula, to automatically determine asset prices, provide continuous liquidity, and balance token pools without human intervention.

Moving From Order Books to Liquidity Pools

In traditional finance, exchanges use an order book where buyers list the price they are willing to pay (bids) and sellers list the price they are willing to accept (asks). A trade only happens when these prices match.

DeFi protocols eliminate the wait for a matching counterparty by using liquidity pools. A liquidity pool is a smart contract that holds reserves of two or more tokens. Users trade directly against the smart contract, and the price of the tokens is determined algorithmically using basic mathematical curves.

The Constant Product Formula

The most famous AMM pricing model is the constant product formula, popularized by Uniswap. It relies on an algebraic equation taught in elementary algebra to graph a hyperbola:

x * y = k

In the context of a DeFi liquidity pool containing two assets (Token A and Token B):

  • x is the total amount of Token A in the pool.
  • y is the total amount of Token B in the pool.
  • k is a constant value that must never change during a trade.

Because k must remain constant, if someone wants to buy Token A (removing some x from the pool), they must put in a proportional amount of Token B (adding to y) to keep the product of the two numbers the same.

How the Math Sets the Price: A Practical Example

To see how this elementary equation dictates pricing, imagine a liquidity pool with the following balances:

  • x = 100 Token A
  • y = 100 Token B
  • k = 10,000 (100 * 100)

At this exact moment, the implied price is 1 Token A for 1 Token B, because their supplies in the pool are equal.

Now, a trader wants to buy 20 of Token A from the pool.

  1. The trader removes 20 Token A, leaving the pool with a new x balance of 80.
  2. Because k must stay at 10,000, the smart contract calculates the new required balance for y using basic division: y = k / x.
  3. y = 10,000 / 80, which equals 125.
  4. Since the pool already has 100 Token B, the trader must deposit the difference: 125 - 100 = 25 Token B.

The trader paid 25 Token B to get 20 Token A. Because of the trade, Token A is now scarcer in the pool, and Token B is more abundant. Consequently, Token A has become more expensive.

The Curve and Infinite Liquidity

When you plot x * y = k on a graph, it forms a curved line called a hyperbola. The curve approaches the x and y axes but never actually touches them (an asymptote).

In practical DeFi terms, this geometry ensures that a pool can never be completely drained of either token. As a buyer tries to purchase more and more of Token A, the price increases exponentially along the curve. Buying the very last fraction of a token would cost an infinite amount of the other token. This guarantees that the market always has liquidity, regardless of how large the trade is, though larger trades suffer from "slippage"—the difference between the expected price and the executed price caused by moving far along the curve.

Variations of the Math

While the constant product formula works well for volatile assets, DeFi protocols also use other basic formulas tailored for different asset types:

  • Constant Sum Formula (x + y = k): Plotting this creates a straight diagonal line. It offers zero price slippage, as the price ratio never changes. However, because the line touches the axes, a pool using only this math can be completely drained of one asset if the market price changes outside the pool.
  • Hybrid Curves: Protocols like Curve Finance combine the constant product (hyperbola) and constant sum (straight line) equations. This creates a curve that is flat in the middle—allowing massive trades of stablecoins with almost zero slippage—Weighting into a steep curve at the ends to prevent the pool from running out of tokens.

Maintaining Accurate Prices Through Arbitrage

An AMM smart contract does not know the price of tokens in the outside world; it only knows the ratio of x and y inside its own pool.

To keep prices accurate, protocols rely on arbitrage traders. If the price of Token A drops on an external exchange, traders will buy it cheaply there and sell it into the DeFi pool. This adds to x and removes from y, sliding the ratio along the mathematical curve until the DeFi pool's internal price matches the global market price.