decentralized finance education

Flash Loans Explained (Aave, dYdX)

So what are flash loans all about? And how can they be used to borrow millions of dollars worth of crypto with no collateral? You’ll find answers to these questions in this article.

Introduction 

A flash loan is a feature that allows you to borrow any available amount of assets from a designated smart contract pool with no collateral. Flash loans are useful building blocks in DeFi as they can be used for things like arbitrage, swapping collateral and self-liquidation.

Flash loans, although initially introduced by the Marble protocol, were popularised by Aave and dYdX. 

So, what’s the catch? 

A flash loan has to be borrowed and repaid within the same blockchain transaction. 

The concept of a transaction on a blockchain such as Ethereum is no different to the traditional definition of a transaction in computer science. 

Transactions 

A transaction represents a set of operations that must be executed in an atomic way – either all the steps are executed or the transaction is rolled back and none of the steps take place. 

Let’s take a simple database transaction as an example. If you’re already familiar with this concept feel free to skip this part. 

Imagine a database table that represents users’ balances. 

Alice’s balance is $1000 and Bob’s balance is $500. Alice sends Bob $500. In this case, we, of course, have to subtract $500 from Alice’s balance and add $500 to Bob’s balance. So we start writing our database update statements. 

This is cool, but what happens if the first update executes but the second one fails for some reason? (maybe id=2 is not present in the system and we made a mistake thinking it’s Bob’s id)

If this happens we would end up in an inconsistent state with Alice’s balance equal to $500 and Bob’s balance also equal to $500. 

To avoid situations like this we have to use transactions. 

In the above situation, in SQL, we just need to wrap both statements with BEGIN; and COMMIT; keywords. Once we do that, we can safely assume that either both statements are executed correctly or none of them are – leaving Alice’s and Bob’s balances untouched. We often say that if different steps are a part of the same transaction they are atomic, hence indivisible – all or nothing. 

Ethereum Transaction

When it comes to Ethereum, every common operation, such as sending ETH, sending ERC20 tokens and interacting with smart contracts are executed within a transaction scope. 

Transactions are grouped together and included in Ethereum blocks. We can easily see all of the transactions that were included in a particular block on any popular block explorer, such as Etherscan. 

One Ethereum transaction can consist of multiple steps, for example, you could supply ETH and borrow DAI on Compound, swap half of your borrowed DAI for USDC on Curve and provide liquidity to DAI/USDC pool on Uniswap – all in one single Ethereum transaction. Of course, if any of these steps result in an error, the whole transaction will be rolled back and none of the steps will take place. Remember – you will still pay gas fees, even for failed contract executions.  

The number of steps in a single transaction is only bounded by the gas cost, so although, in theory, you could create a valid transaction with thousands of steps, realistically it’d be rejected because of the max gas cost limit per block. 

Flash Loans  

Now, let’s dive a little bit deeper into flash loans. 

First of all, the most important part of executing a flash loan is to find a flash loan provider. Projects such as Aave or dYdX developed smart contracts that allow defi users to borrow different coins from a designated pool under the condition that they are repaid within the same Ethereum transaction. There is usually a fixed cost associated with using flash loans. Aave contracts, for example, require the borrower to return the initial amount + an extra 0.09% of the borrowed amount. The fee is split between depositors, who provide the funds that can be borrowed, and integrators, who facilitate the use of Aave’s flash loan API. A part of this fee is also swapped to AAVE tokens and burned. 

Once the amount is borrowed from the lending pool it can be used for any other arbitrary actions assuming that at the end of the chain of different steps, the initial flash loan is repaid.

Because the loan has to be repaid within one transaction, there is no risk of borrowers not repaying their borrowed amount. The only risk is the always present smart contract and platform risk. 

Flash loans are becoming more and more popular with some of the users borrowing as high as 14M DAI on Aave. 

Use Cases

There are 3 most common use cases for flash loans. 

Arbitrage. Flash loans can magnify the profit of executing a successful arbitrage opportunity.

Let’s imagine that there is a price discrepancy in the DAI/USDC pools between Uniswap and Curve. You can trade 1 DAI for 1 USDC on Curve, but you only need 0.99 DAI to buy 1 USDC on Uniswap. Now you can try to execute the following arbitrage. 

  1. Borrow 100,000 DAI from Aave via flash loan 
  2. Swap 100,000 DAI for USDC on Uniswap and receive 101,010 USDC
  3. Swap 101,010 USDC for 101,010 DAI on Curve 
  4. Repay initial 100,000 DAI + 0.09% fee = 100,090
  5. Profit 920 DAI 

This looks nice, but we have to take a few extra things into consideration

  1. Network fees – arbitrage transactions with multiple steps can be quite expensive. Always take transaction fees into account when calculating your profits. 
  2. Price Slippage – always calculate how much price slippage you’ll experience while executing your order (a hint – it depends on the size of your order and the liquidity present in the liquidity pool)
  3. Frontrunning – there is a high chance that someone else will spot the same opportunity and will manage to get their transaction mined ahead of you. On top of that, bots that monitor the mempool can pick up your profitable arbitrage opportunity and send the same transaction with a higher gas fee, profiting them instead of you, basically stealing your arb opportunity.

The next use case for flash loans is a collateral swap

Let’s say you have borrowed DAI from Compound with ETH as collateral. You can swap your collateral from ETH to, for example, BAT in the following way:

  1. Take a flash loan in DAI to cover the amount of DAI that was borrowed
  2. Repay your Compound loan with borrowed DAI 
  3. Withdraw your ETH 
  4. Swap your ETH for BAT on Uniswap 
  5. Supply BAT as collateral on Compound 
  6. Borrow DAI against your BAT collateral 
  7. Repay flash loan with borrowed DAI + fee 

Congrats, you just swapped your collateral from ETH to BAT and paid 0.09% of the borrowed amount for this. 

The last but not least example is self-liquidation.

Imagine the following scenario: You have a loan in DAI on Compound with ETH as collateral. The ETH price keeps going down and you’re approaching the liquidation level. You also don’t have or don’t want to deposit more ETH to decrease your liquidation level and you also don’t have the DAI required to repay the loan. Now, instead of allowing the MakerDAO contract to liquidate your collateral and charge you the liquidation fee, you can take the following steps:

  1. Take a flash loan for the amount of DAI that you owe
  2. Repay your DAI loan and withdraw your ETH
  3. Swap enough ETH to DAI in order to repay the flash loan + fees 
  4. Keep the rest of your ETH 

These were the 3 most common use cases for flash loans. Of course, the concept of flash loans is quite new and there are still a lot of use cases to be discovered in the future. 

Flash Loans and DeFi Hacks

Flash loans, similarly to crypto, can be used for both good and bad. When it comes to the latter, flash loans were used in most of the recent defi hacks and allowed hackers to magnify their potential profits as they do not require any upfront funds. 

One of the most famous hacks was the BzX hack where a flash loan was used to manipulate the Uniswap oracle price. As usual, the problem was not in the use of flash loans, but rather in some incorrect assumptions when it comes to using Uniswap as a price oracle.

Events like these are sometimes costly for the people affected by them, but they usually result in the strengthening of the whole defi ecosystem, making it more and more antifragile in the future.  

Coding & Furucombo 

Although flash loans are predominantly used by developers, it is also possible to use them without doing any coding. Projects such as Collateralswap, Defisaver or Furucombo make it possible. 

You can also check out our guides on how to code a flash loan with Aave here and how to use Furucombo here. So no matter if you are familiar with coding or not – you can find something that works for you. 

So, what do you think about flash loans? Have you ever used them? And do you think they are good for the defi ecosystem?

If you enjoyed reading this article you can also check out Finematics on Youtube and Twitter.

Leave a comment

Your email address will not be published. Required fields are marked *