What Are Flash Loans?

Definition
DEFINITION

A flash loan is a type of loan where a user borrows assets with no upfront collateral and returns the borrowed assets within the same blockchain transaction.

A flash loan is a type of uncollateralized loan that lets a user borrow assets with no upfront collateral as long as the borrowed assets are paid back within the same blockchain transaction.

The decentralized finance (DeFi) ecosystem started out by recreating traditional financial services, such as lending and borrowing, exchanges, futures and options markets, for blockchains. As the ecosystem developed, fundamentally new services were created that are only possible thanks to the inherent properties of blockchain technology and the permissionless composability enabled by smart contract applications.

Like the concept of yield farming, flash loans are an exciting new financial primitive. Flash loans enable users to borrow assets from an on-chain liquidity pool with no upfront collateral as long as the borrowed amount of liquidity, plus a small fee, is returned to the pool within the same transaction. If the borrower does not pay back the loan in the same transaction, then the entire transaction is reverted, including the initial borrow and any actions taken afterward. This innovative mechanism increases access to capital for users across a variety of use cases while ensuring the continued solvency of the underlying on-chain liquidity pool.

In this article, we outline how flash loans work, what they are used for, and what DeFi protocols can do to mitigate the potential attack vectors created by this new financial primitive.

How Do Flash Loans Work?

In the case of collateralized lending, borrowers need to put up capital (collateral) to borrow funds. If the borrower fails to meet the terms of the loan, the lender can still cover the loan using the borrower’s collateral. Flash loans don’t have this requirement; the loan can only exist if the borrower pays it back within the same transaction. As a result, defaulting on a flash loan is not possible, since the entire transaction would simply revert.

For a short period of time—the span of a single transaction—a flash loan can turn anyone into a very well-capitalized actor. The hundreds of millions of dollars of liquidity provided by flash loans creates unique opportunities for arbitrage, liquidations, collateral swapping, and the creation of leveraged positions. It also creates certain risks, especially for a nascent ecosystem of financial protocols with varying degrees of decentralization and security. These risks should be understood by smart contract developers so more robust applications can be built for users.

What Are Flash Loans Used For?

The most common usage of flash loans is for arbitrage. By harnessing a large amount of capital to fill an inefficiency in the market, where an asset has differing exchange rates on different markets, arbitrageurs can generate a profit by bringing the market to an equilibrium and improving liquidity for everyone in the DeFi market.

Another use case for flash loans is liquidations. Many lending protocols incentivize third-party liquidators that can earn a reward for liquidating loans that fail to meet a certain collateralization ratio requirement. Access to large amounts of capital through flash loans can help ensure that undercollateralized loans are liquidated on time and the underlying protocol remains solvent.

Flash loans can also be used for collateral swaps—a technique where a user closes their loan with borrowed funds to immediately open a new loan with a different asset as collateral. Flash loans can also simplify the process of creating a leveraged position or allow loans to be seamlessly transferred across protocols. 

Flash Loans and Price Oracle Attacks

Flash loans have a controversial reputation as, in addition to the use cases above, they can also be used to fund various types of attacks on DeFi protocols. Once a vulnerability is uncovered by a malicious actor, the attacker can manipulate certain functions of the protocol using capital acquired via a flash loan and make a profit while potentially draining funds from its smart contracts. In addition, since flash loan transactions revert on failure, hackers don’t have to put a large amount of their own capital at risk to fund an attack.

It’s important to note a key distinction here—flash loans aren’t inherently the problem, since all they do is provide a source of capital. The real issue at hand is existing vulnerabilities in a protocol that may be revealed through a flash loan-funded attack. Over the long-term, flash loans may even be beneficial for the security of the DeFi ecosystem, as protocol engineers have to consider the potential attack vectors flash loans may uncover by providing instant access to a large amount of liquidity. 

While attacks often differ in methodology and scope, an attack commonly attributed to flash loans involves manipulating protocols that use a spot price from a decentralized exchange (DEX) as their sole price oracle. As explained in The Importance of Data Quality for DeFi Smart Contracts, protocols that fetch prices from a single centralized source are easily exploitable by well-capitalized malicious actors who can manipulate the market with one large trade. DeFi protocols are typically looking to maximize their decentralization and censorship resistance—centralized price oracles undermine this objective by acting as a single point of failure. 

Here’s an example of a flash loan-funded attack on a DeFi lending protocol using a DEX-based spot price feed as its sole price oracle:

  1. Attacker borrows a large amount of token A from a protocol supporting flash loans.
  2. Attacker swaps token A for token B on a DEX (lowering the spot price of token A and increasing the spot price of token B on the DEX).
  3. Attacker deposits the purchased token B as collateral on a DeFi protocol that uses the spot price from the above DEX as its sole price feed, and uses the manipulated spot price to borrow a larger amount of token A than should normally be possible.
  4. Attacker uses a portion of borrowed token A to fully pay back the original flash loan and keep the remaining tokens, generating a profit using the protocol’s manipulated price feed.
  5. As the spot prices of token A and B on the DEX are arbitraged back to the true market-wide price, the DeFi protocol is left with an undercollateralized position.
Steps taken by a malicious actor during a flash loan price oracle attack
Steps taken by a malicious actor during a flash loan price oracle attack

Because the attacker was able to open a flash loan and manipulate the exchange that the DeFi protocol used as its sole spot price oracle, the attacker was able to raise the reported value of the token used as collateral and lower the reported value of the token used as debt. This allowed the attacker to borrow more funds than they should have been able to, creating a position that cannot be fully liquidated, as the collateral became worth less than the debt. This attack can occur within a single transaction but can be repeated many times across multiple transactions, furthering the damage.

Furthermore, when used as price feeds,  single on-chain exchanges also provide extremely limited market coverage, as they represent the trading activity of only one exchange. This leaves protocols relying on the spot price from that DEX vulnerable to manipulated price points if volume shifts to different exchanges or a well-capitalized actor temporarily manipulates the price on that exchange. It’s especially risky for lower liquidity assets, which are increasingly being used as collateral within DeFi lending protocols.

With that in mind, this style of attack is entirely preventable with a decentralized oracle solution with proper market coverage.

How Chainlink Oracles Prevent Flash Loan Attacks

 

In order to generate full market coverage, Chainlink Price Feeds are powered by decentralized networks of nodes that aggregate price data not from a single source but from multiple independent data aggregation firms. These data aggregators track all liquid trading environments—including both centralized and decentralized exchanges—to generate volume-weighted average pricing that reflects the market-wide price of assets. Such data aggregators also commonly take into account various differences between exchanges and actively filter market outliers such as flash crashes and wash trading.

If you’d like a deep dive into the advantages of using Chainlink Price Feeds in DeFi protocols, read How Chainlink Price Feeds Secure the DeFi Ecosystem.

Since flash loans only exist within the timeframe of a single on-chain transaction, any manipulation that occurs is reverted by the end of the transaction. Because Chainlink Price Feeds aggregate price data off-chain from a wide collection of sources and publish data on-chain asynchronously, flash loans have no effect on the aggregated values within oracle reports. 

To prevent flash loan-related price oracle attacks, it is strongly recommended that smart contract developers avoid manipulatable DEX spot prices and instead utilize Chainlink Price Feeds as their source of validated market data. This helps ensure that DeFi protocols always receive an aggregated price point that is reflective of market-wide trading activity and untouchable by flash loans, mitigating an entire category of price oracle attack vectors.

Flash loans are ineffective against Chainlink Price Feeds
Flash loans are ineffective against Chainlink Price Feeds.

Conclusion

Flash loans are a sophisticated financial primitive in DeFi that unlock complex financial applications and lower the barrier to entry for a new wave of market participants. While flash loans have been utilized to fund attacks on DeFi protocols, they are simply a financial tool available to developers and users—they do not create vulnerabilities, but reveal vulnerabilities that may already exist in a protocol, with the most common being faulty price oracle designs.

Chainlink is a decentralized oracle network that helps DeFi protocols become more tamper resistant, particularly regarding the critical function of sourcing real-time market data that will trigger a cascade of other transactions across other Web3 applications. Only through a security-first approach can DeFi protocols adapt to new risks, maintain trust, and sustainably scale to attract billions of users and trillions of dollars of value.

This guide gives a comprehensive breakdown on how to evaluate blockchain oracle security
This guide gives a comprehensive breakdown on how to evaluate blockchain oracle security.

If you’re a developer and want to quickly get your application connected to Chainlink Price Feeds, visit the developer documentation and join the technical discussion in Discord. If you want to discuss the integration more in-depth, reach out here.

Learn more about blockchain technology

Get the latest Chainlink content straight to your inbox.