0

I am trying to programmatically swap ETH for a custom ERC 20 token (let me take a meme token as an example)

Here is my code that I swapped ETH for USDC:

const { ethers } = require("hardhat");
const { fs } = require("fs");
require("dotenv").config();
const SWAP_ROUTER02_ABI = require("./ABIs/SwapRouter02.json");

const provider = new ethers.JsonRpcProvider(process.env.BASE_RPC);
const signer = new ethers.Wallet(process.env.PRIVATE_KEY, provider);

const SWAP_ROUTER02_ADDRESS = "0x2626664c2603336E57B271c5C0b26F421741e481";
const WETH_ADDRESS = "0x4200000000000000000000000000000000000006";
const USDC_ADDRESS = "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913";

const router = new ethers.Contract(
  SWAP_ROUTER02_ADDRESS,
  SWAP_ROUTER02_ABI,
  signer
);

async function main() {
  const inputAmount = ethers.parseEther("0.0001");

  const params = {
    tokenIn: WETH_ADDRESS,
    tokenOut: USDC_ADDRESS,
    fee: 3000,
    recipient: signer.address,
    amountIn: inputAmount,
    amountOutMinimum: 0,
    sqrtPriceLimitX96: 0,
  };

  const data = router.interface.encodeFunctionData("exactInputSingle", [
    params,
  ]);

  const txArgs = {
    to: SWAP_ROUTER02_ADDRESS,
    value: inputAmount,
    data: data,
    gasLimit: "300000",
  };

  const tx = await signer.sendTransaction(txArgs);
  const reciept = await tx.wait();
  console.log(reciept.hash);
}

main();

When I executed the code above, it went well: Base Scan

However, when I change USDC_ADDRESS to some other meme coins, i.e 0x8D70e2C2cB0dD6a5F6670FCA157754e922B1E245, I got the error transaction execution reverted

So my questions are:

  • What is the problem here?
  • Do I have to specify any pool with its fees or anything to swap?
2
  • Needs more information. Usually is some tax mechanic in the s- coin. Sometimes a badly written one that is hard to get around, other times it's just a case of needing to account for the tax on transfer in the minimum amount out or max amount in. Show a transaction hash from a fail and can troubleshoot the specific case.
    – Maka
    Commented Apr 22 at 9:53
  • 2
    In your case the pool doesn't exist. Token addresses plus fee compute the address and there isn't one with those tokens and that fee.
    – Maka
    Commented Apr 22 at 9:56

1 Answer 1

1

Usually is some tax mechanic in the s- coin. Sometimes a badly written one that is hard to get around, other times it's just a case of needing to account for the tax on transfer in the minimum amount out or max amount in

Judging from this transaction at the same address:

It seems it is just a case of not passing a path that exists.

enter image description here

The pool being computed is: 0x1d1a15d767dd76521f9476b0395a03924a2b6407

Which has no liquidity and has never been seeded.

Tiers are 0.01%, 0.05%, 0.3%, and 1% 100 | 500 |3000 | 10000

Check a pool exists by calling the factory contract getPool and passing both token addresses and fee tier, call reserves or get a quote through the quoter to check it is seeded with enough liquidity, before trying to swap on it.

2
  • So I have to get the pool with each corresponding fee tiers, and find ones that have enough liquidity to swap, right?
    – duyanhhz
    Commented Apr 22 at 10:15
  • 1
    Exactly that. Else you might use other methods such as a subgraph query for the pair with those tokens ordered by tvl for example. | cryptomarketpool.com/…
    – Maka
    Commented Apr 22 at 10:40

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.