【问题标题】:Add liquidity to Uniswap pool with Web3使用 Web3 为 Uniswap 池添加流动性
【发布时间】:2021-05-26 21:53:59
【问题描述】:

我正在尝试使用 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);

【问题讨论】:

    标签: web3js


    【解决方案1】:

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

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

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

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

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

    【讨论】:

      【解决方案2】:

      通过 Date.now() 作为截止日期而不是 200 的参数。这解决了我的问题。实际上它期待一个日期,而您正在传递一个整数。

      【讨论】:

        猜你喜欢
        • 2022-10-26
        • 2022-11-17
        • 2022-09-28
        • 2021-02-08
        • 2020-10-04
        • 2021-10-01
        • 2021-09-02
        • 2015-02-07
        • 2021-06-09
        相关资源
        最近更新 更多