0

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

4
  • 1
    You need to approve tokens for the V2 contract before adding liquidity. But even with that I was not able to generate correct parameters for successful execution of the transaction. It would just revert without reason. – Sheraz Arshad Aug 23 '20 at 18:00
  • May I ask how tokens are approved first? Is there a specific uniswap function for this? – Zakoff Aug 23 '20 at 18:38
  • Call approve function on the token contract by providing an amount and address of V2 contract address – Sheraz Arshad Aug 23 '20 at 21:19
  • I got this to work. I approved the uniswap v2 router address (see my javascript function below in the answer). First I approved the uniswap contract, then I ran the code in my original question. Hopefully that works for you too – Zakoff Aug 25 '20 at 21:38
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.