How a flash loan works
Flash Loans
Flash loan is one of most innovative products in DeFi. It is also one of the most controversial features of the decentralized financial sector because its attacks have resulted in millions of dollars in losses. So, what is a flash loan, a (in)famous concept in DeFi?
A flash loan is a type of undercollateralized loan in which the user borrows funds without providing upfront collateral and returns the borrowed assets within the same on-chain transaction. Basically, it works as an ordinary loan: you borrow funds, and then you return it plus an interest on your loan after some time. Almost everything is the same with a flash loan – you take a loan from an on–chain liquidity pool and then you return borrowed assets plus a small fee to the same pool. The main distinction is that it is instant which means that both borrowing and repaying the loan occurs within a single blockchain transaction.
In the case of flash loans, the risk of default is almost zero because both borrowing and repayment are made in a single transaction. One of the most common use cases of flash loans is arbitrage opportunities. When there is a price discrepancy between two decentralized exchanges (DEX), a trader may exploit this inefficiency by buying the token on an exchange where it’s trading at a lower price, and to sell it on another exchange where it’s trading at a higher price. Let’s say, X coin is trading $1 on Uniswap and $1.5 on Sushiswap. By calling a smart contract, a trader can buy 100 X coins on Uniswap for $100 and instantaneously sell them on Sushiswap for $150. Thus, he’ll pocket $50 from the price discrepancy between the two DEXes. Flash loans give the trader leverage; using not only his capital but large amount to fund his trade he can make a fat profit on the price inefficiency.
(How does the leverage work? Say, the price of coin A is $10. You invest $100 in this coin buying 10 coins. The price goes to $15. You sell your holding and receive $150. Your profit is $50, and your return is 50%. Now let’s look at the same case with the use of leverage. You have $100m and you borrow $100 from your friend. Since you have $200, you buy 20 coins. You are lucky, the price goes to $15. You sell your coins for $300. You pay back your friend’s $100. Voila! You end up with $200. Now your profit is $100, and your return is 100%. That’s the power of leverage. If you do your math, you can see that exactly the opposite happens when the price goes down. If you borrowed $100 and the price decreases 50%, you’ll lose not 50% but 100% of your capital. Leverage is a double-edged sword.)