DeFi Explained: Decentralized Exchanges, Automated Market Makers, Lending, Stablecoins and Their Risks

Blockchain Developer 60 min min read Updated: Nov 17, 2026 Intermediate
DeFi Explained: Decentralized Exchanges, Automated Market Makers, Lending, Stablecoins and Their Risks
Intermediate Topic 1 of 3

What Is DeFi?

Decentralized finance means financial services such as trading, lending and saving built from smart contracts that anyone can use with a wallet, without a bank account.

Decentralized Exchanges and AMMs

A traditional exchange matches buyers and sellers. An automated market maker (AMM) uses a pool of two tokens and a formula to set the price.

text Pool has X units of token A and Y units of token B. Rule: X * Y = k (stays the same after each trade, apart from fees)

Python Example of the Formula

python x = 1000.0 # token A in pool y = 1000.0 # token B in pool k = x * y def swap_a_for_b(amount_a): global x, y new_x = x + amount_a new_y = k / new_x received_b = y - new_y x, y = new_x, new_y return received_b print(round(swap_a_for_b(100), 2)) # buy B with 100 A print(round(swap_a_for_b(100), 2)) # price is now worse
Result

90.91 75.76

  • Buying a token removes it from the pool, which raises its price
  • Bigger trades move the price more (slippage)
  • Real DEXs also take a fee, paid to liquidity providers

Liquidity Providers and Impermanent Loss

  • Anyone can deposit equal value of both tokens and earn trading fees
  • If the price of the tokens moves apart, the provider can end with less value than just holding. This is called impermanent loss

Lending and Borrowing

  1. Lenders deposit tokens and earn interest
  2. Borrowers must lock more collateral than they borrow (over-collateralized), for example 150 percent
  3. If the collateral value falls too low, the position is liquidated automatically and a liquidator buys it at a discount
  4. Interest rates change with how much of the pool is borrowed

Stablecoins

TypeHow it stays near one dollarRisk
Fiat-backedIssuer holds cash and short-term assetsTrust in issuer and reserves
Crypto-backedOver-collateralized crypto in smart contractsCollateral crash, liquidation
AlgorithmicCode and incentives with little real backingCan collapse. Several have

Main Risks

  • Smart contract bugs and hacks
  • Oracle manipulation
  • Liquidation in a market crash
  • Rug pulls and fake tokens
  • Regulation changes
  • Impermanent loss and slippage
Education only This lesson explains how the technology works. It is not financial advice. Never put in money you cannot afford to lose.

Practice

  1. Run the Python example with a bigger trade and watch the price impact
  2. Why must borrowers lock extra collateral?
  3. What is slippage?

Conclusion

DeFi runs trading, lending and stablecoins in smart contracts. AMMs price trades with a formula, loans are over-collateralized and every layer carries technical and market risk.

Quick Summary: meaning; AMM; Python; LPs; lending; stablecoins; risks.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators