# DeFi Core Mathematics ## AMM: Constant Product Formula (Uniswap V2) **Core Formula:** `x * y = k` (x, y are the quantities of two tokens, k is a constant) ### Price Calculation ``` Current Price = y / x (how much y you get for 1 x) Example: Pool has 10 ETH + 20000 USDC ETH Price = 20000 / 10 = 2000 USDC ``` ### Swap Calculation (with 0.3% fee) ``` Input Δx, output Δy: Δy = y * Δx * 997 / (x * 1000 + Δx * 997) Example: Swap 1 ETH for USDC, pool has 10ETH + 20000USDC Δy = 20000 * 1 * 997 / (10 * 1000 + 1 * 997) = 19940000 / 10997 ≈ 1813 USDC (price slippage ~9.35%) ``` ### Price Impact ``` The larger the trade size / pool depth, the greater the price impact Price Impact = 1 - x / (x + Δx) Example: 1 ETH / 10 ETH pool = 10% impact (very high) 1 ETH / 1000 ETH pool = 0.1% impact (normal) ``` --- ## Uniswap V3: Concentrated Liquidity V3 introduces the **price range** concept, where LPs provide liquidity only within specified ranges: ``` tick: Price is represented by tick, price(tick) = 1.0001^tick tickLower/tickUpper: Price range where liquidity is active Virtual liquidity formulas: L = Δx / (1/√Pa - 1/√Pb) ← Calculate required token X L = Δy / (√Pb - √Pa) ← Calculate required token Y ``` **Impact of Concentrated Liquidity:** - With the same capital, narrower price ranges yield higher capital efficiency (but greater IL risk) - When price moves out of range, LP holds 100% of the depreciated asset --- ## Interest Rate Model (Compound / Aave) ### Utilization Rate ``` U = Total Borrowed / Total Deposits Example: 10000 USDC deposits, 7000 USDC borrowed U = 7000 / 10000 = 70% ``` ### Kinked Rate Model ``` Slope1 (normal range, U < optimal point): Borrow Rate = BaseRate + U * Slope1 Slope2 (high-risk range, U > optimal point): Borrow Rate = BaseRate + Optimal * Slope1 + (U - Optimal) * Slope2 Example (Aave USDC parameters): BaseRate = 0%, Optimal = 90%, Slope1 = 4%, Slope2 = 60% U=50%: Borrow Rate = 0 + 50%*4% = 2% U=90%: Borrow Rate = 0 + 90%*4% = 3.6% (optimal point) U=95%: Borrow Rate = 3.6% + 5%*60% = 6.6% (steep penalty) U=99%: Borrow Rate = 3.6% + 9%*60% = 9% ``` Deposit Rate = Borrow Rate * U * (1 - Protocol Fee) --- ## Liquidation Mechanism ### Health Factor (Aave) ``` HF = Σ(Collateral Value × Liquidation Threshold) / Total Borrowed Value HF > 1: Safe HF = 1: Liquidation threshold HF < 1: Subject to liquidation Example: Deposit $10000 ETH (liquidation threshold 82.5%) Borrow $6000 USDC HF = 10000 * 0.825 / 6000 = 1.375 (safe) ETH drops 30% → $7000 HF = 7000 * 0.825 / 6000 = 0.9625 → Subject to liquidation ``` ### Liquidation Bonus ``` Liquidator repays 50% of debt (close factor = 50%) Receives collateral + 5-15% bonus (liquidation bonus) Example: User owes $6000 USDC, collateral $7000 ETH (bonus 5%) Liquidator repays $3000 USDC Receives ETH = $3000 * 1.05 = $3150 worth of ETH Arbitrage profit = $150 ``` --- ## Impermanent Loss (IL) ``` IL = 2√r/(1+r) - 1, where r = price change ratio Price Change → IL +25% → -0.6% +50% → -2.0% +100% → -5.7% (2x price = 5.7% loss) +200% → -13.4% -50% → -5.7% Price returns to original → IL = 0 (no permanent loss) ``` **Solidity Implementation Reference:** ```solidity // Calculate sqrt for V3 liquidity calculations function sqrt(uint256 x) internal pure returns (uint256 y) { if (x == 0) return 0; uint256 z = (x + 1) / 2; y = x; while (z < y) { y = z; z = (x / z + z) / 2; } } // Q96 fixed-point number (Uniswap V3 internal format) // sqrtPriceX96 = sqrt(price) * 2^96 uint160 sqrtPriceX96 = ...; uint256 price = uint256(sqrtPriceX96) * uint256(sqrtPriceX96) >> 192; ```