Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the development method of Flash Loan arbitrage program?

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "what is the development method of Flash Loan arbitrage program". In the daily operation, I believe that many people have doubts about what the development method of Flash Loan arbitrage program is. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what is the development method of Flash Loan arbitrage program?" Next, please follow the editor to study!

1. Technology stack used by DEX arbitrage robot

The technology stack used in the tutorial is as follows:

Node.js: basic operating environment

Ethers.js: JS Development Library of Etay Fong

Infura: provide access point for ethernet place

Solidity: arbitrage Intelligence contract

The back end is developed by Node and uses Infura nodes to track the prices of ETH and Dai in Uniswap and Sushiswap contracts. We use Infura endpoints to get the price on each new block produced on the main network.

We use Ethers.js because it is compatible with Typescript (the original language of the project).

2. Use .env to save sensitive information of arbitrage robot.

This is very important! Since we need to store the private key to sign the main network transaction, we put all the sensitive information in the .env file. In addition, the address of the arbitrage contract and the key of the Infura major network endpoint are also stored in this file:

PRIVATE_KEY=FLASH_LOANER=INFURA_KEY=

Ensure that PRIVATE_KEY is the same account as when deploying the FLASH_LOANER contract. In addition, the etheric Fong account corresponding to PRIVATE_KEY needs sufficient funds to cover the cost of GAS.

If you're not sure why we're doing this, read this article, which explains how to avoid uploading private keys to Github. As described in the article, we need to put sensitive information in this .env file and then add it to the .gitignore file, as follows:

. envyarn.lockpackage-lock.jsonnode_modules

In this way, when we push the information to Github, the file will not be included. This is super, super important!

3. Instantiation of DEX contract

Next, we instantiate the Uniswap and Sushiswap contracts on lines 11 and 12:

/ / uni/sushiswap ABIsconst UniswapV2Pair = require ('. / abis/IUniswapV2Pair.json'); const UniswapV2Factory = require ('. / abis/IUniswapV2Factory.json')

Sushiswap is essentially a branch of Uniswap, so they have exactly the same contract ABI and exactly the same functions available to us. This is another reason why they are suitable for arbitrage!

4. Check arbitrage opportunities regularly

Line 50 is the key to arbitrage robots. Every other block time, we will ask Infura to check the prices of ETH and Dai in Uniswap and Sushiswap. We will then compare these figures to get a "spread" or possible profit margin.

Provider.on ('block', async (blockNumber) = > {try {console.log (blockNumber); const sushiReserves = await sushiEthDai.getReserves (); const uniswapReserves = await uniswapEthDai.getReserves (); [...]}} 5, about early trading / Front Running

Early trading is very common in centralized financial transactions, usually using data speed to gain a small advantage. We don't have to worry too much about this, because Uniswap and Sushiswap are decentralized exchanges. Their prices remain on the chain and vary piece by piece.

Even if we want to get information in front of the network subject in some way, the only way to take action is to include the transaction in the next block, which is visible to everyone. We need to pay high GAS fees to stop preemptive trading, but this is probably the biggest gain.

6. Avoid unprofitable transactions-GAS estimates

Such DeFi transactions can be very expensive. While arbitrage may be profitable, profit margins are more likely to be swallowed up by GAS costs. An important check of our arbitrage robot program is to make sure that GAS costs don't eat up profits. We do this and include it in the shouldSendTx:

Const shouldSendTx = shouldStartEth? (gasCost / ETH_TRADE) < spread: (gasCost / (DAI_TRADE / priceUniswap)) < spread

For common trading pairs such as ETH and Dai, there are not many profit opportunities. These trading pairs have a lot of trading volume, and Uniswap and Sushiswap are popular exchanges. From an economic point of view, arbitrage opportunities are the result of market inefficiency. If a lot of people are using these currency pairs, then we are unlikely to find many opportunities. So you need to find an updated token or exchange!

7. Develop arbitrage intelligent contracts

Click here to view the source code of the arbitrage contract.

Basically, the contract is our arbitrage intermediary. When the program detects a profitable opportunity, it sends funds and trading orders to the contract. Our arbitrage contract is relatively simple. Most of the code comes from Uniswap's sample code.

The contract constructor can pass in some hard-coded data, such as the contract address of Uniswap and Sushiswap. The contract also has a function, uniswapV2Call, in which we borrow tokens from one exchange optimistically, perform the exchange on another exchange, and then immediately repay the first loan:

Pragma solidity = 0.6.6 × import'. / UniswapV2Library.sol';import'. / interfaces/IUniswapV2Router02.sol';import'. / interfaces/IUniswapV2Pair.sol';import'. / interfaces/IERC20.sol';contract FlashLoaner {address immutable factory; uint constant deadline = 10 days; IUniswapV2Router02 immutable sushiRouter; constructor (address _ factory, address _ uniRouter, address _ sushiRouter) public {factory = _ factory; sushiRouter = IUniswapV2Router02 (_ sushiRouter) } function uniswapV2Call (address _ sender, uint _ amount0, uint _ amount1, bytes calldata _ data) external {address [] memory path = new address [] (2); uint amountToken = _ amount0 = = 0? _ amount1: _ amount0; address token0 = IUniswapV2Pair (msg.sender). Token0 (); address token1 = IUniswapV2Pair (msg.sender). Token1 (); require (msg.sender = UniswapV2Library.pairFor (factory, token0, token1), "Unauthorized") Require (_ amount0 = = 0 | | _ amount1 = = 0); path [0] = _ amount0 = = 0? Token1: token0; path [1] = _ amount0 = = 0? Token0: token1; IERC20 token = IERC20 (_ amount0 = = 0? Token1: token0); token.approve (address (sushiRouter), amountToken); / / no need for require () check, if amount required is not sent sushiRouter will revert uint amountRequired = UniswapV2Library.getAmountsIn (factory, amountToken, path) [0]; uint amountReceived = sushiRouter.swapExactTokensForTokens (amountToken, amountRequired, path, msg.sender, deadline) [1]; / / YEAHH PROFIT token.transfer (_ sender, amountReceived-amountRequired);}}

If there is any profit, the contract sends it to the address where the transaction was initiated (_ sender).

At this point, the study on "what is the development method of Flash Loan arbitrage program" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report