Select Page

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.

No Collateral Loans

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?

What Are flash loans used for?

Flash Loan Arbitrage

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:

  1. Start with £10
  2. Give £10 to Person B and receive 1 widget.
    • Now have £0 and 1 Widget
  3. Give 1 widget to Parson A and receive £12
    • Now have £12
  4. £2 Profit!

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:

  1. We haven’t put any money in so have no money to lose.
  2. If we have made a mistake and can’t pay back the loan the whole transaction is cancelled.

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.

Other Uses

There are a bunch of other uses too. A collateral swap is what we are going to describe for the rest of this post.

Real Life Example – Using a Flash Loan to Collateral Swap A COMP Farm

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.

Flash Swap Solidity Code To Leverage Farm Comp

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 Many Possibilities

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: