You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Decentralised Finance (DeFi) is a financial ecosystem built on blockchain that aims to recreate and improve upon traditional financial services — lending, borrowing, trading, insurance — without intermediaries. Tokens are the building blocks of DeFi.
DeFi replaces traditional financial intermediaries (banks, brokers, exchanges) with smart contracts:
| Traditional Finance | DeFi Equivalent | Example |
|---|---|---|
| Bank savings account | Lending protocol | Aave, Compound |
| Stock exchange | Decentralised exchange (DEX) | Uniswap, SushiSwap |
| Loan officer | Collateralised lending | MakerDAO |
| Insurance company | Decentralised insurance | Nexus Mutual |
| Asset manager | Yield aggregator | Yearn Finance |
| Derivatives exchange | Decentralised derivatives | dYdX, GMX |
Ethereum token standards define how tokens behave and interact with wallets and protocols.
ERC-20 is the standard for fungible (interchangeable) tokens:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20 {
constructor() ERC20("MyToken", "MTK") {
_mint(msg.sender, 1000000 * 10 ** decimals());
}
}
| Function | Description |
|---|---|
totalSupply() | Returns the total token supply |
balanceOf(address) | Returns the token balance of an address |
transfer(to, amount) | Transfers tokens to an address |
approve(spender, amount) | Authorises a spender to transfer tokens on your behalf |
transferFrom(from, to, amount) | Transfers tokens using an allowance |
allowance(owner, spender) | Returns the remaining allowance |
ERC-721 is the standard for unique, non-fungible tokens:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
contract MyNFT is ERC721 {
uint256 private _nextTokenId;
constructor() ERC721("MyNFT", "MNFT") {}
function mint(address to) public {
uint256 tokenId = _nextTokenId++;
_safeMint(to, tokenId);
}
}
| Feature | ERC-20 | ERC-721 |
|---|---|---|
| Fungibility | Fungible (1 token = 1 token) | Non-fungible (each token is unique) |
| Divisibility | Divisible (e.g. 0.5 tokens) | Indivisible (whole tokens only) |
| Use cases | Currencies, governance tokens | Digital art, collectibles, game items |
| Identification | Identified by amount | Identified by unique token ID |
ERC-1155 combines fungible and non-fungible tokens in a single contract:
| Feature | Description |
|---|---|
| Batch operations | Transfer multiple token types in one transaction |
| Gas efficiency | Significantly cheaper than deploying separate ERC-20 and ERC-721 contracts |
| Semi-fungible | Tokens can be fungible, non-fungible, or semi-fungible |
| Use cases | Gaming items, event tickets, mixed asset portfolios |
Traditional exchanges use order books. DeFi introduced Automated Market Makers — smart contracts that hold liquidity pools and price assets algorithmically.
x * y = k
Where:
x = amount of Token A in the pool
y = amount of Token B in the pool
k = constant (invariant)
Example:
Pool: 1000 ETH * 2,000,000 USDC = 2,000,000,000 (k)
Swap 10 ETH for USDC:
New x = 1010 ETH
New y = k / 1010 = 1,980,198 USDC
USDC received = 2,000,000 - 1,980,198 = 19,802 USDC
Effective price: ~1,980 USDC per ETH (with slippage)
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.