I'm trying to make a swap programmatically, I did the following approvals using ethers.js:

token0.approve(wallet.address, amount);
token1.approve(wallet.address, amount);
token0.approve(UNISWAP_ROUTER_ADDRESS, amount);
token1.approve(UNISWAP_ROUTER_ADDRESS, amount);

Then I calculate the amounts and minimum just fine and submit my transaction, but although the approvals go through and everything is fine my transaction fails with the following error: Fail with error 'TransferHelper: TRANSFER_FROM_FAILED'.

Should I approve something else also, what am I missing? (sry but I'm new into this)

tx hash on rinkeby: https://rinkeby.etherscan.io/tx/0xa172511c960e67abb1a8e8bb90241048fd4d393113976052f2e6d7f6c63e048a

sample coin on rinkeby: https://rinkeby.etherscan.io/token/0xf2f0f119f819c457a8b8230306396985dff06082

PS1: Using another wallet I submit my coin to my local uniswap installation which is connected to rinkeby and a pool is created. However if I interact with uniswap with my other wallet (that is used programmatically) then the issue above doesnt happen anymore...

PS2: The error originates from UniswapRouter02 right here but my solidity knowledge is limited.

function safeTransferFrom(address token, address from, address to, uint value) internal {
    // bytes4(keccak256(bytes('transferFrom(address,address,uint256)')));
    (bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x23b872dd, from, to, value));
    require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: TRANSFER_FROM_FAILED');

In Uniswap, each token pair has its own address, that is the address for that liquidity pool. In your code above, you have given approval to the router address and not the token pair address. You can call the getPair(address token1, address token2) of UniswapFactory and give approval to that token pair.

  • That's not exact if you are calling the router you have to approve the router address.
    – Ismael
    Aug 22 '21 at 19:45

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.