How Flash Loan Actually Works — Step By Step

Melvin Wong
3 min readJan 10, 2022

--

How flash loan works — step by step

I’m back into exploring the perpetually-changing realm of blockchain and cryptos after about a one-year hiatus.

Recently, I encountered something new and pretty cool called “flash loan.” If you’ve been Googling like me, you might understand that the name itself implies how this sort of loan is processed in the DeFi world — you borrow some cryptos and pay it back in a flash! Yes. The flash here means within one transaction on the blockchain.

Let’s slow down a bit here and get to the basics. The best articles to learn the fundamentals of flash loans are at CoinDesk and Decrypt.co so I’m not going to repeat them.

I’m here to explain the actual workflow of a flash loan. It’s pretty mind-boggling made possible with the power of blockchain and DAO.

I posted this question on Reddit to validate my assumptions and it turned out to be pretty accurate but with more juice poured in by fellow Redditors.

What Happens in a Flash Loan Smart Contract In a Flash

Here’s what I understand. The workflow below is programmed inside a smart contract (let’s call it Flashyloan.sol contract) where it interacts with other contracts — liquidity pool contract, DEX contract, etc.

  1. Say 10,000 USDT is lent out from a liquidity pool contract to Flashyloan. The liquidity pool here can be from Aave, the pioneer of flash loans.
  2. The borrowed 10,000 USDT is then used to swap (buy) 2 ETH at DEX C, at the rate of 5,000 ETH/USDT which means 1 ETH costs 5,000 USDT.
  3. Then, the 2 ETH with Flashyloan is swapped (sold) with USDT again but at a different DEX, say DEX D with a rate of 5,010 ETH/USDT which means 1 ETH for 5,010 USDT.
  4. At this point, you should have made a profit of 20 USDT from the swap (10 USDT from each ETH). Otherwise, there’s no point in executing this flash loan.
  5. Finally, Flashyloan returns 10,009 USDT it borrowed (in Step #1) to the liquidity pool with the interest of say 0.09% so that would be 9 USDT. That’s your cost and their gain. So, in the end, you pocket 11 USDT. Btw, I do not take into account gas fees here.
  6. And they live happily ever after, inside that single transaction.

Here’re the important ingredients.

  • Flashyloan is programmed to ensure it makes a profit from the arbitrage trade above. If it detects otherwise, it will cancel the entire operation.
  • The liquidity pool contract also has failsafe protection where prior to lending out, it checks if it can get back the money (checks for a specific function inside Flashyloan).

All in all, flash loan is very flashy to serve the need for uncollateralized lending fueled by blockchain by getting everything done within one single transaction.

Feel free to add more flavor to this article with your comments. If you find this article useful, please kindly clap.

Disclaimer: I’m in no way affiliated with the company, websites, products or services mentioned above. And I never personally used flash loans.

--

--