Mastering Decentralised Finance Ecosystems
AMM Architecture
The Constant Product Formula
Decentralised exchanges (DEXs) need a way to determine asset prices without relying on a central authority or a traditional order book. Many of the first and most influential DEXs, like Uniswap v2, solved this with an elegant mathematical relationship called the Constant Product Formula. It's the engine behind what's known as a Constant Product Market Maker (CPMM).
Imagine a liquidity pool with 10 ETH and 20,000 DAI. According to the formula, our constant k is 10 * 20,000 = 200,000. This value must be maintained. If a trader wants to buy 1 ETH, they must add enough DAI to the pool to keep k at 200,000. They can't just take the ETH. Instead, the pool's smart contract calculates the new balances. After the trade, there will be 9 ETH left. To find the new amount of DAI (y), we solve for it: 9 * y = 200,000. This gives us y ≈ 22,222.22. The trader paid the difference, 2,222.22 DAI, for their 1 ETH. This makes the implicit price of that ETH 2,222.22 DAI, which is higher than the pre-trade price of 2,000 DAI (20,000 / 10). This price change is a core feature of AMMs.
This mechanism works because of individuals called liquidity providers (LPs). They deposit an equivalent value of both assets into the pool to create the market. In return for providing their capital and enabling trades, they receive LP tokens. These tokens represent their share of the pool and are used to claim a portion of the trading fees generated.
LP tokens are essentially a receipt for a user's stake in a liquidity pool. They can be held, transferred, or even staked in other DeFi protocols.
Price Impact and Arbitrage
In our previous example, the price of ETH changed mid-trade. The difference between the market price before the trade (2,000 DAI) and the effective price paid (2,222.22 DAI) is known as price impact. For large trades in a small pool, price impact can be significant. The larger the trade relative to the pool's total liquidity, the more the price will move. Slippage is a related concept. It's the difference between the price a trader expects to pay and the price they actually pay. This can happen if other trades execute before theirs, changing the price.
But what keeps the price on a DEX aligned with the broader market, like prices on major centralised exchanges? The answer is arbitrageurs. When the price of an asset in a pool, say ETH, drifts too low compared to other exchanges, arbitrage bots see an opportunity. They will buy the cheap ETH from the pool, pushing its price back up, and simultaneously sell it on another exchange for a profit. This constant, rapid activity by arbitrageurs ensures that AMM prices don't stray too far from the global market price for long.
Concentrated Liquidity
The classic x * y = k model is capital inefficient. Much of the capital sits idle, waiting for the price to reach extreme highs or lows. For stablecoin pairs like DAI/USDC, which should always trade around 1:1, liquidity provided from $0.50 to $1.50 is largely wasted. Protocols like introduced concentrated liquidity to solve this. Instead of providing liquidity across the entire price curve (from zero to infinity), LPs can choose a specific price range in which to deploy their capital. This concentrates their funds where most trading activity occurs, allowing them to earn more fees with less capital.
For example, an LP in an ETH/DAI pool might believe ETH will trade between $2,800 and $3,200 for the next week. They can provide their liquidity exclusively within that range. If the price of ETH stays within their selected range, they will earn trading fees. If the price moves outside their range, their position becomes inactive, and they stop earning fees until the price returns. This approach requires more active management but can dramatically increase the returns for savvy LPs.
Concentrated liquidity automated market makers (AMMs), such as Uniswap v3, enable liquidity providers (LPs) to earn liquidity rewards by depositing tokens into liquidity pools.
These architectural innovations, from the simple constant product formula to the capital-efficient concentrated liquidity model, are what enable the fluid and permissionless trading that defines decentralised finance.
Ready to test your understanding?
In a Constant Product Market Maker (CPMM), what does the formula represent?
A liquidity pool holds 50 ETH and 100,000 USDC. A trader wants to buy 10 ETH. How much USDC will they have to add to the pool to complete the trade?
Understanding these AMM mechanics provides a solid foundation for navigating the world of decentralised exchanges and making informed decisions about trading and liquidity provision.
