Overview
Curated: · Written: · Reviewed:
DeFi Primitives (AMMs, Lending, Liquidity Pools)
In one line
An AMM prices swaps from pool reserves under x·y = k, and a lending market lends against overcollateralised positions. Start from the invariant, then state the cost and the failure: what the trade loses to the curve, what the LP gives up to divergence, and what breaks first when price moves fast.
Key definitions
- Constant product (x·y = k): price is the reserve ratio y/x. No price is stored; it moves because reserves move.
- Price impact: execution price against spot, set by trade size relative to reserves.
- Divergence (impermanent) loss: LP value minus hold value. In a 50/50 pool it depends only on the price ratio r.
- Liquidation threshold: the collateral-weighted limit at which a position becomes liquidatable. Always at or above max LTV; the gap is the buffer.
- Health factor (HF): sum of collateral × price × threshold, over sum of debt × price. Below 1.0 is liquidatable.
- Close factor: the maximum share of debt one liquidation may repay.
- TWAP: time-weighted average price. It raises manipulation cost by requiring the distortion to persist.
Formulas and complexity
Swap output with fee f taken on input (Uniswap v2 style, f = 0.003):
Divergence in a 50/50 constant-product pool, r = new price / old price:
Health factor:
A swap is O(1) in the pool's arithmetic. Check version-specific parameters (fees, thresholds, bonuses) against current docs at integration.
How it works
Worked trace: 10 ETH into a 100 ETH / 200,000 USDC pool. Effective input is 9.97 ETH. Output is 200,000 − 20,000,000 / 109.97 = 18,132.2 USDC. That is 1,813.2 per ETH, 9.34% below the 2,000 spot. The curve alone costs 9.09%; the fee costs the rest. The fee stays in the pool for LPs.
Lending trace: 10 ETH at 2,000 with an 82.5% threshold and 12,000 USDC debt gives HF = 16,500 / 12,000 = 1.375. Liquidation triggers at P = 12,000 / (10 × 0.825) = 1,454.5, a 27% drop. A liquidator repays up to the close factor and receives the bonus in collateral.
When to use and when not to
| Situation | Use | Avoid |
|---|---|---|
| Swapping in a volatile pair | Deep reserves, trade small relative to reserves | Large size: walk the curve or split, compare external liquidity |
| Stable pairs | StableSwap, which quotes near 1:1 while the peg holds | Constant-sum behaviour off peg, where arbitrageurs drain the pool |
| Concentrated LP | Range you expect price to stay in, high fee volume | Trending markets that exit the range: zero fees and a fully losing asset |
| Oracle from a pool | Long TWAP over a liquid, actively arbitraged pool | Thin pools, short windows, thin observation history |
| Collateral pricing | Validated external feed, or long TWAP | Any single-block spot read |
Traps interviewers probe
- Reading spot as the oracle. Fix: a TWAP over a deep, arbitraged pool or a validated feed. Moving a $4M pool 12x costs about $35,000 in fees and flash-loan costs, which is cheap against a large borrow.
- Treating minOut as sandwich protection. Fix: a tight bound caps your loss at the bound; it does not stop the attacker. Add a short deadline and protected submission, and disclose relay trust.
- Calling HF above 1 safe. Fix: measure how fast HF can cross 1.0 from volatility, oracle cadence and collateral depth. A 27% buffer is thin for a volatile asset over a block.
- Accounting from balanceOf. Fix: use internal accounting or getReserves(). Anyone can donate tokens to a pair and change balanceOf, and sync() and skim() expose the same surface.
- Empty-vault share inflation. Fix: virtual shares or offset, a burned minimum first deposit, and rounding against the caller. One share plus a 10,000 donation let an attacker take about 5,000 from a 20,000 deposit in the worked trace.
Interview tips
Open with the invariant and one number: "constant product, so 10 ETH into this pool returns about 18,132 USDC, 9.3% under spot."
Expected follow-ups:
- A 10x trade: walk the curve from current reserves, not the spot price.
- A one-block move: oracle staleness, liquidation latency and collateral liquidity.
- Who pays bad debt: socialisation, reserve or backstop, pause rules.
- What you monitor at 3am: oracle age and divergence, HF distribution below 1.05, utilisation, bad debt, AMM depth per collateral.
Oracle checks to name: updatedAt freshness against the feed heartbeat, decimals() (USD feeds use 8, so an 8-decimal answer of 300000000000 is $3,000, and read as 1e18 WAD it is $0.0000003), a positive answer inside a sane band, and a pinned proxy address.
30-second outline:
- Invariant: x·y = k, price is the reserve ratio.
- Fee on input, output formula, impact number for the trade size.
- Protect the user: minOut from current state, short deadline.
- LP: divergence 2√r/(1+r) against fee volume over time in range.
- Lending: threshold buffer, HF, validated oracle, and what bad debt does.
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.