Introduction
Every financial action covered so far assumed you were the only one involved: holding an asset, moving it across a network. Decentralized finance (DeFi) introduces other participants: traders, liquidity providers, borrowers and lenders, all interacting through code instead of an institution.
This lesson explains what DeFi actually replaces, how a decentralized exchange fills a trade without an order book, how liquidity pools and automated market makers set prices, and the real risks, like impermanent loss, that come with participating.
By the end of this lesson you will understand how a swap executes without a company matching orders, why liquidity providers earn fees, what yield farming and staking actually mean in DeFi, and how to weigh the risk of a protocol before using it.
Smart Contracts
A smart contract is self-executing code deployed on a blockchain. Once deployed, its logic runs exactly as written: no company can quietly change the rules, freeze a specific user's funds, or make an exception behind the scenes.
Every DeFi protocol (every exchange, lending market, or yield strategy) is ultimately a set of smart contracts. Calling a protocol "trustless" means you're trusting published, inspectable code instead of a company's internal promises.
Decentralized Exchanges (DEXs)
A decentralized exchange lets people trade assets directly from their own wallets, using smart contracts to execute trades instead of a company holding custody of funds and matching orders internally.
Traditional exchanges (CEXs) use an order book, matching buyers and sellers at agreed prices. Most DEXs use a different model entirely: liquidity pools priced algorithmically by an automated market maker, covered in the next two sections.
Both models coexist across the industry: a DEX simply removes the custodian from the trading process.
Liquidity Pools
A liquidity pool is a smart contract holding two (or more) assets, deposited by liquidity providers (LPs), that traders swap against directly. Instead of matching a buyer with a seller, a trade adjusts the pool's own reserves.
In exchange for depositing assets, liquidity providers receive LP tokens representing their share of the pool, and earn a portion of the trading fees generated by every swap.
The relationship between the two reserves is exactly what an automated market maker uses to set price, covered next.
Automated Market Makers (AMMs)
An automated market maker is the formula a liquidity pool uses to price trades, based only on the pool's own reserves: no order book, and no external price feed required for the trade itself.
The most common model keeps the product of both reserves constant: as a trade removes some of one asset, the pool automatically raises its price relative to the other, in proportion to how much was removed.
This same mechanism is also what creates impermanent loss for liquidity providers, explained later in this lesson.
Yield Farming
Yield farming means actively moving assets between DeFi protocols to capture the highest available return, which can come from more than one source at once.
- Trading fees. A share of the fees generated by swaps in a pool you've provided liquidity to.
- Lending interest. Interest paid by borrowers in a lending market, covered in Module 06.
- Incentive rewards. Extra tokens a protocol distributes to attract liquidity, on top of its normal fees.
Advertised returns often combine several of these sources: and incentive rewards specifically tend to fade as a protocol matures.
Staking
In DeFi, "staking" is used in two related but distinct ways: locking a network's native coin to help secure that network, or depositing an asset into a protocol's own contract to earn rewards.
Protocol-level staking is common: depositing an LP token to earn extra incentive rewards, or locking a governance token for a period in exchange for a share of protocol fees.
Read a staking contract's terms carefully: lock-up periods and reward sources vary significantly between protocols.
Impermanent Loss
Impermanent loss is the gap between what a liquidity provider's deposited assets would be worth if simply held, versus what they're worth withdrawn from the pool, after the pool's prices have moved.
It happens because an AMM automatically rebalances a pool's reserves as prices shift: the pool ends up holding more of the asset that fell in relative price and less of the one that rose.
The loss is called "impermanent" because it can shrink back to zero if prices return to their original ratio before you withdraw: but it becomes permanent the moment you do.
DeFi Risks
Removing a company from the process doesn't remove risk: it changes what kind of risk you're taking on.
- Smart contract risk. A bug in a contract's code can be exploited, and losses are frequently irreversible.
- Impermanent loss. Covered in the previous section: a real cost of providing liquidity, not just a hypothetical one.
- Oracle risk. Protocols that rely on external price feeds can be manipulated if that feed is inaccurate or compromised.
- Rug pulls. A project's creators abandon it and drain its liquidity, sometimes by design from the start.
- Composability risk. Protocols building on top of each other means a failure in one can cascade into every protocol depending on it.
Summary
DeFi rebuilds trading, lending, and earning yield as open smart contracts instead of company-run services. Decentralized exchanges use liquidity pools priced by automated market makers, replacing a traditional order book.
Liquidity providers earn fees but take on impermanent loss when pool prices diverge. Yield farming and staking describe different ways of putting assets to work across protocols: and every one of these activities carries risk that doesn't disappear just because there's no company involved.
Glossary
- DeFi
- Decentralized finance: financial services rebuilt as open smart contracts instead of company-run services.
- Smart Contract
- Self-executing code deployed on a blockchain that defines exactly how a protocol behaves.
- DEX
- Decentralized Exchange: a trading protocol that executes trades against smart contracts instead of a company's order book.
- Liquidity Pool
- A smart contract holding two or more assets that traders swap against directly.
- AMM
- Automated Market Maker: the formula a liquidity pool uses to price trades based on its own reserves.
- LP Token
- A token representing a liquidity provider's share of a pool, used to withdraw their deposit and earned fees.
- Yield Farming
- Actively moving assets between protocols to capture the highest available return.
- Staking
- Locking an asset in a network or protocol's contract in exchange for rewards.
- Impermanent Loss
- The gap between holding assets and providing them to a pool, caused by price divergence between reserves.
- Rug Pull
- When a project's creators abandon it and drain its liquidity, leaving other participants with worthless holdings.
Quiz
Check your understanding of this lesson before moving on. Incorrect answers point you straight back to the relevant section.
1. What does a smart contract guarantee, compared to a company-run service?
Correct, well done.
Not quite, review Smart Contracts and try again.
2. How does a typical DEX differ from a centralized exchange?
Correct, well done.
Not quite, review Decentralized Exchanges and try again.
4. What determines the price of a trade in an AMM-based pool?
Correct, well done.
Not quite, review Automated Market Makers and try again.
5. Which of these is a real source of yield farming returns?
Correct, well done.
Not quite, review Yield Farming and try again.
6. What is "protocol staking," as distinct from network staking?
Correct, well done.
Not quite, review Staking and try again.
7. When does impermanent loss become permanent?
Correct, well done.
Not quite, review Impermanent Loss and try again.
9. Why doesn't removing a company from a financial service eliminate risk?
Correct, well done.
Not quite, review DeFi Risks and try again.
You scored 0 out of 9 correct.