MultiversX Tracker is Live!

Flash loans: a crash course on DeFi's most outlandish, mind-bending, and unnerving invention. If you have the patience for a long read, I believe this will blow your mind.

All Cryptocurrencies

by COINS NEWS 91 Views

Disclaimer

This is not in any way a recommendation to use flash loans, and certainly not for the attacks with which they are often associated. I just want to explain what they are from a technical and conceptual viewpoint, because they are incredibly interesting, and they exist whether we like it or not.

This post is very long but if you have the patience for it, I think you will find it very worthwhile.

Introduction

What if I told you that you could anonymously borrow $1 billion+ dollars in the blink of an eye without posting any collateral, and without even assuming any liability for the loan?

This sounds impossible on many levels, and would be an outrageous concept in traditional finance, but it has been a reality in DeFi for several years. With a little effort, you could be borrowing millions of dollars by the end of the day with no collateral.

(For my examples, I will use the Ethereum chain, because that is where flash loans were first developed, but they now exist on other smart contract chains like BSC, among others. The concept is the same regardless of the chain.)

The first step in understanding flash loans is learning about the main two limitations.

The chief limitation of flash loans is absolutely critical: the loan must be repaid (with interest, which is usually a bit under 1%) within seconds of when you take it out. More specifically, it must be repaid within the same Ethereum transaction. (Technically, this means that the taking of the loan and the returning of the loan are simultaneous, but the real time between when the transaction launches and when finality is reached can be thought of as being as low as a single block time, which on Ethereum is ~13 seconds. I digress.)

The other big limitation is that everything you do with the funds in between borrowing them and returning them must happen inside the Ethereum ecosystem; you cannot move those assets off the Ethereum network.

This still doesn’t make sense, right? What happens if you don’t or can’t repay it? What does it even mean to repay a loan inside the same transaction that you took it in? What is the point of having $1 billion for an instant? To answer these questions, we need to first learn a few key concepts.

Nested contract calls, atomicity, and reversibility

The first thing we need to understand is Ethereum transactions. Thanks to smart contracts, Ethereum transactions aren’t just a simple transfer of assets; they can contain any arbitrary logic because they can call smart contracts. Smart contracts can call other smart contracts, so operations within a transaction can nest inside each other basically without limit. The transaction at the top level which contains all the nested smart contract calls can only succeed if every operation within it succeeds.

This last sentence is a very important concept known as atomicity (which comes from ancient Greek for “indivisible”). For smart contract platforms, the property of atomicity means that a transaction must either entirely succeed or entirely fail; it can’t partly succeed. So, if a single operation inside a transaction fails, then the entire transaction will fail, which means every operation it contains will fail, and therefore nothing at all will actually happen on the blockchain, besides a record of the failed transaction, and gas fees, which you still need to pay even for failed transactions.

Only once a transaction has fully succeeded is it added to the blockchain as an immutable fact of history. Until that moment, everything that happens on the Ethereum network is reversible. Ethereum knows how to backtrack any arbitrary sequence of operations in the case that the parent transaction has failed.

For example, let’s say I make a transaction containing 3 operations: one involving borrowing something on Aave, another involving selling something on SushiSwap, and the third involving buying something on Uniswap. Now, let’s say the Aave borrow, succeeds, the SushiSwap sell succeeds, but then the Uniswap buy fails (due to insufficient gas limit for example). This failure causes the entire transaction to fail, which will cause the SushiSwap sell and the Aave borrow to reverse. In effect, those things never actually happened. All that is added to the blockchain is a record of that failed transaction that was attempted.

If, however, all 3 operations succeed, then the whole transaction will complete successfully, and it will then be added to the blockchain, meaning all 3 operations have actually happened, and now can’t be reversed.

Flash loans

This finally brings us back to flash loans. When you take out a flash loan, an Ethereum transaction begins. The first operation inside this top-level transaction is the actual transferring of the funds you are borrowing to your address. Next, you are free to do any sequence of operations you like in order to try to turn a profit on the funds you’ve borrowed. You can interact with any protocols, DEXes, AMMs, or whatever kind of contracts you like, in whatever way and whatever order. The only limit is that you cannot move the funds outside of the Ethereum network; otherwise, you would simply be able to take the money and run, since the loan is anonymous and uncollateralized.

No matter what operations you include in the smart contract, the very last operation of a flash loan must always be full repayment of the loan with interest. If you succeed in repaying the loan and interest, then the entire flash loan transaction will complete successfully. The lender will get their funds back plus interest, and you get to keep any additional profits you managed to create with whatever you did between borrowing and returning the funds. This entire transaction will now be added to the blockchain as an immutable fact of history.

If, however, you cannot repay the loan with interest by the end of the transaction (say you somehow managed to lose some of the funds in the few seconds since the flash loan started), then the final operation (the repayment one) will fail. Due to atomicity, this will cause the whole flash loan transaction to fail, meaning every operation will fail, reversing every action taken by your smart contract, including even the first operation in which you received the borrowed funds.

In other words, if you can’t repay your flash loan with interest by the end of the transaction, then you never even borrowed the funds in the first place! Flash loans are thus kind of like Schrodinger's loans: if they turn a profit, then they are real; otherwise, they never existed.

So, how does one actually use the funds to turn a profit during the few seconds between the beginning and end of the flash loan transaction? To my knowledge, the only legitimate use-case people have worked out so far is arbitrage (the act of taking advantage of a price difference between two markets for the same asset by buying in the cheaper market and selling in the more expensive one and pocketing the difference). So, a realistic flash loan smart contract would most likely involve a bot that is searching for sufficiently large arbitrage opportunities, and then, upon finding one, taking out a huge flash loan, using those funds to execute the arbitrage play in a huge way, and then repaying the funds and pocketing the profit.

In a sense, a flash loan is like a brief, anonymous partnership between two parties who each bring an important resource to the alliance. The lender(s) is basically saying “I have tons of money and am interested in multiplying it, but I don’t have the patience or know-how to do it”. The borrower is basically saying “I have extensive knowledge of DeFi, smart contracts, and arbitrage, so I know how to multiply money, but I don’t have enough capital to make it worth my while”. For a few seconds, these people anonymously join forces, and, if it works out, the lender walks away with their 0.9% interest, and the borrower walks away with the remainder of the profits. If it doesn’t work out, then the flash loan never happened in the first place; no harm, no foul (except some gas fees).

These parties can sometimes walk away with millions of dollars in profit after a 10 second transaction, and neither party assumes any risk at all for the flash loan (besides inherent smart contract risk, and, again, gas fees that must be paid no matter what). If it doesn’t work out, it simply never happened; this is why you don’t need a credit check or collateral or anything. The lender doesn’t need to worry about a loan default, and the borrower doesn’t need to worry about being saddled with debt liability.

Flash loan attacks

So, if people can anonymously borrow huge amounts of money with (almost) no risk for either party, why are flash loans not mainstream?

Well, for one, they just feel wrong. Flash loans don’t really sit well with anyone. It feels like having your cake, and eating it too. It just seems like it shouldn’t be possible to borrow $1 billion with no risk (by the way, there is no theoretical limit to flash loan sizes; I just keep saying $1 billion because that is the largest one I've ever heard of, which was borrowed through Aave during the infamous Beanstalk attack. It’s only limited by lending liquidity).

The other (and main reason) that the crypto world has been very hesitant in embracing flash loans is that they have been used for quite a few high-profile DeFi exploits. Basically, some extremely savvy users have found ways to use flash loans combined with complex strings of interactions with various protocols in order to do things like momentarily trick price feed oracles or briefly de-peg stablecoins on a single exchange, or whatever. Flash loans allow these exploiters to drastically multiply how much profit they can get from their ploys. These attacks require extremely deep knowledge of all the protocols involved, and often involve 4 or 5 steps, all very nuanced and clever. These exploits have all been immediately patched when they happen; after all, the vulnerabilities exist not in the flash loans themselves, but in whatever protocols are used in the exploit. If someone can do these exploits with flash loans, then somebody else who simply has that much money to begin with could have done the exact same thing.

The biggest flash loan attack happened earlier this year: someone came up with an exploit against the Euler lending protocol and made away with $197 million in a matter of seconds (understanding the sequence of moves they made to pull it off is beyond my pay grade). This was very far from being the first big flash loan attack; they have been happening for over 3 years.

Because the only news stories that even mention flash loans have generally been about big flash loan attacks, most people have only ever heard of them in the context of exploits, and thus most people associate flash loans with nothing but hacks and attacks.

I am sure the day will come when they will be normalized, but today is not that day. One thing is sure though: they can’t be de-invented. The cat is out of the box. As long as there are DeFi protocols willing to support flash loans and DeFi users willing to use them, then they will be forever available to anyone willing to take the plunge.

Anyway, this is getting atrociously long, so I will end it here. I hope you enjoyed the read, and that it has left you as intrigued by (and as uncomfortable with) the idea of flash loans as I am!

submitted by /u/pseudoHappyHippy
[link] [comments]
Get BONUS $200 for FREE!

You can get bonuses upto $100 FREE BONUS when you:
💰 Install these recommended apps:
💲 SocialGood - 100% Crypto Back on Everyday Shopping
💲 xPortal - The DeFi For The Next Billion
💲 CryptoTab Browser - Lightweight, fast, and ready to mine!
💰 Register on these recommended exchanges:
🟡 Binance🟡 Bitfinex🟡 Bitmart🟡 Bittrex🟡 Bitget
🟡 CoinEx🟡 Crypto.com🟡 Gate.io🟡 Huobi🟡 Kucoin.



Comments