Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

Flash loans in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Flash Loan Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Solidity flash loan example?
Consider this simplified Solidity code snippet for a flash loan. What will be the value of balanceAfter after the executeFlashLoan function runs successfully?
Blockchain / Solidity
contract FlashLoanExample {
    uint256 public balanceAfter;

    function executeFlashLoan(uint256 amount) external {
        uint256 balanceBefore = address(this).balance;
        // Simulate flash loan received
        uint256 loan = amount;
        // Use the loan for some operation (omitted)
        // Repay the loan
        balanceAfter = address(this).balance;
        require(balanceAfter >= balanceBefore, "Loan not repaid");
    }
}
AbalanceAfter is less than balanceBefore, causing the require to fail and revert.
BbalanceAfter is zero because the contract balance is reset after the function.
CbalanceAfter is greater than balanceBefore, indicating profit from the loan.
DbalanceAfter equals balanceBefore, meaning the loan was fully repaid.
Attempts:
2 left
💡 Hint
Think about what the require statement enforces about the contract's balance after the loan.
🧠 Conceptual
intermediate
1:30remaining
Why are flash loans considered risk-free for lenders?
Which of the following best explains why flash loans do not expose lenders to default risk?
ABecause the lender holds the borrower's private keys during the loan.
BBecause the loan is given only to trusted addresses with collateral.
CBecause the loan must be repaid within the same transaction or the whole transaction reverts.
DBecause the loan amount is always very small and insured by the protocol.
Attempts:
2 left
💡 Hint
Think about what happens if the borrower does not repay the loan in the same transaction.
🔧 Debug
advanced
2:30remaining
Identify the error in this flash loan repayment code snippet
This Solidity function attempts to repay a flash loan but causes a runtime error. What is the cause?
Blockchain / Solidity
function repayLoan(uint256 amount) external {
    // Attempt to repay loan
    (bool success, ) = lender.call{value: amount}();
    require(success, "Repayment failed");
}
AThe call is missing the function signature, so it sends plain Ether without calling repay function.
BThe amount variable is not declared, causing a compilation error.
CThe call should use transfer() instead of call() to send Ether.
DThe require statement is incorrectly placed after the call.
Attempts:
2 left
💡 Hint
Consider how low-level call works when sending Ether and invoking functions.
📝 Syntax
advanced
1:30remaining
Which option correctly defines a flash loan function in Solidity?
Select the option that correctly declares a flash loan function that takes a loan amount and a callback address.
Afunction flashLoan(uint256 amount, address callback) external { ... }
Bfunction flashLoan(uint amount, address callback) public returns (bool) { ... }
Cfunction flashLoan(uint256 amount, address callback) external payable { ... }
Dfunction flashLoan(uint256 amount, address callback) external view { ... }
Attempts:
2 left
💡 Hint
Remember that flash loan functions usually modify state and do not return values.
🚀 Application
expert
2:00remaining
How many external calls are made in this flash loan transaction?
Given this sequence in a flash loan transaction: 1. Borrower requests loan from lender contract. 2. Lender contract calls borrower's executeOperation function. 3. Borrower performs arbitrage and repays lender. How many external contract calls occur during this transaction?
A4
B2
C3
D1
Attempts:
2 left
💡 Hint
Count calls between different contracts, including lender to borrower and borrower to lender.

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

  1. Step 1: Understand flash loan basics

    Flash loans allow borrowing without collateral but require repayment in the same transaction.
  2. 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.
  3. Final Answer:

    You can borrow funds without collateral but must repay within the same transaction -> Option A
  4. Quick Check:

    Flash loan = no collateral + instant repayment [OK]
Hint: Flash loans = borrow now, repay instantly [OK]
Common Mistakes:
  • Thinking collateral is required
  • Assuming repayment can be delayed
  • Confusing flash loans with regular loans
2. Which of the following is the correct Solidity function signature to implement a flash loan callback?
easy
A. function repayLoan(uint256 amount) external
B. function flashLoan(address borrower, uint256 amount) public
C. function startLoan(address asset, uint256 amount) external returns (bool)
D. function executeOperation(address[] calldata assets, uint256[] calldata amounts, uint256[] calldata premiums, address initiator, bytes calldata params) external returns (bool)

Solution

  1. Step 1: Identify the standard flash loan callback

    The Aave protocol requires implementing executeOperation with specific parameters.
  2. Step 2: Match function signature

    function executeOperation(address[] calldata assets, uint256[] calldata amounts, uint256[] calldata premiums, address initiator, bytes calldata params) external returns (bool) matches the exact signature needed for flash loan execution and repayment.
  3. Final Answer:

    function executeOperation(address[] calldata assets, uint256[] calldata amounts, uint256[] calldata premiums, address initiator, bytes calldata params) external returns (bool) -> Option D
  4. Quick Check:

    executeOperation signature = function executeOperation(address[] calldata assets, uint256[] calldata amounts, uint256[] calldata premiums, address initiator, bytes calldata params) external returns (bool) [OK]
Hint: Flash loan callback is always executeOperation with specific params [OK]
Common Mistakes:
  • Using incorrect function names
  • Missing required parameters
  • Wrong return type
3. Given this simplified Solidity snippet inside executeOperation:
uint256 amountOwing = amounts[0] + premiums[0];
IERC20(assets[0]).approve(address(LENDING_POOL), amountOwing);
return true;
What does this code do?
medium
A. Transfers the loan amount to the borrower
B. Approves the lending pool to withdraw the loan plus fee for repayment
C. Withdraws the loan amount from the lending pool
D. Rejects the flash loan request

Solution

  1. Step 1: Understand the approval call

    The code approves the lending pool contract to spend the loan amount plus premium from this contract.
  2. Step 2: Interpret the purpose

    This approval is necessary so the lending pool can pull repayment automatically after the operation.
  3. Final Answer:

    Approves the lending pool to withdraw the loan plus fee for repayment -> Option B
  4. Quick Check:

    approve() = allow repayment withdrawal [OK]
Hint: approve() lets lending pool pull repayment [OK]
Common Mistakes:
  • Confusing approve with transfer
  • Thinking it sends funds to borrower
  • Missing the premium fee in amount
4. Identify the error in this simplified flash loan executeOperation snippet:
function executeOperation(address[] calldata assets, uint256[] calldata amounts, uint256[] calldata premiums, address initiator, bytes calldata params) external returns (bool) {
    uint256 amountOwing = amounts[0] + premiums[0];
    IERC20(assets[0]).transferFrom(msg.sender, address(this), amountOwing);
    return true;
}
medium
A. Incorrect function parameters
B. Missing return statement
C. Using transferFrom instead of approve for repayment
D. Not calling the lending pool to borrow funds

Solution

  1. Step 1: Analyze repayment method

    The code tries to pull repayment using transferFrom from msg.sender, which is incorrect.
  2. Step 2: Correct repayment approach

    Flash loans require approving the lending pool to pull funds, not transferring from msg.sender.
  3. Final Answer:

    Using transferFrom instead of approve for repayment -> Option C
  4. 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

  1. 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.
  2. Step 2: Approve repayment and finish

    After trading, approve the lending pool to pull the loan plus fee, then return true to complete.
  3. Final Answer:

    Borrow funds -> Buy low on DEX1 -> Sell high on DEX2 -> Approve repayment -> Return true -> Option A
  4. Quick Check:

    Arbitrage flow = borrow -> trade -> approve -> return [OK]
Hint: Trade first, then approve repayment [OK]
Common Mistakes:
  • Approving repayment before trading
  • Trying to trade before borrowing
  • Not approving repayment at all