Flash Loans: What Can an Ethereum Transaction Do?
Editor's Note: This article is from:Ethereum enthusiasts (ID: ethfans)Editor's Note: This article is from:
Ethereum enthusiasts (ID: ethfans)Ethereum enthusiasts (ID: ethfans)
, Translator & Author: A Jian, reprinted with authorization by Odaily.
title:
Flashloans, the New Invention of Magic Internet Money
The chain circle has always claimed to change the financial industry, and it has finally begun to show signs recently, because young bankers and technicians have begun to form an alliance.
The latest game is called "flash loan", that is to say, you can use code to specify a series of operations, as long as your operations can guarantee that the repayment is completed within one transaction, you can unconditionally loan .
You can do a lot of things in one "transaction" on the blockchain; you can not only transfer money once, you can even complete 50 fund transfers in one transaction. Because smart contracts are like computer programs, when you send a transaction, it is equivalent to "calling a certain function in a certain smart contract"; many functions can be called in one transaction, and naturally many operations can be completed.
What the "flash loan" requires is that all operations must be completed within one transaction (if the repayment cannot be made within one transaction, the funds cannot be loaned). So you have to program all the steps into a transaction sent to the smart contract, including "borrowing, transfer (execution), repayment".
If you don't make your repayments when the transaction closes, the transaction fails as if nothing had happened.
Assuming that the flash loan contract gave you 10,000 ETH at the beginning, if the 10,000 ETH is not repaid at the end of the transaction, it means that you have never lent 10,000 ETH, because when the node is executing the transaction, as long as the transaction fails , all operations in this transaction will be rolled back, as if nothing happened.
Counter-intuitive, right? Unless all operations are performed and sufficient funds are successfully returned, those ETHs are as if they had not been moved.
Well, we also think this is magic. This magic is the latest invention on a Turing-complete network (Turing-complete programming language + self-executing "accounts" or smart contracts).
All computer nodes running Ethereum software run this flash loan smart contract. When this program (smart contract) is activated by the transaction on the chain, it is equivalent to the contract performing the operation specified by the transaction. If it can be completed in the end If the repayment is made, the contract will report "execution is successful", and the state of the entire network will be changed; if the repayment cannot be completed, the contract will report "failure", and the status of other contracts affected by the transaction will fail to change, and only the flash loan contract will be changed status.
It's okay if you're confused, because this is new territory. You need to know programming to use this kind of contract, because you need to specify each step coherently. However, if you are willing to spend a few weeks following the Solidity tutorial, you can also get free money for arbitrage, which is used to capture all profitable opportunities that can be done in a coherent series of steps.
……
For example, one guy made $360,000 in one go, one deal! This incident also made Flash Loan catch everyone's attention.But this is not the only invention in the field of decentralization. For example, Fulcrum has super liquidity; dYdX smart contract supports free flash borrowing (flash borrowing), which is the same as flash loan. A new DeFi project, Aave, is also offering such functionality in the name of flash loans.
There is also a view that CDP (the user's stablecoin debt warehouse in the MakerDAO single-collateral stablecoin system) can be transformed into a token like a promissory note, which is somewhat similar to a permanent loan. (Translator's Note: Before the MakerDAO system upgrade (November 2019), as long as you provide collateral, you can loan DAI, and the interest will not be repaid in real time, but only when you repay, so as long as your collateral Enough, and you can borrow forever, so it approximates perpetual debt.)
……
title:
lightning boy
The so-called arbitrage is to make money by taking advantage of the price difference in different markets. Arbitrage opportunities exist in all financial markets, and digital assets are no exception. Arbitrage activities can help reduce the price difference of an asset in different markets, because arbitrage activities can also increase liquidity
Flash Loan is designed to allow developers to borrow at will without providing collateral. The entire loan and repayment process will be completed in one transaction! Developers can lend funds from the savings pool of the Aave protocol, on the condition that after the transaction is completed, the liquidity lent from the pool will be returned to the pool as it is. If that much liquidity cannot be returned, the transaction will fail and be rolled back, ensuring that the funds in the savings pool will not be lost.
Flash loans have many interesting uses, including:
Arbitrage between decentralized exchanges
……
Rebalancing, for example: lend DAI from the Aave protocol, close your MakerDAO pledged debt position and get back your collateral, deposit these collateral in Compound, lend DAI, and then return the DAI and some fees to Aave
< https://etherscan.io/tx/0x4555a69b40fa465b60406c4d23e2eb98d8aee51def21faa28bb7d2b4a73ab1a9>
This way, more people can participate in arbitrage and liquidation because there is no initial capital requirement. Arbitrage typically has little capital requirement ($100 to $10,000), but liquidation requires substantial capital to close out the debtor's position. Closing positions in Compound and single-collateral DAI (aka SAI) often requires investing more than $1 million worth of ETH or DAI.
To demonstrate the limitless potential of flash loans, we did an arbitrage, but in fact it can be used in many other places. You can check out this deal we sent:
(Translator's Note: As can be seen from the above two figures, the operations involved in the transaction are coherent, transferring funds from address A to address B, then transferring from address B to address C, and finally returning to the contract address)
The specific process is not shown here, but we can see the detailed analysis provided by the PeckShield and Hydro communities:
PeckShield: Analysis of hard-core technology, the whole story of bZx protocol being attacked by hackers
DeFi low-key sharing | How to empty-handed white wolf 51 BTC
From these detailed analysis, we can know that the key to the hacker’s profit is that he used borrowed funds to manipulate asset prices on Uniswap and Kyber, and the bZx protocol uses Kyber as its price information providing mechanism (also known as "oracle machine"), and finally gave the hacker a lot of tokens in the leverage function. After the hacker gets these assets and sells them, he can repay the initial flash loan loan. Of course, the hacker did not close his leverage on the bZx protocol, that is, he did not pay back the money.
After the incident came out, the bZx team used the management key to freeze the hacker's income.
This matter has sparked a lot of discussions in the whole circle, including but not limited to: Is bZx’s handling reasonable? Should DeFi protocols have admin keys? Are decentralized oracles safe? Is the emergence of flash loans a good thing or a bad thing.
There is a term in computer science called "atomization", which refers to whether related operations can always succeed and fail at the same time when processing transactions, and will never fall into a situation where some operations succeed and some operations fail; If it can, it guarantees "atomicity".
But if the processing of these two things is atomic, you can directly perform an operation to grab tickets and book a hotel at the same time. If the time comes, if the ticket grabbing fails, the hotel will be automatically canceled. This can bring great convenience.
If you change to a specific example, you will find that this concept is not unfamiliar: you want to travel somewhere, and you always hope that transportation and accommodation can be confirmed at the same time. Hotel A is closer to the station; but if you can't get a high-speed rail ticket, you will want to pick another hotel B, or even consider changing a tourist location.
At this time, you will find that if each item has to be determined separately, you can only install many apps and switch between them constantly, which is very troublesome. For example, if you don’t book A, you are afraid that you won’t be able to book a hotel; if you book A, if you can’t get the ticket, you have to cancel the subscription.
But if the processing of these two things is atomic, you can directly perform an operation to grab tickets and book a hotel at the same time. If the time comes, if the ticket grabbing fails, the hotel will be automatically canceled. This can bring great convenience.
In the same way, when you need to change trains to reach your destination, you also hope that you can either buy all the tickets on the journey at one time, or not buy them all, so as not to have to manually refund the tickets.
It is very simple to say, and it is in line with everyone's wishes, but this kind of thing is still extremely rare in our lives. We still have to download many apps and follow many official accounts and small programs.
Not so with Ethereum. Think of Ethereum as a computer, and smart contracts as programs. The transactions initiated by users are just like you are operating a computer. The operations that can be included in a transaction are naturally atomic. The degree is one thing, but - you can really use these programs all at once, and once an operation does not get the result you expected, you can undo all your operations as if nothing happened.
Beautiful and powerful to the point of being unreal.
When it comes to DeFi, many people will mention composability. At the beginning, everyone thought that (for example) with MakerDAO, we have the stable currency DAI; with DAI, we can do the lending market, and we can also do margin trading (providing leverage). Everyone has two imaginations, one is: these products can be freely combined, and there are no access measures for each other, so of course the functions will be more powerful, and of course it can create value; the other is: this means that DeFi has Creative potential, which must flourish in the long run, because the combination of each other always breeds more powerful functions.
But both ideas offer little indication of what the different protocols will look like when they come together, or any notion of time when that combination will take place. In other words, it is more imagined and explained from the perspective of complementary functions, not from the perspective of computers.
However, the concept of atomicity (flash loans) means that any number of contracts can either work together or be indifferent together in a transaction. Either really combined in an instant, or together as if nothing happened.
I can't help but think that flash loans are the most extreme form of "combinability", so extreme that everything happens in a flash.
People have finally discovered that the most authentic appearance of an agreement is to think from the dimension of "the moment of the transaction", just like the example given by the founder of the dForce project, Mr. Renren Dao, using lightning loans can instantly become the liquidity of Uniswap The provider, after finishing the transaction he wants, then quickly withdraws the capital, which can reduce the handling fee from 0.3% to 0.05%; the security of the protocol should also be designed from the granularity of "moment", otherwise bZx will appear Situation, directly penetrated by lightning.
Now people know that the seamless flow of value is both good wine and poison. The DeFi protocol can either strengthen the body under good wine or break the heart.
There are two more interesting things.
First, many friends have felt the destructive power of flash loans, thinking that large flash loans are like a loaded weapon thrown on the street. But some friends think that this is a good thing, because the best way to make progress is to eliminate the bad things directly; because early detection of problems can avoid accumulating systemic risks.


