0

I hope somebody can help

I am trying to sell a token via Uniswap V2 Router02 (Token CA: 0x4ed4E862860beD51a9570b96d89aF5E1B0Efefed) (This token is a legit one, there is no scam here), I have successfully bought it using swapExactETHForTokens: Base Scan

But when it comes to swapExactTokensForETH, though I approved the spending token to Uniswap V2 Router02: Base Scan, the transaction always gets reverted!: Base Scan

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

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

const SWAP_ROUTER02_ADDRESS = "0x4752ba5dbc23f44d87826276bf6fd6b1c372ad24";
const WETH_ADDRESS = "0x4200000000000000000000000000000000000006";
const TOKEN_ADDRESS = "0x4ed4E862860beD51a9570b96d89aF5E1B0Efefed";

const erc20 = new ethers.Contract(TOKEN_ADDRESS, ERC20_ABI, signer);

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

async function sell() {
  const balance = await erc20.balanceOf(signer.address);
  console.log(balance);
  const approveTokenTx = await erc20.approve(
    "0x4752ba5dbc23f44d87826276bf6fd6b1c372ad24",
    ethers.parseEther("10000000000000")
  );
  const receipt1 = await approveTokenTx.wait();
  console.log(receipt1);

  const params = {
    amountIn: ethers.parseEther(balance.toString()),
    amountOutMin: 0,
    path: [TOKEN_ADDRESS, WETH_ADDRESS],
    to: signer.address,
    deadline: Math.floor(new Date().getTime() / 1000) + 60 * 20,
  };

  const tx = await router.swapExactTokensForETH(
    params.amountIn - params.amountIn / BigInt(2),
    params.amountOutMin,
    params.path,
    params.to,
    params.deadline,
    {
      gasLimit: 300000,
    }
  );
  const receipt = await tx.wait();
  console.log(receipt.hash);
}
sell();

What might go wrong here?

1 Answer 1

0

It says ERC20InsufficientBalance.

Are you meaning to pass that amountIn? 4878975283589739380.500000000000000000

Seems you don't actually have that number of token to move.

"value":"4878975283589739380500000000000000000",
"from":"0xc6487276cd76df5075125c90c524ebcf6be901bb"
"to":"0x7c327d692b72f60b28aecedbcc1ba784712fe7b2"

Seems you have 9.757950567179478761

*10**18 =9757950567179478761

enter image description here

Use that number.

2
  • Thank you so much!
    – duyanhhz
    Apr 23 at 8:15
  • 1
    @duyanhhz Much love
    – Maka
    Apr 23 at 8:21

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.