How do Flash Loans Work?

Read our Advertiser Disclosure.
Contributor, Benzinga
April 25, 2022
verified by Ryan McNamara

In many ways the development of decentralized finance (DeFi) has been closely tied to things that traditional finance has done in the past, with the primary marginal benefit of reducing censorship; adding public, transparent governance; and lifting geographic and economic barriers to access for certain actions. While these are certainly important advances, in some sense they seem to fall short of the revolutionary advances promised with the advent of Web3 and DeFi. 

However, DeFi’s unique utility has been significantly expanding since 2020, with concepts like algorithmic stablecoins with user-facing arbitrage incentives, decentralized exchanges – also known as DEXs – liquidity pools, and token airdrops changing the conversation about how DeFi compares to traditional finance. One particular concept leverages the speed, programmability and abundance of on-chain projects: flash loans.

Flash Loans Explained

Flash loans are uncollateralized lending positions that leverage smart contracts through a lending platform to execute within a single block. What this means is that anybody can leverage large amounts of cryptocurrency capital for a single transaction – around 10 seconds – as long as the capital plus an additional fee is paid back at the end of the transaction, as guaranteed by a smart contract. If you fail to pay back your loan, the single transaction that the entire flash loan is contained in doesn’t happen, so it’s as if you’d never gotten the loan in the first place.

Sounds pretty crazy, right? As expected, the notion of trustless split-second loans with huge power and no collateral flies in the face of every bit of traditional finance intuition and is thus quite controversial before use cases even enter the picture. These uncollateralized loans (or unsecured loans) utilize the blockchain network to your advantage, but there can be drawbacks.

What’s the Point of Flash Loans?

With the massive power inherent to the concept of a flash loan, there are quite a few use cases with a range of effects to the DeFi space at large. 

Arbitrage

Arbitrage is defined as taking advantage of a market’s inefficiencies in pricing an asset to buy and sell the asset at differing prices without holding the asset for a significant period of time. While arbitrage opportunities can exist without flash loans, the price-stabilizing impact of arbitrageurs is amplified massively by the amount of capital that an arbitrageur can command using a flash loan. The process here is relatively simple: identify an asset with pricing discrepancies between two marketplaces, take out a large enough flash loan such that you can make a profit higher than the flash loan fees from the discrepancy without exceeding the current liquidity of either market. Here’s the very first example of a flash loan powered arbitrage transaction from ArbitrageDAO, which borrowed 3,100 DAI and made around 6 DAI in profit. While this doesn’t sound like very much, keep in mind that the trade took place in a few seconds using a bot.

Collateral Swaps

Flash loan contracts can also be used in conjunction with other lending protocols to safely switch the type of collateral that a loan position is backed by. Suppose you take out a typical, collateralized loan position using a potentially volatile asset like Ethereum or Bitcoin as your collateral. If the prices of either of those were to change, your collateral might deviate from the value needed to secure the funds that you took the loan out for, which isn’t ideal. To resolve this, you could take out a flash loan for the same price as your original collateral, which you could then swap to DAI and deposit as collateral. From here, you can withdraw my original collateral and pay off the flash loan, thus changing the collateral on your original loan position with minimal cost. Collateral Swap is a user-friendly service that uses flash loans to do exactly this. 

Self-Hedging and Self-Liquidation

In a vaguely similar sense to collateral swaps, flash loans can also interact with other lending positions so as to wind them down or close them. Generally speaking, lending positions in the DeFi space get liquidated past a specific collateralization, which incurs a 15% liquidation penalty fee. Because of this, it makes sense to liquidate your own position using a flash loan and to use your locked collateral to pay back the flash loan, which means that you’d ultimately dodge the debt but would need to eat the flash loan fee in order to avoid the liquidation penalty. 

This flash-loan-powered loan wind-down can also be done to a partial extent to self-hedge a risky position and can be automated so as to maintain a target collateralization ratio.

High-Profile Flash Loan Exploits

If you’ve read this far, then it’s clear that flash loans are insanely powerful, giving you more power to use the wealth you’ve built up through trading platforms. This fact, when combined with their relative newness and obscurity, means that they can be used against badly designed and audited protocols or smart contracts to do some serious damage. Check out this example of a flash loan attack and how these ideas are often expounded upon by other hackers.

Here is an archetypal example of a flash loan ttack, wherein malicious actors used dYdX to take out a flash loan of 70,000 ETH which was then used on the Origin Protocol to inflate the supply of their token, OUSD, through a bug. Because of this supply shock, the attacker’s OUSD tokens could be swapped using Sushiswap and Uniswap for a net $7 million profit. The 70,000 ETH was finally paid back along with the 0.000000000000000002 ETH flash loan fee. 

The recent Beanstalk attacks on flash loans that drained the protocol of $182 million were done similarly through the use of a nearly $1 billion flash loan from the AAVE protocol. This temporary capital was deployed to take 67% of the available DAO governance tokens (digital assets) and to subsequently pass a governance proposal to drain the protocol’s stablecoin collateral pool, resulting in an $80 million net profit for the attacker.

Another class of exploit that is made possible using flash loans is NFT wash trading. Wash trading – which is explicitly illegal in traditional finance – is the practice of falsely trading an asset from a seller to themselves – a practice that’s especially easy to do in crypto by individuals having multiple wallets and the ability to command vast amounts of capital for single transactions through flash loans. A motivation for doing this is to artificially increase the perceived value of an asset – or a collection of assets. It follows pretty naturally from these facts that flash loans work well in NFT wash trading, inflating the perceived value of some NFTs and collections massively. 

How to Get a Flash Loan

By virtue of being new and somewhat code-heavy on the backend, flash loans aren’t terribly user friendly just yet, and most solutions rely on the command line to some extent – but not all. Broadly speaking, you need a smart contract which requests a flash loan from protocols like AAVE, dYdX or Uniswap. Remember, these are not like traditional loans (secured loans.)

Are Flash Loans Good or Bad for Web3?

On balance, it’s hard to say whether flash loans are currently good or bad for Web3, but the recent attacks are mostly down to protocols being designed or audited poorly, with these mistakes being exposed by flash loans. Theoretically, any sufficiently rich person or collective could perform these attacks; flash loans simply lower the economic barriers. Maturity and eventual mainstream adoption and availability of flash loans will likely tighten auditing standards in the long term and help to reduce the frequency of these sorts of attacks.

The Crypto Rocketship: Weekly Newsletter
  • Exclusive Crypto Airdrops
  • Altcoin of the Week
  • Insider Interviews
  • News & Show Highlights
  • Completely FREE
Aadharsh Pannirselvam

About Aadharsh Pannirselvam

Aadharsh Pannirselvam is a student at the University of Chicago studying Economics and Data Science while building with Blockchain Chicago and the Chicago DAO. Aadharsh works on creating easily digestible web3 and DeFi content at Benzinga while learning off of the bleeding edges of blockchains and digital assets and exploring a career in the space. He holds positions in Ethereum, Bitcoin, and various other DeFi protocols and ecosystems. Aadharsh was previously affiliated with Flipside Crypto and is currently affiliated with Galaxy Digital. Aadharsh’s opinions are his own and not financial advice. The best way to get in touch with Aadharsh is via Twitter, @aadharsh2010 or via LinkedIn.