0
 function addLiquidity(
    address tokenA,
    address tokenB,
    uint amountADesired,
    uint amountBDesired,
    uint amountAMin,
    uint amountBMin,
    address to,
    uint deadline
) external virtual override ensure(deadline) returns (uint amountA, uint amountB, uint liquidity) {
    (amountA, amountB) = _addLiquidity(tokenA, tokenB, amountADesired, amountBDesired, amountAMin, amountBMin);
    address pair = UniswapV2Library.pairFor(factory, tokenA, tokenB);
    TransferHelper.safeTransferFrom(tokenA, msg.sender, pair, amountA);
    TransferHelper.safeTransferFrom(tokenB, msg.sender, pair, amountB);
    liquidity = IUniswapV2Pair(pair).mint(to);
}
  • I have minted 2 ERC Token say Token1 & Token2 and also approved the address of deployed Router v2 contract from Token contracts.
  • Also generated the INIT_CODE_HASH in the factory contract and added to the hash value by removing 0X.
  • While trying to transact addLiquidity function I am getting an error "TransferHelper: TRANSFER_FROM_FAILED".

I have used the deployed code of Router & Factory contracts from ether scan. I am unable to point out the issue any help will be appreciated.

Thanks in Advance

2
  • Have you been approving your contract in both token1 and token2 contract (ie spender=your contract, token owner=msg.sender and value=amountA or B, accordingly) ? Nov 28, 2021 at 22:29
  • Agree with DrGorilla, without knowing more, I think the two ERC20 tokens you deployed can't be moved by the contract that's implementing addLiquidity. You may need to do: tokenA.approve(ContractCallingSafeTrasnfer, AmountUsuallyMAXINT)
    – ori
    Feb 14, 2022 at 5:57

1 Answer 1

1

UniswapV2Pair needs to be deployed as well for addLiquidity to work. Use remix editor to deploy the contract and call addLiquidity from remix. If transaction still fails remix debugging tool can be used to track the code flow step by step

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Nov 28, 2021 at 22:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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