[SDK] Calculation of the required gas estimateGas
#370
-
EnvironmentMainnet Acknowledgement
Issue DescriptionThere is a dependence of transaction cost estimation on the gas limit. For this reason, there are questions:
Expected BehaviorEstimation of gas showing a close estimate of gas to the actual value so that a user in metamask doesn't see $13 instead of 5 cents of the real value. Code ExampleWith zkSync SDK const provider = new Provider(chain.rpcUrls.default.http[0])
const data = encodeFunctionData({
abi,
functionName,
args,
})
const zkSyncGas = await provider.estimateGas({
to: toAddress,
data,
value: 0n,
}) with Viem const gas = await publicClient.estimateContractGas({
address: toAddress,
abi,
functionName,
args,
account,
}) Equally inaccurate Repo Link (Optional)No response |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
-
@RomanYarinski Note, that since state diffs are used, the subsequent transactions in a batch can have smaller fee due to the fact that they reused the state storage slots. So depending on the sort of the contract you are calling you can get very different cost of the first and the second call, but when estimating gas we never know whether your transaction will come as one of the first ones in a batch (and will have to pay the full price) or it will continue receiving the benefits of the previous transactions in the current batch that have already paid for the state changes. Also, the calculations in the table seem to be a bit off. On the 23rd of February the gas price on zksync was around 0.1 gwei, in other words 4200000 gas would probably cost around |
Beta Was this translation helpful? Give feedback.
@RomanYarinski Note, that since state diffs are used, the subsequent transactions in a batch can have smaller fee due to the fact that they reused the state storage slots. So depending on the sort of the contract you are calling you can get very different cost of the first and the second call, but when estimating gas we never know whether your transaction will come as one of the first ones in a batch (and will have to pay the full price) or it will continue receiving the benefits of the previous transactions in the current batch that have already paid for the state changes.
Also, the calculations in the table seem to be a bit off. On the 23rd of February the gas price on zksync was around…