Uniswap Liquidity – Adding Liquidity with JavaScript and Truffle

truffleuniswap

I am trying to add a liquidity to Uniswap on the Ropsten network using truffle and javascript.

I am connecting to the addLiquidityETH function as described here in the Uniswap documentation:

https://uniswap.org/docs/v2/smart-contracts/router02/#addliquidityeth

When I connect to the Uniswap Router V2's addLiquidityETH function, I get a TRANSFER_FROM_FAILED error.

My javascript code is as follows:

  createPair() {
    const that = this;
    return new Promise((resolve, reject) => {

      const contract = require('@truffle/contract');
      const transferContract = contract(UniSwapRouterAbi);
      transferContract.setProvider(that.web3);
      transferContract.at('0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D').then(function(instance) {
        console.log('we are inside the router contract');
        return instance.addLiquidityETH( 
           '0xff5254f440816a778851b21d2dfe879b7bad182d',
           10000000000,
           5000000000,
           1000000000,
           '0xaC3c34E28D0679442e550a1177a6ce472F8C4156',
           1598313600,                     
          {
            from: that.account,
            gas: 4000000,
            value: 1000000000
          });
      }).then(function(status) {
        if (status) {
          console.log('adding liquidity worked');
          return resolve(status);
        }
      }).catch(function(error) {
        console.log(error);
        return reject('transfer.service error');
      });
    });
  }

Here '0xff5254f440816a778851b21d2dfe879b7bad182d' represents an example token I created to test the interface. The other address is from my testing account on metamask

I am able to successfully connect to the function and router contract. Is there something wrong with the way I am using the function or the parameters I am adding? Also I am assuming the values are sent in wei denominations.

The transaction fails on the network: https://ropsten.etherscan.io/tx/0xdaf67fc7c25028305d6330e10d717b90e775dba50f80084f06218386ee30b8c5

Thanks

Best Answer

I got this to work. I needed to approve the ERC20 token contract to send to the Uniswap V2 router contract, using the standard ERC20 approve(...) function.

My javascript code for this is:

  approveToken() {
    const that = this;
    return new Promise((resolve, reject) => {
      const contract = require('@truffle/contract');
      const transferContract = contract(tokenAbi);
      transferContract.setProvider(that.web3);
      transferContract.at('0xff5254f440816a778851b21d2dfe879b7bad182d').then(function(instance) {
        console.log('we about to approve it');
        return instance.approve( 
           '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D',
           Web3.utils.toWei("100000000", "ether"),                     
          {
            from: that.account,
            gas: 4000000,
          });
      }).then(function(status) {
        if (status) {
          console.log('erc20 approved');
          return resolve(status);
        }
      }).catch(function(error) {
        console.log(error);
        return reject('transfer.service error');
      });
    });
  }

Related Topic