What are flash loans?

Eduardo Freitas
KogeCoin
Published in
6 min readDec 3, 2021
Photo by: CoinMarketCap

Flash Loans — borrowing millions of dollars worth of crypto with no collateral?

Definition

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 were originally introduced by the Marble protocol but were popularized by protocols like Aave and dYdX.

The catch

So what’s the catch? A flash loan has to be borrowed and repaid within the same blockchain transaction.

How transactions work

Photo by: Orienteed

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.

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 and included in ethereum blocks. We can easily see all the transactions that were included in a particular block on any popular block explorers, 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 borrow DAI for Usdc on Curve, and provide liquidity to DAI-Usdc pool on Uniswap, all in one single ethereum transaction.

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.

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

Where and how do you get a flash loan?

Photo by: Binance Academy

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 flash loans in 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 total amount borrowed plus an additional 0.09 %. The fee is split between depositors who provided the funds and integrators who facilitate the use of the Aave flash loan API.

Since by definition a flash loan must be repaid in the same transaction, those funds are once again available for any future flash loan transactions.

There is no risk of borrowers not repaying their borrowed amount because the smart contract requires it, however, this assumes the smart contract doesn’t have bugs or can’t be manipulated. Smart contract and platform risk are always present.

What are flash loans used for? Here are the three most common use cases.

Arbitrage

Photo by: Binance Academy

Arbitrage is when you buy a token on one market and then sell it on another. Since each liquidity pool represents its own “market” there are often small price differences where arbitragers can turn a profit. For example, you might notice a 1% price difference between two markets. So by buying on the lower-priced market and selling on the higher-priced market you could potentially make a guaranteed 1% return on a trade by taking advantage of the price difference of different markets. Using a flash loan for even more capital can magnify the profit of executing a successful arbitrage opportunity. However, it often isn’t that simple and there are other things to consider before considering using flash loans to arbitrage.

  • Network fees: arbitrage transactions with multiple steps can be quite expensive. Always take transaction fees into account when calculating your profits.
  • 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. The bigger the size order, the larger pool must be. If you try and trade too large of a position, the price slippage alone will erase the profit from the arbitrage.
  • Front Running: 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 pool can pick up your profitable arbitrage opportunity and send the same transaction with a higher gas fee, profiting them instead of you, and stealing your arbitrage opportunity.

Collateral Swap

Photo by: Ledger

The next use case for flash loans is a collateral swap. Let’s say you have borrowed DAI from Compound with ETH as collateral. However, you’d prefer if your collateral was a different token. You’ll need to swap your collateral from ETH into something else. This would require several steps and several transactions doing this manually, and you would have to remove your DAI from whatever profitable opportunities it is currently staked in. Instead, you could use a flash loan to do it all in one step and keep your DAI staked. Essentially you use a flash loan to close the collateral positions with borrowed funds and immediately open a new collateral position with a different asset. In this way you can swap the collateral you are using for your loan, only pay 0.09 % for the cost of the flash loan, and have the benefits of swapping collateral without having to unstake and repay the DAI you originally borrowed.

Self-liquidation

Photo by: BitcoinKE

The last 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 increase your liquidation level, and you also don’t have the DAI required to repay the loan.

Now instead of allowing the maker dollar 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 to repay the flash loan plus fees.

Therefore, you keep the rest of your ETH.

Hacking issues

Photo by: TipRanks

Flash loans can be used for both good and bad. When it comes DeFi hacks, flash loans allow the hacker 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. The problem arose from some incorrect assumptions when it comes to using Uniswap as a price oracle. Flash loans themselves aren’t bad, but they DO have the ability to magnify problems found in other protocols because a hacker can have access to enormous amounts of funds to manipulate problems found in other protocols.

Hacks always hurt at the moment, but DeFi is so incredibly new that there are many lessons yet to be learned. These types of events almost always result in strengthening DeFi as a whole, making it more and more antifragile in the future.

How Can You Use Flash Loan?

Although flash loans are predominantly used by developers, it is also possible to use them without doing any coding. Projects such as DeFi saver, and Fury combo make it possible.

You can also check Aave’s flash loans docs for further information.

If you have any other questions about flash loans, please stop by the KogeFarm Telegram or Discord communities, where you’ll always find someone willing to help you out.

--

--

Eduardo Freitas
KogeCoin

A crypto enthusiast, dedicated to promote financial freedom and education.