Maximizing Your Arbitrage: Flash Loans
By: Patrick Collins
Tags: Solidity, Python, Flash Loan, Arbitrage, Aave

Maximizing Your Arbitrage: Flash Loans

Using Aave flash loans is a great way to maximize your arbitrage. If your function doesn’t make money, it doesn’t execute, all you have to worry about is other bots beating you to the punch… Which does happen pretty often. Patrick Collins Follow Apr 26 · 5 min read

Original Image from Amitai Angor AA VFX

When I first got started in crypto, I was but a wee quantitative engineer wannabe. I was enamored by the brilliance of the research engineers at the hedge fund I worked at, and wanted to try out some strategies.

Crypto seemed cool, and I met some people who knew a thing or two, and I started learning that you could buy an asset on one exchange, and if another exchange had a “bad” price, you could sell it there and make a profit.

My mind was blown, and I was like “Wow we found the money printer”. I later learned that this was one of the most basic of trading strategies and that you typically need a lot of capital to turn a profit.

What is arbitrage

Arbitrage is literally “buy low sell high”, but you buy and sell on different markets. Let’s say Europe is selling apples for $5, and in the USA, apples go for $10. And easy way to make money, would be to buy an apple at $5 in Europe, and then sell in the States for $10. Easy $5.

The bigger play is to buy thousands of apples for $5 and pray that the apples stay at $10 in the states by the time you get back to America to sell them.

This is arbitrage. And there is a huge issue. If you buy thousands of apples, and sail back to the USA and find out that now the price of apples in America are $3, well, you just took a fat L on that one.

After some time, I decided that it was a strategy that took a lot of time to find the right opportunities, and an opportunity can quickly die just as fast as you found it, so not something that I wanted to put a lot of thought into. One of the major drawbacks was that you had to pray that if you saw an opportunity, it would still be there by the time you wanted to act on it. Otherwise, maybe you bought some worthless asset that is about to plummet to 0.

I started thinking it was sleazy, but now I think I’ve matured to the point that it’s important for someone to do it, so markets can stay efficient.

This issue, (where you might get stuck with some worthless asset) is removed with Flash Loans and is a crazy powerful tool in the DeFi ecosystem.

Fixing the arbitrage issue

In the DeFi world, Ethereum does a lot of math. When you call a function, it’ll check to see if what you’re doing will “break” anything. For example, if you tried to make 2 + 2 = 7, it would “revert” and tell you to go back to middle school.

However, if you borrow $1,000,000 from someone, and pay it back, all in the same transaction, Ethereum doesn’t bat an eye. So long as you go back to the initial state in a single transaction, Ethereum is happy.

This is how flash loans came to be.

Flash Loans

I’ve written about DeFi being the new fintech , and preventing flash loan attacks in the past, but I’ve never taught people. It’s not really hard to do, and in the hands of the good, can be a great tool.

Flash loans fix the exact scenario spelled out above. They are when a user borrows an asset and pays it back in the same transaction. This is highly advantageous to someone like me who is nervous about getting “stuck”, since you never even have to worry about holding the “bad” asset for longer than an instant.

You can also, therefore, just have a bot running that is constantly trying to look for opportunities, and the transaction will only go through if it makes a profit. You can actually have a “profit checker” in the code, that says “if profit, make transaction, else, don’t”. Literally the most brain-dead way to make money, if you can front run all the other bots doing the same thing.

This has been a large piece of the Flashbots work, combatting bots running up the gas prices finding these arbitrage opportunities. Being able to borrow unlimited money so you can find an arbitrage opportunity is an insane perk, AND it’s hard to punish since you’re never left on the bad side of an arbitrage opportunity.

For those of you who want to see an awesome video tutorial with an amazing intro, check it out here:

Flash Aave Loan Tutorial

Creating a Flash Loan Contract

We are going to be using Brownie and Python to get started here. All the code we are going to use can be located in either the brownie mix or my repo (I recently made a PR that I hope they add).

We will be borrowing WETH (Wrapped Ether) and doing nothing with it. But you could easily adapt this contract to do a check to Uniswap or something for arbitrage.

Get Started

1. Install Brownie and python if you haven’t already.

2. Sign up for Infura and generate an API key. Store it in the WEB3_INFURA_PROJECT_ID environment variable. You can learn more about environment variables here. If you’re unfamiliar with environment variables you can just add all these commands to your .env file and run source .env when you’re done.

export WEB3_INFURA_PROJECT_ID=YourProjectID

3. Download the mix, or git clone my edition.

brownie bake aave-flashloan

Or

4. Add your PRIVATE_KEY environment variable, with a private key from your wallet. Note: If using metamask, you’ll have to add a `0x` to the start of your private key)

Quickstart (Kovan)

We can see our flash loans on Etherscan via the Kovan testnet.

Get some WETH. We need this to pay the preimum that flash loans cost.

brownie run scripts/get_weth.py --network kovan

2. Deploy the flash loan contract. This will also fund the contract with WETH to pay the flash loan fee if it’s not funded.

brownie run scripts/deployment_v2.py --network kovan

3. Execute the flash loan

brownie run scripts/run_flash_loan_v2.py --network kovan

And you’ll get an output with a URL for the transaction on Etherscan, and it’ll look like this:

You’ll see, that we made 3 token transfers.

Our contract got the wrapped ETH from the Aave contract. We got some aToken from Aave We paid back the flash loan WITH A LITTLE EXTRA

The fee for this flash loan was 0.0009 WETH. So we had to pay a little extra back.

The Config File

You’ll see in the brownie config file there are a bunch of addresses

networks:

default: mainnet-fork

mainnet-fork:

aave_lending_pool_v2: "0xB53C1a33016B2DC2fF3653530bfF1848a515c8c5"

weth: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"

kovan:

aave_lending_pool_v2: "0x88757f2f99175387ab4c6a4b3067c77a695b0349"

weth: "0xd0a1e359811322d97991e03f863a0c30c2cf029c"