This question already has an answer here:

I am using web3.js v1 in order to sign and send transactions:

async function signAndSend(transaction) {
    let options = {
        to  : transaction._parent._address,
        data: transaction.encodeABI(),
        gas : SOME_GAS_AMOUNT
    };
    let signedTransaction = await web3.eth.accounts.signTransaction(options, PRIVATE_KEY);
    return await web3.eth.sendSignedTransaction(signedTransaction.rawTransaction);
}

In order to compute how many gas units were used, I do:

let gasPrice = new BigNumber(await web3.eth.getGasPrice());
let bgnBalance = new BigNumber(await web3.eth.getBalance(PUBLIC_KEY));
await signAndSend(myTransaction);
let endBalance = new BigNumber(await web3.eth.getBalance(PRIVATE_KEY));
console.log(`Total cost: ${bgnBalance.minus(endBalance).div(gasPrice)} gas units`);

I observe the following:

  • When SOME_GAS_AMOUNT is too low, I get base fee exceeds gas limit
  • When SOME_GAS_AMOUNT is high enough, I always get the same total cost

My conclusion is, just use a large enough (hard-coded) value.

Am I right?

If yes, then what's the point in this parameter to begin with?

The docs for function signTransaction say:

gas - String: The gas provided by the transaction.

What does "gas provided by the transaction" even mean? I thought that I was the one providing gas for the transaction to take place!

By the way, initially, I used to set the gas parameter as follows:

gas: await transaction.estimateGas({from: PUBLIC_KEY});

However, this occasionally gave me the base fee exceeds gas limit error.

So I figured I was kind of on the wrong track with that one.

In short, any clarifications on the gas parameter would be highly appreciated.

Thank you very much for you help!

UPDATE:

I am not asking about the meaning of the term "gas".

Please refer to my observation (quoting from above):

I observe the following:

  • When SOME_GAS_AMOUNT is too low, I get base fee exceeds gas limit
  • When SOME_GAS_AMOUNT is high enough, I always get the same total cost

My conclusion is, just use a large enough (hard-coded) value.

Am I right?

If yes, then what's the point in this parameter to begin with?

If no, what exactly should I set this parameter to?

I initially used to set the gas parameter as follows:

gas: await transaction.estimateGas({from: PUBLIC_KEY});

However, this occasionally gave me the base fee exceeds gas limit error.

So I figured I was kind of on the wrong track with that one.

marked as duplicate by smarx, Achala Dissanayake, Richard Horrocks, Ismael, eth Aug 17 at 7:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • @smarx: Your proposed duplicate does not answer my inline questions. Moreover, it states something opposite to my observation: "If you set a very high gas price, you will end up paying lots of ether for only a few operations". Here, I explicitly stated that I see the same total cost no matter how high I set the gas parameter. I would be happy(ier) if you could refer to the specific doubts expressed in my question. I am not asking about the general meaning of the term "gas". Thank you. – goodvibration Aug 16 at 12:53
  • When code asserts or executes some other invalid opcode, all remaining gas is consumed. So if you set the gas limit too high, you're losing more ether. Your call to estimateGas doesn't have the right transaction details. (E.g. it's missing the to and data fields.) – smarx Aug 16 at 12:59
  • @smarx: So you reckon fixing my call to estimateGas will solve the base fee exceeds gas limit error??? – goodvibration Aug 16 at 13:01
  • You also have to have enough ether to cover gasLimit * gasPrice, even if you end up not needing to use all that gas. There's also a block gas limit, and miners may not attempt to run your transaction if it looks like it could exceed the limit for the block. So just using a really big number isn't a great strategy, though for transactions you know will exceed, aiming just a little high is usually fine. – smarx Aug 16 at 13:01
  • When you use multiple question marks like that, it looks (to me) like you're yelling. One question mark is sufficient to indicate a question. And yes, if you fix your estimateGas call, you should get the correct gas estimate, which will let you pass the right gasLimit. – smarx Aug 16 at 13:02