Flash loans: a dive into DeFi’s most bizarre, outlandish, and intimidating innovation. If you’re not yet familiar with flash loans and how they work, this will probably blow your mind.

Warning: Very long (but also super interesting, I promise)!What if I told you that you could anonymously borrow $200+ million 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 is a reality in DeFi. With a little effort, you could be borrowing millions of dollars by the end of the day with no collateral.Of course, there are a few limitations that I have not yet mentioned. For one thing, as far as I know, there is still no user-friendly way to do this. You would need to be able to write and deploy the Solidity smart contract yourself (there are a few guides on how to do this floating around the web). Eventually, it is expected that Aave and other protocols will offer flash loans in their user interface rather than requiring that you interact directly with their lending pools using your own smart contract.The next 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 by the time the Ethereum transaction ends.The third 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 $200 million for 10 seconds? 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. Thanks to smart contracts, Ethereum transactions aren’t just a simple transfer of assets; they can contain any arbitrary logic. Moreover, these transactions can contain more transactions inside themselves (and these transactions can even contain transactions in themselves). So, Ethereum transactions can nest in each other. The top-level transaction can only succeed if every single transaction it contains also 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 sub-transaction inside a top-level transaction fails, then the entire top-level transaction will fail, which means every sub-transaction it contains will fail, and therefore nothing at all will actually happen on the blockchain.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 transactions in the case that the parent transaction has failed.For example, let’s say I make a transaction containing 3 sub-transactions; 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 transaction succeeds, the SushiSwap transaction succeeds, but then the Uniswap fails (due to insufficient gas limit for example). This failure causes the entire top-level transaction to fail, which will cause the SushiSwap sell and the Aave borrow to reverse. In effect, those things never actually happened. Nothing will be added to the blockchain.If, however, all 3 transactions succeed, then the top-level transaction will complete successfully, and it will then be added to the blockchain, meaning all 3 sub-transactions have actually happened, and now can’t be reversed.This finally brings us back to flash loans. When you take out a flash loan, an Ethereum transaction begins. The first sub-transaction 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 transactions 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 sub-transactions you include in the smart contract, the very last sub-transaction 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 top-level transaction (say you somehow managed to lose some of the funds in the few seconds since the flash loan started), then the final sub-transaction (the repayment one) will fail. Due to atomicity, this will cause the whole flash loan transaction to fail, meaning every sub-transaction will fail, reversing every action taken by your smart contract, including even the first sub-transaction 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? The only real 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, and then 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, Solidity, 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.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). 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.So, if people can anonymously borrow huge amounts of money with no risk for either party, why are flash loans not mainstream?Well, for one, they are quite a new invention. Moreover, 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 $200 million with no risk (by the way, there is no theoretical limit to flash loan sizes; I just keep saying $200 million because I believe that’s the biggest one ever taken so far. It’s only limited by lending liquidity).For these reasons, flash loans have seen slow, hesitant adoption among DeFi profotols and users (even extremely savvy ones). Nevertheless, for people who are actually willing to learn how to write flash loan arbitrage contracts, it’s basically free money sitting on the ground.One final reason that the crypto world has been very hesitant in embracing flash loans is that they have been used for 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.(By the way, if you’re looking for deeper and more challenging reading on flash loans, I highly recommend looking up the couple major flash loan attacks that have happened. They are extremely interesting, nuanced, and ingenious, regardless of your position on the ethics surrounding them.)Because the only news stories that even mention flash loans have been about the 2 or 3 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 August 28, 2021 at 02:48AM

No comments:

Post a Comment