The Pros and Cons of Flash Loans in Defi

Written by web3maya | Published 2023/01/11
Tech Story Tags: decentralized-finance | flash-loan | crypto-trading | blockchain-technology | yield-farming | crypto-arbitrage | defi-top-story | defi

TLDRFlash Loans are un-collateralized loans that are approved, executed/used & repaid all within the same transaction. This technology enables users to borrow millions of dollars without needing to provide collateral or wait for loan approval. If the user can’t repay the loan before the transaction is completed, a smart contract cancels the transaction and returns the money to the lender.via the TL;DR App

Flash loans are a revolutionary new way of borrowing and lending money directly from or to the blockchain, that has become popular across many Decentralized Finance (DeFi) projects on various blockchains.

Flash Loans are un-collateralized loans that are approved, executed/used & repaid all within the same transaction, which usually lasts a few seconds (for Ethereum, its around 13 sec)

This technology enables users to borrow millions of dollars without needing to provide collateral or wait for loan approval, but there is a catch: you have to repay the loan in the same transaction.

If the user can’t repay the loan before the transaction is completed, a smart contract cancels the transaction and returns the money to the lender.

How do Flash Loans Work?

In traditional banking, if you’re looking to take out a loan, there are several documents that need to be provided, including a formal ID & proof of income. None of this is necessary in the case of a flash loan. As the name itself suggests, such loans can be granted instantaneously, allowing users fast access to funds

To interact with flash loan lenders, borrowers must develop a contract consisting of 3 parts — Borrow, Interact & Return.

This is how the flash loan process looks:

Transfer: loan provider transfers the requested asset to the borrower

Invoke: the borrower invokes the pre-designed operations

Run Operations: user interacts with different smart contracts to execute arbitrage, liquidation, collateral swap, etc. with the borrowed assets

Repay: once complete, the borrower returns the borrowed assets to the flash loan provider

Check State: lastly, the flash loan providers will check their balance, If the borrower has returned insufficient funds, the providers will reverse the transaction immediately.

How Can These Flash Loans Be Used?

TRADING ARBITRAGE

If you buy SOL tokens (native cryptocurrency of Solana blockchain) for $100 at Gemini (a cryptocurrency exchange) & sell them for $101 at Coinbase (another cryptocurrency exchange), you’ll make a profit of $1 on the trade.

With flash loans, you can borrow millions of dollars to do this trade, let’s see how — You take a flash loan of $100 million from Aave (decentralized crypto lending platform), for a small fee of 0.09% on the borrowed amount (i.e. $9000 ). Now you go & buy $100 million worth of SOL tokens from the Gemini exchange & sell it at $101 million at Coinbase. Congrats !! Using flash loans, you just made a profit of 1 million dollars. Now you return the loan of 100 million dollars to Aave plus the fees of $9000 & enjoy the profit you made without putting in your money.

COLLATERAL SWAP

This is not used much for profit but is a useful tool for borrowing & lending. Let’s say you have $1 million worth of Ethereum that you want to lend out to earn interest. To do this, you borrow $800,000 of DAI ( a stablecoin ). Now, let’s say you decide to trade that Ethereum for another coin. To get access to Ethereum again, you’ll need to pay back the stablecoin first. You could then trade the Ethereum with a new coin & deposit the new coin back into the lending platform where you could then again borrow back your $800,000.

Sounds complicated? because it is. It’s a very long & complicated process.

But using flash loans, you can do all this within the same transaction simultaneously without having to go through these multiple steps. You could simply write a Flash Loan that borrowed money to pay back your loan, swap out your Ethereum for another coin and then deposit it back, and that way, you essentially swapped out your collateral without touching the true $800,000 that you borrowed.

SELF LIQUIDATION

You are bullish on Ethereum & believe it would go up & also want to earn interest on it so you deposited $20,000 worth of Ethereum last year. But you needed urgent money for some reason & you took out a loan of $16,000 worth of tether (a stablecoin) based on your Ethereum. You cashed out the tether, put it in your bank account, and used the money.

A year has passed & the $20,000 worth of Ethereum you deposited is now worth $200,000 but it’s locked up as collateral & to get that you need to repay the $16,000 worth of tether you had borrowed, but you have none, as you cashed it all out.

Using a flashloan you can solve this problem. You take out a flashloan of $16,000 of tether to repay your loan & get back your $200,000 worth of Ethereum. You immediately take some of that Ethereum, swap it for tether & pay back your loan.

You have managed to free up your $184,000 worth of Ethereum without putting any of your money upfront.

You liquidated yourself.

Flash Loan Attacks

Though Flash loans help reduce inefficiencies in the market, they can also be used to heavily exploit small errors.

Flash loans are taken from DeFi platforms & used to manipulate the price of a cryptocurrency asset on one exchange before quickly selling it on another. Flash loan attacks are the most common types of DeFi attacks because they are the cheapest to execute and the easiest to conceal. They’ve been making headlines since DeFi’s popularity surge in 2020, and they got worse in 2021, with several hundred million dollars in losses to date.

ATTACK ON CREAM FINANCE

In 2020, a flashloan occurred on the Cream Finance platform. In this attack, a malicious actor used a flashloan to borrow a large amount of DAI (stablecoin) from the Cream Finance platform. The attacker then used the DAI to manipulate the price of certain cryptocurrencies on the platform, causing the prizes to spike.

The attacker then sold the cryptocurrencies at the artificially inflated price, pocketing the profit. After completing the transaction, the attacker repaid the transaction & then disappeared, leaving the Cream Finance platform to bear the losses. This attack drained $37 million from the protocol & in 2021, again a similar attack happened on Cream Finance, this time moving $92 million to one address & $23 million to another. From here the funds were distributed to an array of different wallets.

Flash loan attacks like these can be difficult to detect & prevent, as they involve borrowing & repaying large amounts of assets within a very short timeframe of a few seconds. This can make it hard for platforms to identify & stop the attack before its completed. As a result, all Defi platforms need to implement robust security measures & get regular audits from verified auditors to protect against such attacks.

Conclusion

Though flash loans have made recent headlines for being used to exploit vulnerable DeFi protocols, leading to losses of millions of dollars & facing a lot of criticism, supporters believe flash loans are an innovative and useful tool for the finance industry for arbitrage & quick trades that weren’t possible before blockchains.

In conclusion, they are a powerful tool for users to borrow and lend money quickly and securely. However, it is important to be aware of the risks involved and to use them responsibly.


Also published here.

Thanks for reading! If you enjoyed the blog, please share it to help others! Feel free to leave a comment 💬 below. Have feedback? Let’s connect on Twitter.


Written by web3maya | Defi enthusiast | Bug hunter at Code4Arena
Published by HackerNoon on 2023/01/11