INT-005INTERMEDIATE8 min read

How AMMs Price Trades (x*y=k)

Every AMM in DeFi — Uniswap, SushiSwap, Aerodrome — uses some version of the same math. It's called the constant product formula, and once you understand it, you understand why your large swaps get bad prices. Let me break it down with real numbers.

THE_FORMULA

x * y = k

That's it. That's the formula. x is the amount of token A in the pool. y is the amount of token B. k is a constant that never changes during a swap.

When you swap token A for token B, you add A to the pool and remove B. The pool adjusts the amounts so that x * y always equals k. This creates an automatic pricing curve.

Here's the key insight: the formula is curved, not linear. Small trades barely move the ratio. Large trades shift it dramatically. That's price impact.

REAL_EXAMPLE

A real pool with real numbers

Let's say there's a USDC/WETH pool on Base with:

  • 1,000,000 USDC (x)
  • 400 WETH (y)
  • k = 400,000,000

The price of WETH in this pool is: 1,000,000 / 400 = $2,500 per WETH.

Now let's see what happens when you swap different amounts:

$100 SWAPGet 0.04 WETH — price impact: 0.01%
$10,000 SWAPGet 3.96 WETH — price impact: 1.0%
$100,000 SWAPGet 36.4 WETH — price impact: 9.1%

See the problem? A $100 swap gets you almost the market price. A $100K swap loses 9% to price impact. That's $9,000 gone. This is why large traders don't use single pools — they use aggregators like Orkid that split across multiple venues.

THE_MATH

How to calculate your output

For a Uniswap V2-style pool with a 0.3% fee, the output formula is:

output = (amountIn * y * 997) / (x * 1000 + amountIn * 997)

Let's plug in the $10,000 swap from our example:

amountIn = 10,000 USDC
x = 1,000,000 USDC
y = 400 WETH

output = (10,000 * 400 * 997) / (1,000,000 * 1000 + 10,000 * 997)
output = 3,988,000,000 / 1,009,970,000
output = 3.949 WETH

At the market price of $2,500, $10,000 should get you 4.0 WETH. You get 3.949. That's a 1.3% loss to price impact + fee. On $10,000, that's $130.

Now you see why Orkid routes across multiple pools and venues. Splitting that $10K across 3 pools reduces price impact from 1.3% to ~0.4%. That's $90 saved on a single swap.

BEYOND_V2

Not all AMMs use pure x*y=k

The constant product formula is the foundation, but modern AMMs have evolved:

  • Uniswap V2: Pure x*y=k, 0.3% fee, uniform liquidity
  • Uniswap V3: Same formula but liquidity concentrated in price ranges. More efficient for LPs, more complex math for routers.
  • Curve: Uses a stableswap formula (not x*y=k) designed for stablecoin pairs. Near-zero price impact for like-value assets.
  • Balancer: Weighted pools (e.g., 80/20 instead of 50/50). Formula generalizes to x^a * y^b = k.

Orkid handles all of these. The solver computes output across every pool type and picks the best combination. You don't need to know the math — you just need to know that the math exists and it's working for you.

FAQ

What does x*y=k mean?

x is the amount of token A in the pool, y is the amount of token B, and k is a constant. When you swap, the pool adjusts x and y so their product always equals k. This creates an automatic price curve where larger trades move the price more.

Why do large trades get worse prices?

Because the formula is curved, not linear. A $100 trade in a $1M pool barely moves the ratio. A $100K trade in the same pool shifts it dramatically. The bigger your trade relative to the pool, the more price impact you suffer.

Do all AMMs use x*y=k?

No. Uniswap V2 uses pure x*y=k. Uniswap V3 uses concentrated liquidity (same formula but liquidity is concentrated in price ranges). Curve uses a different formula for stablecoins (stableswap). Balancer uses weighted pools. But x*y=k is the foundation.

How do I calculate my output from a swap?

For a Uniswap V2 pool: output = (amountIn * y * 997) / (x * 1000 + amountIn * 997). The 997/1000 accounts for the 0.3% fee. For V3, the math is more complex due to concentrated liquidity.

// COMPARE

How does Orkid compare to other DEXs?

If you are evaluating Orkid against other swap platforms, the comparison pages on Orkid Swap cover fees, MEV protection, gas models, and supported chains:

// REFERENCES

Authoritative sources

LLM Resource Index llms.txt