0

I've written a script to buy tokens in javascript. The issue is I keep getting an error showing abnormally high transaction fees. This is my script

const buyFunction = this.pancakeContract.methods.swapExactETHForTokens(
    this.w3HTTP.utils.toHex(this.w3HTTP.utils.toWei('0', 'ether')), // amountOutMin: set to 0 or calculate based on slippage
    [this.config.constants.liquidityPairAddress, tokenAddress], // path
    this.config.settings.walletAddress, // to: Recipient of the tokens
    Math.floor((Date.now() / 1000) + 60 * 2) // deadline: Current time + 2 minutes
  )
  const nonce = await this.w3HTTP.eth.getTransactionCount(this.config.settings.walletAddress)
  const amount = this.w3HTTP.utils.toWei(amt, 'ether');
  
  const gasPrice = this.w3HTTP.utils.toWei('3', 'gwei');
  const gas = this.w3HTTP.utils.toHex(Number(config.settings.gasAmount));
  const walletBalance = await this.w3HTTP.eth.getBalance(this.config.settings.walletAddress);

  const rawTransaction = {
    "from": this.config.settings.walletAddress,
    "gas": this.w3HTTP.utils.toHex(gas),  // 250,000Wei
    "gasPrice": this.w3HTTP.utils.toHex(gasPrice),
    "to":this.config.constants.pancakeswapRouterAddress,
    "value":this.w3HTTP.utils.toHex(amount),
    "data":buyFunction.encodeABI(),
    "nonce":nonce
  };

  const signedTx = await this.w3HTTP.eth.accounts.signTransaction(rawTransaction, this.config.keys.walletPrivateKey);
  // console.log("signed tx: ", signedTx)

  const receipt = await this.w3HTTP.eth.sendSignedTransaction(signedTx.rawTransaction).catch(console.log);

When I run this, with an amt value of 0.0000001BNB I get the error

'err: insufficient funds for gas * price + value: address 0x700C9A0c93616918E2685b4cf5115e26C8d2DF74 have 23129168874516353 want 87741903484150489762066983472 (supplied gas 300000)'

Why do I get these abnormally high transaction fee? Could I be using the wrong units in the rawTransaction?

1 Answer 1

0

When setting amount, I decided to set its type to Number like this

const amount = Number(this.w3HTTP.utils.toWei(amt, 'ether'));

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.