You can deploy the contract in your development
testrpc, and check the gas for that transaction.
const tx = await MyContract.new();
const gasUsed = tx.receipt.gasUsed;
Hope this helps.
When you sign an Ethereum transaction, you specify a gasLimit
. Not the block gasLimit
of 29941438 that appears in your data, but your transaction gasLimit
. This is the maximum number of gas units that you will ever pay for. A plain ETH transfer to an EOA always consumes 21000 gas units, so for such a transaction you would set the transaction gasLimit
to 21000.
Suppose that your transaction ends up getting mined in a block with a baseFeePerGas
of 10 gwei. This means that for each 1 of the 21000 gas units that your transaction will consume, you will pay a 10 gwei “block inclusion fee”. This fee is a “protocol fee” and it is burned, it doesn’t go to the miner. If you have offered a maxFeePerGas
of more than 10 gwei, e.g. 15 gwei, then there is a 5 gwei surplus (per gas unit). The miner will try to claim as much as possible of those 5 gwei as “priority fee” (tips)… but only up to maxPriorityFeePerGas
. So e.g. if you have specified maxPriorityFeePerGas
of 2 gwei, then the miner will take 2 gwei and you will be refunded the extra 3 gwei you had paid upfront (when you offered 15 gwei per gas unit). If on the other hand you had specified a higher maxPriorityFeePerGas
of e.g. 9 gwei, then the miner will take the entire 5 gwei as tips, but not any more that that, because you specified an absolute maximum maxFeePerGas
of 15 gwei.
So the absolute maximum amount of ETH you will pay per gas unit is always maxFeePerGas
. So if you specify a transaction gasLimit
of 21000 gas units, and specify a maxFeePerGas
of 15 gwei per gas unit, then the absolute maximum ETH that you could ever possibly be charged is 21000 × 15 = 315000 gwei, or 0.000315 ETH.
The USD value of those 0.000315 ETH will depend on the current market rate.
Best Answer
Found it! https://docs.ethers.io/v5/api/providers/provider/#Provider-getGasPrice
Hint: the search functionality on docs.ethers.io isn't great - this method didn't show up by searching for "gasprice": https://docs.ethers.io/v5/search/?search=gasprice