Many hacks on the Ethereum platform have used flash loans. An attacker can borrow a huge amount of funds without collateral, use it to make a profit, and then repay the loan in the same transaction. In this article, we will explain why we don't see this kind of attack in Cardano's DeFi. You will understand one of the advantages of the UTxO model.
The Principle Of Flash Loans
A flash loan is a loan that is borrowed and repaid in the same transaction. It's one way to potentially make substantial gains without having to risk your own money.
Most of us are familiar with normal loans. A lender loans out money to a borrower to be eventually paid back in full. A loan typically has to be paid back steadily over a period of months or years. Often lenders require borrowers to put up collateral to ensure that if the borrower can't pay back the loan the lender is still able to get their money back. This is a secured loan.
Flash loans are unsecured since no collateral is required. This lack of collateral does not mean the lender will not get its money back. Instead of offering collateral, the borrower needs to pay back the money right away. This can be ensured with a smart contract.
A flash loan is instantaneous. By leveraging the atomicity of transactions on Ethereum, flash loans enable users to perform multiple actions in a single transaction, as long as the transaction time does not exceed the block time.
This concept of atomicity is crucial for the functioning of flash loans. A smart contract for a flash loan typically involves borrowing funds, performing some operations (like arbitrage or liquidation), and then repaying the loan, all within a single transaction. If the loan is not repaid by the end of the transaction, the entire transaction is reversed, and it’s as if the loan never happened. This ensures that the lender does not lose funds, even though the loan is uncollateralized.
In other words, atomicity ensures the all-or-nothing nature of transactions.
It is best to explain with an example. Let’s say we have three parties involved: a lending protocol (A), a borrower (B), and a target protocol C. These three actions take place in a single transaction: Borrow, Action, and Repay.
- Borrow: The borrower (B) initiates a transaction by borrowing funds from the lending protocol (A). This debits the balance of A and credits the balance of B.
- Action: The borrower (B) then uses these borrowed funds to interact with another protocol C. This could be for arbitrage, liquidation, or any other operation that could yield a profit. This debits the balance of B and credits the balance of C.
- Repay: Finally, the borrower (B) repays the loan to the lending protocol (A), plus any fees or interest. This debits the balance of B and credits the balance of A.
When the transaction ends, it appears that the funds were not borrowed.
Smart contracts on Ethereum can be complex autonomous programs that can perform a wide range of operations and interact with other contracts. This allows for complex interactions between multiple smart contracts within a single transaction, which is a key requirement for executing a flash loan. Also notice that in a flash loan transaction, multiple balances are involved. Each balance can be debited and credited multiple times in a single transaction.
Many of these concepts are fundamentally different from the simplicity of Plutus scripts and the UTxO model. This is the reason why there has been no flash loan attack on Cardano yet.
Why Are There No Flash Loan Attacks On Cardano?
The purpose and functionality of validator scripts on Cardano and smart contracts on Ethereum are different, and this impacts the implementation of flash loans.
Validator scripts on Cardano are used to determine whether a UTxO can be spent. They do this by checking whether certain conditions are met. If the conditions are not met, the transaction is not validated, and the UTXO remains unspent.
One of the other reasons for greater security on Cardano is the UTxO model.
Each UTxO is a separate object that is independent of its surroundings. The UTxO model does not have the concept of balances, unlike the account-based model used by Ethereum.
One or more UTxOs can be the input of a transaction in which they can be spent only once and completely. New output UTxOs will be created from input UTxOs.
A Cardano transaction can be seen as a simple function that consumes input UTxOs and produces output UTxOs. UTxOs that enter the transaction are immutable and it is possible to ensure exclusive access. Within the transaction, it is ensured that the value of all input UTxOs is the same as the value of all output UTxOs (this is also a condition of smart contracts that implement flash loans).
In Ethereum, transactions are stateful, meaning they can change the state of the system. This is a necessary prerequisite for complex interactions between multiple smart contracts within a single transaction, which is a key requirement for executing a flash loan.
UTxO model is more stateless. During transaction validation, the script only has access to input UTxOs, transaction context, Datum, and Redeemer. The script does not have the ability, for example, to interact with other UTxOs in the system.
The key difference between Cardano and Ethereum is that it is not possible to move UTxO multiple times to different addresses during Plutus script validation. Moreover, Cardano does not support chaining smart contracts in a single transaction, unlike Ethereum. They can be chained over a series of transactions.
Cardano processes operations within a transaction simultaneously. A single Cardano transaction represents an activity that occurs simultaneously, not in series. This means that the traditional concept of flash loans on Ethereum, where funds are borrowed and repaid within a single transaction, doesn’t directly translate to Cardano due to these constraints.
Flash loans cannot be created on Cardano because it would be necessary to use multiple transactions for borrowing funds, using funds, and repaying funds activities. Cardano does not allow users to create flash loans by design.
Would it be possible to create a flash loan on Cardano somehow?
The logic of the validator script would have to be as follows: allow this UTxO to be spent under the condition that it is spent back into the same script address. However, this does not make sense. UTxO cannot be temporarily borrowed within a transaction. The counterparty would not be able to use UTxO.
In the article, we talk about flash loans negatively, because they are often used for attacks. It is fair to say that flash loans are a powerful tool for traders with the necessary technical skills and knowledge. The same can be said for Ethereum. For some use cases, it is advantageous that developers can chain smart contracts in one transaction.
SC platforms are just tools and they can be used to build useful financial services, or exploited by hackers. The goal of the entire industry is to create such a platform on which it will be possible to create reliable and secure services. Cardano is secure by design, but some things are more laborious and complicated to implement compared to Ethereum. It's always about finding the right balance.