BTC…ETH…ETH gas…Fear & Greed…Market data · not advice

Academy School · Chapter 8 of 14

DeFi

How decentralized finance works and where its main risks come from.

10 minute lesson4 required questionsFull score to continue
Course progress0 / 14

← Academy School overview

A liquidity pool connects two token reserves, with deposits, swaps and withdrawals.

DeFi Primitives

Decentralized finance (DeFi) uses smart contracts to perform jobs that traditional intermediaries such as brokers, banks and clearing houses usually do. A smart contract is a program stored on a blockchain that holds funds and follows fixed rules when someone calls it. The building blocks are called primitives.

FunctionUsual intermediaryDeFi version
ExchangeBroker and exchangeAutomated market maker pool
LendingBank lending deskPooled lending contract with collateral rules
Margin callsClearing houseLiquidations that anyone may trigger
Dollar paymentsBank transferStablecoin token contract

What actually happens in one swap

  1. You sign an approval allowing the exchange contract to move a set amount of your token.
  2. You call the swap function, stating the minimum amount you will accept in return.
  3. The pool calculates your output from its reserves and checks its pricing rule.
  4. Both transfers settle in the same transaction. If any step fails, the whole transaction reverts and nothing moves, although you still pay the network fee.

Because every step is a public function, one transaction can chain several protocols, for example borrowing a stablecoin and swapping it in one go. This is called composability. It also means a failure in one contract can spread to every contract built on it.

Removing the intermediary does not remove trust; it moves it. You now rely on the code, on whoever holds admin keys (many protocols use an upgradeable contract), on the price oracle and on the website you use to sign.

Core idea

For any DeFi product, name the intermediary it replaces, then list what you trust instead: the contract code, its admin keys, its price oracle and the front end. If you cannot fill in all four, you do not yet understand the product.

Lending and Borrowing

DeFi lending pools match depositors with borrowers without checking anyone's identity, so collateral does the work a credit check would do. Every loan is over-collateralised: you lock more value than you borrow. If the collateral's value falls too far, anyone may repay part of the debt and claim some of the collateral at a discount. That process, liquidation, is what keeps lenders whole.

Aave, one of the largest lending protocols, defines the health factor as collateral value multiplied by the liquidation threshold, divided by debt. Below 1.0 the position can be liquidated. Collateral prices come from an oracle, so the oracle's price, not the one on your screen, decides when you cross the line.

Worked example: finding your liquidation price

You deposit 10 ETH at $3,000 ($30,000) into a market with an illustrative 80 percent liquidation threshold, and borrow 15,000 of a dollar stablecoin.

  • Health factor: 30,000 × 0.80 ÷ 15,000 = 1.60.
  • Liquidation price: debt ÷ (ETH units × threshold) = 15,000 ÷ (10 × 0.80) = $1,875, a 37.5 percent fall.
  • Borrow 20,000 instead and it becomes 20,000 ÷ 8 = $2,500, a fall of only about 16.7 percent.

When liquidation happens, the liquidator repays debt and receives collateral worth more, through a bonus set by the protocol. With a 5 percent bonus, repaying $7,500 earns $7,875 of your ETH. The $375 difference is your penalty, on top of having sold at a low price.

How rates are set

Rates follow utilisation: borrowed ÷ supplied. A common two-slope model raises the borrow rate gently up to an optimal point, then steeply. With an optimal point of 90 percent, a 4 percent first slope and a 60 percent second slope, 50 percent utilisation gives 4 × 50 ÷ 90 ≈ 2.2 percent, while 95 percent utilisation gives 4 + 60 × 5 ÷ 10 = 34 percent. The steep part pushes borrowers to repay so depositors can withdraw. Lenders earn the borrow rate × utilisation × (1 minus the reserve factor kept by the protocol).

Core idea

Before borrowing, compute your liquidation price: debt ÷ (collateral units × liquidation threshold). Then compare the fall it allows with the largest single-day fall that asset has had.

AMMs, Liquidity, and Stablecoins

An automated market maker (AMM) replaces the order book with a pool of two tokens and a pricing formula. The Uniswap v2 design keeps the product of the two reserves constant (x × y = k) and charges traders 0.30 percent, which goes to liquidity providers (LPs).

Worked example: price impact

A pool holds 100 ETH and 300,000 USDC, so k = 30,000,000 and the spot price is $3,000. You sell 10 ETH. After the fee, 9.97 ETH counts, so the pool must end with 30,000,000 ÷ 109.97 ≈ 272,801.67 USDC. You receive about 27,198.33 USDC, an average of $2,719.83 per ETH, about 9.3 percent below spot. The bigger the trade relative to the pool, the worse the price, which is why slippage limits matter.

Worked example: impermanent loss

You deposit 1 ETH and 3,000 USDC ($6,000). ETH doubles to $6,000. Arbitrage traders rebalance the pool until your share is about 0.7071 ETH and 4,242.64 USDC, worth $8,485.28. Had you simply held, you would have $9,000, so you are 5.7 percent worse off before fees. That gap is impermanent loss. Fees must exceed it, and LPs also carry the contract risk of the pool itself.

Stablecoins and the pools that hold them

Stablecoins differ by what backs them. Reserve-backed coins rely on an issuer and its banks: on 10 March 2023 Circle disclosed that $3.3 billion of USDC reserves were at the failed Silicon Valley Bank, and USDC traded below 87 cents before recovering once US authorities protected depositors. Algorithmic designs rely on market incentives: Terra's UST began losing its peg on 7 May 2022 and never recovered. When a stablecoin wobbles, traders sell it into AMM pools, so LPs end up holding more of the failing coin.

Core idea

An AMM pool always ends up holding more of whichever token the market is selling. Before providing liquidity, ask which of the two tokens you would least like to be left holding.

DeFi Security Basics

In DeFi, capital preservation comes before yield, for a simple arithmetic reason. An 8 percent yield on $5,000 earns $400 a year. A single exploit that takes the deposit wipes out twelve and a half years of that interest. Avoiding one total loss is worth more than finding a slightly higher rate.

How approvals become a weakness

The ERC-20 token standard lets you approve a spender to move up to a set amount of your tokens. Many interfaces request an unlimited amount for convenience. That permission stays in place until you revoke it, so if the spender is malicious or later compromised, it can take your tokens without asking again. The token approval and wallet drainer entry shows what these requests look like.

Two dated cases

  • BadgerDAO, 2 December 2021: attackers injected a script into the project's website that asked users for unlimited approvals to the attacker's address. Roughly $120 million was taken from user wallets. The smart contracts themselves were not exploited.
  • Euler Finance, 13 March 2023: an attacker used a flash loan and a flaw in the protocol's code to take about $197 million from an audited protocol. The recoverable funds were returned by early April, which is rare. An audit lowers risk; it does not remove it.

A routine for every interaction

  1. Open the app from a saved bookmark, never from a search advert or a direct message.
  2. Read the wallet prompt: which function, which spender address and what amount.
  3. Approve only the amount you are about to use.
  4. Try a small transaction first on any contract you have not used before.
  5. Limit how much of your money sits in any one protocol, and review and revoke old approvals regularly.
Core idea

Put capital preservation before yield: approve exact amounts, cap what any one contract could cost you, and treat an unexpected signature request as an attack until you have proved otherwise.

Further reading

On Ledgers Academy

Primary sources