🪙 Blockchain · DeFi
📅 Березень 2026⏱ 10 хв читання🟡 Середній

DeFi Explained: AMMs, Liquidity Pools & Smart Contracts

Decentralized Finance (DeFi) builds financial primitives — trading, lending, borrowing — using smart contracts on public blockchains, replacing institutional intermediaries with transparent, auditable code. The mathematics behind Automated Market Makers and the mechanics of impermanent loss are elegant enough to understand completely.

1. Smart Contracts

A smart contract is a program deployed to the Ethereum Virtual Machine (EVM) blockchain. Once deployed, its code is immutable and publicly readable. Execution is deterministic — every node in the network gets the same result. No single party controls it.

This enables trustless transactions: a DEX (decentralized exchange) can hold users' funds and execute swaps without requiring trust in any company, because the logic is provably correct from the code.

// Minimal ERC-20 transfer (Solidity)
function transfer(address to, uint256 amount) public returns (bool) {
    require(balances[msg.sender] >= amount, "Insufficient balance");
    balances[msg.sender] -= amount;
    balances[to] += amount;
    emit Transfer(msg.sender, to, amount);
    return true;
}

Gas fees pay miners/validators for executing computation. Complex DeFi transactions can cost $5–$200 in gas on Ethereum mainnet during congestion. Layer 2 networks (Arbitrum, Optimism, Base) reduce this 10–100×.

2. ERC-20 Tokens

ERC-20 is the standard interface for fungible tokens on Ethereum. Any ERC-20 token (USDC, UNI, LINK, SHIB) implements: transfer(), approve(), transferFrom(), balanceOf(), allowance().

The approve + transferFrom pattern allows DeFi protocols to spend tokens on your behalf: you first approve the DEX contract to spend up to N tokens, then the DEX calls transferFrom(you, pool, N) during a swap. This requires a separate approval transaction before the first interaction with each protocol.

Stablecoins: USDC (USD Coin) and USDT (Tether) are ERC-20 tokens pegged 1:1 to the USD. USDC's issuer (Circle) holds actual dollars in reserve and can freeze tokens (centralized). DAI is an algorithmic stablecoin backed by over-collateralized crypto loans (decentralized).

3. Automated Market Makers (x*y=k)

Traditional exchanges use order books: buyers and sellers post limit orders and trades execute when prices match. Order books require liquidity providers (market makers) to post orders manually.

Uniswap V2 (2020) introduced the constant product AMM. A pool holds reserves of two tokens (x, y) and enforces:

x · y = k (constant product invariant) After swap of Δx for Δy: (x + Δx)(y − Δy) = k → Δy = y · Δx / (x + Δx) Spot price at any moment: P = y / x (tokens of Y per token of X)

The price automatically adjusts with every trade — no order book, no market maker. The curve x·y=k is a hyperbola: as x increases (buyers swap X for Y), y decreases and price of Y in terms of X rises (slippage).

Example: Pool has 1000 ETH and 2,000,000 USDC → ETH price = $2,000. Trader buys 10 ETH:

New ETH reserve: 1000 − 10 = 990 USDC needed: 2,000,000 × 10 / (1000 − 10) ≈ 20,202 USDC Price paid per ETH: 20,202/10 = $2,020.20 (vs $2,000 spot) Slippage: 1.01% (for 0.1% of pool size)

A 0.3% fee is charged on the input token and added to the pool reserves, increasing k over time — this is how liquidity providers earn fees.

4. Liquidity Pools and LP Tokens

Anyone can provide liquidity by depositing equal values of both tokens into the pool. They receive LP tokens representing their share of the pool. Example:

Uniswap V3 (2021) introduced concentrated liquidity: LPs can restrict their capital to a specific price range (e.g., $1,800–$2,200 for ETH/USDC). This concentrates capital where trades actually happen, increasing capital efficiency ~4000× for stablecoin pairs, but requiring active management.

5. Impermanent Loss

When the external market price of one token changes relative to the other, arbitrageurs trade against the pool until its price matches. This rebalancing changes the ratio of tokens held — LPs end up holding more of the token that fell and less of the one that rose, compared to simply holding both tokens.

IL formula for price ratio change r = P_new / P_old: LP value = √r·2/(r+1) × HODL value IL = LP value / HODL value − 1 = 2√r/(r+1) − 1 Examples: Price 2× (r=2): IL = 2·√2/3 − 1 ≈ −5.7% Price 5× (r=5): IL ≈ −25.5% Price 10× (r=10): IL ≈ −42.3%

IL is "impermanent" — if the price returns to its original ratio, IL disappears. It only crystallizes when you withdraw. In volatile asset pairs, IL often outweighs trading fee income. IL is minimal for stablecoin pairs (USDC/USDT) where price ratio stays near 1.

6. Lending Protocols

Aave and Compound allow overcollateralized lending: deposit 3 ETH worth $6,000, borrow up to $4,500 USDC (75% collateral factor). Interest rates adjust algorithmically based on utilization rate (borrowed / supplied):

Utilization U = Total Borrowed / Total Supplied Borrow rate (Aave V3): U < UOptimal (80%): rate increases linearly 0 → R_slope1 U > UOptimal: rate jumps steeply (kink model) At U=100%: APY may reach 300%+ (disincentivizes all borrowing)

If collateral value falls below the liquidation threshold (e.g., borrowed/collateral > 80%), a liquidation bot repays part of the borrower's debt and seizes their collateral at a 5–10% discount. This keeps the protocol solvent without a central credit team.

Flash loans: borrow any amount from an Aave pool within a single transaction with zero collateral, as long as it is returned by the transaction's end. Used for arbitrage, self-liquidations, and collateral swaps.

7. Risks in DeFi

⚠ Financial risk warning: DeFi carries substantial and often total loss risk. Code bugs, oracle manipulation, governance attacks, and regulatory changes have resulted in billions lost. Never invest more than you can afford to lose entirely.