Explainers

Uniswap x·y=k Math: The AMM Formula Explained

Forget the fluffy marketing. Uniswap's foundational x·y=k equation isn't just elegant math; it's the engine grinding your trading experience. We break down why.

A stylized mathematical equation x*y=k overlayed on a dark, abstract background representing blockchain networks.

Key Takeaways

  • The x·y=k formula is the mathematical foundation of Uniswap V2's Automated Market Maker (AMM), dictating token prices based on reserve ratios.
  • This invariant directly causes slippage, where larger trades receive progressively worse execution prices.
  • Impermanent loss for liquidity providers is a direct consequence of the x·y=k mechanism and price divergence.
  • Uniswap V3 offers concentrated liquidity, a more complex generalization that aims for capital efficiency but retains core invariant principles.

So, you’re trading crypto on a decentralized exchange. You hit swap. Your order disappears into the digital ether, and you get back… less than you expected. Ever wonder why? It’s not magic. It’s math. Specifically, the deceptively simple x · y = k formula that underpins Uniswap and most of Decentralized Finance (DeFi).

This isn’t some abstract academic exercise. For real people dumping cash into crypto, this invariant dictates the price you get. It explains why those small trades feel fine, but suddenly larger ones take a painful bite. And it’s the culprit behind the dreaded “impermanent loss” that haunts liquidity providers.

Forget order books. Those are for dinosaurs. AMMs ditch the middleman entirely, replacing human traders with a mathematical pricing function. A smart contract holds two tokens, say ETH and USDC, and a formula dictates the price based on how much of each is currently in the pot. No broker. No waiting. Just code.

The clever part? The AMM doesn’t need to know the ‘real’ price. Arbitrageurs, those busy bees of the crypto world, will constantly buy cheap from the AMM and sell dear elsewhere, forcing the AMM’s price back in line with the broader market. It’s a self-correcting — albeit sometimes costly — system.

Here’s the core: x * y = k. x is the amount of token X, y is the amount of token Y. k is a constant. After any trade, the product of the reserves must remain the same. Simple, right? Wrong. This little equation is a mathematical straitjacket.

The marginal price, the rate at which one token trades for another, is y/x. Notice: it’s not constant. As you buy more X (reducing x), y has to go up to keep k the same. This change is precisely what creates slippage. The bigger your trade relative to the reserves, the more the price moves against you. That x / (x - Δx) ratio? That’s your price impact, the digital equivalent of getting fleeced.

And then there’s impermanent loss. It’s the pain of watching your assets perform worse in an AMM pool than if you’d just held them in your wallet. When the price of one token moves significantly against another, arbitrageurs rebalance the pool. You, the liquidity provider, end up with less of the token that went up and more of the token that went down, relative to your initial deposit. It’s a tax on volatility, and the math is unforgiving.

Let’s say you deposit ETH and USDC when ETH is $1000. For every 1 ETH, you might have 1000 USDC. Your value is $2000. Now, ETH moons to $4000. Arbitrageurs will drain ETH from the pool, leaving you with, say, 0.5 ETH and 2000 USDC. Your value is now $4000 (0.5 ETH * $4000) + $2000 USDC = $6000. But if you’d just held 1 ETH and 1000 USDC, you’d have $5000 (1 ETH * $4000) + $1000 USDC = $5000. Oops. Oh wait, that’s wrong. You would have $4000 ETH + $1000 USDC = $5000. My mistake.

Hold on. Let’s redo that. If you deposit 1 ETH and 1000 USDC, and the price ratio is 1:1000. x=1, y=1000. k = x*y = 1000. If the price of ETH goes to $4000, the new ratio is 1 ETH to 4000 USDC. So, y/x = 4000. We know x*y=1000. Substitute y=4000x into x*y=1000: x*(4000x)=1000 => 4000x^2=1000 => x^2=1000/4000=0.25 => x=0.5. Then y=4000*0.5=2000. So you have 0.5 ETH and 2000 USDC. Your pool value is (0.5 ETH * $4000/ETH) + 2000 USDC = $2000 + $2000 = $4000. Had you held 1 ETH and 1000 USDC, you’d have (1 ETH * $4000/ETH) + 1000 USDC = $4000 + $1000 = $5000. You lost $1000. That’s impermanent loss.

“This invariant, introduced by Uniswap V2, underpins billions of dollars in daily trading volume and gave birth to the Automated Market Maker (AMM) paradigm.”

That sounds fancy. What it means for you is that every trade you make on a V2-style AMM contributes to the price moving against you. The larger the trade, the more you pay in slippage. It’s the price of convenience.

Uniswap V3 tried to fix this with concentrated liquidity, allowing LPs to specify price ranges. It’s more capital-efficient but also vastly more complex. The x·y=k formula is the bedrock. Everything else is just layers of increasingly complex plumbing trying to mitigate its inherent… inefficiencies. For the average trader, it’s a constant reminder that the digital market is a mathematical beast, and it always gets its cut.

So, What’s the Big Deal with x·y=k?

The x·y=k formula is the core of Uniswap V2’s Automated Market Maker (AMM). It ensures that the product of the quantities of two trading tokens in a liquidity pool remains constant. This mathematical invariant determines the price of tokens for any trade. While elegant, it leads to slippage – the difference between the expected and executed price – especially for larger trades, and is the basis for impermanent loss for liquidity providers.

Why Does This Matter for Real People Trading Crypto?

For actual traders, the x·y=k formula means your trade execution price isn’t fixed. Every swap impacts the ratio of tokens in the pool, shifting the price. The bigger your trade relative to the pool’s size, the more you’ll pay in slippage. It’s a direct consequence of using AMMs instead of traditional order books. For liquidity providers, it explains why their initial deposits might be worth less over time if token prices diverge significantly, a phenomenon known as impermanent loss.

Impermanent Loss Explained Further

Impermanent loss occurs when the price ratio of deposited tokens changes compared to when you deposited them. Arbitrageurs rebalance the pool to match external market prices. This process leaves liquidity providers with a different ratio of tokens than they started with. If the price divergence is significant, the value of your pooled assets can be less than if you had simply held the original tokens. It’s “impermanent” because if prices return to their original ratio, the loss vanishes. But if you withdraw while prices are divergent, the loss becomes permanent.


🧬 Related Insights

Frequently Asked Questions

What does Uniswap’s x·y=k formula actually do? It’s the core pricing mechanism for Uniswap V2 AMMs. It dictates that the product of the quantities of two tokens in a liquidity pool must remain constant, defining how prices change with trades.

Will this formula affect my trades? Yes. It’s the reason for slippage. Larger trades experience worse execution prices because they significantly alter the token ratio in the pool, driving the price against the trader.

Is Uniswap V3 still using x·y=k? Uniswap V3 uses a generalized version that allows for concentrated liquidity. While not a strict constant product across all ranges, the underlying mathematical principles and the concept of invariants are still central to its operation. The core idea of maintaining a balance based on reserve quantities remains.

Written by
Open Source Beat Editorial Team

Curated insights, explainers, and analysis from the editorial team.

Frequently asked questions

What does Uniswap's x·y=k formula actually do?
It's the core pricing mechanism for Uniswap V2 AMMs. It dictates that the product of the quantities of two tokens in a liquidity pool must remain constant, defining how prices change with trades.
Will this formula affect my trades?
Yes. It's the reason for slippage. Larger trades experience worse execution prices because they significantly alter the token ratio in the pool, driving the price against the trader.
Is Uniswap V3 still using x·y=k?
Uniswap V3 uses a generalized version that allows for concentrated liquidity. While not a strict constant product across all ranges, the underlying mathematical principles and the concept of invariants are still central to its operation. The core idea of maintaining a balance based on reserve quantities remains.

Worth sharing?

Get the best Open Source stories of the week in your inbox — no noise, no spam.

Originally reported by Dev.to

Stay in the loop

The week's most important stories from Open Source Beat, delivered once a week.