Uniswap – Fix TransferHelper: TRANSFER_FROM_FAILED Error When Adding Liquidity on Uniswap v2

blockchainethereumjsnodessolidityuniswap

 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

Best Answer

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

Related Topic