Nathaniel's blog
Back to posts

Understanding DeFi Protocols: AMMs, Lending, and Yield

Nathaniel LinFebruary 6, 20269 min read2 views
Understanding DeFi Protocols: AMMs, Lending, and Yield

Decentralized Finance — DeFi — replaces traditional financial intermediaries with smart contracts. No banks, no brokers, no business hours. Just code running on a blockchain, accessible to anyone with a wallet. Let's break down how the major protocol categories work.

Automated Market Makers (AMMs)

Traditional exchanges use order books: buyers and sellers post prices, and a matching engine pairs them. AMMs like Uniswap throw this out entirely.

Instead, liquidity providers deposit token pairs into pools. A mathematical formula determines the price:


x * y = k

where:
  x = reserve of token A
  y = reserve of token B
  k = constant product

When you swap token A for token B, you add A to the pool and remove B. The ratio changes, which changes the price. The larger the pool relative to your trade, the less price impact (slippage) you experience.

function getAmountOut(
    uint256 amountIn,
    uint256 reserveIn,
    uint256 reserveOut
) internal pure returns (uint256) {
    uint256 amountInWithFee = amountIn * 997; // 0.3% fee
    uint256 numerator = amountInWithFee * reserveOut;
    uint256 denominator = (reserveIn * 1000) + amountInWithFee;
    return numerator / denominator;
}

Liquidity providers earn a 0.3% fee on every trade. The risk? Impermanent loss — if the price of one token moves significantly, LPs would have been better off just holding.

Lending Protocols

Protocols like Aave and Compound let you lend and borrow crypto without a bank:

  1. Deposit ETH or tokens into a lending pool → earn interest

  2. Borrow against your deposit as collateral → pay interest

  3. If your collateral value drops below a threshold → liquidation

Interest rates are algorithmic, based on utilization:


utilization = totalBorrowed / totalDeposited
borrowRate = baseRate + (utilization * multiplier)

When utilization is low, rates are low (plenty of supply). When it spikes, rates increase to attract more depositors and discourage new borrows.

Flash Loans

The wildest DeFi innovation: borrow any amount with zero collateral, as long as you repay it within the same transaction. If you don't repay, the entire transaction reverts as if it never happened.

Use cases: arbitrage between exchanges, collateral swaps, self-liquidation.

Yield Farming

Yield farming combines multiple DeFi protocols to maximize returns:

  1. Deposit USDC into Aave → earn 3% APY + receive aUSDC tokens

  2. Deposit aUSDC into a yield aggregator → earn additional rewards

  3. The aggregator auto-compounds, reinvesting rewards

Yield aggregators like Yearn automate this process. You deposit tokens, and smart contracts find the best yield across multiple protocols, shifting capital dynamically.

The Risks

  • Smart contract risk: A bug in any protocol in the chain can drain funds

  • Oracle manipulation: Price feeds can be manipulated in low-liquidity markets

  • Impermanent loss: Providing liquidity to volatile pairs

  • Regulatory uncertainty: DeFi exists in a legal gray area

The Composability Advantage

DeFi's real power is composability — protocols build on each other like Lego blocks. A lending protocol uses an AMM for liquidations. A yield aggregator uses both. A derivatives protocol uses all three.

This composability is only possible because everything runs on the same blockchain, using the same token standards, with permissionless access. No APIs to apply for, no partnerships to negotiate. Just call the smart contract.

The space is still young and risky, but the architectural pattern — replacing intermediaries with deterministic code — is here to stay.

Share this post

Reactions