The coolest innovation in the past year in crypto and defi are flash loans. Flash loans allow you to borrow money with no collateral provided you pay it back in the same transaction.
In this post I will explain what flash loans are, how they work and what they are used for. Then I will write some code for a real-life use case of a flash loan.
But first, confused about flash loans? To wrap your head around this we need to understand a bit more about how the Ethereum network works.
Think of the Ethereum network as one computer that everyone shares. You pay Ethereuem to use the computer and run your operations. This is known as gas.
In this Ethereum Computer (EVM) each transaction is processed sequentially. That means that until the current operation has finished no other operation can happen and nothing else can change. If that transaction is cancelled it is just removed and the EVM continues to the next one like the transaction never existed in the first place.
With a flash loan an anonymous person with no collateral can borrow millions of dollars with no questions asked. How is that possible? Because of the way the Ethereum network works as described above.
A transaction on the network can be undone as long as it is reverted before it finishes. So with a flash loan the lender has the power to revert everything that happened in that transaction if their requirements are not met. And their requirement is they get their money back plus interest before the end.
Imagine in this below diagram that steps 1-6 all happen in the same transaction.
Pretty clever huh?
The most obvious use of flash loans is arbitrage. Arbitrage was my full-time business for many years and was the basis of the original name of this blog (the Arbing blog) so you can see why I think flash loans are so cool. Let’s look at a simple arbitrage situation:
Here we are making £2 by buying a widget from Person B and selling it to person A. In reality the transaction would go as follows:
The problem with this arb is that we need £10 to begin with!
Which is fine for a 20% profit. But the reality is that in the real world arbitrage opportunities are not 20%. 0.5% would be more typical. So to make a £1,000 on an arb you would need to start with £200,000. Which is a lot of money. Even more so in crypto where the volatility on that £200k could wipe out our profits.
But with a flash loan anyone can do such an arb by borrowing the required money at no risk.
Why no risk? Borrowing money sounds dangerous right? Because:
Can you see why I think flash loans are awesome!
– Note: The only cost of a failed transaction is the gas fees which is the cost of using the Ethereum virtual machine.
There are a bunch of other uses too. A collateral swap is what we are going to describe for the rest of this post.
In episode 1 we talked about how there are opportunities in COMP farming. Where you can be paid to borrow money, and therefore can multiply your returns. To understand this example I suggest reading that post first.
In that post I gave a simplistic way to COMP farm that can be done using the user interface. The result looked like this:
$8,540 DAI was deposited. It was lent out and used to borrow USDC. Which was lent out and used to borrow DAI. Which was lent out and used to borrow USDC etc.
Later on in that post I said that a way to improve this play would be to lend out and borrow the same currency. It would reduce the risk of liquidation and allow you to just use the token that has the highest returns COMP returns (currently DAI).
The problem is that moving my money from the situation above to lending out and borrowing just DAI is very difficult. Either I deposit a lot more DAI or I do lots of very small transactions to stay within the borrow limit. Which would cost a lot in gas fees.
Here I will show you the fist step and much simplified version of a smart contract I designed to automate my COMP farming. It is programmed in solidity and will run on the Ethereum network.
Our code is going to borrow just under 3x what the amount we are trying to invest. Lend both the original investment and the original money out. Then borrow enough to repay the loan. This effectively leverages us up to a bit below the max we are allowed.
NOTE: We can borrow a max of 75% of the DAI we lend out. So if we start with 2,500 DAI our max end position will be 10,000 DAI lent and 7,500 DAI borrowed. We want to do less than the full amount so we don’t get liquidated by interest accumulation.
Here is the simple code. The flash loan code is taken from the excellent money-legos and indirectly from kollateral. I use DyDx for the flash loan because it is pretty much free. You only need to repay 2 Wei more than you borrow.
Note this code is just for demonstrations purpose only. Code I use in production is much more complicated. I recommend thoroughly testing all code using Ganache before deploying to the main Ethereum network.
Then to get out we just do the same but in reverse.
I hope you can see the potential for smart contracts like this. The one above can be extended to create a fully automated Farm manager. The code I am currently using automates not just the getting in and out of position but the farming too. It claims the COMP accumulated, sells it for DAI and then reinvest that DAI to get compounded returns.
So what do you think of Flash loans? Getting ideas on how they can be used?
Here are some arbs or attacks that use flash loans:
The financial game Half-Rekt (NME) is a very interesting study for anyone interested in arbitrage or front running in the…
Another two months since my last report and a lot has changed once again. September was the most profitable month…
Early morning on Tuesday 29 September an arber/hacker found an exploit with the brand new Eminence (EMN) system that allowed…
Another two months since my last report and perhaps the two busiest and most profitable months in my life. Which…
It has been another two months since my last report and a lot of time has happened since then. The…
In March I launched a proprietary fund to trade on cryptocurrency believing that the opportunities I found as a professional…