Web3js

在 swapExactTokensForTokens (UniswapV2Router02, web3.js) 期間恢復交易

  • August 22, 2021

我正在嘗試使用 Uniswap 路由器將 ETH 換成 DAI,但遇到了交易被還原的問題,以及以下消息:Transaction has been reverted by the EVM.

我正在Rinkeby測試網上對此進行測試。

這是完整的消息,以及隨附的塊資訊:

Transaction has been reverted by the EVM:
   {
     "blockHash": "0xfaa3effa60d646ef4db959cba460dac74aafd1c0b667c91758be71a735f949ce",
     "blockNumber": 9146763,
     "contractAddress": null,
     "cumulativeGasUsed": 1678679,
     "effectiveGasPrice": "0x3b9aca09",
     "from": "0xbd8b57fdbd794f125a9d6a3a7bd8958d46201b37",       
     "gasUsed": 30348,
     "logs": [],
     "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
     "status": false,
     "to": "0x7a250d5630b4cf539739df2c5dacb4c659f2488d",
     "transactionHash": "0x7c5b33a7a751430ba0e0347a29b01a12db0b1da71a17a36deb4c773ea5ff483c",
     "transactionIndex": 6,
     "type": "0x0"
   }

     at Object.TransactionError (../node_modules/web3-core-helpers/lib/errors.js:87:21)
     at Object.TransactionRevertedWithoutReasonError (../node_modules/web3-core-helpers/lib/errors.js:98:21)
     at ../node_modules/web3-core-method/lib/index.js:394:57

我還將提供我正在執行的 NodeJS 程式碼:

import Web3 from "web3";

const IERC20 = require('@uniswap/v2-periphery/build/IERC20.json')
const IPair = require('@uniswap/v2-core/build/IUniswapV2Pair.json')  
const IFactory = require('@uniswap/v2-core/build/IUniswapV2Factory.json')
const IRouter = require('@uniswap/v2-periphery/build/IUniswapV2Router02.json')

export class TransactionService {

async transact() {
   const privKey = "<privkey ommitted>";
   var web3 = new Web3('https://rinkeby.infura.io/v3/<infura-rinkeby-path>');
   var factory = new this.web3.eth.Contract(IFactory.abi, "0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f");
   var address = "0xbd8B57fdBD794f125a9D6A3A7bd8958D46201b37";
   var daiExchangeAddress = "0xc7AD46e0b8a400Bb3C915120d284AafbA8fc4735";
   var ethExchangeAddress = '0xc778417E063141139Fce010982780140Aa0cD5Ab';
   var rinkebyRouterAddress = '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D';
   var router = new this.web3.eth.Contract(IRouter.abi, rinkebyRouterAddress);
   var token0 = new this.web3.eth.Contract(IERC20.abi, ethExchangeAddress) //henceforth T0
   var token1 = new this.web3.eth.Contract(IERC20.abi, daiExchangeAddress) //and T1    
   var pair = new this.web3.eth.Contract(IPair.abi,
     (await factory.methods.getPair(token0.options.address, token1.options.address).call()));
   
     const gasPrice = await web3.eth.getGasPrice()
     const gasNeeded = (0.15*10**6)*2 
     const path = [token0.options.address,token1.options.address]
     var amountIn = 999999999999;
     var tx0 = {
       to: token0.options.address, 
       gas: gasNeeded, 
       data: token0.methods.approve(router.options.address,amountIn).encodeABI()
   }

   var signedTx0 = await web3.eth.accounts.signTransaction(tx0, privKey);
   var receipt0 = await web3.eth.sendSignedTransaction(signedTx0.rawTransaction)
   
   console.log(
       `Tx mined\n`+
       `Tx hash: ${receipt0.transactionHash}\n`
       )
   const tx1 = {
       to: router.options.address, 
       gas: gasNeeded,
       data: router.methods.swapExactTokensForTokens(
           amountIn,
           0,
           path,
           address,
           (Date.now() + 250)
       ).encodeABI()
   }

   var signedTx1 = await web3.eth.accounts.signTransaction(tx1, privKey);
   var receipt1 = await web3.eth.sendSignedTransaction(signedTx1.rawTransaction);
}

}

如您所見,進行了兩筆交易。一個approve和一個swapExactTokensForTokens。可以在此處找到契約 ABI 。Router02 的一些文件在這裡。契約本身可以在這裡找到。感謝您提供任何幫助。

要呼叫swapExactTokensForTokens發件人需要在ethExchangeAddress合約中有代幣。

發件人沒有,因此交易恢復為

失敗並出現錯誤“TransferHelper:TRANSFER_FROM_FAILED”

有兩種選擇:

  • 通過交易使用swapExactETHForTokens和發送價值。
  • deposit使用from將一些乙太幣包裹在合約中ethExchangeAddress

引用自:https://ethereum.stackexchange.com/questions/107495