Reunderstanding Flash Loans: Solutions for Credit Risk and the Source of DeFi Risks

Huobi Ventures
2021-05-26 20:44:32
Collection
Flash loans provide a solution to credit risk for Ethereum, but they also bring attack risks.

Author: Huobi Ventures

In mid-2018, Marble first introduced the concept of flash loans, but it wasn't until a year later that flash loans began to develop slowly and entered the blockchain users' view in 2020. Flash loan transactions are based on the atomic nature of blockchain, making them a special type of unsecured lending transaction. The atomic nature requires that all related operations by the user must occur within a single transaction hash, including the repayment operation. If any requirement is not met, the entire transaction involving multiple operations must roll back, and failed transactions cannot be packaged.

Under this setup, theoretically, flash loan transactions allow users to borrow all tokens in a liquidity pool without collateral, but they must return the borrowed tokens and fixed lending costs before the transaction ends, after performing a series of swap and collateral liquidation operations. This lending method perfectly addresses the default risk of current on-chain lending. Compared to traditional on-chain lending, which relies on over-collateralization, flash loans present a new possibility for blockchain users.

Before this possibility was discovered, flash loans entered the public eye due to two attacks. In February 2020, bZx suffered two flash loan attacks on the 15th and 18th, with losses of $330,000 and $640,000, respectively. Since the DeFi wave triggered by liquidity mining, DeFi has entered a period of frequent flash loan attacks.

The following chart shows 12 flash loan attack incidents discovered to date, with total losses reaching $99.65 million. The highest loss came from an attack on Cream.Finance on February 13, 2021. Interestingly, two months after the attack, Cream.Finance proposed to offer cross-protocol flash loan services on April 8, which would be completed through Iron Bank.

Summary of flash loan attack incidents, source: Huobi DeFi Labs

From the perspective of blockchain block time, the maturity period of flash loans on the blockchain is zero. This means that the interest on flash loans should be zero, as the lending rate multiplied by the maturity period is zero. However, among the mainstream protocols currently providing flash loan services, only dYdX does not charge any so-called lending fees, while Aave charges 0.09%, and Uniswap V2 charges a higher fee of 0.3%. Huobi DeFi Labs believes that in the distant future, the service fee rates of flash loan protocols will tend towards zero. From this perspective, dYdX seems to be more user-friendly for flash loan users, while it remains uncertain whether Aave and Uniswap V2 can separate their flash loan business and provide service fee rates independent of the overall protocol settings. However, currently, in this flash loan competition, the three protocols occupy the market with their respective advantages and characteristics.

Uniswap V2 was the last among the three mainstream protocols to offer flash loan services, but as the leading DEX, it stands out in terms of trading frequency and borrowed tokens due to its high liquidity, capturing 60-70% of the flash loan trading market. As the earliest protocol platform to discover flash loan transactions, dYdX, despite only providing three mainstream assets for lending—ETH (WETH), USDC, and DAI—should not be underestimated. Flash loan transactions on dYdX involve as many as 21 DeFi protocols. Aave, as the most authentic lending protocol among the three, differs from the other two by providing callable flash loan functions; currently, Aave is the main target for multiple DeFi protocols to call flash loans, with DeFi Saver and Furucombo being major clients of Aave's flash loan transactions.

Beyond these three mainstream protocols, Huobi DeFi Labs also pays attention to Instadapp, which provides flash loan services concurrently with Uniswap V2. This protocol initially offered leveraged liquidity mining and debt management for COMP and triggered a small wave of flash loan transactions within a month of its launch. It was also the first protocol to propose the concept of batch flash loans, but it fell silent after the launch of Uniswap V2 and the rise of Aave.

This report focuses on the on-chain data of these four protocols, tracking approximately 110,000 flash loan transaction data, aiming to present a comprehensive view of flash loan transactions on-chain through data analysis. The analysis covers transaction counts, address analysis, types of borrowed tokens, and the amounts borrowed for mainstream assets, while summarizing and comparing almost all protocols in the market related to flash loan functions.

Approximately 110,000 flash loan transactions from four protocol platforms

The on-chain data for flash loans in this report is up to March 16, 2021. Based on the fact that the protocols providing flash loans on Ethereum are Uniswap V2, Aave, dYdX, and Instadapp, Huobi DeFi Labs tracked a total of 108,000 historical flash loan transactions from these four major protocols. Different protocols adopt different methods to provide basic flash loan services, and Huobi DeFi Labs used different methods to obtain on-chain data based on the characteristics of each protocol.

The overall methods are summarized as shown in the figure. Aave directly provides users with packaged flash loan functions, tracking flash loan transaction hashes through the invocation of these functions; dYdX obtains transaction hashes based on four event logs; Uniswap V2's on-chain transaction data for flash loans is determined through Dune Analytics' UNISWAPV2CALL; Instadapp's transaction data acquisition method is consistent with Aave, tracking through its flash loan function invocation.

Summary of sources and acquisition methods for on-chain flash loan data, source: Huobi DeFi Labs

The overview of flash loan transaction data obtained from the four protocols is shown in the figure below. Instadapp has not had new flash loan transaction data since July 2020, but due to its considerable transaction data, it remains one of the subjects of this report's on-chain data study.

Flash loan transactions are frequent on Uniswap V2

The flash loan market enters a stable period with the bull market

Time distribution chart of flash loan transaction counts, source: Etherscan.io, Huobi DeFi Labs

The time distribution of historical flash loan transactions on Ethereum is shown in the figure. The first flash loan transaction occurred on June 21, 2019, generated by dYdX. Flash loans only occurred on dYdX in 2019, with only 148 transactions that year, until the second protocol to generate flash loan transactions, Aave, appeared on January 18, 2020. However, at this time, flash loans were still not widely known until the famous bZx attack incidents occurred on February 15 and 18.

In April and May 2020, Instadapp and Uniswap V2 launched their flash loan services, and the transaction count data in June indicated the beginning of the first round of outbreaks. This outbreak was related to Compound's launch of liquidity mining. The beginning of liquidity mining triggered a DeFi frenzy, and Instadapp timely launched COMP leveraged liquidity mining, debt management, and collateral swap management functions, becoming the main driving force behind this outbreak.

The second outbreak occurred last October, driven by Uniswap V2, and from that month, flash loan attacks entered a high incidence period. The proportion of flash loan transactions occurring on Uniswap V2 has consistently maintained around 60%, even reaching nearly 90% at its peak. However, after the launch of Aave V2, Aave V2 captured a portion of the flash loan transaction market. Currently, the trading frequency in the flash loan market stabilizes with Uniswap V2 accounting for 70%, Aave for 20%, and dYdX for 10%.

Time distribution chart of flash loan transaction proportions, source: Etherscan.io, Huobi DeFi Labs

Uniswap V2 has a high failure rollback rate of 9.1% for flash loan transactions

Due to the nature of flash loan transactions, they can result in failures and rollbacks. However, due to different data acquisition methods, we only captured the failure rollback phenomenon on Uniswap V2. Among the 66,000 transactions on Uniswap V2, approximately 6,000 transactions failed and rolled back, resulting in a failure rollback rate of 9.1%.

Uniswap V2 experienced a large number of failed rollback transactions on July 22, 2020, and January 22, 2021, with specific values of 1,139 and 308 transactions, respectively. This phenomenon may be due to users frequently debugging their strategies on those days. In this report, all data analysis results, unless otherwise specified, do not include the data from these rollback transactions.

Historical distribution of failed rollback flash loan transactions on Uniswap V2, source: Etherscan.io, Huobi DeFi Labs

Batch flash loan functionality may still be in its early stages

On December 3, 2020, Aave announced the launch of its V2 version on the mainnet, accompanied by multiple feature updates. This included batch flash loans, which allow users to borrow multiple different tokens in a single flash loan. However, this is not the first instance of batch flash loans.

Before March 12, 2021, a total of 145 batch flash loan transactions occurred on Ethereum, accounting for only 0.13% of the total historical transaction count. The distribution of transaction counts is shown in the figure below. Although Aave was the last to provide batch flash loan functionality, over 60% of batch flash loan transactions occurred through Aave. Uniswap V2 and Instadapp_batch each accounted for about 15% of batch flash loan transactions, while dYdX did not have any batch flash loan transactions.

Distribution chart of batch flash loan transactions by protocol, source: Etherscan.io, Huobi DeFi Labs

The following figure shows the historical distribution of batch flash loan transactions based on on-chain data, indicating that the first-ever batch flash loan occurred on June 17, 2020, on Instadapp. The chart also shows that batch flash loan transactions provided by various protocol platforms are concentrated in different time periods, with some later entrants seemingly completely replacing the batch flash loan functionality of earlier ones.

Since Aave launched the batch flash loan feature, it has almost encompassed all batch flash loan transactions. However, the number of batch flash loan transactions is still too small, and due to the lower arbitrage potential of flash loan transactions on Aave compared to other protocol platforms, we believe that the true value of batch flash loans remains to be discovered.

Historical transaction frequency distribution of batch flash loans, source: Etherscan.io, Huobi DeFi Labs

An average of 1.5 callers corresponds to 1 strategy smart contract

There are two types of accounts on Ethereum: external accounts and contract accounts. External accounts do not contain code, while contract accounts contain contract code. Based on this understanding, the From Address of flash loan transactions parsed by etherscan can be either an external account or a contract account, while the To Address is a contract account. In other words, the From Address is the caller of the contract account, which can be a regular user or a bot, while the To Address is the flash loan strategy itself.

For ease of analysis, this report considers one To Address corresponding to one strategy. However, in reality, the situation is more complex, as the To Address should be the contract executing the flash loan strategy, meaning that one To Address may contain multiple strategies.

Address longevity is primarily short-term

Prior to this, Huobi DeFi Labs first analyzed the time series of From Address and To Address. Among 8,365 From Addresses, the longest time interval between the first and latest transaction was 583 days; among 5,591 To Addresses, the longest time interval was 613 days. Additionally, in this report, the time interval between the first and latest transaction is defined as longevity, but it should be noted that these addresses may engage in flash loan transactions again in the future, so the latest transaction does not represent the last transaction.

The ratio of From Addresses to To Addresses is 1.5, indicating that on average, about 1.5 users/bots call and execute the flash loan strategy contract.

The longevity distribution chart shows that both types of addresses have about 60% of cases where flash loan transactions occurred only on the same day, suggesting a potential failure of many flash loan strategy contracts; or that the on-chain market conditions do not meet the trading conditions of the flash loan strategies, forcing these addresses to wait for the on-chain market conditions to reach preset judgment criteria before they can trigger/call again.

Approximately 35% of From/To Addresses have time intervals within six months, and 20% within 30 days. This result reveals that flash loan strategies, as specialized transactions, may have extremely short longevity; or that most are tailored transactions targeting specific hot protocols, tokens, or special purposes, thus limited by on-chain market conditions, and many strategies may only last for a period as the market changes.

Longevity distribution chart of From/To Addresses, source: Etherscan.io, Huobi DeFi Labs

Longevity and frequency historical trends

The following chart shows the historical data of all transactions for From Addresses and To Addresses, with transaction counts greater than 100 and greater than 500; the size of the circles indicates the size of the transaction counts, with the horizontal axis representing the time of the first transaction for a given address and the vertical axis representing the time of the latest transaction for that address. Thus, both From and To addresses are distributed in the upper left area of the chart.

Before March 2020, the number and area of scatter points were relatively small, indicating that flash loans were in an occasional state. Starting in March 2020, the number of scatter points increased significantly, showing that the number of both From and To addresses entered a phase of explosive growth, while the increasing area of scatter points over time also indicates that the initiation of From Addresses and the invocation of To Addresses exhibited a similar heated trend.

The chart shows that most scatter points are distributed around the 45-degree line, indicating that the From Addresses/To Addresses represented by the scatter points exhibit only short-term behavior, meaning that the time interval between the first and latest transactions is short. Some scatter points distributed at the top of the horizontal axis indicate that these addresses still engage in flash loan transactions recently.

Distribution chart of transaction times and frequencies for From Addresses and To Addresses, source: Etherscan.io, Huobi DeFi Labs

Different protocols exhibit differentiated address distribution patterns for flash loans

Distribution chart of From Addresses and To Addresses across various protocols, source: Etherscan.io, Huobi DeFi Labs

As mentioned earlier, the total ratio of From Addresses to To Addresses is 1.5. When focusing on different protocols, the ratios for AAVEd119, Uniswap V2, and dYdX are approximately 1.3, close to 1.5. However, the From/To ratio for AAVEc7a9 and Instadapp, along with its batch flash loan Instadappbatch, is quite different. Instadapp has an equal number of From Addresses and To Addresses, while AAVEc7a9 shows a completely different ratio, with only 33 To Addresses.

The four major protocols dominate different flash loan token markets

More than half of flash loan transactions involve borrowing tokens like ETH, DAI, and USDC

On-chain transaction data for flash loans shows that a total of 1,015 tokens have been borrowed through flash loans, with over 53% of transactions involving borrowing tokens being ETH, DAI, and USDC, accounting for 28%, 15%, and 11%, respectively. These three tokens play different roles on Ethereum, with ETH being the native asset, while DAI and USDC are stablecoins on Ethereum.

The top ten most frequently borrowed tokens account for 61% of the total transaction count, with their frequency distribution across various protocols shown in the figure below. In addition to the three commonly borrowed tokens, the other tokens include the stablecoin TUSD, the tokenized asset WBTC, the tradable index token DeFi Pulse Index based on 14 DeFi assets, the algorithmic stablecoin project tokens Dynamic Set Dollar and Frax, the oracle token LINK from Chainlink, and KP3R, which only occurs on Uniswap V2.

Frequency distribution chart of the top ten borrowed tokens across protocols, source: Etherscan.io, Huobi DeFi Labs

Through the proportion distribution of the top ten borrowed tokens across various protocols, we find that only 30% of flash loan transactions on Uniswap V2 come from these top ten borrowed tokens. As a leading DEX, Uniswap V2 provides all tokens from the pools on the platform, with actual data indicating that a total of 927 tokens have been borrowed through flash loans on Uniswap V2. This advantage shows that Uniswap V2 occupies the flash loan trading market for long-tail assets.

Additionally, dYdX only offers trading for ETH, USDC, and DAI, primarily dominating the flash loan trading market for these three major tokens, with the 1% of other tokens on dYdX representing cases where the borrowing token analysis result is empty.

Proportion distribution chart of the top ten borrowed tokens across protocols, source: Etherscan.io, Huobi DeFi Labs

The cumulative borrowing amounts for the top three borrowed tokens on dYdX are several times that of other protocols

Historical transaction data for flash loans shows that the three most frequently borrowed tokens are DAI, USDC, and ETH. Based on these three tokens, we further study the distribution of borrowing amounts across different protocols. Since the prices of these three tokens vary at different times, the amounts are indicated by quantity; and all data in the report do not distinguish between WETH and ETH.

The results shown in the figure below indicate that the amounts for the top three flash loan borrowed tokens on dYdX stand out with overwhelming advantages across multiple protocols. This situation reflects the absolute attractiveness of low or even zero transaction fees for flash loan transactions on dYdX. In contrast, Uniswap, Aave, and Instadapp exhibit irregular token quantity distributions.

Distribution chart of total cumulative borrowing amounts for flash loans, source: Etherscan.io, Huobi DeFi Labs

Flash loan transactions involve a wide range of protocols

The tokens borrowed in flash loan transactions are often used for trading on other platforms, and the transaction fees, liquidity pool numbers, and sizes vary across different protocol platforms. Therefore, the DeFi protocols involved in flash loan transactions reflect the complexity and wide-ranging applications of flash loan strategies. Etherscan and Bloxy both analyze on-chain transaction data at the protocol field level, but the degree of analysis differs between the two, leading to discrepancies in statistical results. For this reason, the data results in this section take the highest value from both as the final statistical result.

The following chart shows the DeFi protocols involved in flash loan transactions. On the left are the four major protocol platforms providing flash loan functionality: Uniswap V2, Aave, dYdX, and Instadapp. On the right, there are a total of 22 protocols (including dYdX) involved in each flash loan transaction.

The statistical results show that flash loan transactions occurring on the dYdX protocol involve the highest number of DeFi protocols, reaching up to 21 protocols. This indicates that the strategies used on this protocol are more complex and diverse, allowing the flash loan transactions generated to involve a wider range of protocol platforms. In contrast, the flash loan transactions on the Instadapp protocol involve the fewest protocols, with only 8, including Curve, Kyber V3, Oasis DEX, Uniswap and its V2, Sushiswap, Aave V1, and dYdX.

Among the invoked DeFi protocols, those with invocation counts exceeding ten thousand include Sushiswap (~24,000 times), Uniswap V2 (~21,000 times), Balancer (~12,000 times), and Compound (~10,000 times). Protocols with invocation counts below one hundred include dYdX (79 times), Bancor (37 times), Lendf.ME (3 times), and EtherDelta (1 time).

Distribution chart of DEX involved in flash loan transactions, source: Etherscan.io, Bloxy, Huobi DeFi Labs

An ecosystem based on flash loan functionality is being established

Over 10 protocol platforms provide flash loan services directly or indirectly

Huobi DeFi Labs views flash loans as a functional technology of blockchain, around which a flourishing ecosystem can develop. Currently, statistics show that at least 10 protocol platforms have flash loan functionality. In addition to the Uniswap V2, Aave, dYdX, and Instadapp analyzed in this report, projects like DeFi Saver, Furucombo, Kollateral, Deerfi, Collateralswap, and NFT20 also provide flash loan services directly or offer related services by invoking the flash loan functionality of other protocols.

1: The collateral and debt conversion under Loan Shifter in DeFi Saver requires an additional 0.25% service fee
2: Small flash loans of USDC, ETH, and USDC are directly used through dYdX. Other tokens or large borrowings of DAI and USDC must first borrow from dYdX, and then collateralize to MakerDAO, Compound, and AAVE to borrow tokens. Source:
https://twitter.com/smykjain/status/1361325922591277058
3: Source:
https://docs.collateralswap.com/for-devs/integrating-collateral-swap/assets
4: Source:
https://twitter.com/daveytea/status/1224760425272745991

Uniswap V2's flash loan service offers the highest degree of freedom

Flash loan functionality is often used as one of the means for arbitrage, so the degree of freedom in invoking flash loans greatly limits the possibilities of their invocation. The degree of freedom for flash loans can be measured from two basic dimensions:

  1. Invocation methods can be at the code level or the front-end level. Typically, invoking flash loan protocols directly through code offers a higher degree of freedom than front-end level protocols, as code-level flash loan protocols can be used for a variety of flash loan strategies implemented through smart contracts. Smart contracts consist of complex operational instructions, making code-level flash loan protocols easier to integrate with smart contracts. For instance, Furucombo, although designed for users without coding experience to build flash loan tools, allowing users to customize DeFi strategies through simple drag-and-drop, has weaker flash loan strategies compared to those achieved by invoking Uniswap V2's flash swap functionality.

  2. The types and amounts of tokens that flash loan protocols can provide. Compared to dYdX and Aave, Uniswap V2 can offer a much larger variety of flash loan tokens, thus the degrees of freedom for these two protocols are lower than that of Uniswap V2.

Ranking of flash loan protocol degrees of freedom (marked as currently having acceptable liquidity and operational status), source: Etherscan.io, Bloxy, Huobi DeFi Labs

DeFi Saver aims to create a one-stop management solution for DeFi protocols, offering a rich product experience based on Aave as the foundation for flash loans, with unique features like Boost and Repay, CDP Automation, and 1-transaction CDP Closing. Users can manage their debt positions in MakerDAO, Aave, and Compound.

The Deerfi protocol essentially follows the Uniswap model, but it currently has a limited number of pools, and asset accumulation will take a long time; additionally, it provides a front-end page where users can provide the contract address containing strategies to invoke Deerfi's flash loans.

Kollateral aims to aggregate the liquidity of large pools like dYdX and Aave and provide it to developers through a simple interface. Its disadvantage is that flash loan funds are from a third-party platform, which may not be as appealing to developers as developing their own solutions.

Collateralswap helps users quickly swap collateral assets without needing to repay outstanding debts. Its functionality is similar to DeFi Saver's Loan Shifter, but currently, Collateralswap's ALPHA version only supports MakerDAO.

NFT20, as a decentralized exchange and swapping protocol for NFTs, announced the launch of NFT flash loans on March 26, supporting collateral-free borrowing of NFTs from various NFT pools. Currently, there are a total of 61 pools with liquidity greater than 0, and no fees are charged at this time. The NFT20 team claims that the implementation of its flash loan functionality is similar to Aave, with use cases including arbitrage and reward collection. As one of the most atypical flash loan implementations, NFT20 offers a higher degree of freedom compared to front-end controlled types.

Cream.Finance, as a newly launched cross-protocol flash loan service provider, supports 64 borrowing tokens and charges only a 0.03% fee.

Conclusion

The total number of flash loan transactions is only 110,000, which is not a particularly significant trading figure on Ethereum. As a native function of Ethereum, flash loans can help users achieve collateral swaps, liquidations, debt management, and other operations. At the same time, as a special lending tool, flash loans, with their unsecured characteristics, have become a means for special attacks and arbitrage, thus becoming a source of risk in DeFi. It is evident that flash loans provide a solution to credit risk for Ethereum but also bring attack risks. As one of the sources of risk, current DeFi projects need to consider how to effectively prevent flash loan attacks, but the occurrence of three flash loan attack incidents in February this year indicates that current DeFi projects seem to lack effective defense mechanisms.

The dYdX, Aave, and Uniswap V2 analyzed in this report provide flash loan services with certain differences in their unique ways. dYdX only offers three mainstream assets—USDC, DAI, and ETH—and dominates the flash loan market for these assets. Uniswap V2 offers a special flash loan service where the repayment token can differ from the borrowed token, which can reduce the steps users must take to swap back the borrowed token during transactions, thereby lowering certain gas fees. Aave provides a packaged flash loan function, allowing users to directly obtain the calculated borrowing amount and fees through the invocation of the smart contract (the flash smart contract) when repaying, while the other two protocols require external calculations and manual repayments. Additionally, Aave's flash loan functionality is also the main target for other service protocols (such as DeFi Saver and Furucombo).

Currently, the flash loan services provided by these three mainstream protocols in the market exhibit a trend of diversification in terms of technical aspects (invocation methods) and ecological conditions (number and size of liquidity pools). However, some hope to achieve integration at the technical level. Developers of Yield Protocol proposed a standardized version of the flash loan proposal EIP-3156 in November 2020, and a standardized version of the batch flash loan proposal EIP-3234 at the end of January 2021. Integration at the technical level may reduce the difficulty of developing flash loan functionalities, but perhaps for flash loan users, the size and variety of liquidity that flash loan protocols can provide are more important.

The future development direction of flash loans is still worth contemplating, such as the possibility of cross-chain flash loan transactions. In September 2020, FlashEx published an article on Medium about cross-chain stablecoin flash loans; the project envisioned completing cross-chain flash loans between Bitcoin, Ethereum, and Polkadot, but there have been no updates on the project's official website or community since then. Nevertheless, we still look forward to breakthroughs in cross-chain flash loans after cross-chain lending matures.

Additionally, based on the concept of flash, flash minting has also become a topic of interest for a few researchers and users. Compared to the borrowing amounts of flash loans being limited by the liquidity pool size, flash minting breaks the constraints of liquidity pool size. However, since no mature protocols or market data for flash minting were observed during the writing of this report, this report does not analyze flash minting. This does not mean that Huobi DeFi Labs is pessimistic about this functionality; on the contrary, we have noticed the existence of projects like Yield Protocol and WETH10 that feature flash minting. We look forward to more innovative functionalities and related protocols emerging to enrich the overall blockchain market.

ChainCatcher reminds readers to view blockchain rationally, enhance risk awareness, and be cautious of various virtual token issuances and speculations. All content on this site is solely market information or related party opinions, and does not constitute any form of investment advice. If you find sensitive information in the content, please click "Report", and we will handle it promptly.
banner
ChainCatcher Building the Web3 world with innovators