A flash loan is an uncollateralized loan of where the borrower has to pay back the assets within the same transaction on a blockchain.
Or, described in a more juvenile way: a flash loan allows you to borrow a million dollars so long as you give it back immediately.
In the context of the real world, this seems rather silly. However, smart contracts enable these types of loans to be used to accomplish a lot. In this article, we will describe:
Flash loans are unsecured (uncollateralized) loans where a borrower must repay their entire loan back to a lender in the same transaction. They are unique financial products, only available in the DeFi world because smart contracts can force a user to pay the loan back immediately. In contrast, no such primitive exists in traditional finance. DeFi protocols like Aave and DyDx support flash loans. People believe protocols like MakerDAO and Uniswap support flash loans too, but technically, they are “flash mints,” which are very similar.
It’s easier to understand flash loans by understanding how they work. Let's dive in.
The mechanics are pretty straightforward. Here is an example of a flash loan of USDC (a dollar-pegged stablecoin), end to end:
flashloan
function on the smart contract, which executes the following actions either “all at once” or “none at all”:And that’s it.
All blockchain transactions are atomic
and so-called because either all of it happens or none of it does. This property also applies to flashloans
, where all flash loans are atomic. Atomic, in this context, means that if the user doesn’t repay the loan right away, they never got the loan in the first place.
Sounds like magic, right? Let’s take a look at some pseudo-Solidity code to explain it.
A user calling a flashloan
function will essentially call a function that looks like this in a smart contract. If the code hits the revert
line, the whole transaction won’t succeed or finalize, meaning the user never borrowed the money in the first place!
Smart contracts work like this: anytime a revert
statement is hit, the blockchain automatically reverts all state changes to the blockchain directly from the transaction back to its original state.
To get even more technical, most of the EVM community follows the EIP-3156 standard for supporting flash loan functionality. Where the most important function in a flash loan compatible contract looks like this:
Typically, an externally owned wallet (aka, a non-smart contract wallet, aka Metamask in our images) won’t be the receiver
in such a function call. Instead, the flash loan contract will send the borrowed tokens to another smart contract, which typically has the functionality to “do” stuff.
Below, we will expand on this for what the User's receiver contract might do.
In practice, flash loans are often used for reasons similar to what regular loans are used for. The most common is to “gain leverage” or capital for opportunities like:
We won’t go over all these, though we do recommend you look deeper into each of them. We will explain flash loan arbitrage since it’s one of the most powerful visualizers of flash loan use.
Arbitrage is a financial strategy that takes advantage of price differences of the same asset in different markets. Imagine eBay and Amazon (online resellers) sell apples for $5, while Alibaba (another online reseller) sells them for $1. If you have $100, what would be an easy way to make money?
This type of financial strategy exists in almost every market in the world, but profit margins are often very slim.
In DeFi, such opportunities exist on decentralized exchanges like Uniswap.
Now, let’s take this same scenario with the $1 and $5 apples. We only spent $100 buying apples since that’s all we had, but if we had more money, we could have made a bigger profit. And this is where flash loans come in.
Let’s run through the above scenario again, but imagine we can do it after taking out a flash loan first.
This is the power of a flash loan. Anyone, with no collateral, can exploit arbitrage opportunities.
One of the things I love about flash loans is that they equalize the financial playing field. If a small investor, or you at home reading this, find an amazing arbitrage opportunity in traditional finance, they will never do as well as a well-capitalized institution since the institution has more funds! But in DeFi, since flash loans exist, everyone has access to the same resources as a massive hedge fund! For a single transaction, anyway.
To use a flash loan, you must:
receiver
contact to execute whatever you want during the flash loanreceiver
pays back the full loan amount at the end.In the Cyfrin Updraft Security and Auditing curriculum, we go over flash loans and how to use them since they are crucial to understanding price oracle manipulation attacks (more on that later).
To run a flash loan test script using Foundry and see how it works end to end, you can take this test here, and then run:
This specific example will also show you a flash loan being used to exploit a security vulnerability!
Flash loans are one of the most important considerations DeFi developers need to address when designing projects.
A flash loan is simply a tool/mechanism to exploit a gap in a codebase, where that gap historically was only exploitable by rich users/whales. Since flash loans really “temporarily make you rich,” they make it easier for less well-capitalized users to exploit a hole where traditionally only a rich person could.
One of the common attack vectors involving flash loans is a “price oracle manipulation” attack. This is when a protocol uses an exchange’s liquidity to determine the price of an asset, and a flash loan crashes the price. You can read more about these attacks in our article here.
Often, people mistakenly classify exploits as “Flash Loan Attacks,” when often they would be more accurately classified as “Price Oracle Manipulation.”
Typically, as a protocol or smart contract, defending against such attacks is as simple as not using a decentralized exchange to get your pricing information!
Flash loans are one of the most powerful and fantastic tools for investors, and they are only possible in the smart contract world. They involve temporarily taking out a loan without collateral, credit, or KYC as long as it's atomically paid back thein the exact same transaction. Flash loans are a tool that can be used for arbitrage, liquidations, collateral swaps, and other MEV opportunities. When designing a protocol, be sure to keep flash loans in mind, or risk being exploited!