Discover more from Economics Design Newsletter
Good and Bad Flash Loans
Welcome, premium subscribers! Thank you for subscribing.😘
TLDR below. This is not financial advice.
What is Flash Loan?
The concept of Flash Loans for the first time was "called by the name" by Max Wolff, the creator of Marble Protocol in 2018.
Flash Loans is an uncollateralised loan provided that the loan amount must be returned to the lending platform in the same transaction. It is usually used for Ethereum or ERC20 assets to be borrowed only for the amount of time it takes to complete a transaction block on the blockchain. As long as the loan is repaid before the next block of transactions begins, the borrower will not incur interest charges.
ELI5: You borrow and return the loan + interest within the same transaction.
Wait a minute: why will I do that?
Because during the same transaction, you are also doing other activities like borrowing USDC to get ETH and use that ETH to get DAI on a DEX, and change that to USDC via Curve and keep the profits. When do you see this in action? In arbitrage.
In essence, a transaction can contain a lot of other different execution orders in it and most users only use one executable, which is to transfer tokens. (Hint: that also means cheaper transaction fees!)
Thus, Flash Loans users can utilise the various execution commands as long as the loaned amount is finished.
More Detailed Example
You borrow 10.000 $USDC with Flash Loans
Then swap 10.000 $USDC to 10 $ETH via Uniswap
Then swap 10 $ETH to 11.000 $DAI on Mooniswap
Then use 11.000 $DAI swap to 12.500 $USDC on Curve
And pay 10.000 + 0.50 (interest) $USDC for the borrowed platform.
So you earn 2.000 $USDC profit. And all those execution orders will have to happen in 1 transaction.
Some projects provide Flash Loans services such as Aave, dYdX.
Benefits of Flash Loans
1) "Zero" Loan Fee
If the traditional lender bears two forms of risk: liquidity risk and opportunity cost, Flash Loans solves these two problems. Basically, It works as follows:
I lend you the money you want in a single transaction. But at the end of this transaction, you must pay me back at least the amount I lent you. If you don't pay, I will automatically reinstate your transaction, which means it looks like things haven't started yet.
Flash loans are really risk-free with no opportunity cost. This is because borrowers are "frozen time" during their Flash Loans so the system's capital is never risky and never impeded, so it cannot earn interest in other places (i.e. it has no opportunity cost).
Basically, the main cost to flash loans is the transaction cost.
Flash loans cannot be charged in the traditional way, because loans are valid for a period of zero:
(1+r)^t -1 = 0, (t=0). (Compound Interest)
e^(r*t) -1 = 0, (t=0). (Continuous Compounding Interest)
And of course, if organisations offer Flash Loans with higher interest rates, they will quickly be charged with lower interest rates by other flash lending pools.
Some projects currently apply zero-fee Flash Loans, such as dYdX or AAVE, which charge 0.09% of the principal on Flash Loans.
2) Optimising the crypto ecosystem
Users can execute the arbitrage strategy thanks to Flash Loans. In addition to economic benefits they are bringing a price balance to the asset, namely bringing the asset where it is expensive to where it is priced.
3) Helping users avoid asset liquidation
In the event of a user's collateral deteriorating, the user can use Flash Loans to exchange the collateral into Debt token and then use it to repay Flash Loans. All these actions only cost 0.09% + gas fee instead of ~ 13% liquidation penalty.
4) Add more capital to your collateral
Users can use the Flash Loans Collateral Swap which allows users to swap an ineffective collateral for a more valuable collateral without having to close CDP.
5) Refinancing quickly
By using Flash Loans, users can transfer their loans from one protocol to another.
Flash Loans Application
Flash Loans has 3 main applications including: Arbitrage, Collateral Swap, Self-Liquidation.
The example above is a case of arbitrage trading between asset classes on different platforms.
Imagine that you recognise an opportunity to make money at a different price, but you do not have money or little money. So how? Flash Loans is the solution for you.
But to use Flash Loans requires an understanding of code, so anyone who is not familiar with code can use Fucumbo application.
In fact most loans are used for arbitrage.
But the volume is very small. AAVE had only nearly $100 Million borrowed in the past 30 days. This is very small compared to the market for spreads and liquidations on DeFi.
It is also because most arbitrage transactions are carried out by competitive arbitrage traders running complex programs. They participate in on-chain priority gas auctions and use the gas token to optimise transaction fees. It is a very competitive market - these people are perfectly happy to keep some tokens on their balance sheet to maximise earnings.
On the other hand, borrowing on AAVE costs about 80K gas and charges 0.09% of capital, a price too expensive to pay a competitive trader on a small profit margin. In fact, in most AAVE arbitrage traders, borrowers end up paying more fees for the lending pool than they receive.
In the long run, arbitrage traders will not be able to use Flash Loans except in special cases.
But Flash Loans have other more attractive use cases available at DeFi. An example is refinancing loans.
Example: Let us say you have a Maker vault (CDP) with $100 ETH locked in it and you borrowed 40 $DAI from it. So you had a net position of $60 minus debt. Now you want to refinance into Compound for better interest rates.
Usually, it is necessary to repurchase 40 $DAI at other place, possibly on the exchange, to top up the CDP as it requires some upfront capital. Instead, you can quickly borrow 40 $DAI, pay $100 CDP, send $60 unlocked $ETH into Compound, convert another $40 ETH back into $DAI through Uniswap and use that money to pay Flash Loans.
Suppose you are mortgaging $ETH to borrow $DAI in Compound, but now you do not want to use $ETH as collateral and use $UNI which you do not possess.
Then you need:
Using Flash Loans exactly the $DAI that you have borrowed in Compound.
Pay $DAI and receive $ETH back.
Use $ETH swap to $UNI.
Mortgage $UNI into Compound and brorw $DAI.
Use $DAI to pay Flash Loans + Fee.
So you have successfully swapped collateral from $ETH to $UNI thanks to Flash Loans.
Let us say your mortgage $ETH to borrow $DAI from MakerDAO. But the price of $ETH is dumping and you don't have enough $DAI to pay back the borrowed money.
Instead of having to be liquidated the amount of $ETH pledged with MakerDAO penalty fee, you used Flash Loans to liquidate themselves.
Borrowing $DAI from Flash Loans.
Pay $DAI and receive $ETH back.
Swap enough $ETH to pay Flash Loans + fees to $DAI.
Keep the remaining $ETH.
Flash Loans Attack
Although Flash Loans has good applications, but what makes Flash Loans even more dangerous is the ability to attack and hack DeFi applications with millions of dollars in damage each time.
The highest loss belonged to Harvest Finance project (lost $33.8 million).
ⓒCollected by Economics Design
In the past, a common attack was aimed at manipulating the Price Feed of projects using only Price Oracle from a certain DEX exchange.
But the recent attacks have a much more complex variation, and the huge amount of Flash Loans is usually more than $10 million.
Also, one thing to keep in mind is the increasing frequency of DeFi projects being hacked by Flash Loans.
There are two main reasons that Flash Loans are especially attractive to attackers.
Many attacks require a lot of capital in advance (such as oracle manipulation attacks).
Flash loans minimise the risk of disclosure for attackers. If I have an idea of how to manipulate oracle with $10 million ETH, even if I own that much $ETH, I may not want to risk my capital. My ETH will be affected, exchanges can reject my deposits, and the money will be harder to go with. It is quite risky! But if I quickly borrow $10 million, who cares? All the opposite. It is not like dYdX's mortgage pool would be considered badly hit since that is where your loan came from.
In the Bitcoin whitepaper, Satoshi stated that Bitcoin is safe from attacks because:
"He [Attacker] ought to find it more profitable to play by the rules [...], than to undermine the system and the validity of his own wealth"
With Flash Loans, attackers no longer need to spend a lot of money on games. Flash Loans significantly change risks for attackers.
Remember, Flash Loans can pile up! You can aggregate every quick loan in a single transaction (up to $50 million) and bring all that capital down to a vulnerable contract. That was huge money that anyone can join now as long as they have money. It is so crazy!
Now, of course, you wouldn't be able to attack a protocol if you had a lot of money. If DeFi is as secure as it is claimed, all of this won't matter.
However, we can see Ethereum itself is subject to a 51% attack for under $200K an hour. If Ethereum's own security model is fundamentally built around capital constraints, why are we so quick to set this point for DeFi applications to be successfully hacked for as little as $10 million?
How Can I Be Protected From Flash Loans Attack?
Stop Providing Flash Loans Service
Strictly speaking, trying to get creditors to stop providing Flash Loans is like trying to prevent noise pollution.
Offering Flash Loans is subject to the personal interests of every protocol, and there are good reasons why their users want this functionality. So we can safely get rid of this.
Forcing Important Transactions Spans Two Blocks
Remember, Flash Loans allow you to borrow money for a period of a single transaction. If you require a large capital transaction spanning two blocks, the user must borrow at least two blocks, defeating every flash loans attack. (Note: For this to work, users must lock their value between the two blocks, preventing them from repaying the loan).
Obviously, this leads to a UX balance: It means transactions are no longer in sync. It is attractive to the user and also looks quite secure.
Many developers complain about asynchronous smart contract operations, such as interacting with layer 2 communication or cross-sharding in Ethereum 2.0.
Ironically, asynchronousness really makes these systems safe from Flash Loans attacks, since you cannot go through a segment or a layer 2 in a single small transaction. This means that there are no Flash Loans attacks on ETH 2.0 segments or against DEXs on layer 2.
Request Online Proof Of A User's Previous Balance
We can beat Flash Loans attacks if there are several ways to find out what a user's real balance is — ie how much they are before they borrow.
Before a user interacts with a platform's protocol, the platform requires Merkle evidence proving that at the end of the previous block, they have enough balance to account for the amount of capital they are currently using. The platform needs to track this for each user in each block.
This solution has some complex problems: verifying these on-chain proofs is extremely expensive on-chain, and no well-thought-out user wants to create them and pay gas fees for all of this. Alternatively, a user may have previously changed their balance in the same block for perfectly plausible reasons. So, theoretically, it has some value, but it is not a practical solution.
No solution can completely prevent Flash Loans attacks, but two applications that can mitigate these attacks are market-based price oracle and governance token. (From Dragonfly Research's idea)
For oracle on market-based pricing such as Uniswap or OasisDEX, Flash Loans make it impossible for you to use mid-market rates like an oracle.
The attackers plan to move the average market price in a single transaction and create a flash incident, damaging the price oracle.
The best solution here is to use a weighted average of the last X block through TWAP or VWAP. Uniswap v2 will provide this solution. There is also Polaris, a generalised approach to providing a moving average for DeFi protocols.
On-chain governance is often determined by coin-weighted voting among the holders of governance tokens. But if those governance tokens are on a group of Flash Loans, any attacker can take a huge chunk of money and rely on whatever results they want.
Of course, most governance protocols require token blocking during the voting period, which helps to defeat Flash Loans attacks. But some forms of voting do not require this, such as carbon voting or Maker's executive contract. With current Flash Loans, these forms of voting should be considered completely disrupted.
The most plausible is that the governance token is completely lendable, but it depends on the market. Therefore, all administration actions should require locks to prevent Flash Loans attacks. Compound's new COMP token goes a step further by timing all the protocol's votes, instantly weakening even the regular loan attack against the governance token.
More broadly, all governance tokens take time. Timelock executes all administrative decisions that have to wait a while before they take effect (for Compound's timelock is 2 days). This allows the system to recover from any unforeseen administrative attacks.
Is Flash Loans Bad Or Good?
Along with the strong growth of DeFi, Flash Loans has gained popularity and extremely high profits in a short time. This has attracted professional coders to enter with the million-dollar Flash Loans Attack.
Although Flash Loans Attack's losses are not small (average $4-5M), it must be said that this is an evolutionary journey that DeFi needs to overcome to develop into a sustainable ecosystem.
Flash Loans Attack will take you away from projects that are weak, not serious about security. In other words, it helps projects that want to go the long way to scrutinise product quality before launching officially.
Looking back at 2017, thousands of ICO "drawing cakes" were released to the market with a bluff, not guaranteed in terms of quality, causing a lot of damage to the community.
And in 2020, we will see again the DeFi wave that has brought about tremendous growth. Without progress, DeFi will go into the old ICO rut.
Flash Loans is one of the interesting ideas in DeFi, but has yet to be fully applied, as evidenced by costly attacks. Even so, we cannot deny that it has brought an interesting product, making the DeFi space more efficient.
DeFi is evolving and difficulties will appear in the meantime. Flash Loan is the current problem, but that doesn't mean we'll get rid of it entirely.
Ps: Order the textbook "Economics and Math of Token Engineering and DeFi" today!