Performing a Flash Loan

Flash Loans are special uncollateralised loans that allow the borrowing of an asset, as long as the borrowed amount (and a fee) is returned before the end of the transaction. There is no real world analogy to Flash Loans, so it requires some basic understanding of how state is managed within blocks in blockchains.

Flash Loans are an advanced concept aimed at developers. You must have a good understanding of Ethereum, programming, and smart contracts to take advantage of them.

For developers, a helpful mental model to consider when developing your solution:

  1. Your contract calls our LendingPool contract, requesting a Flash Loan of a certain amount of a reserve.

  2. After some sanity checks, our LendingPool transfers the requested amount of the reserve to your contract, then calls executeOperation() on your contract (or another contract that you specify as the _receiver).

  3. Your contract, now holding the Flash loaned amount, executes any arbitrary operation in its code. When your code has finished, you transfer the Flash loaned amount of reserve back to our LendingPool.

  4. Our LendingPool contract compares the balance of the reserve before and after your code execution, ensuring that the balance of the reserve is exactly the same as before plus the Flash Loan fee.

    • If the balance of the reserve before and after your code execution is correct, then the LendingPool function continues to completion.

    • If the balance of the reserve after your code execution is incorrect, then the function reverts, undoing any state changes your contract made.

  5. All of the above happens in 1 transaction (hence in a single block).

Applications of Flash Loans

Aave Flash Loans are already being used in the wild. Some examples include:

  • Arbitrage between assets, without needing to have the principal amount to execute the arbitrage. Example: ArbitrageDAO.

  • Swapping collateral of loan positions, without having to repay the debt of the loan positions. Example: Collateral Swap, DeFiSaver.

  • Some other examples and ideas are listed on our blog here and here.

Implementing Flash Loans

Depending on your development environment, the following tutorials may be helpful.

If you have an existing development environment and want the most straight forward approach to implementing Flash Loans:

If you have a Truffle project and want to implement Flash Loans:

If you want to keep it simple and use Remix to test and deploy:

Other official resources

The following are resources that Aave developers have created or conducted.

Code your first flash loan @ ETHLondonUK (video)

Flash loan workshop @ ETHLondon (code)

Community resources

Also see the community contributed resources.

Last updated