Understanding the Constant Product Formula in DeFi and Mathematics
Dec, 21 2025
The constant product formula isn’t just a math equation from a textbook-it’s the engine behind some of the biggest trades on blockchain networks today. If you’ve ever swapped tokens on Uniswap, SushiSwap, or any other decentralized exchange, you’ve interacted with this formula without even realizing it. At its core, the formula is simple: xy = k. But behind that simplicity lies a powerful idea that keeps liquidity pools balanced, prices stable, and trades possible-without middlemen.
What Is the Constant Product Formula?
The constant product formula is a mathematical relationship where two variables multiply to give the same result, no matter how they change. If you increase one, the other must decrease to keep the product constant. For example, if x = 5 and y = 10, then k = 50. If x becomes 25, y must drop to 2 to keep 25 × 2 = 50. This is called inverse proportionality.
This concept isn’t new. It’s been used since ancient times to describe physical laws like Boyle’s Law in physics, where pressure times volume equals a constant (P·V = k). But in the world of DeFi, it became the foundation for automated market makers (AMMs). Instead of order books, AMMs use this formula to set prices automatically based on the ratio of assets in a liquidity pool.
How It Works in DeFi Liquidity Pools
Imagine a liquidity pool with two tokens: ETH and USDC. The pool holds 10 ETH and 10,000 USDC. The constant product is 10 × 10,000 = 100,000. This number, k, stays fixed unless someone adds or removes liquidity.
Now, someone wants to buy 1 ETH. To do that, they must add USDC to the pool. The formula forces the price to adjust. If the buyer adds 1,111.11 USDC, the pool now has 11 ETH and 11,111.11 USDC. The product is still 100,000 (11 × 9,090.91 ≈ 100,000). The price of ETH rose because there’s now less USDC per ETH in the pool.
This is how prices are set without order books. The more of one token you buy, the more expensive it gets-because the pool’s balance shifts. It’s a self-correcting system. And it works because the formula ensures there’s always a price, even for illiquid tokens.
Why This Formula Matters More Than You Think
Before AMMs, trading crypto meant matching buyers and sellers. That required liquidity providers to sit on the sidelines, waiting for others to place orders. The constant product formula changed that. It turned liquidity pools into always-on markets.
It’s not perfect. When one token becomes extremely rare in the pool, the price can spike dramatically. That’s why large trades often cause slippage. But for small trades, it’s efficient, transparent, and permissionless. You don’t need a broker. You don’t need to wait. You just swap.
As of October 2023, Uniswap alone processed over $347 billion in trading volume using this exact formula. That’s not a fluke. It’s a design choice-because the math works.
How It Compares to Direct Proportionality
Many people confuse the constant product formula with direct proportionality. They’re opposites.
- Direct proportionality: y = kx. If x doubles, y doubles. Like distance = speed × time.
- Constant product: xy = k. If x doubles, y halves. Like pressure and volume in a sealed gas container.
Students often mix them up. A 2021 study in the Journal of Mathematical Behavior found only 63% of learners correctly identified inverse proportionality scenarios. That’s why DeFi newcomers struggle too. They expect buying more ETH to lower its price. But the formula does the opposite-it makes it cost more.
Visualizing this helps. The graph of xy = k is a hyperbola. It never touches the axes. That’s why liquidity pools never run out of either token-they just get more expensive to trade.
Real-World Examples Beyond DeFi
The constant product formula isn’t just for crypto. It shows up everywhere:
- Physics: Boyle’s Law (P·V = k) explains how air pressure changes as you compress a gas.
- Electrical circuits: In parallel resistors, current and resistance are inversely proportional.
- Engineering: Torque and rotational speed in mechanical systems follow the same rule.
These aren’t coincidences. They’re all examples of conservation laws-where the product of two changing factors stays fixed. DeFi just applied it to digital assets.
Problems and Limitations
It’s not magic. The constant product formula has flaws.
First, it’s not optimized for stablecoins. If you swap USDC for DAI, both are worth $1. The formula still forces price changes, causing unnecessary slippage. That’s why newer AMMs like Curve use different formulas for stable assets.
Second, during extreme market events-like the TerraUSD collapse in 2022-the assumption that k stays constant can break. If panic selling drains one side of the pool, the price can become detached from real market value. That’s not the formula’s fault-it’s a design limitation when used without safeguards.
Third, large trades hurt everyone. The more you buy, the more the price moves against you. That’s why institutional traders use limit orders on centralized exchanges instead.
How to Use It in Practice
If you’re trading on a DEX, here’s how to think about the formula:
- Check the pool size. A larger pool (e.g., $10M in ETH/USDC) means less slippage.
- Use a swap calculator. Most wallets show estimated price impact before you confirm.
- Avoid swapping large amounts in small pools. A $500 trade in a $10,000 pool will move the price drastically.
- Understand that the price you see is based on the current ratio, not the market. It can differ from Coinbase or Binance.
For liquidity providers, it’s even more important. You’re not just depositing tokens-you’re betting on their price staying relatively stable. If ETH spikes 500% and you’re in an ETH/USDC pool, you’ll lose value compared to just holding ETH. That’s called impermanent loss. It’s not a bug-it’s a direct result of the constant product formula.
What’s Next for the Formula?
The formula is being upgraded. New AMMs now use hybrid models:
- Concentrated liquidity (Uniswap V3): Liquidity providers can set price ranges, so k isn’t constant across the whole curve.
- Dynamic k: Some protocols adjust k based on volatility or trading volume.
- Machine learning integration: Researchers at MIT are testing if AI can predict optimal k values in real time.
But the core idea remains. Even with upgrades, the constant product formula is still the baseline. It’s the reason DeFi exists in its current form.
Final Thoughts
You don’t need to be a mathematician to use DeFi. But understanding xy = k helps you avoid costly mistakes. It explains why your trade cost more than expected. Why your liquidity position lost value. Why some pools are safer than others.
This formula has survived 2,300 years-from Greek geometry to quantum physics to blockchain. It’s not going away. The question isn’t whether it works. It’s whether you understand how it works.
Is the constant product formula only used in DeFi?
No. The formula xy = k has been used for centuries in physics, engineering, and economics. It describes Boyle’s Law in gases, lever mechanics, and electrical resistance in parallel circuits. DeFi adopted it because it’s a simple, reliable way to price assets without order books-but it didn’t invent it.
Why does buying more of a token make it more expensive in a liquidity pool?
Because the formula xy = k forces the ratio between the two tokens to stay balanced. When you buy ETH, you’re removing ETH from the pool and adding USDC. That means there’s less ETH relative to USDC. To keep the product constant, the price of ETH must rise. It’s not a market opinion-it’s pure math.
Can the constant k change in a liquidity pool?
Only when someone adds or removes liquidity. If you deposit more ETH and USDC into the pool, k increases. If you withdraw, k decreases. But during trades, k stays fixed. That’s what makes the pricing automatic and predictable.
What’s impermanent loss, and how is it related to the constant product formula?
Impermanent loss happens when the price of one asset in a liquidity pool changes compared to the other. Because the formula forces the pool to rebalance, your share of the pool may be worth less than if you’d just held the tokens outside. It’s called "impermanent" because if the price returns to its original level, the loss disappears. But if it doesn’t, the loss becomes real. The constant product formula is why this happens-it doesn’t account for external price movements.
Is the constant product formula the best pricing model for DeFi?
It’s not the best for everything, but it’s the most widely used because it’s simple and works without human intervention. For volatile assets like ETH and BTC, it’s effective. For stablecoins, it causes unnecessary slippage, which is why protocols like Curve use different formulas. Newer models like concentrated liquidity (Uniswap V3) improve efficiency, but they still rely on the core idea of constant product as their foundation.
What to Do Next
If you’re new to DeFi, start by swapping small amounts on Uniswap or SushiSwap. Watch how the price changes as you trade. Use a calculator to simulate what happens if you buy 10% of the ETH in a pool. See how k stays fixed. Then try adding liquidity yourself-just with a small amount. You’ll see firsthand how the formula shapes every interaction.
If you’re already trading, pay attention to slippage settings. Never accept more than 0.5% slippage on small trades. Avoid pools with less than $1 million in liquidity unless you know what you’re doing. And always remember: the price you see isn’t the market price-it’s the pool’s price, governed by xy = k.
Tyler Porter
December 21, 2025 AT 14:57This formula is wild, right? Like, you just swap tokens and boom-price shifts because math says so. No middleman, no drama, just xy=k doing its thing. I used to think crypto was all hype, but this? This is actual science.
Luke Steven
December 21, 2025 AT 15:18It’s beautiful how something so ancient-Boyle’s Law, Greek geometry-becomes the backbone of a financial revolution. We’re not inventing anything new… just repurposing the universe’s own rules. xy=k is just nature whispering through blockchain. 🤔
Ellen Sales
December 22, 2025 AT 14:00so like… if i buy 1 eth and the price goes up… am i the reason the pool hates me? 😅 also why does everyone act like this is magic when it’s just high school math??
Vijay n
December 23, 2025 AT 09:10Alison Fenske
December 23, 2025 AT 20:12i swear i thought i was just swapping tokens but turns out i was doing calculus with my coffee. like… why does this feel like a breakup? you give more, you get less? 😭
Aaron Heaps
December 25, 2025 AT 18:12This is why retail loses. You think you’re trading, but you’re just feeding the algorithm. Slippage isn’t a bug-it’s the feature. Stop pretending this is fair.
Tristan Bertles
December 26, 2025 AT 19:47Try this: open Uniswap, pick a tiny pool, and swap $50 of a random token. Watch the price jump 15%. That’s the formula in action. It’s not broken-it’s just brutally honest. No fluff. No lies. Just math.
Megan O'Brien
December 28, 2025 AT 09:47AMM? More like A-Messed-Up. Why are we still using this 2020-era relic? Concentrated liquidity is 2023. If you’re still using constant product, you’re not a degens-you’re a museum piece.
Earlene Dollie
December 30, 2025 AT 07:18impermanent loss is just crypto’s way of saying ‘you thought you were investing but you were just emotionally attached to a number’… and now you’re crying in your crypto hoodie
Dusty Rogers
December 31, 2025 AT 22:13Most people don’t get it because they treat DeFi like a stock app. It’s not. It’s a self-regulating ecosystem. The formula isn’t the problem. The mindset is.
Kevin Karpiak
January 2, 2026 AT 10:44USA invented this? No. China’s quantum math teams have had this figured out since 2018. This is just a watered-down version for the masses. They don’t want you to know the real system.
Amit Kumar
January 3, 2026 AT 21:05Bro, this is the same as how our village grain markets worked centuries ago. If you take too much wheat, the price rises. If you bring more, it drops. This isn’t new-it’s wisdom. We just call it blockchain now.
chris yusunas
January 5, 2026 AT 13:16man i just wanted to swap my dogecoin for some usdc and now i’m thinking about hyperbolas like some kinda math nerd… thanks for that
Mmathapelo Ndlovu
January 7, 2026 AT 10:39xy=k feels like love sometimes… you give more, you get less… but if you both hold steady, the bond stays strong 💕
Brian Martitsch
January 8, 2026 AT 04:27If you don’t understand calculus, you shouldn’t be touching DeFi. This isn’t a game for amateurs. You’re not ‘participating’-you’re being exploited by the math-literate.
Rebecca F
January 8, 2026 AT 18:35everyone’s so proud of this formula like it’s genius… but it’s just a glorified seesaw. And we’re all just kids on it, screaming while someone else holds the pivot
vaibhav pushilkar
January 9, 2026 AT 04:50For beginners: always check liquidity depth before swapping. A $10M pool? Safe. $500K? You’re playing Russian roulette with your funds. Simple as that.
SHEFFIN ANTONY
January 9, 2026 AT 20:49Uniswap is a scam. This formula was designed to drain small wallets. The big players front-run the math. You think you’re trading? You’re just feeding the whales. Wake up.