A flash loan is a smart contract that creates a loan in cryptocurrency where you can borrow millions of dollars with absolutely no collateral. The catch is you must pay back the flash loan in the same transaction that you bought it in.
In this article we are going to explain what a flash loan is, and what the purposes of it are.
Let’s dig in.
Like I said at the beginning, a flash loan is a loan where you can borrow massive amounts of money for free, but you must pay it back almost immediately. How can we do this? Well, we can borrow some money, do some transactions with it using some code that we wrote, have the computer check if by the end of the transactions we would be able to pay it back, and if it can… we can successfully borrow the money for a short period of time.
When I say a short period of time, I mean like 13 seconds, because that’s around how long an Ethereum block takes to be validated. Right now, there are 3 main uses of a flash loan. We are going to cover all 3.
Use 1: Trading Arbitrage
Imagine a situation where you could buy an apple for $1 from your mom, and sell that apple to your dad for $2. You could easily double your money. You’d just keep taking the money you have and doubling it. Well, this is exactly what trading arbitrage is.
Except in the case of cryptocurrency, you might be able to buy the Basic Attention Token for $100 at Coinbase and sell it to Gemini for $101, making a single dollar of profit. However, with flash loans, you can borrow $100,000,000 of money that isn’t yours to do this trade over and over and over again.
Let’s go through that example. You could buy $100,000,000 worth of BAT on Coinbase and then sell it to Gemini for $101,000,000, making a nice $1,000,000 profit. However, the platform that you borrowed it from charges you a fee for using their money. Since it’s a very short time period, they only charge you a small amount. I think AAVE charges .09%, so that would be $9000. Hah, a fee of $9000 to borrow $100,000,000.
This is one of the most common uses of a flash loan, and you should know that it didn’t take long for people to write bots or automated systems that did this for them. This is why it’s now rare to be able to create a flash loan that takes advantage of a trading arbitrage, however it is possible.
Use 2: Collateral Swap
This one has less potential for profit, but creates a useful tool for those borrowing and lending.
Let’s say you had $500,000 in ETH that you were lending out, and because of that you borrowed $400,000 DAI. Maybe you wanted to trade that ETH for something else. Well, you would have to pay back your DAI to be able to access that ETH so you could trade it for something else then deposit it back into the lending platform, where you could then borrow back your $400,000.
If you used a flash loan, you could do this all at once, instead of using multiple steps.
Maybe you’re less bullish on ETH and wanted to trade your collateral to an Ethereum version of Litecoin (wLTC) instead. (You can watch our video on wrapped tokens if you’re not sure what that means) You could write a flash loan that borrowed money to pay back your loan, swap your ETH for LTC, then deposit it all back and that way you essentially changed out your collateral without touching the true $400,000 that you borrowed.
Use 3: Self Liquidation
Let’s say you deposited around 100 ETH into AAVE, a lending platform, about a year ago. At that time, it was worth $200 each, so you deposited $20,000 worth of ETH. You did this because you were bullish on ETH and thought it would go up, plus you wanted to earn some interest on it. However, you needed some money, so you took out a loan based on your ETH. You took out $16,000 in tether, took it to your bank account, and cashed it out so you could pay bills.
Now let’s say that was a year ago. Now ETH is worth $2000 each. So you still have $200,000 worth of ETH locked up as collateral, but now it’s worth WAY more and you want access to it. You just have to pay back your original loan of $16,000. However, you don’t have any tether, because it’s been a year and you paid bills with it.
You can take out a flash loan of $16,000 tether to repay your loan, and get access to the 100 ETH. Then you can take some of that ETH, convert it to tether, and repay your original loan. So now you have withdrawn around $184,000 worth of Ethereum without actually putting any of your own money up front. This is the third use of a flash loan—you borrowed $16,000 in one transaction and used code so that you pay it back in the same transaction. Of course there were fees, but now you have $184,000 sitting in your crypto wallet.
Pancakeswap hack? Flash Loan Attack
Flash Loans can also be used to exploit vulnerabilities in certain smart contracts. For example, someone found out that there was a bug in the PancakeBunny protocol. PancakeBunny is a yield farm that pays users in PancakeBunny for users lending their coins and tokens. In short, there was a bug the exploiters found that created a lot of PancakeBunny.
They borrowed around $700 million dollars total, used it to exploit PancakeBunny, and paid it back in one transaction. What did they end up with? They found a way to mint 7,000,000 PancakeBunny tokens and immediately sold them… earning them a nice $45,000,000. In short, they borrowed $700 million for free, used it to exploit a bug that gave them $45,000,000… and then paid back the original $700 million.
This is one of the ongoing risks with cryptocurrencies—bugs and exploits can be in the code… and Flash Loans allows attackers to maximize.
Many people on the outside looking in may think this should be illegal—that it shouldn’t be allowed. There are actually many developers who believe it is totally legal and ethical, because it was just a bug in code—anyone could’ve found it, including the developers who created PancakeBunny. What do you think? Leave a comment below.