Flash Loan Primer: Flash Loans for the Uninitiated
The concept of flash loans gained a lot of attention earlier this year, first with the bZx “attack” in February and then afterward with Aave protocol introducing flash loans as a specific feature on their platform.
Many newcomers to DeFi read news articles about flash loans and understandably are curious and even eager to take advantage of the opportunity for potential profit, but it’s not quite as simple as some portray it. Is it possible to profit from flash loans? Yes. Is it easy? Not exactly.
Flash loans are not a consumer feature on the Aave protocol. Flash loans are aimed at developers; in order to execute a flash loan the user needs an intermediate level understanding of Solidity and knowledge of object-oriented programming concepts. Essentially, they need to be a programmer, either as a result of past work experience or have a current willingness to learn.
DyDx offers flash loans, though not in as straightforward a manner as Aave. In DyDx users need to chain together three different functions to achieve a flash loan: withdraw, call, and deposit, and DyDx does not offer any tutorials to reduce the learning curve.
What is Needed to Successfully Execute a Flash Loan?
There are several component parts to a executing a flash loan. It is not simply a matter of copying and pasting code into Remix (a web browser IDE for deploying smart contracts) and then deploying that smart contract.
Even if you follow the well-documented tutorials that Aave offers here, their code consists of a bare-bones template. Your flash loan has to do something: perform some logic to end up with a profit to cover the 0.30% fee that Aave platform charges.
Coding your flash loan to perform an arbitrage is arguably one of the top two popular use cases: liquidating is the other. Successfully executing a flash loan requires quickly spotting arbitrage opportunities.
Flash loans are only valid, or outstanding, for the length of one block, which is about 12 seconds. If you can’t figure out a way to generate a profit in a series of instantaneous token exchanges and be able to pay back the initial flash loan amount plus the fee within that limited time frame, your flash loan transaction will revert. In that case, when a transaction fails, it is as though you never asked for the loan in the first place, whether you were requesting a loan with a USD equivalent value of $1 or $1m.
If the transaction does fail, your cost is the gas fees paid to attempt the transaction, although those fees will be significantly less than what you would have spent if the transaction had succeeded.
The Backend Infrastructure for Executing Flash Loans
Quickly spotting arbitrage opportunities requires coding a Node.js application and running it 24/7 to monitor price fluctuations. Perhaps, you want to buy on Uniswap and sell on Bancor. The code for your deployed flash loan smart contract (think of this contract as your “front end”) will have to interact with these two different DeFi protocols. It’s not enough for your Node.js bot to query the relevant Uniswap and Bancor smart contracts for pricing information and make a direct comparison. The amount of a particular token that you want to buy with the funds from your flash loan will impact the price: a concept called slippage.
Other risks to executing flash loans involve someone (often a miner) attempting to step in front of your transaction by putting the same request through but with a higher gas fee. Successful flash loan arbitrageurs need to take this into consideration and dynamically calculate gas fees that they’re willing to pay.
A final risk to consider if you’re interested in flash loans for arbitrage is that ETH 2.0 will be launched in the coming months. You can spend a lot of time and effort perfecting a system to profit from flash loans, and its usefulness might be short-lived. Arbitrage opportunities won’t necessarily be non-existent on ETH 2.0; however, the opportunities will be different.
It wouldn’t be possible for me to cover everything related to the concept of flash loans in one article. Just realize that flash loans exist on multiple DeFi platforms including:
- Uniswap V2 (called “flash swaps”)
The best way to approach flash loans is for the pursuit of knowledge for its own sake. If you’re only interested in profiting from the concept, you will likely be disappointed.
Profit is possible, but that shouldn’t be your main goal. After programming smart contracts to execute flash loans, you will understand more about Solidity, object-oriented programming, how the Ethereum blockchain works, and how to interact with multiple DeFi protocols. If Decentralized Finance eventually upends Wall Street and the legacy banking system, those skills will be a good foundation in the rapidly evolving world of peer-to-peer financing.