I wanna send transaction and before that i wanna calculate the gas fee and input value as much as : finalAmount = value - gasFee

For doing this i get gas price from provider this way:

const gasP = _provider.getGasPrice()

And also estimate gas:

const estimateGas = await _signer.estimateGas(user.address)

I set the value this way:

 const fee = (await gasP).mul(estimateGas)

 const finalAmount = amount.sub(fee)

And thats my transaction function:

async function claim() {

      from: user,
      to: Receptient,
      gasPrice: gasP,
      gasLimit: ethers.utils.hexlify(100000),
      value: finalAmount,

But when i call the function, my MetaMask pops up and want to send transaction with this amount of fee: 0.00000411GoerliETH

And transaction fails with this amount fee.

How can i fix that? it looks estimate gas less than the amount which i need to send my transaction.

1 Answer 1

const gasPrice = await _provider.getGasPrice();

Use the getGasPrice method provided by the _provider object to get the current gas price on the Ethereum network, and then use that gas price when sending your transaction.

  • Actually, i get it as 'gasP', but my transaction fails, i solved it by adding gas price manually , but i don't like this way i want to calculate gas fee from blockchain data.
    – Alireza
    Dec 10, 2022 at 4:40
  • getGasPrice should get the gas price from the network - do you get any errors?
    – VX3
    Dec 11, 2022 at 22:10
  • No, when i call gas price, it get gas price from network for me, but the problem is when multiply gasPrice and and estimateGas together, this number can not cover transaction fee
    – Alireza
    Dec 12, 2022 at 7:10
  • I could be missing something but why gasprice x estimategas?
    – VX3
    Dec 30, 2022 at 23:04
  • So how can i calculate & set enough gas for cover transaction fee? its wrong?
    – Alireza
    Jan 2, 2023 at 7:44

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.