FlashLoans: A Deeper Understanding

R.F. Capital
8 min readAug 5, 2021

What Are They?

On February 15, 2020 a brand new type of transaction appeared on the Ethereum Blockchain. An arbitrage trade profit of $360,000 was achieved in seconds with a loan of 10,000 ETH (~ $2,850,000) through a unique use of a smart contract. A flashloan of 10,000 Ether (ETH) was borrowed from the dYdX trading platform. 5,000 ETH of the 10,000 ETH was invested on the Compound platform to convert into Wrapped Bitcoin (WBTC), and the other 5,000 ETH was invested on the bZx platform for shortening the same amount WBTC. At the same time, the price of WBTC dropped on the Uniswap platform resulting in a massive arbitrage opportunity after settlement. In one atomic swap, the first ever news worthy flashloan was successfully achieved.

The large profit was earned by a virtually risk-free loan with no collateral. Aave developers express flashloans as, “a possibility to borrow in an undercollateralized fashion from the reserve [aave liquidity pools] […] a certain amount of liquidity, must be returned within the same transaction [the amount borrowed]”, with a fee of .09%. The catch here is that the flashloan must be paid back within the same transaction, in one block on the Ethereum Blockchain. The extremely short time between loan borrowing and repayment may seem odd, but the example above shows the potential of this new technology enabled by decentralized finance (DeFi).

Most people are familiar with collateralized loans in both traditional finance as well as DeFi. In order to obtain a loan, the borrower must put up some collateral (e.g. a home, car, cryptocurrency/stable coin) in order to access the lended funds. Uncollateralized loans (flashloans) are much different in that the borrower does not need to put up anything to use the loan. In an uncollateralized loan, the borrower will only receive a loan if they can achieve a net positive return on their investment inclusive of fees, otherwise the loan cannot be repaid and the actual payout is not possible. If the smart contract determines that the function(s) being performed are not going to provide enough profit for the flashloan and fees to be paid back, the funds will not be granted and the smart contract will never be deployed. This provides an almost risk-free lending opportunity without the need to put up collateral.

Applications

Arbitrage

If the price of a currency on one exchange platform is lower than the price on another platform, the currency can be bought on the lower priced platform and sold on the higher priced one, and an arbitrage profit can be made. One issue is that these spreads are usually very small and an investor would need a large sum of money to make the trade profit worth it. Not everyone has millions of dollars to invest into arbitrage…flashloans give anyone access to millions of dollars in liquidity for a very brief period of time. An example of an arbitrage trade using a flashloan is found in the image below.

Link: https://etherscan.io/tx/0x4555a69b40fa465b60406c4d23e2eb98d8aee51def21faa28bb7d2b4a73ab1a9

In this example a flashloan of 3,137.407 DAI is taken from an Aave lending pool. After the flashloan is taken out the user converted DAI into the same amount of single collateral DAI also referred to as SAI through MakerDAO’s Migration contract by burning the 3,137.407 DAI and minting 3,137.407 SAI. After minting SAI, they went to Uniswap and converted 3,137.407 SAI back into DAI. As seen in the transaction, Uniswap has a higher conversion price of SAI to DAI than Maker. So, when the user converted the SAI into DAI on Uniswap, they ended up with 3,157.413 DAI. After that conversion process, the flashloan is repaid back to Aave at a price of 3,148.388 DAI, that is the original amount borrowed plus the fee. After paying back the flashloan the user is left with a 3.294 DAI profit. Despite the small profit, this is the first demonstration of an arbitrage flashloan working, and proves the concept to be possible.

Closing Collateralized Debt Position (CDP)

A collateralized debt position is having a loan with some collateral backing the loan. Nearly all loans in DeFi are overcollateralized with typical liquidation thresholds of 60–80% loan-to-value (LTV). Large drops in value greater than 50% occur multiple times a year trigger liquidations across lending platforms. Flashloans provide a quick and cost effective way to rebalance debt and avoid costly liquidations. Here’s an example:

John wants to take a loan out for DAI by depositing ETH as collateral. After a month, the value of John’s ETH has plummeted and he is approaching his liquidation threshold. John needs to get his ETH back as soon as possible in order to avoid liquidation of his ETH, but doesn’t have the money to pay off the loan. Using a flashloan, he is able to pay back the DAI and retrieve his ETH. Once the CDP is closed, he converts his ETH collateral into the amount of DAI needed to pay back the flashloan plus the fee (generally .09%). He then keeps the remaining ETH that was initially deposited as collateral minus that which he paid for fees and avoids paying a greater fee that would have been incurred from a liquidation event.

Refinancing Debt

Mary has an overcollateralized loan on one platform for a repayment interest rate of 10%, but she sees she can borrow the same amount with 8% on a different platform. She wants to change her loan from the 10% platform to the 8% one. Mary takes out a flashloan to pay off the borrowed amount at the 10% interest platform. The loan is now redeemed and the position is closed. Mary now has her initial collateral back (minus a .09% fee) and uses that collateral to borrow on the platform with an 8% interest rate. Mary was able to switch platforms without having to use any of her own money up front and also limiting the amount of gas fees she would’ve paid if executed in multiple transactions.

Collateral Swap

Increases in the value of currencies carries the possibility of obtaining higher loans with the currencies that have increased in price. Some people may want to take advantage of this but have a loan in a different currency, and have no further funds for collateral on another loan. In order to change from one collateral to another, it may be necessary to first redeem the entire loan and then take it up again with a new collateral. In order to avoid having to raise further funds to repay the loan, a flashloan can also be used here. With a flashloan, a new currency is deposited as collateral and the other currency is withdrawn. The flashloan and the protocol fee are then repaid.

Wash Trading

Wash trading is a form of market manipulation in which an investor simultaneously sells and buys the same financial instruments to create misleading, artificial activity in the marketplace which manipulates trading volumes and artificially manipulates the price.

The biggest hurdle to wash trading is the large volume of owned cryptocurrencies needed to signal a sufficient percentage increase in trading volume. This restriction is especially limiting for most DeFi exchanges since the majority of volume is settled on-chain rather than off-chain via layer 2's. With the help of flashloans it is possible to reach very high trading volumes in a very short period of time without having to raise large amounts of own funds.

The distinction between using flashloans for wash trading as opposed to arbitrage trading is that the user loses money at the end of the transaction. This is indicating that the user wanted to push trading volume and did not care about making a profit. As long as they had enough to pay back the flashloan, the loan would carry-out. Because flashloans can be borrowed at very large sums of money, trading volumes can be artificially increased by large percentages. In one example, one transaction increased the trading volume by 25%.

Flashloan Attacks

Flashloan attacks are a type of DeFi attack in which a “cyberthief” obtains a flashloan through a lending protocol (such as aave) and manipulates the market in their favor using various types of deceit.

The most popular types of DeFi exploits are in fact flashloan attacks. The reasoning behind this is because they are simply the cheapest to carry out and the easiest to get away with. They’ve been making headlines since DeFi’s meteoric rise in popularity in 2020, and they appear to be getting much worse in 2021, with hundreds of millions of dollars in losses to date. One of the largest flashloan attacks in 2021 resulted in Alpha Homora (Cream’s lending platform) losing $37 million.

The hacker used the Alpha Homora dapp to repeatedly borrow sUSD from Iron Bank, tripling the amount borrowed each time. This was accomplished through a two-transaction procedure in which the hacker lent funds to Iron Bank and received Yearn Synth sUSD (cySUSD) in return.

The cyberthief then took out a flashloan from Aave for 1.8 million USD Coin (USDC), which he subsequently exchanged into sUSD via Curve. The sUSD was used to repay the flashloan and lend to Iron Bank, allowing them to borrow and lend more of them while receiving a corresponding amount of cySUSD each time.

Essentially the hacker rinsed and repeated this procedure several times, allowing them to steal large quantities of Creamy cyUSD, which they then used to borrow other cryptocurrencies from Iron Bank. They borrowed $13K WETH, $3.6 million USDC, $5.6 million USDT, and $4.2 million DAI as a result.

Flashloan attacks are extremely low-risk, easy to get away with, and very rewarding, making them some of the most common and successful attacks in the DeFi space.

Conclusion

The point of this paper was to help others understand the new concept of flashloans and to provide a brief analysis of their use. The result is an overview of how they work, the possible applications, as well as some of the attacks that they can bring. It has been shown that arbitrage trading is the most common application currently used by flashloans through dYdX’s lending protocol as well as Aave’s, and closing collateralized debt positions are the second most used by almost solely Aave lending protocol. It is unclear how much of the flashloan space is used by wash traders, as most of them come through unidentified addresses as well as large involvements in mempool manipulations of the addresses. In the flashloan attack section it became clear that flashloans have uncovered weaknesses of smart contracts and the interaction of protocols with oracle contracts.

It is easy to see that flashloans offer a large array of applications, both good and bad, and that the number of transactions are not very high compared to the cumulative number of opportunities. It will be exciting to see what the future holds for flashloans as they seem promising, but mainstream adoption is still far away and will be the ultimate test in the validity of this new and exciting technology.

--

--

R.F. Capital

Cryptocurrency investment firm focused on projects and tokens fundamental to the network and infrastructure of the entire sector.