[Dev Tools] Discrepancy between gas cost computed inside paymaster and from tx response #794
-
Team or ProjectNo response EnvironmentTestnet Select the Dev Tool you are usingHardhat Plugins Provide the version of the tool (if applicable)No response Provide a brief description of the functionality you're trying to implement and the issue you are running into.I am running HardHat test against our Paymaster implementation. I am seeing a discrepancy between the gas cost computed inside our paymaster: uint256 gasCost = transaction.gasLimit * transaction.maxFeePerGas; and the one computed in the same way from the tx response (ethers.js): const gasCost = response.gasLimit * response.maxFeePerGas!; The outer one is slightly more. This is with the era in-memory node. Repo Link (Optional)No response Additional DetailsNo response |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 8 replies
-
It appears you are encountering a discrepancy in gas cost calculations between your Paymaster implementation in Solidity and the transaction response handled by ethers.js. This issue might be due to several factors:
To further investigate and resolve this issue, consider the following steps:
For more detailed discussions and support, you might want to visit the ZKsync developers discussions on Github. Your feedback helps us serve you better. If this fully addresses your question, please give it a thumbs up. If it doesn't, please give it a thumbs down. This will inform the community and team so we can improve our support. |
Beta Was this translation helpful? Give feedback.
-
Hi @cesarenaldi, could you please provide the paymaster contract source code you are using to reproduce the error? |
Beta Was this translation helpful? Give feedback.
-
Would this be enough? https://block-explorer.testnet.lens.dev/address/0x115B6D4aED14AD0F900a20819ABDAf915111bf50#contract |
Beta Was this translation helpful? Give feedback.
-
Implementation: https://block-explorer.testnet.lens.dev/address/0x534B89ea1ce82b2953F0eA662b42fDAcF03D4F0B#contract |
Beta Was this translation helpful? Give feedback.
-
Hi @cesarenaldi - The discrepancy you're noticing is expected behavior when interacting with a paymaster. Paymaster txs typically consume more gas than standard txs due to additional computations and operations involved in the process. For a detailed explanation of how gas estimation works with paymasters, check out the documentation here: Estimating Gas with Paymasters. Let me know if there's anything I can clarify! |
Beta Was this translation helpful? Give feedback.
-
Hi @cesarenaldi I checked the code you provided in private, but could not reproduce this issue. Based on my test run |
Beta Was this translation helpful? Give feedback.
Hi, there is a good answer for a similar problem here. Basically, you cannot unfortunately get an exact fee paid by a paymaster during paymaster execution in the transaction. You can improve your estimation by using
_maxRefundedGas
in postTransaction as explained in that discussion.I would recommand going with the approach I proposed above and avoid accounting the spending immediately during the transaction. I would observe the difference between the last recorded paymaster balance and the current balance during each operation where funds are moved (deposited or withdrawn) outside of a paymaster transaction. This way you can accurately and in bulk account the transactions. As an addition…