I have noticed that there is a discrepancy between the amount of gas used by a transaction is almost identical:
I'm making a call to a smart contract,
with the same parameters twice in a row,
and the only difference between the 2
is that I'm setting gasLimit
to the exact value
returned by eth_estimateGas
in the 1st one,
and I'm setting gasLimit
to eth_estimateGas * 1.1
in the 2nd one.
// with exact estimated amount of gas
const estimatedGas2 = (await expendGasSc.estimateGas.updateState(1_000_000_123n)).toBigInt();
console.log('estimatedGas2', estimatedGas2);
const gasLimit2 = estimatedGas2 * 1n;
console.log('gasLimit2', gasLimit2);
const txResponse2 = await (await expendGasSc
.updateState(
1_000_000_123n,
{ gasLimit: gasLimit2 },
))
.wait();
const gasUsed2 = txResponse2.gasUsed.toBigInt();
console.log('gasUsed2', gasUsed2);
// with 110% of estimated amount of gas
const estimatedGas3 = (await expendGasSc.estimateGas.updateState(1_000_000_123n)).toBigInt();
console.log('estimatedGas3', estimatedGas3);
const gasLimit3 = estimatedGas3 * 11n / 10n;
console.log('gasLimit3', gasLimit3);
const txResponse3 = await (await expendGasSc
.updateState(
1_000_000_123n,
{ gasLimit: gasLimit3 },
))
.wait();
const gasUsed3 = txResponse3.gasUsed.toBigInt();
console.log('gasUsed3', gasUsed3);
Here's the output, which shows that:
- when
gasLimit
is set to 400,000,gasUsed
is 320,000 - when
gasLimit
is set to 440,000,gasUsed
is 352,000
estimatedGas2 400000n
gasLimit2 400000n
gasUsed2 320000n
estimatedGas3 400000n
gasLimit3 440000n
gasUsed3 352000n
Why does the 2nd invocation expend an additional 32,000 in gas?
(I expected the gasUsed
value to be the same in both cases.)
Here's the smart contract:
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.18;
contract ExpendSomeGasDemo {
uint256 public state;
function updateState(
uint256 newState
)
public
returns (uint256 updatedState)
{
state = newState;
updatedState = newState;
}
}
Note that this contract is deployed on Hedera Testnet:
0x9C58D0159495F7a8853A24574f2B8F348a72424c
Note that the Javascript example above is using ethers.js.