DeFi basics: a price from a pool of two tokens
DeFi — decentralized finance — rebuilds financial tools as smart contracts. One core building block is a liquidity pool, where a price emerges automatically from the ratio of two token reserves. (Conceptual only — DeFi carries real risk; not advice.)
The big idea
An automated market maker sets price from the ratio of two pooled reserves, keeping their product constant as people trade.
See it in code
In DeFi there's no order book setting prices. A liquidity pool holds reserves of two tokens, and the price of one in terms of the other is simply their ratio:
# In a liquidity pool, price is simply the ratio of two reserves.
# Conceptual only - DeFi carries real financial risk. Not advice.
coin_a = 1000.0
coin_b = 2000.0
print("Price (B per A):", coin_b / coin_a)Price (B per A): 2.0
Price falls straight out of the reserves: 2000 / 1000 = 2. No buyers or sellers needed — just the ratio. But what keeps that ratio honest when people trade?
The pool guards one number: the product of the two reserves, k = a * b. Every trade must leave k unchanged, which is what forces the price to move. First, just compute it:
# The pool protects one value: the product of its reserves, k = a * b.
# Conceptual only - DeFi carries real financial risk. Not advice.
coin_a = 1000.0
coin_b = 2000.0
k = coin_a * coin_b
print("k (a * b):", k)
print("Price (B per A):", coin_b / coin_a)k (a * b): 2000000.0 Price (B per A): 2.0
k is 2,000,000 and the price is still 2. Hold k fixed while someone adds token A, and the price has to shift — that's the trade below.
Now the trade: someone adds 100 A, and the pool gives out B to keep a * b = k fixed — so the price shifts automatically:
# A liquidity pool: price is the ratio of two reserves.
# Conceptual only - DeFi carries real financial risk. Not advice.
coin_a = 1000.0
coin_b = 2000.0
k = coin_a * coin_b # the product stays constant
print("Start price (B per A):", coin_b / coin_a)
# someone adds 100 A; B leaves the pool to keep a * b = k
coin_a += 100
coin_b = k / coin_a
print("After trade, price:", round(coin_b / coin_a, 4))Start price (B per A): 2.0 After trade, price: 1.6529
No buyer, no seller, no exchange — just a formula. Adding A made A more plentiful, so its price in B fell. This 'constant product' rule (a * b = k) is the actual math behind major real-world exchanges, running as public smart-contract code.
Pricing something by supply and demand via a fixed rule is a clean idea with echoes elsewhere: dynamic pricing, matchmaking ratings, resource allocation. DeFi's twist is doing it transparently in code that anyone can inspect and use — which is powerful, and also why bugs and volatility can be so costly.
Try it yourself
Add a much larger amount of A and see how sharply the price moves — big trades cause 'slippage'. Then compute how much B a trader actually receives (the change in coin_b), the real output of the swap.
The common mistake
Assuming DeFi is safe because it's 'just math'. The math is transparent, but smart-contract bugs, extreme price swings, and scams are very real, and there's often no one to appeal to if funds are lost. Understanding the mechanism is education — it is not a reason to invest.
What it unlocks
DeFi mechanics build on smart contracts, operators, and transactions.