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 getbase 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 getbase 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.
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.assert
s 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 toestimateGas
doesn't have the right transaction details. (E.g. it's missing theto
anddata
fields.)estimateGas
will solve thebase fee exceeds gas limit
error???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.estimateGas
call, you should get the correct gas estimate, which will let you pass the rightgasLimit
.