Flash loans let you borrow money instantly without collateral, but you must pay it back in the same transaction. This helps you do quick trades or actions without needing your own funds.
Flash loans in Blockchain / Solidity
Start learning this pattern below
Jump into concepts and practice - no test required
contract FlashLoanReceiver is FlashLoanReceiverBase { function executeOperation( address[] calldata assets, uint256[] calldata amounts, uint256[] calldata premiums, address initiator, bytes calldata params ) external override returns (bool) { // Your logic here // Assume single asset at index 0 address asset = assets[0]; uint256 amount = amounts[0]; uint256 premium = premiums[0]; // Repay the loan plus fee uint256 totalDebt = amount + premium; IERC20(asset).approve(address(LENDING_POOL), totalDebt); return true; } }
This example shows the core function you must implement to receive a flash loan.
You must repay the loan plus a small fee before the transaction ends, or the whole transaction fails.
function executeOperation(address[] calldata assets, uint256[] calldata amounts, uint256[] calldata premiums, address initiator, bytes calldata params) external override returns (bool) { // Use the borrowed amount // For example, arbitrage or swap tokens // Assume single asset address asset = assets[0]; uint256 amount = amounts[0]; uint256 premium = premiums[0]; // Approve the Lending Pool to pull the owed amount uint256 totalDebt = amount + premium; IERC20(asset).approve(address(LENDING_POOL), totalDebt); return true; }
function requestFlashLoan(address asset, uint256 amount) external {
address receiverAddress = address(this);
address[] memory assets = new address[](1);
assets[0] = asset;
uint256[] memory amounts = new uint256[](1);
amounts[0] = amount;
uint256[] memory modes = new uint256[](1);
modes[0] = 0;
bytes memory params = "";
LENDING_POOL.flashLoan(receiverAddress, assets, amounts, modes, address(this), params, 0);
}This contract requests a flash loan and repays it immediately without extra logic. It shows the minimal working flash loan flow.
pragma solidity ^0.8.0; import "@aave/protocol-v2/contracts/flashloan/base/FlashLoanReceiverBase.sol"; import "@aave/protocol-v2/contracts/interfaces/ILendingPool.sol"; import "@aave/protocol-v2/contracts/interfaces/ILendingPoolAddressesProvider.sol"; import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; contract SimpleFlashLoan is FlashLoanReceiverBase { constructor(address _addressProvider) FlashLoanReceiverBase(_addressProvider) {} function executeOperation( address[] calldata assets, uint256[] calldata amounts, uint256[] calldata premiums, address initiator, bytes calldata params ) external override returns (bool) { // Example: just approve repayment for the single asset address asset = assets[0]; uint256 amount = amounts[0]; uint256 premium = premiums[0]; uint256 totalDebt = amount + premium; IERC20(asset).approve(address(LENDING_POOL), totalDebt); return true; } function startFlashLoan(address asset, uint256 amount) external { address receiverAddress = address(this); address[] memory assets = new address[](1); assets[0] = asset; uint256[] memory amounts = new uint256[](1); amounts[0] = amount; uint256[] memory modes = new uint256[](1); modes[0] = 0; bytes memory params = ""; LENDING_POOL.flashLoan(receiverAddress, assets, amounts, modes, address(this), params, 0); } }
Flash loans must be repaid in the same transaction or the whole transaction fails.
They are useful for atomic operations that need temporary liquidity.
Always test flash loans on test networks before using real funds.
Flash loans let you borrow without collateral but must be repaid instantly.
They enable quick, complex operations like arbitrage or refinancing.
Implement executeOperation to use and repay the loan within one transaction.
Practice
flash loan in blockchain?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 AQuick Check:
Flash loan = no collateral + instant repayment [OK]
- Thinking collateral is required
- Assuming repayment can be delayed
- Confusing flash loans with regular loans
Solution
Step 1: Identify the standard flash loan callback
The Aave protocol requires implementingexecuteOperationwith specific parameters.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.Final Answer:
function executeOperation(address[] calldata assets, uint256[] calldata amounts, uint256[] calldata premiums, address initiator, bytes calldata params) external returns (bool) -> Option DQuick Check:
executeOperation signature = function executeOperation(address[] calldata assets, uint256[] calldata amounts, uint256[] calldata premiums, address initiator, bytes calldata params) external returns (bool) [OK]
- Using incorrect function names
- Missing required parameters
- Wrong return type
executeOperation:
uint256 amountOwing = amounts[0] + premiums[0]; IERC20(assets[0]).approve(address(LENDING_POOL), amountOwing); return true;What does this code do?
Solution
Step 1: Understand the approval call
The code approves the lending pool contract to spend the loan amount plus premium from this contract.Step 2: Interpret the purpose
This approval is necessary so the lending pool can pull repayment automatically after the operation.Final Answer:
Approves the lending pool to withdraw the loan plus fee for repayment -> Option BQuick Check:
approve() = allow repayment withdrawal [OK]
- Confusing approve with transfer
- Thinking it sends funds to borrower
- Missing the premium fee in amount
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;
}Solution
Step 1: Analyze repayment method
The code tries to pull repayment usingtransferFromfrommsg.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 CQuick Check:
Repayment needs approve(), not transferFrom() [OK]
- Confusing transferFrom with approve
- Forgetting to approve lending pool
- Misunderstanding msg.sender role
executeOperation to profit and repay the loan?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 AQuick Check:
Arbitrage flow = borrow -> trade -> approve -> return [OK]
- Approving repayment before trading
- Trying to trade before borrowing
- Not approving repayment at all
