DeFi changes how money works by using computers to do banking without banks. It makes finance open and fair for everyone.
Why DeFi reimagines finance in Blockchain / Solidity
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Blockchain / Solidity
DeFi uses smart contracts on blockchains like Ethereum.
Example smart contract function:
function transfer(address to, uint amount) public {
require(balance[msg.sender] >= amount);
balance[msg.sender] -= amount;
balance[to] += amount;
}Smart contracts are like computer programs that run on the blockchain automatically.
They make sure rules are followed without needing a bank or middleman.
Examples
Blockchain / Solidity
function deposit() public payable {
balance[msg.sender] += msg.value;
}Blockchain / Solidity
function borrow(uint amount) public {
require(collateral[msg.sender] >= amount);
loans[msg.sender] += amount;
balance[msg.sender] += amount;
}Sample Program
This simple DeFi contract lets users deposit money and send it to others without a bank.
Blockchain / Solidity
pragma solidity ^0.8.0; contract SimpleDeFi { mapping(address => uint) public balance; function deposit() public payable { balance[msg.sender] += msg.value; } function transfer(address to, uint amount) public { require(balance[msg.sender] >= amount, "Not enough balance"); balance[msg.sender] -= amount; balance[to] += amount; } }
Important Notes
DeFi uses code to replace banks and middlemen.
It works on blockchains, which are like shared computers everyone trusts.
Smart contracts run automatically and cannot be changed once live.
Summary
DeFi makes finance open and automatic using smart contracts.
It removes banks and lets people control their own money.
DeFi works 24/7 and is available to anyone with internet.
Practice
1. What is one main way DeFi changes traditional finance?
easy
Solution
Step 1: Understand DeFi's core feature
DeFi uses technology to remove banks and middlemen from financial processes.Step 2: Compare options to this feature
Only By removing banks and middlemen matches this key idea; others contradict it.Final Answer:
By removing banks and middlemen -> Option AQuick Check:
DeFi removes middlemen = C [OK]
Hint: Think about who controls money in DeFi [OK]
Common Mistakes:
- Thinking DeFi needs physical banks
- Assuming DeFi limits users by location
- Confusing DeFi with cash-only systems
2. Which of the following is the correct way to describe a DeFi smart contract?
easy
Solution
Step 1: Define smart contract in DeFi
Smart contracts are computer programs that run automatically on blockchain.Step 2: Match options to this definition
Only self-executing code on blockchain matches this definition.Final Answer:
A self-executing code on blockchain -> Option CQuick Check:
Smart contract = code on blockchain [OK]
Hint: Smart contracts run automatically, no paper needed [OK]
Common Mistakes:
- Thinking smart contracts are physical papers
- Confusing manual approval with automation
- Assuming banks sign smart contracts
3. Consider this simple DeFi smart contract code snippet in Solidity:
What will
contract SimpleBank {
mapping(address => uint) balances;
function deposit() public payable {
balances[msg.sender] += msg.value;
}
function getBalance() public view returns (uint) {
return balances[msg.sender];
}
}What will
getBalance() return after a user sends 2 ether to deposit()?medium
Solution
Step 1: Understand deposit function behavior
The deposit function adds the sent ether (msg.value) to the sender's balance in wei (smallest ether unit).Step 2: Understand getBalance return value
getBalance returns the balance in wei, so sending 2 ether means balance is 2 * 10^18 wei.Final Answer:
2 ether in wei units -> Option AQuick Check:
Balance returned in wei units = D [OK]
Hint: Ether amounts are stored in wei (smallest unit) [OK]
Common Mistakes:
- Assuming balance returns ether directly
- Thinking deposit is not payable
- Confusing zero balance with deposit amount
4. This Solidity code snippet aims to let users withdraw their balance:
What is the main issue with this code?
contract SimpleBank {
mapping(address => uint) balances;
function withdraw(uint amount) public {
require(balances[msg.sender] >= amount);
payable(msg.sender).transfer(amount);
balances[msg.sender] -= amount;
}
}What is the main issue with this code?
medium
Solution
Step 1: Analyze withdrawal order
The code sends ether before updating the balance, which can allow reentrancy attacks.Step 2: Identify security best practice
Best practice is to update balance before sending ether to prevent reentrancy.Final Answer:
It subtracts balance after transfer, risking reentrancy attack -> Option BQuick Check:
Transfer before update risks reentrancy = B [OK]
Hint: Update balance before sending funds to avoid attacks [OK]
Common Mistakes:
- Ignoring reentrancy risks
- Thinking transfer vs send is main issue
- Missing importance of deposit function here
5. You want to create a DeFi app that lets users stake tokens and earn rewards automatically. Which combination best reimagines finance using DeFi principles?
hard
Solution
Step 1: Identify DeFi principles
DeFi automates finance tasks with smart contracts and removes middlemen.Step 2: Match options to these principles
Only the option using smart contracts to automate without intermediaries matches.Final Answer:
Use smart contracts to automate staking and rewards without intermediaries -> Option DQuick Check:
Automation + no middlemen = A [OK]
Hint: Choose automation and no middlemen for DeFi apps [OK]
Common Mistakes:
- Thinking manual bank visits fit DeFi
- Assuming paper contracts are needed
- Limiting access contradicts DeFi openness
