gpt4 book ai didi

web3js - 使用 Web3 为 Uniswap 池增加流动性

转载 作者:行者123 更新时间:2023-12-05 06:50:40 34 4
gpt4 key购买 nike

我正在尝试使用 Web3 在 Ropsten 网络上为 Uniswap 增加流动性。

我的 JavaScript 代码如下:

  (async () => {
console.log("account: ", this.state.account);
const deployedContract = await new web3.eth.Contract(
UniswapRouter02Contract.abi,
"0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D"
);
console.log(web3.currentProvider);
console.log("pair address ", this.state.pairAddress);
console.log(this.state);
const liq = await deployedContract.methods
.addLiquidity(
this.state.firstAddress,
this.state.secondAddress,
1000,
1000,
0,
0,
this.state.pairAddress,
200
)
.send(
{
from: this.state.account,
gas: "2000000"
},
function(error, transactionHash) {}
)
.on("error", error => {
console.log("my error", error.message);
});

//console.log("Events", pairCreated.events);
})();

我的错误:

Transaction has been reverted by the EVM: { "blockHash": "0xc4bcbfe7c4e6045d20b466f7eab2a7af1693cb3e11be7a197722855876554eaa", "blockNumber": 9707061, "contractAddress": null,
"cumulativeGasUsed": 4110545, "from": "0xe3a6752cf416bd9fb766b046782a21b8722bcc3c", "gasUsed": 23341,
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "status": false, "to": "0x7a250d5630b4cf539739df2c5dacb4c659f2488d", "transactionHash": "0xf23421d4a6cba30515e01e288893e8ecda482fccc1e3b969966187855298120c", "transactionIndex": 8, "events": {} }

我还有一个问题:我必须在 Uniswap addLiquidity 函数的参数“to”中输入的地址和截止日期是什么?

功能:

function addLiquidity(
address tokenA,
address tokenB,
uint amountADesired,
uint amountBDesired,
uint amountAMin,
uint amountBMin,
address to,
uint deadline ) external returns (uint amountA, uint amountB, uint liquidity);

最佳答案

您在 ropsten.etherscan.io 中的错误显示已过期,这可能意味着交易执行时间超过 20 分钟(假设您使用的是默认截止日期),因此失败。

请参阅以下内容 https://ethereum.stackexchange.com/questions/98678/fail-with-error-uniswapv2router-expired .

在我看来像是时间戳问题。

此外,对于 v2,您必须确保

  1. 你先调用approve方法
  2. 提供 UTC 时间戳,比当前时间早 20 分钟。在您的情况下,时间戳是 200,这肯定会导致问题。将“200”替换为当前时间戳 + 20 分钟

关于web3js - 使用 Web3 为 Uniswap 池增加流动性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66353261/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com