0
0
Blockchain / Solidityprogramming~10 mins

Flash loans in Blockchain / Solidity

Choose your learning style9 modes available
Introduction

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.

You want to swap tokens quickly to take advantage of price differences.
You want to refinance a loan by paying off one and taking another instantly.
You want to perform arbitrage between different exchanges without upfront money.
You want to add or remove liquidity in a pool without holding the tokens first.
You want to execute complex multi-step operations atomically in one transaction.
Syntax
Blockchain / Solidity
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.

Examples
This function is called by the lending pool after giving you the loan. You do your work and then approve repayment.
Blockchain / Solidity
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;
}
This function requests a flash loan from the lending pool contract.
Blockchain / Solidity
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);
}
Sample Program

This contract requests a flash loan and repays it immediately without extra logic. It shows the minimal working flash loan flow.

Blockchain / Solidity
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);
    }
}
OutputSuccess
Important Notes

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.

Summary

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.