Flash Loans Explained
Source: Binance Academy
Flash Loans are loans that are borrowed and repaid in the same transaction. Borrowers don't need to provide regular requirements such as proof of income, reserves or collateral.
This form of borrowing is possible with the use of smart contracts in DeFi Transactions. Smart contracts set out the rules for flash loans. It usually requires the borrower to pay back the full loan amount before the transaction is completed.
If this rule is broken, the smart contract automatically reverses the transaction and the loan is cancelled as if it never happened.
Flash loans happen usually in a few seconds or minutes. This is how they can offer unsecured loans as the borrower must return the full amount borrowed almost immediately.
Put this in relatable terms, imagine taking a loan from a bank to make an buy an asset and you are required to repay the entire loan in a few minutes.
Sound impractical, even impossible.
However, Flash loans are very useful in DeFi. It has three major uses cases: Arbitrage, Collateral Swap and Self-Liquidation.
Arbitrage
Arbitrage refers to an immediate trade of an asset in different markets to make a profit from tiny differences in the asset’s listed price. Traders tend to move between various crypto exchanges in search of little differences in the prices of various crypto assets.
For instance, imagine BTC/USDT is trading on Binance at $39,131.26, while the same BTC/USDT is trading on Bitfinex at $39,120.00. That's an $11.26 difference. A trader can buy BTC on Bitfinex and sell on Binance to make a profit of $11.26 per BTC sold. This example uses centralized crypto exchanges, however, the same applies to decentralized exchanges (DEx) like dYdX, UniSwap, PanCakeSwap etc. Flash loans are used to take advantage of a price arbitrage on DExs, this is because of their instant nature and their applicability across the blockchain.
Source: Finematics via Youtube
Here is an example of a person exploiting the price arbitrage on Curve and UniSwap.
Here the DAI/USDC is trading on Curve at $1, while DAI/USDC is trading on UniSwap at $0.99.
A trader can exploit this opportunity using a flash loan by doing the following (as shown above):
- Take a flash loan of 100,000 DAI from Aave
- Swap the 100,000 DAI for 101,010 USDC on UniSwap
- Swap the 101,010 USDC for 101,010 DAI on Curve
- Repay the 100,000 DAI borrowed plus 0.09% fee to make a total of 100,900 DAI to Aave
- Keep the 110 DAI as profit.
Collateral Swap
This involves a quick swap of the collateral used to back a user’s loan from one asset to another.
This helps DeFi users swap the collateral they initially provided on a DeFi lending platform.
Source: Finematics via Youtube
In this example, a person used their ETH as collateral for a loan taken in DAI on the Compound lending platform and they want to swap the ETH for BAT.
A user can swap the ETH for BAT using a flash loan by doing the following (as shown above):
- Take a flash loan in DAI from Aave
- Use the DAI to repay the loan on Compound
- Retrieve your ETH used as collateral
- Swap ETH for BAT on UniSwap
- Deposit BAT as collateral to take a loan in DAI on Compound
- Repay the loan in DAI plus 0.09% fee to Aave
Self-Liquidation
In DeFi, there are liquidation penalties or fees for loans, it currently ranges around 3% to 15% depending on the platform.
DeFi liquidation occurs when the price of the asset used as collateral falls below a certain pre-determined point. This is usually to prevent a situation whereby the value of the collateral is too low to cover the loan borrowed.
In a situation where the value of the collateral reaches the liquidation point, the smart contract sells the crypto asset to cover the debt. The user loses the asset and is charged the fee.
DeFi users can use flash loans to self-liquidate to repay the loan and recover the asset used as collateral to avoid getting liquidated and paying the fee.
Source: Finematics via Youtube
In this example, the user took a loan in DAI with ETH as collateral from Compound. The price of ETH is decreasing and is approaching the level of liquidation, the a take a flash loan to avoid getting liquidated by the smart contract by doing the following:
- Take a loan in DAI from Aave
- Use the DAI to repay the loan on Compound
- Retrieve your ETH used as collateral
- Swap ETH for DAI on UniSwap
- Repay the loan in DAI plus 0.09% fee to Aave
These are the most common use case of flash loans and there are more that are yet to be discovered. These use cases should how useful flash loans are in DeFi. However, flash loans are often subject to attack. These are called Flash Loan Attacks.
This occurs when a borrower is able to trick the lender into thinking that the loan has been repaid even when it has not. DeFi systems are beginning to develop methods and technology to protect against these attacks.