0

I am unsuccessful trying to create an LP pair with IUniswapV2Factory and adding tokens to with IUniswapV2Router's addLiquidityETH or addLiquidity programmatically (via ethersjs).

I have ensured that both the tokens I am trying to add have approval allowances. But continually getting the error:

Error: VM Exception while processing transaction: reverted with reason string 'TransferHelper: TRANSFER_FROM_FAILED'

Below is my ethersjs code I am using:

    // APPROVE A TOKENS
    const txA = await aContract.approve(deployer.address, BigNumber.from("0x16345785D8A0000"));
    console.log("https://ftmscan.com/tx/" + txA.hash);
    await txA.wait();
// APPROVE B TOKEN
const tx1 = await bContract.approve(deployer.address, BigNumber.from("0x16345785D8A000"));
console.log("https://ftmscan.com/tx/" + tx1.hash);
await tx1.wait();

// CREATE SPOOKY LP PAIR
const tx = await spookyFactoryContract.createPair(
  aContract.address,
  bContract.address,
);
console.log("https://ftmscan.com/tx/" + tx.hash);
await tx.wait();

// GET SPOOKY LP PAIR
const pair = await spookyFactoryContract.getPair(
  aContract.address,
  bContract.address,
);
console.log("a/b " + pair);

Then upon trying to add liquidity with:

    // ADD A AND B TOKENS TO SPOOKY LP PAIR
    const tx1 = await spookyRouterContract.addLiquidity(
      aContract.address,
      bContract.address,
      BigNumber.from("0x16345785D8A00"),
      BigNumber.from("0x16345785D8A00"),
      BigNumber.from("0x16345785D8A00"),
      BigNumber.from("0x16345785D8A00"),
      deployer.address,
      BigNumber.from("0x621df7f5000"),
    );
    console.log("https://ftmscan.com/tx/" + tx1.hash);
    await tx1.wait();

I get the error mentioned above:

Error: VM Exception while processing transaction: reverted with reason string 'TransferHelper: TRANSFER_FROM_FAILED'

I know that the creating LP pair isn't necessary as it is internal to the UniswapRouterV2's addLiquidity but just for completeness I am showing it. Thanks for your help.

rsmets
  • 141
  • 1
  • 6

1 Answers1

0

I got it to work! These solutions 1, 2, 3 were super helpful for me to piece it together...

I needed to either: A) include the {value: 125} param to the addLiquidityEth for the transfer of the LP to my address or B) I could have approved the spending on the pair's contract then used addLiquidity. This would be necessary if pairing with none native (i.e. weth on ethereum or wftm or fantom) tokens I believe.

Solution A):

    // ADD A TOKENS TO SPOOKY LP PAIR
    console.log("spookyRouter: addLiquidityETH");
    const tx1 = await spookyRouterContract.addLiquidityETH(
      aContract.addr,
      BigNumber.from("0x16345785D8A000"),
      BigNumber.from("0x16345785D7A000"),
      BigNumber.from("0x16345785D7A000"),
      deployer.address,
      BigNumber.from("0x621df7f5000"),
      {value : 125}
    );
    console.log("https://ftmscan.com/tx/" + tx1.hash);
    await tx1.wait();

Solution B):

    // APPROVE THE LP PAIR
    console.log("uniswapPair: approve");
    const txAlp = await spookyPairContract.approve(deployer.address, BigNumber.from("0x2B5E3AF16B188000"));
    console.log("https://ftmscan.com/tx/" + txAlp.hash);
    await txAlp.wait();
// ADD VINYL TOKENS TO SPOOKY LP PAIR
console.log("spookyRouter: addLiquidity");
const tx2 = await spookyRouterContract.addLiquidity(
  contractInfo.tomb.addr,
  wftmAddress,
  BigNumber.from("0x16345785D8A00"),
  BigNumber.from("0x16345785D8A00"),
  BigNumber.from("0x16345785D8A00"),
  BigNumber.from("0x16345785D8A00"),
  deployer.address,
  BigNumber.from("0x621df7f5000"),
);
console.log("https://ftmscan.com/tx/" + tx2.hash);
await tx2.wait();

rsmets
  • 141
  • 1
  • 6