What is Flash Loan?

Original link: https://daimajia.com/2022/05/23/whta-is-flash-loan

In my opinion, flash loan is really a fascinating financial innovation, breaking the unfairness of opportunities brought by capital monopoly in the traditional world; in the blockchain world, you can lend hundreds of millions of dollars at extremely low interest and use a very short period of time. time, so that capital itself will not become a barrier to cognitive realization.

Taking advantage of the epidemic at home, I plan to organize a series of articles on flash loans. From basic concepts, to real arbitrage case studies, to personal visions for the future.

Flash Loan Series 1: What is Flash Loan?

Because I saw that this article on Reddit was very easy to understand, I directly used DeepL to assist in translating it (slightly adjusted)

warn. Very long (but also super fun, I promise)!

What if I told you that you could anonymously borrow more than $200 million in the blink of an eye without any collateral or even any loan liability?

This sounds impossible in many ways, and an outrageous concept in traditional finance, but a reality in DeFi. With a little effort, you can end up borrowing millions of dollars without collateral.

Of course, there are some limitations that I haven’t mentioned yet. The first limitation: You need to be able to write and deploy Solidity smart contracts yourself (there are code writing guides online). Eventually, Aave and other protocols are expected to offer flash loans within their user interfaces, rather than requiring you to interact directly with their loan pools using your own smart contracts.

The second important limitation of flash loans: The loan must be repaid within seconds of you taking out the loan (along with interest, usually just under 1%). More specifically, it must be repaid at the end of the Ethereum transaction.

The third limitation is that between borrowing money and paying it back, everything you do with your funds must happen within the Ethereum ecosystem; you cannot move those assets out of the Ethereum network.

It still doesn’t make sense, does it? What if you don’t pay or can’t pay? What does it even mean to repay the loan within the same transaction as you took the loan? What’s the point of having 10 seconds with $200 million? To answer these questions, we need to take a look at how flash loans actually work.

The first thing we need to understand is Ethereum transactions. Due to the existence of smart contracts, Ethereum transactions are not just simple asset transfers, they can contain any arbitrary logic. Additionally, these transactions can contain more transactions within them (and these transactions can even contain transactions within them). So, Ethereum transactions can be nested within each other. A top-level transaction can only succeed if every transaction it contains also succeeds.

This last sentence is a very important concept called atomicity (from ancient Greek for “indivisible”). For smart contract platforms, the property of atomicity means that a transaction must either succeed completely or fail completely; it cannot succeed partially. So if one sub-transaction in a top-level transaction fails, the entire top-level transaction fails, which means every sub-transaction it contains fails, so there’s really nothing on the blockchain except the failed transaction record will not happen.

Only when a transaction is completely successful is it added to the blockchain as an immutable fact. Before that moment, everything that happens on the Ethereum network is reversible. Ethereum knows how to backtrack any arbitrary sequence of transactions if the parent transaction fails.

For example, let’s say I make a transaction with three sub-transactions; one involves borrowing something on Aave, another involves selling something on SushiSwap, and the third involves buying something on Uniswap. Now, suppose the Aave transaction succeeds, the SushiSwap transaction succeeds, but the Uniswap transaction fails (for example, due to insufficient gas). This failure caused the entire top-level transaction to fail, which would lead to a sell of SushiSwap and a reversal of the borrow of Aave. In fact, these things never actually happened. All that is added to the blockchain is the record of the failed transaction that was attempted.

However, if all 3 transactions succeed, then the top transaction will complete successfully and then it will be added to the blockchain, which means that all 3 subtransactions have actually occurred and cannot be reversed now.

This finally brings us back to flash loans. When you take out a flash loan, an Ethereum transaction begins. The first sub-transaction in this top-level transaction is the actual transfer of the funds you borrowed to your address. Next, you are free to do whatever sequence of trades you like to try and make a profit on your borrowed funds. You can interact with any protocol, DEXs, AMMs or any kind of contract you like, in any way and in any order. The only limitation is that you cannot transfer funds outside the Ethereum network; otherwise, you would be able to simply run away with the money, since the loan is anonymous and unsecured.

No matter what sub-transactions you include in your smart contract, the last sub-transaction of a flash loan must always be a full repayment of the loan with interest. If you successfully repay the loan and interest, the entire flash loan transaction will be completed successfully. Lenders will get their funds and interest, and you can keep any extra profits you manage to create between borrowing and returning the funds. This entire transaction will now be added to the blockchain as an immutable historical fact.

However, if you can’t repay the loan with interest before the top-level transaction closes (like you manage to lose some funds within seconds of the flash loan starting), then the last sub-transaction (the repayment transaction) will fail. Due to atomicity, this will cause the entire flash loan transaction to fail, meaning every sub-transaction will fail, reversing every action your smart contract takes, even the first sub-transaction where you receive the loan.

In other words, if you can’t repay your flash loan with interest at the close of the deal, you’re not even borrowing money in the first place! This is a flash loan. So flash loans are a bit like Schrödinger’s loans : if they make a profit, they’re real; otherwise, they never existed.

So how do people actually use the funds to make a profit in the seconds between the start and end of a flash loan transaction? So far, the only real use case for people is arbitrage (taking advantage of the price difference between two markets for the same asset, then buying in the cheaper market and selling in the more expensive market, pocketing the difference). So a realistic flash loan smart contract would likely involve a bot that is looking for a large enough arbitrage opportunity and then, after finding one, takes out a large flash loan and uses those funds to execute the arbitrage game in a huge way, Then repay the funds and pocket the profits.

In a sense, flash loans are like short-lived, anonymous partnerships between two parties that bring important resources to the alliance. The lender is basically saying, “I have a lot of money and am interested in multiplying it, but I don’t have the patience or knowledge to do it”. The borrower is basically saying, “I have a lot of knowledge about DeFi, smart contracts, Solidity, and arbitrage, so I know how to make money fast, but I don’t have enough capital.” Within seconds, these people band together anonymously, and if successful, the lender leaves with their 0.9% interest, and the borrower leaves with the remainder of the profit. If it doesn’t work out, the flash loan doesn’t happen in the first place; no harm, no mistakes.

These parties can sometimes walk away with millions of dollars in profits after a 10-second transaction, and neither party takes any risk (other than the inherent smart contract risk) for the flash loan. If it didn’t work out, it didn’t happen at all; that’s why you don’t need a credit check or collateral or anything. Lenders don’t need to worry about loan defaults, and borrowers don’t need to worry about being indebted.

So, if people can borrow huge sums of money anonymously, with no risk to both parties, why are flash loans not mainstream?

Well, first of all, they are a fairly new invention. Besides, they just don’t feel right. Flash loans are not a good fit for anyone. It feels like eating your cake and eating it too. It doesn’t look like a $200 million loan should be borrowed without risk (by the way, there is no theoretical limit to the size of a flash loan; I just keep saying $200 million because I believe this is the largest loan to date. It is only limited by loan liquidity).

For these reasons, the adoption of flash loans among DeFi protocols and users (even the extremely savvy ones) has been slow and hesitant. Still, for those who are really willing to learn how to write flash loan arbitrage contracts, it’s basically free money sitting on the ground.

The final reason the crypto world has been so hesitant to embrace flash loans is that they have been used in some high-profile DeFi breaches. Basically, some very savvy users have figured out ways to use flash loans with a complex string of interactions with various protocols to do things like cheat price feedback momentarily or briefly decouple stablecoins on one exchange, or whatever. Flash loans enable these exploiters to dramatically increase the profits they make from their ploys. These attacks require an extremely deep understanding of all the protocols involved, often involving 4 or 5 steps, all very subtle and subtle. These bugs were patched immediately after they occurred; after all, the bugs were not in the flash loan itself, but in whatever protocol was used in the bug. If someone can do these exploits with a flash loan, someone else can do the exact same thing with that much money in the first place.

(By the way, if you’re looking for a more in-depth and challenging read on flash loans, I highly recommend looking up several major flash loan attacks that have occurred. Regardless of your stance on the ethical issues surrounding them , they are all very interesting, detailed and ingenious).

Because the only news stories that mention flash loans are about these two or three large flash loan attacks, most people have only heard of them in terms of vulnerabilities, so most people associate flash loans with hacks and attacks .

I believe they will be normalized one day, but certainly not today. But one thing’s for sure: they’re here to stay. The cat has come out of the box. As long as there are DeFi protocols willing to support flash loans and DeFi users willing to use them, they will always be available to anyone willing to take the risk.

Anyway, this article has gotten pretty long, so I’ll end here. I hope you enjoyed reading and hope it made you as curious (and uncomfortable) as I was about the idea of ​​flash loans! I hope you can find the answer here.

EDIT: Many commenters mentioned something very valid that I forgot to include. Regardless of whether the transaction is successful or not, you must pay the GAS fee for the transaction. These gas fees can be quite high if there are many complex secondary transactions. So technically, flash loan operations can lose money due to gas fees. It’s just that you don’t have to take responsibility for the loan itself, and the lender doesn’t have to take the risk of default.

Edit 2: I realize, I implied that flash loans only exist on Ethereum, just didn’t mention any other blockchain. In fact, they’re also on BSC, and I think I’ve heard they’re coming to several other chains as well. I’m just talking about Ethereum by default because it’s the ecosystem I’m most familiar with.


What is Flash Loan? was first posted on May 23, 2022 at 1:06 pm.
©2017 “ Code Home ”. Use of this feed is for personal non-commercial use only. If you are not reading this article in your feed reader, then the site is guilty of copyright infringement. Please contact me at [email protected]

This article is reprinted from: https://daimajia.com/2022/05/23/whta-is-flash-loan
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment