Flash⚡️ Loans on Ethereum are a Decentralised Finance (DeFi) tool for borrowing money from pooled assets. In essence Flash Loans, as the name suggests, are instant uncollateralised loans with a catch: (1) you take the loan, (2) use the funds and (3) repay the loan including interest in one transaction. If not all conditions are met, the transaction is reverted. Flash Loans are designed with zero counterparty risk in mind: the likelihood of those involved in the transaction of defaulting on their obligations is non-existent. If a borrower fails to repay his loan including interest the transaction never executes, thus nobody loses anything. The Flash Loan originates from pooled tokens, provided by individuals and investors who are incentivised by the interest they could earn. There are no background checks or credit ratings involved in the process.

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., docs.aave.com

The lending protocol gives loans to anyone who manages to return the money (plus interest) within the same transaction. The average Ethereum transaction takes 16 seconds, which would mean that a Flash Loan would exist no longer than that. Sixteen seconds might not sound a lot, but people have come up with many powerful use cases and hacks.

Flash Loans Use Cases

Since Flash Loans don't last long, borrowing a volatile asset such as ether doesn't pose a price risk. The most commonly used assets currently are Dai and ETH. A Flash Loan can be performed with any ERC-20 asset provided there is enough liquidity in the pool. Interesting use-cases include:

  • Arbitrage between decentralised exchanges
  • Liquidation of crypto loans on lending platforms like Compound, dYdX, etc.
  • Refinancing Crypto Loans or Maker Vaults

Arbitrage between Decentralised Exchanges

Let's take, for example, Uniswap and Kyber. A speculator would look for price discrepancies between both exchanges und will try to buy low and sell high by using a Flash Loan. By taking a Flash Loan from Aave this arbitrage would be done in one transaction time. The more liquidity you tap on the DEX the worst your average asset price will be (also known as slippage). If the Ethereum network is congested, it might take for the transaction too long to execute which would mean the price discrepancy you initially spotted no longer exists. This is part of the risk involved in trading. Of course, such actions are not performed manually. Traders write trading bots to take advantage of arbitrage opportunities.

Liquidation of Crypto Loans

Borrowers on Compound and similar platforms are required to put up collateral before they take a crypto loan. Such loans are always overcollateralised. One other aspect of taking a crypto loan is the variable interest rates. Both interest rates and asset prices could play a crucial role in the decision to close a position by quickly repaying the loan. If the price of the underlying collateral drops fast and reaches, a liquidation stage one could lose the collateral and also pay a penalty fee. By using Flash Loans, one could self-liquidate the loan by losing the collateral while avoiding the penalty fee. In the DeFi ecosystem, the liquidations penalties for loans could range between 3% and 15% depending on the platform. Avoiding the penalty fee could be a considerable cost saving. Obviously, Flash Loans also impose fees, but it is much better to pay under 1% fee for a Flash Loan rather than 15% penalty fee.

Loan self-liquidation example - step-by-step
Example loan self-lquidation

The concept of self-liquidation could also be applied to Maker Vaults (also known as CDPs). If a Vault's position approaches 150% collateralisation, the risk of automatic liquidation rises. Taking out Dai Flash Loan equal to the outstanding debt would enable investors to repay the debt, unlock the collateral and convert enough collateral into Dai on a Decentralised Exchange to repay the loan. All actions happen in one transaction. Platforms like DeFi Saver offer a simple user interface to perform basic Flash Loan actions such as Maker Vault self-liquidation.

Refinancing Crypto Loans or Maker Vaults

Anyone familiar with MakerDAO knows the risks associated with CDPs (Collateralised Debt Positions). CDPs recently got renamed to Maker Vaults. Both terms describe the same concept: (1) lock up collateral, (2) issue stablecoins, (3) fully repay stablecoins, (4) unlock collateral.

Depending on the market situation, a user might want to refinance their debt loan. There are many collateral options on the market. One could decide to put up ETH, REP, BAT or a stablecoin as collateral. Often users put up ETH as collateral which could become volatile. A Flash Loan enables the user to swap ETH for another asset, without fronting any collateral.

Flash Loans also enable the swapping of outstanding debt. Let's assume that a loan is denominated in Dai stablecoins. Not all stablecoins are alike. A bug or a market crash could force the price of Dai to move. A simple Flash Loan transaction enables the user to swap their Dai for USDT or other stablecoins without closing their position in the process.

Crypto Loan refinancing with Flash Loans
Refinancing Crypto Loan with a Flash Loan

Refinancing options are limitless. The above illustration demonstrates simplified collateral or debt swap on the same platform. Another scenario would be switching platforms by moving your loan to a different provider. The reason might be better conditions (lower interest rates). Let's take, for example, Compound and DyDx. Compound might have 7% interest on Dai while DyDx might offer 5% on USDC. A Flash Loan could repay your Compound position, unlock the collateral and move the collateral to DyDx where a USDC loan is initiated. The USDC then is converted to Dai on Uniswap or any DEX, and the Flash Loan gets repaid. The final result is that the borrower refinanced the loan from 7% Dai to 5% USDC in one transaction.

Performing a Flash Loan

Flash Loans are currently aimed at developers. There is no universal user interface where one could customise every step of a Flash Loan. The basic logic of the Flash Loan smart contract is:

  1. Call the Lending Pool requesting some amount of ETH or other available assets
  2. The Lending Pool transfers the requested amount
  3. The user adds their logic to the smart contract
  4. The smart contract checks the final balance after execution. If the balance checks out the transaction are accepted and if not - reverted

All of the above actions happen in one transaction. Aave provides developers with  Flash Loan templates.

Companies like Furucombo offer drag and drop user interface for creating custom Flash Loans. While being very helpful, these solutions can not cover all Flash Loan use cases. Performing Flash Loan transactions often require granular customisation of all steps. The only way of achieving full customisation is by writing the code by yourself. Nevertheless, such platforms are a very good place to start learning about Flash Loans.

FURUCOMBO sample user interface animation
Source: FURUCOMBO

Flash Loan Hacks

Flash Loans offer endless opportunities. This, of course, opens many attack vectors for some DeFi protocols. Any arbitrage opportunity or code mistake could result in a loss of funds. With Flash Loans hackers have millions of dollars at their disposal. One of the earlier Flash Loan hacks happened in February 2020 where a hacker used his Flash Loan to manipulate the price of wBTC (a BTC synthetic asset on Ethereum). With 112 wBTC on his disposal, he placed a large sell order on Kyber (DEX) while also shorting wBTC. The result was a profit of $350,000. In this case the attacker was able to make profit by manipulating market prices on a Decentralised Exchange.

Sometimes it is the developer's fault for not holding on to the Ethereum token recommendations. ERC-20 is Ethereum universal standard for token creation. It covers all the basics for a token to be functional while secure. Developers of the SET token failed to comply with the ERC-20 standard when they introduced a new economic model to their token: each transaction would require a 1% fee on top of the Ethereum fees. This error made $500 000 in profits for a hacker who took a simple Flash Loan worth 104 000 ETH.

References: