Add liquidity to Uniswap pool with Web3

Viewed 2078

I am trying to add liquidity to Uniswap on the Ropsten network using Web3.

My JavaScript code is as follows:

  (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);
})();

My error :

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": {} }

I have another question : what is the address and deadline I have to put in the arguments "to" in Uniswap addLiquidity function?

function :

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);
2 Answers

Your error in ropsten.etherscan.io says Expired, which probably means the transaction took longer than 20 mins to execute (assuming you were using the default deadline) so it failed.

See the following https://ethereum.stackexchange.com/questions/98678/fail-with-error-uniswapv2router-expired .

Seems to me like a timestamp issue.

Also, for v2, you have to make sure that

  1. You call the approve method first
  2. Supply a timestamp in UTC, 20 minutes ahead of the current one. In your case the timestamp is 200 and that is causing the issue for sure. Replace '200' with current timestamp + 20 minutes

Pass Date.now() as param for deadline instead of 200. This resolved my issue. Actually its expecting a date and you are passing an integer.

Related