Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Flash Loans Basics
📖 Scenario: You are learning how flash loans work in blockchain smart contracts. Flash loans let you borrow tokens without collateral, but you must pay back the loan in the same transaction.We will create a simple smart contract that requests a flash loan, uses it, and repays it immediately.
🎯 Goal: Build a basic smart contract that requests a flash loan from a lending pool, performs a simple action, and repays the loan in the same transaction.
📋 What You'll Learn
Create a smart contract with a function to request a flash loan
Set up a variable to store the flash loan amount
Implement the callback function that executes after receiving the loan
Print/log the success message after repaying the loan
💡 Why This Matters
🌍 Real World
Flash loans are used in decentralized finance (DeFi) to perform arbitrage, refinancing, or collateral swaps without upfront capital.
💼 Career
Understanding flash loans is important for blockchain developers working on DeFi protocols, smart contract security, and financial applications.
Progress0 / 4 steps
1
Create the flash loan contract and amount variable
Create a smart contract named FlashLoanExample and declare a public variable loanAmount of type uint256 set to 1000 ether.
Blockchain / Solidity
Hint
Use contract FlashLoanExample {} to create the contract and declare uint256 public loanAmount = 1000 ether; inside it.
2
Add the flash loan request function
Inside the FlashLoanExample contract, add a public function requestFlashLoan() that will be used to start the flash loan process. For now, just add the function signature and an empty body.
Blockchain / Solidity
Hint
Define function requestFlashLoan() public {} inside the contract.
3
Implement the flash loan callback function
Add a function executeOperation() inside FlashLoanExample that takes parameters uint256 amount, uint256 fee, and returns bool. Inside it, write code to simulate using the loan and then returning true.
Blockchain / Solidity
Hint
Define executeOperation with the correct parameters and return true inside.
4
Print success message after loan repayment
Add a public function printSuccess() that prints the message "Flash loan repaid successfully!" using emit and an event named Success. Declare the event Success(string message) at the top of the contract. Then call printSuccess() at the end of executeOperation().
Blockchain / Solidity
Hint
Declare event Success(string message); at the top. Use emit Success("Flash loan repaid successfully!"); inside printSuccess(). Call printSuccess() inside executeOperation().
Practice
(1/5)
1. What is the main feature of a flash loan in blockchain?
easy
A. You can borrow funds without collateral but must repay within the same transaction
B. You borrow funds with collateral and repay anytime
C. You borrow funds and repay after 30 days
D. You borrow funds only for staking purposes
Solution
Step 1: Understand flash loan basics
Flash loans allow borrowing without collateral but require repayment in the same transaction.
Step 2: Compare options
Only You can borrow funds without collateral but must repay within the same transaction correctly states no collateral and instant repayment.
Final Answer:
You can borrow funds without collateral but must repay within the same transaction -> Option A
Quick Check:
Flash loan = no collateral + instant repayment [OK]
C. Using transferFrom instead of approve for repayment
D. Not calling the lending pool to borrow funds
Solution
Step 1: Analyze repayment method
The code tries to pull repayment using transferFrom from msg.sender, which is incorrect.
Step 2: Correct repayment approach
Flash loans require approving the lending pool to pull funds, not transferring from msg.sender.
Final Answer:
Using transferFrom instead of approve for repayment -> Option C
Quick Check:
Repayment needs approve(), not transferFrom() [OK]
Hint: Repay by approve(), not transferFrom() [OK]
Common Mistakes:
Confusing transferFrom with approve
Forgetting to approve lending pool
Misunderstanding msg.sender role
5. You want to use a flash loan to perform arbitrage between two decentralized exchanges (DEXs). Which sequence correctly describes the steps inside executeOperation to profit and repay the loan?
hard
A. Borrow funds -> Buy low on DEX1 -> Sell high on DEX2 -> Approve repayment -> Return true
B. Borrow funds -> Approve repayment -> Buy low on DEX1 -> Sell high on DEX2 -> Return true
C. Approve repayment -> Borrow funds -> Buy low on DEX1 -> Sell high on DEX2 -> Return true
D. Buy low on DEX1 -> Borrow funds -> Sell high on DEX2 -> Approve repayment -> Return true
Solution
Step 1: Understand flash loan flow
You first borrow funds, then use them to buy low on one DEX and sell high on another to gain profit.
Step 2: Approve repayment and finish
After trading, approve the lending pool to pull the loan plus fee, then return true to complete.
Final Answer:
Borrow funds -> Buy low on DEX1 -> Sell high on DEX2 -> Approve repayment -> Return true -> Option A