0

I'am trying to swap multiple tokens in single transaction (multicall). The transaction is going into pending state and failing after some time. The code snippet and the transaction hash is attached below . https://goerli.etherscan.io/tx/0x12d2045c8d08f24baabecff7e27dc69b08a1fac628c4bb0c4c224cbd522173d3

Code Snippet:

    const walletAddress = "0x2142EA5a7A922a3F5A98A81567FA691187E550B1";
 
    const param1 = {
      tokenIn: WETHAddress,
      tokenOut: USDCAddress,
      fee: 3000,
      recipient: walletAddress,
      deadline: Math.floor(Date.now() / 1000) + 60 * 10,
      amountIn: ethers.utils.parseEther("0.00001"),
      amountOutMinimum: 0,
      sqrtPriceLimitX96: 0,
    };

    const a = uniswapContractInstance.interface.encodeFunctionData(
      "exactInputSingle",
      [param1]
    );

    const param2 = {
      tokenIn: WETHAddress,
      tokenOut: UNIADDRESS,
      fee: 3000,
      recipient: walletAddress,
      deadline: Math.floor(Date.now() / 1000) + 60 * 10,
      amountIn: ethers.utils.parseEther("0.00001"),
      amountOutMinimum: 0,
      sqrtPriceLimitX96: 0,
    };
    const b = uniswapContractInstance.interface.encodeFunctionData(
      "exactInputSingle",
      [param2]
    );
   
    const calls = [a, b];
    const multiCall = uniswapContractInstance.interface.encodeFunctionData(
      "multicall",
      [calls]
    );
    const provider = await new ethers.providers.Web3Provider(window.ethereum);

    const targs = {
      to: V3SwapRouterAddress,
      from: accounts,
      data: multiCall,
      gasLimit: "3456780",
      // value: "100000000",
      // // gasPrice: "345706",
      // maxFeePerGas: "15000000000",
      // maxPriorityFeePerGas: "1500000000",
    };
    // const a = await window.ethereum.request({ method: "eth_accounts" });
    // const account = window.ethereum._state?.accounts;
    // let check = await provider.getCode(V3SwapRouterAddress);
    // console.log({ check });
    const signer = await provider.getSigner();

    const tx = await signer.sendTransaction(targs);
    // const estimate = await signer.estimateGas(targs);

    const reciept = await tx.wait();
    console.log({ reciept });
  };  ```

2 Answers 2

1

Please check the status message on Etherscan. In this case it is:

Status: Fail with error 'STF'

The STF code means that the safeTransferFrom function failed. This means that either:

  • that you don't have allowance to transfer the required amount of tokens;
  • or that the source address does not have enough tokens.

In the linked transaction's case it is the former, insufficient allowance for WETH.

To solve it, approve more tokens by calling the approve() function of the token's contract (in your case the token is WETH), and passing the Uniswap router's address as the first parameter:

const tokenContract = new ethers.Contract(tokenAddress, tokenABI, provider);
tokenContract.approve(UNISWAP_ROUTER_ADDRESS, amount);
5
  • Hi @kfx , Can you please add the code snippet for approving tokens which you have mentioned above Jan 10, 2023 at 7:13
  • @BejoJeffrin done
    – kfx
    Jan 10, 2023 at 10:16
  • Tried the approval and sendtransaction method , but the transaction is still failing @kfx. I have attached the transaction below goerli.etherscan.io/tx/… Jan 11, 2023 at 8:57
  • That is out of gas. Maybe @Omar was prescient :) Seriously though, it's not failing with STF, so I consider the question answered. Don't add new questions in comments.
    – kfx
    Jan 11, 2023 at 19:44
  • i was swapping the order of token in and token out . Now i corrected it , it is working fine . Thanks a lot Jan 12, 2023 at 5:32
0

The transaction you provided is failing due to an "out of gas" error, which means it ran out of gas before it could be completed.

You can try increasing the gasLimit value in your transaction parameters to fix this issue because some operations in the transaction are more complex or require more gas to execute.

1

Your Answer

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

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