Flash Loans 101: Features, Types, and How To Prevent Flash Loan Attacks

Umbrella Marketing Team
Umbrella Network
Published in
7 min readJun 15, 2021

A flash loan is a relatively new type of unsecured lending in the world of decentralized finance. Initially pioneered by Aave back in early 2020, it has since grown in popularity and become available across many lending protocols.

Many crypto industry experts have become sincere advocates of flash loans as they provide innovative ways to arbitrage, execute quick trades, and provide a host of other novel features that previously were not possible in traditional finance.

Most of us are familar with a traditional loan, where a lender loans out money to a borrower, and is then paid back after a fixed period of time and with a set premium or interest in addition to the principal. A flash loan has the same fundamentals but includes several unique traits:

  1. It is an unsecured loan, meaning that the borrower does not need to secure it with any assets or deposit to get the loan. Furthermore, unlike traditional unsecured loans, there is no credit check process.
  2. All flash loans are done via smart contracts on the blockchain, and with the provision that if the borrower does not return the funds within a single blockchain transaction, the lending process is reversed as if it never happened. This key differentiator is why borrowers are able to take out flash loans without any collateral or credit checks, as it removes any risk to the lender.
  3. The lending process is instantaneous, so once the loan is extended, the borrower must call other smart contracts to utilize the flash loan to try to execute almost instant trades before returning the funds back before the single block transaction ends, usually within a few seconds.

Given the advantages of zero risk to the lender on their capital, and no collateral or credit check obligations for the borrower, it’s no wonder that flash loans have grown so rapidly across DeFi since last year.

Let us look at a couple of flash loan use cases. In the first instance, suppose a user has borrowed DAI, using ETH holdings as collateral. If the price of ETH starts going down, the value of the collateral dips, and the user faces the threat of having his loan liquidated at some point.

To tackle this scenario, the user can leverage a flash loan. Through this flash loan feature, the borrower can swap fluctuating ETH for a stablecoin. Instantly, the value of the collateral becomes steady, negating any scope of liquidation.

While this use case pertains to the category of collateral swapping, you can also use flash loans to swap out your debt. Consider the earlier example where you had borrowed funds in DAI.

If there is a sudden rise in the demand for DAI loans, the interest rate on DAI will increase more than what you had to pay earlier. To avoid paying more interest payments, you can swap the DAI for any other less borrowed currency, safeguarding your investment capital.

Flash Loan Attacks

In principle, flash loans allow a user to borrow as much as they want without requiring any collateral. A borrower can, therefore, take thousands of dollars, even hundreds of thousands of dollars, worth of Ether as a loan, without any collateral or KYC process.

This has led to the rise of flash loan attacks, where malicious agents take out large flash loans and then use those funds to manipulate the market and exploit various DeFi protocols to run off with significant profits, and often at the expense of regular investors and platform users.

These attackers thread the borrowed flash loan amounts through a series of vulnerable on-chain protocols to fetch hundreds of thousands of dollars in stolen assets before paying back their loans.

There have been multiple flash loan attacks over the past year, and the frequency of these attacks seem to be increasing.

We will look at a few of them in detail to understand the phenomenon better.

Examples of Flash Loan Attacks

The first flash loan attack occurred in 2020, where a borrower took an ETH flash loan using the DeFi lending protocol dYdX. They then split the loan in two and sent it to the lending platforms Compound and Fulcrum.

On Fulcrum, part of the flash loan was leveraged to short ETH against WBTC. Fulcrum went on to acquire WBTC from Uniswap, a popular decentralized exchange, via another DeFi protocol known as Kyber.

Since Uniswap was low on WBTC liquidity, the price of the asset went up. As a result, Fulcrum paid a higher price than usual to acquire WBTC.

Meanwhile, the borrower also took a WBTC loan from Compound and flipped it on Uniswap, where the price of WBTC had already pumped up.

By manipulating multiple protocols and artificially hiking the price of WBTC, the borrower made a significant profit — to not only repay his ETH loan but also pocket an excess ETH profit.

While the borrower made significant gains, Fulcrum got tricked into acquiring WBTC for a much higher above market price.

In another separate flash loan attack, an attacker exploited and manipulated the bZX protocol again, on which Fulcrum was built. First, the borrower took a part of his ETH loan and put a large order to buy sUSD on Kyber.

The smart contracts recognize currencies and their prices, but they do not comprehend that stable currencies are pegged against the USD. The large order created an upsurge in the prices of sUSD, spiking up to $2 apiece, going against the very foundation of what a stablecoin should be able to do.

Since sUSD had double the purchasing power, the borrower leveraged it to borrow a much larger ETH loan than what he had taken earlier. Subsequently, he paid back the first ETH loan and fled with the rest.

In this instance, the user fooled Kyber into believing that sUSD could be priced substantially higher than 1 USD.

How to Prevent these Flash Loan Attacks?

Since these attacks take advantage of DEXs believing their own or a singular price feed, which can be manipulated by placing a huge order for a currency, it is prudent to leverage decentralized pricing oracles for ascertaining the correct price of an asset.

There are multiple ways a dApp can protect itself from flash loan attacks and some of the most common ones are:

Decentralized Oracles — Easily the safest option is the usage of decentralized oracles that utilize multiple sources to find out the ‘true price’. Some decentralized oracles, such as Umbrella Network — our own, go a step further and ensure the reliability of data by committing them on the Blockchain.

This means that if a bad actor tries to perform a flash attack on a dapp that sources its feeds from a decentralized oracle, the price manipulation will fail, the transaction time will elapse, and the entire transactions would reverse — unprocessed.

High-Frequency Pricing Updates — This is a simple fix on paper but could be more expensive in practice. Here, we simply increase the frequency of the number of times the liquidity pool queries an oracle for a fresh price. The logic is that with more updates, the price of a token within the pool would get updated faster and invalidate the price manipulation.

Time Weighted Average Pricing — The usual practice has been to use the mean (or more recently, the median) to calculate the price in the liquidity pool. However, TWAP suggests using the averaging of prices across multiple blocks.

This helps offset the flash loan attacks as the entire sequence of attacking transactions need to be processed within the same block but the TWAP cannot be manipulated without manipulating the entire Blockchain.

Another strategic suggestion to prevent such attacks has been to use two transaction blocks for the transaction cycle rather than one.

Assumably, it would make the process more complex and dissuading for the attacker. However, it also runs the risk of taking a toll on the DeFi UI.

Some protocols have also integrated flash loan attack detection tools, helping in prompt identification, swift response, and neutralization. However, it is difficult to confirm how effective these tools are unless there are not enough examples of averted attacks.

Conclusion

DeFi is still an emerging field. It is going through many innovations and rapid radical shifts in the ways it operates. Quick changes, even if innovative, often lead to overlooking the critically vulnerable.

There will continue to be loopholes that attackers exploit, but with each incident, the prevention mechanisms would also become stronger as the overall ecosystem evolves.

While there are ways to help mitigate the risks such as using decentralized oracles, higher frequency pricing updates, or TWAP strategies, it will still take some time before flash loans become less of a potential tool for exploit as the overall DeFi industry adopts more effective methods to combat it.

--

--