New to blockchain software development? Read my beginners guide here

Guide to flash loans - full walkthrough

Created on August 2022 โ€ข Tags: ethereumsolidityguides

A guide to flash loans with a full walkthrough

Table of Contents for Guide to flash loans - full walkthrough

Flash loans are a very interesting topic in smart contracts.

They let you borrow an amount (tokens, or sometimes NFTs) for a fee. They have to be returned by the end of the transaction. If it is not returned then the entire transaction will fail.

A very basic example of what a flash loan is, and how it works

This article assumes you know the basics about how transactions work on Ethereum, and how a smart contract can call another contract.

Flash loans will often be set up and used in your Solidity programs. The basic idea follows this pattern:

  • You call a function on a smart contract
  • that smart contract function interacts with the flash loan provider, and gets a loan.
    • For example, maybe it is now the owner of 100 APE tokens
  • Your smart contract function can then do whatever it needs to do, using its balance of 100 APE coin
  • But before the transaction ends, your function must return 100 APE coin to the flash loan provider. You also have to pay a fee to the flash loan provider.
  • If the loan is not fully repayed, the whole transaction will fail.

You can only borrow with a flash loan for a maximum time of one transaction. Either you borrow and fully repay it (with interest), or the whole transaction fails.

In depth walk through of what a flash loan is

  • the โ€˜flashโ€™ part of flashloan refers to the speed - the loan is taken out and repayed within a single transaction. That means you are bound by the amount of gas you can spend.
  • They are just smart contracts, normally written in Solidity.
  • You take out the loan by a function call, and you will get the eth or token transferred to your wallet. You can then use it - and return it by the end of the transaction. If it is not returned, the transaction will be reverted
  • You also have to pay a fee for taking out the flash loan. Typical fee is around 0.1%
  • People will set up flash loans to do things like arbitrage between different exchanges. For example if a token is trading at $10 on exchange A, but $12 on exchange B you could take out a huge flash loan, pocket the difference and just pay the 0.1% flash loan fee.
  • One of the more popular platforms to use flash loans is Aave.
    • Aave is on Ethereum, Avalanche, Optimism, Fantom, Polygon, Artibtrum and more.
    • Aave charges 0.09%
  • There is no guarantee that you can always take out a loan - it depends on the liquidity. If there is no ETH to lend out, you canโ€™t borrow it. This is less of a problem for ETH, but becomes a problem for lower volume ERC20 tokens.

What are the use cases for flash loans

  • Arbitrage - If you realise that you can trade between two tokens at different prices on different exchanges, you can use this to make a profit. If you use flash loans you can potentially borrow a lot more, and also make more profit (even after paying the fee for the flash loan).
  • Debt refinancing If you have a collateralized loan, you may realise another platform has a better rate. You can use a flash loan to pay off your current (more expensive) loan, and then immediately take out the cheaper loan.

This post is incomplete and a work-in-progress
I'll update it soon and flesh it out with more info!

Spotted a typo or have a suggestion to make this crypto dev article better? Please let me know!

See all posts (70+ more)

See all posts (70+ more)

Was this post helpful? ๐Ÿ“ง

If you liked this content and want to receive emails about future posts like this, enter your email. I'll never spam you.

Or follow me on @CryptoGuide_Dev on twitter

By using this site, you agree that you have read and understand its Privacy Policy and Terms of Use.
Use any information on this site at your own risk, I take no responsibility for the accuracy of safety of the information on this site.