3 min read

Unboxing Flash Loans

Unboxing Flash Loans

Everybody understands what regular loans are. You need to provide proof of income, reserves, or other information a bank may need to take one.

There are, of course, uncollateralized loans where you don't need to put forward any collateral i.e., you don't need to provide any asset in case you don't repay the loan.

But there are also secured loans where you need to put up collateral, especially when you want to borrow a large amount of money.

What are flash-loans?

Flash loans are a way to borrow a large amount of money from a lending protocol like Aave without collateralization, only for a certain fee paid at the end. The caveat is it needs to be returned within one transaction block. If not, the transaction will be reversed.

How does it work?

Flash loans use smart contracts; custom programs run on the Blockchain that incorporates specific rules for the program to be valid. In the case of a flash loan, the rule is the borrower must pay back the loan before the transaction ends, otherwise, the smart contract reverses the transaction, making your transaction invalid.

The transaction to flash loan contract can be divided into three parts.

  1. Receive the loan
  2. Perform actions with the loan
  3. Repay the loan

All of the above needs to happen within the confines of one transaction.

I won't go much into details on how to do this from a technical point of view. There is a great website called money-legos.studydefi.com that have section on flash loans.

Here's a code example from the official flash loan truffle-box.

pragma solidity ^0.6.6;

import "./aave/FlashLoanReceiverBase.sol";
import "./aave/ILendingPoolAddressesProvider.sol";
import "./aave/ILendingPool.sol";

contract Flashloan is FlashLoanReceiverBase {

    constructor(address _addressProvider) FlashLoanReceiverBase(_addressProvider) public {}

    /**
        This function is called after your contract has received the flash loaned amount
     */
    function executeOperation(
        address _reserve,
        uint256 _amount,
        uint256 _fee,
        bytes calldata _params
    )
        external
        override
    {
        require(_amount <= getBalanceInternal(address(this), _reserve), "Invalid balance, was the flashLoan successful?");

        //
        // Your logic goes here.
        // !! Ensure that *this contract* has enough of `_reserve` funds to payback the `_fee` !!
        //

        uint totalDebt = _amount.add(_fee);
        transferFundsBackToPoolInternal(_reserve, totalDebt);
    }

    /**
        Flash loan 1000000000000000000 wei (1 ether) worth of `_asset`
     */
    function flashloan(address _asset) public onlyOwner {
        bytes memory data = "";
        uint amount = 1 ether;

        ILendingPool lendingPool = ILendingPool(addressesProvider.getLendingPool());
        lendingPool.flashLoan(address(this), _asset, amount, data);
    }
}

To understand what's exactly is happening, I encourage you to check Aave's documentation.

Flash Loans

Use cases

The primary use case is for arbitrage. You can't purchase anything long-term with a flash loan as you need to repay the loan quickly, but you can make a profit with an arbitrage opportunity on DEXes. Imagine 1% difference in value due to arbitrage. It may not seem much, but that 1% is from a substantial amount of money.

Flash loans bundle several smart contract transactions into one, so they can reduce transaction fees as several transactions are wrapped into one.

Another use case would be swapping collateral backing the user's loan for another type of collateral without paying too much gas costs.

The most nefarious use case is manipulating the price oracle and different DeFi protocols to exploit them and steal funds from them. They even can be used to buy governance tokens and push a vote in the attacker's favor.

One of such flash loan enabled attacks was PancakeBunny attack and all forks that followed.


Flash loans are still a new concept. There isn't even a standard financial instrument that would be close to what flash loans offer. Use cases are somewhat limited, but people are finding new, creative ways to use them. There's no doubt the flash loan is an innovative concept that will only grow and lie the foundations for new applications in DeFi.


Thanks for reading, and if you like my writing, you can subscribe to my blog to receive the daily newsletter as I'm currently in the middle of 100 days of blogging challenge. Subscription box below 👇

If the newsletter is not your thing, check out my Twitter @adrianhetman, where I post and share exciting news from the Blockchain world and security.

See you tomorrow!