在 swapExactTokensForTokens (UniswapV2Router02, web3.js) 期间恢复交易
Posted
技术标签:
【中文标题】在 swapExactTokensForTokens (UniswapV2Router02, web3.js) 期间恢复交易【英文标题】:Transaction reverted during swapExactTokensForTokens (UniswapV2Router02, web3.js) 【发布时间】:2021-10-22 10:11:06 【问题描述】:我正在尝试使用 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可以在here找到。 Router02 上的一些文档是here。合约本身可以在here 找到。感谢您提供的任何帮助。
【问题讨论】:
【参考方案1】:尝试批准配对地址,而不是路由器地址
var tx0 =
to: token0.options.address,
gas: gasNeeded,
data: token0.methods.approve(pair,amountIn).encodeABI()
【讨论】:
以上是关于在 swapExactTokensForTokens (UniswapV2Router02, web3.js) 期间恢复交易的主要内容,如果未能解决你的问题,请参考以下文章
web3.py swapExactTokensForTokens 交易失败
在 swapExactTokensForTokens (UniswapV2Router02, web3.js) 期间恢复交易
ethers.js swapExactETHForTokens 和 swapExactTokensForTokens on pancake swap
NOIP 2015 & SDOI 2016 Round1 & CTSC 2016 & SDOI2016 Round2游记