0

This is my code in vsc

I logged the gas numbers:

Max fee for gas: 240394607536 Gas estimate: 149447303768

async function main() {

  //create new wallet
  const wallet = new Wallet(document.getElementById("prvkey").value, alchemy)
  const abi = ["function transfer(address to, uint256 value)"];
  const amountToSend = 30000;
  //this should work now
  const decimals = 18;
  const amountToSendInDecimals = BigNumber.from(amountToSend).mul(decimals);
  console.log(amountToSendInDecimals);

  const iface = new Utils.Interface(abi);
  const data = iface.encodeFunctionData("transfer", [faucetAddress, Utils.parseUnits(amountToSendInDecimals.toString(), "wei"),]);

  //get gas values
  const feeData = await alchemy.core.getFeeData();
  console.log("Max fee for gas: " + feeData.maxFeePerGas);
  console.log("Gas estimate: " + feeData.gasPrice);

  const transaction = {
    to: HMSBcontract,
    nonce: await alchemy.core.getTransactionCount(wallet.getAddress()),
    maxPriorityFeePerGas: feeData.maxPriorityFeePerGas, // This is the fee that the miner will get
    maxFeePerGas: feeData.maxFeePerGas, // This is the maximum fee that you are willing to pay
    type: 2, // EIP-1559 transaction type
    chainId: 137, // Corresponds to POlYGON_MAINNET - 5 would be goerli
    data: data, // encoded data for the transaction
    gasLimit: Utils.parseUnits(feeData.maxFeePerGas.toString(), "wei"), // normal 250.000 - this causes error?
  };

  // Send the transaction and log it.
  const sentTx = await wallet.sendTransaction(transaction);
  console.log(sentTx);

  await delay(3000);

  getHBalance();

}

I try to send a transaction on the polygon blockchain using ethers.js which is built into the alchemy sdk. I created a dapp using webpack and the getBalance functions and the rest is working, only the transaction fails. I had the issue of the amountToSendInDecimals being too big at first but now that I am using a BigNumber that should be fine and I don't think this new issue is related to that but who knows. I want to send an erc20 token.

Paul Play
  • 1
  • 1
  • I was able to fix this issue by using feedata.maxFeePerGas for maxPriorityFeePerGas as well. No idea why, but that works! Also, my gasLimit was wrong! I set it to "250000" now and that does the job. – Paul Play May 17 '23 at 20:04

0 Answers0