Developing arbitrage robot with lightning loan (I)

Blockchain tutorial 2022-04-05 17:32:57 阅读数:574

developing arbitrage robot lightning loan

Interest arbitrage (Arbitrage)、 Smart contract development (Contract Coding) And optimistic transfer (Optimistic Transfer) Together created an impressive new tool portfolio . This is an innovation built on the innovation of public blockchain . The two innovations are integrated with each other , Created a really powerful and unique profit mechanism . This tutorial is divided into two parts , In the first part , We will explain the flash loan (Flash Loan) And lightning exchange (Flash Swap) Some basic concepts behind it . In the second part , We will show how to build our own Ethereum arbitrage robot (Arbitrage Bot), The robot is Infura Up operation , Use flash loans to capture and execute arbitrage opportunities .

Learn in a familiar language The etheric fang DApp Development Java | Php | Python | .Net / C# | Golang | Node.JS | Flutter / Dart

1、 Interest arbitrage /Arbitrage

Arbitrage trading (Arbitrage Trading) Not blockchain or Flash loan (Flash Loan) A unique concept , When you have the same assets on different exchanges and their prices are different , You have the opportunity to take advantage of arbitrage trading to make a profit .

Let's take Uniswap and Sushiswap these two items. DEX Take arbitrage as an example to see how it works .Sushiswap yes Uniswap A branch of , This means that they run on the same smart contract code . Although they are two different decentralized exchanges , But we can use the same code to perform the same transaction . in addition , because Sushiswap It's a newer exchange , So the robots that support it (Bot) Maybe less .

The working principle of arbitrage is roughly as follows :1 ETH stay Uniswap Medium value 80 DAI, And in the Sushiswap Middle value 100 DAI. So we're in Uniswap Buy on 1 ETH, And then immediately in Sushiswap On sale , You can earn 20 Dai( Before deducting gasoline and expenses ). This is a typical arbitrage transaction .

2、 Flash loan vs. Lightning exchange

 Insert picture description here

Flash loan (Flash Loan) And lightning exchange (Flash Swap) It is a unique concept of blockchain . The figure above shows some key differences between the two . Let's further explain the main points .

Flash loan (Flash Loan) It must be repaid with the same assets you borrowed : If you borrow Dai, You need to repay Dai. Interest bearing agreement Aave Charge for flash loans 0.09% The cost of . It requires at least three operations :

  1. stay Aave Borrow money
  2. In a DEX Exchange on
  3. In another DEX Carry out arbitrage transactions on to realize profits

Lightning exchange (Flash Swap) Allow traders to pay later in the transaction ( Or return ) Before assets , Receive the asset first and use it elsewhere . As in the Uniswap That's what I did on the Internet , There is no fixed fee for lightning exchange , It's an exchange fee . Compared with flash loan , Lightning exchange can be regarded as “ free ” The loan , Because the exchange fee is deducted from the transaction order , We don't have to pay separately . The last difference : We can repay any assets exchanged . If we use lightning exchange to get ETH purchase Dai, Then use Dai or ETH Repayment is OK , This allows us to perform more complex operations .

Both flash exchange and flash loan adopt optimistic transfer (Optimistic Tranfer), This is a fascinating technology that we will introduce later .

3、 Flash loan and smart contract

Another concept of flash loan that needs to be understood is the nature of Ethereum transaction . All Ethereum transactions originate from externally held accounts (EOA), This is a human operated Ethereum address . Ethereum trading can start from a EOA Go to another EOA, Just like you pay your friends . Ethereum trading can also be done from EOA Go to the smart contract and execute the code . This contract can call another contract , And so on , Until your deal runs out GAS until .

* Be careful : If you are not familiar with Ethereum trading , You can click on the here View relevant accounts 、 Further introduction to contract and Ethereum transaction types .

We'll see later , The function needs to be called more than once during its execution , This is for EOA It's impossible . therefore , We will deploy a smart contract with multi-step process to realize lightning loan . The arbitrage transaction we initiated will start from EOA Execute to AAVE contract , Just provide the address to the deployed contract . Besides , Due to the complexity of flash loan transactions , May result in high transaction costs , So we also need to work for EOA The account number provides enough to pay for the transaction GAS Cost ETH. please remember , The price of a transaction depends on how much computing power it requires .

 Insert picture description here

4、 Optimistic transfer / Optimistic Transfer

Both flash loan and flash exchange are called ” Optimistic transfer “ Technology . This is amazing DeFi Innovative technologies allow users to make unsecured loans or exchange transactions , As long as the user pays off the loan at the end of the transaction , You can execute the transaction . To better understand the idea , Let's look at some code .

This is a Aave(V1) Of LendingPool.sol The code of the contract . Let's pay attention to flashLoan Implementation of function , In particular, the following methods :

 Insert picture description here

stay 877 In line , We can see smart contracts “ Optimistically ” Transfer funds to the user's contract - It doesn't check the balance of the user's contract to ensure that the user has enough assets to repay the loan , Just transfer directly .

How could it be ? Don't users run away with these tokens ? ok , If this is the end of the deal , Then that will be a serious problem . But as you can see , The code is not over yet . Whether the user's contract retains these tokens depends on the next few lines of successful execution .

After optimistic transfer , In the 881 That's ok , We see Aave The contract then uses the optimistic balance of the token (Optimistic Balance) Call the user's contract with any parameters passed in by the user . These parameters will be anything the user needs to pass to their contract . The transaction was executed successfully . Now? , The transaction workflow is in Aave The contract is suspended , And move to the user's contract to continue , The contract will carry out its logic and is expected to succeed in trading .

In the 884 That's ok , The user's contract code has been executed , The workflow now returns to Aave contract , The contract uses require Statement to check the value returned by the user contract . Optimism ends here , Now we have to see if the user contract has executed a profitable transaction ! If so ,Aave The contract will be able to deduct its costs . If the deal doesn't make a profit , Then require The declaration will fail , This means that the whole deal will fail , It also means that our previous optimistic transfer will not actually happen .

Let's take a look Uniswap How to achieve its optimistic transfer . This is from their UniswapV2Pair.sol From the contract , Particular attention swap function :

 Insert picture description here

Optimistic transfer occurs in 170-171 That's ok , This is this. _safeTransfer Method .Uniswap It even gives a note of this optimistic transfer , To better identify it ( By the way ,Uniswap Provide a large number of information about their agreements and contracts Documentation and tutorials ). The next line is Uniswap The contract calls the user's contract with an optimistic balance .

Again , Suppose the transaction workflow is in Uniswap The contract is suspended , And carry the optimistic transfer token to the user contract to continue to execute . Once the user contract is executed , The transaction workflow will return to Uniswap contract . then ,Uniswap The contract allocates the new balance of the user's contract , And try to charge the exchange fee ( The first 180-181 That's ok ).

If the user contract fails to profit from the token , be require About 182 Your declaration will fail , And the whole transaction will be rolled back . This is the key to optimistic transfer -100% Depends on the success of the transaction . If it doesn't work , Then the transaction is invalid , And will return to the state before the token transfer . however , If it works , You can remain optimistic , And users can make a profit .

In the second part of this series , We will gradually build an arbitrage robot . You can preview... Here Code .

Link to the original text : Lightning loan arbitrage Robot Development Tutorial Part 1 — Huizhi. Com

版权声明:本文为[Blockchain tutorial]所创,转载请带上原文链接,感谢。