0

I have been successfully swapping erc20 tokens using the exactInputSingle method, but I can't figure out how to do a swap with ETH on either side of the swap and I can't find it in the docs or anywhere else..

I've tried using the multicall function with wrapETH and unwrapWETH9 but I always get STF errors when doing so.

Any help or an example would be so greatly appreciated, I've been stuck for a very long time.

1
  • STF means that SafeTransferFrom failed: either you don't have the correct approvals, or insufficient amounts in wallet.
    – kfx
    Commented Oct 25, 2022 at 6:09

1 Answer 1

1

to swap token, you have to interact with the swapRouterContract and allow that contract to get ether out of your wallet. For this, you need to use ERC20 contract's approve function to allow swapRouterContract. First step is create an ERC20 contract

  const provider = new ethers.providers.JsonRpcProvider(URL)
  // this is goerli
  // https://goerli.etherscan.io/token/0xb4fbf271143a4fbf7b91a5ded31806e42b2208d6
  // you could find erc20 abi online
  addressOfWeth="0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6"
  const ERC20Contract = new ethers.Contract(addressOfWeth, ERC20ABI, provider);

then you have to call the approve

await ERC20Contract
    // connectedWallet is the signer
    .connect(connectedWallet)
    // define approvalAmount
    .approve(swapRouterAddress, approvalAmount)


// https://docs.uniswap.org/protocol/reference/deployments
const swapRouterAddress = "0xE592427A0AEce92De3Edee1F18E0157C05861564";

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.