DeFi (decentralized finance) trading means swapping, lending, borrowing and providing liquidity through smart contracts on a blockchain instead of through an exchange or bank. The core building block is the automated market maker (AMM), a pool of tokens that prices trades by formula. DeFi lets anyone trade any token from a self-custody wallet, but it adds costs and risks that do not exist on a centralized exchange, including price impact, impermanent loss and smart contract failure.
Key takeaways
- A constant-product AMM keeps the product of its two token balances fixed, so every trade moves the price, and larger trades move it more.
- Liquidity providers earn trading fees but suffer impermanent loss when prices move: about 5.7 percent of value relative to holding if one token doubles against the other.
- Concentrated liquidity raises fee income per dollar but magnifies impermanent loss and requires active management.
- DeFi lending lets traders borrow against collateral, with automatic liquidation when collateral value falls too far.
- Smart contract bugs, oracle failures and governance attacks can cause losses no matter how good the trade was.
On this page
How a constant-product AMM works
The most common AMM design, popularized by Uniswap, holds two tokens, x and y, and enforces:
x × y = k
where k stays constant during a swap (fees aside). The price of x in terms of y is y / x. To buy x, you add y and remove x, keeping the product fixed. Because the curve gets steeper as one side depletes, each additional unit costs more.
See CEX vs DEX for how AMMs compare with order books.
Worked example: price impact
A pool holds 100 ETH and 200,000 USDC. k = 100 × 200,000 = 20,000,000, and the price is 200,000 / 100 = $2,000 per ETH.
A trader swaps 10,000 USDC for ETH. Ignoring fees:
- New USDC balance: 210,000.
- New ETH balance: 20,000,000 / 210,000 = 95.238 ETH.
- ETH received: 100 − 95.238 = 4.762 ETH.
- Average price paid: 10,000 / 4.762 = $2,100, which is 5 percent above the starting price.
- New pool price: 210,000 / 95.238 = $2,205.
With a 0.30 percent fee taken from the input, 9,970 USDC enters the curve and the trader receives about 4.748 ETH, an average price of about $2,106.
The trade was 5 percent of the pool's USDC side and moved the average price by 5 percent. A $1,000 swap in the same pool, after the fee, gets about 0.496 ETH at an average of about $2,016, under 1 percent worse. Price impact grows with trade size relative to pool depth, which is why liquidity matters.
Slippage tolerance
Because other trades can land before yours, DEX interfaces ask for a slippage tolerance: the worst price you will accept. If the price moves beyond it, the transaction fails instead of executing. Setting this too wide invites sandwich attacks; too narrow and swaps fail in volatile markets.
Providing liquidity
Anyone can deposit both tokens into a pool, in proportion to the current price, and receive a share of the trading fees. Liquidity providers (LPs) are effectively passive market makers: they always quote both sides.
Impermanent loss
When the price moves, arbitrage traders rebalance the pool. LPs end up holding more of the token that fell and less of the token that rose. Compared with simply holding the original tokens, the LP position is worth less. This shortfall is called impermanent loss (IL). It is "impermanent" only in that it disappears if the price returns to where it started; if you withdraw after a move, it is a real loss.
For a 50/50 constant-product pool, if the price ratio changes by a factor r:
IL = 2 × √r / (1 + r) − 1
| Price change of one token vs the other | Impermanent loss |
|---|---|
| +25% (r = 1.25) | −0.6% |
| +50% (r = 1.5) | −2.0% |
| +100% (r = 2) | −5.7% |
| +200% (r = 3) | −13.4% |
| +300% (r = 4) | −20.0% |
| −50% (r = 0.5) | −5.7% |
| −75% (r = 0.25) | −20.0% |
Worked example: impermanent loss
An LP deposits 1 ETH and 2,000 USDC when ETH is $2,000. Total value: $4,000. Their share of the pool has k = 1 × 2,000 = 2,000.
ETH rises to $4,000. Arbitrage rebalances the position so that x × y = 2,000 and y / x = 4,000:
- x² = 2,000 / 4,000 = 0.5, so x = 0.7071 ETH.
- y = 4,000 × 0.7071 = 2,828.43 USDC.
- LP position value: 0.7071 × 4,000 + 2,828.43 = $5,656.85.
Had the LP just held 1 ETH and 2,000 USDC: 4,000 + 2,000 = $6,000.
Impermanent loss = 5,656.85 / 6,000 − 1 = −5.7 percent, or $343.15. The LP is still up from $4,000, but less than a holder. Fees earned must exceed $343.15 for providing liquidity to have been worthwhile.
If ETH instead falls to $1,000, the position becomes 1.4142 ETH and 1,414.21 USDC, worth $2,828.43, against $3,000 for holding. Same −5.7 percent.
Concentrated liquidity
Newer AMMs, starting with Uniswap v3 in 2021, let LPs place liquidity within a chosen price range. Capital in a narrow range earns much more in fees per dollar while the price stays in range. But if the price leaves the range, the position converts entirely to the weaker token and stops earning fees, and impermanent loss within the range is larger than in a full-range pool. Concentrated liquidity behaves more like active market making than passive investing, and many LPs underperform simple holding once IL and gas are counted.
Lending, borrowing and on-chain leverage
Lending protocols such as Aave and Compound let users deposit collateral and borrow other assets. Interest rates float with supply and demand. Each collateral asset has a liquidation threshold: the share of its value that can be borrowed against before liquidation.
Worked example: health factor
A trader deposits 10 ETH at $3,000 ($30,000) with a liquidation threshold of 80 percent and borrows 15,000 USDC.
- Health factor = (collateral value × threshold) / debt = (30,000 × 0.80) / 15,000 = 1.6.
- Liquidation occurs when the health factor falls below 1.
- Liquidation price: 10 × P × 0.80 = 15,000, so P = $1,875.
ETH can fall 37.5 percent before liquidation. When it happens, liquidators repay part of the debt and seize collateral at a discount (the liquidation bonus), so the trader loses more than the shortfall. Traders build leverage by borrowing stablecoins, buying more ETH and depositing it again ("looping"), which raises exposure and pulls the liquidation price closer. The math of crypto liquidations on perps applies in spirit, though DeFi liquidations depend on oracle prices and can be triggered by oracle errors.
Yield and its risks
High advertised yields usually come from one of four sources: trading fees, borrowing demand, token emissions or leverage. Emission-driven yields are paid in tokens that often fall in value. Yields far above what lending markets pay usually carry hidden risk.
Smart contract and protocol risks
- Bugs and exploits. Billions of dollars have been stolen from DeFi protocols through code flaws, flawed upgrades and bridge hacks. Audits reduce but do not remove this risk.
- Oracle manipulation. Protocols that rely on thin price feeds can be tricked into mispricing collateral.
- Admin keys and governance. A team or token-holder vote may be able to change contracts or move funds.
- Stablecoin and collateral depegs. See stablecoins.
- Approval risk. Token approvals granted to a contract that is later exploited can expose your wallet. See wallet security.
Common mistakes
- Swapping large amounts in shallow pools without checking price impact.
- Providing liquidity for the advertised APR without estimating impermanent loss.
- Looping leverage without a margin of safety on the health factor.
- Chasing yield on new, unaudited protocols.
- Leaving unlimited approvals on contracts you no longer use.
FAQ
What is impermanent loss?
It is the shortfall in value a liquidity provider suffers compared with simply holding the tokens, caused by the pool rebalancing as prices move. In a 50/50 pool, a doubling or halving of one token's relative price causes a loss of about 5.7 percent versus holding.
How do AMMs set prices?
A constant-product AMM keeps the product of its two token balances fixed. The price is the ratio of the balances, and each trade shifts that ratio, so larger trades get progressively worse prices.
Is providing liquidity profitable?
It can be when fee income exceeds impermanent loss and costs, typically in pools with high volume relative to price volatility. Many LPs, especially in volatile pairs and concentrated positions, earn less than they would by simply holding.
What happens when a DeFi loan is liquidated?
If the health factor drops below 1, liquidators repay part of the debt and take collateral at a discount. The borrower keeps the borrowed funds but loses that collateral plus the liquidation bonus.