In polkadotJS there are two ways of getting gas estimates (specifically WeightV2
) before you send a contract transaction.
The first uses query
and returns a gasRequired
value in the result.
const extrinsic = contract.query[message.method](address, initialOptions, ...encodedArgs)
const response = await extrinsic
if (response.result.isOk) {
// create a new set of options using gasRequired and storageDeposit
let options = getOptions(this.api, message.isMutating, value, response.gasRequired, response.storageDeposit)
The second uses tx
and takes a value from paymentInfo
// options here uses the gasRequired from the initial query as the gasLimit
const extrinsicTx = contract.tx[message.method](options, ...encodedArgs)
const paymentInfo = await extrinsicTx.paymentInfo(address)
paymentInfo
always gives a higher refTime
value than gasRequired
.
gasRequired 8,827,448,665
paymentInfo 9,536,660,665
Using the value from gasRequired
causes OutOfGas
errors when batching transactions. Using the value from paymentInfo
causes the batched transactions to succeed.
Why are there two different ways of getting gas estimates and why do they differ in value?
Note: This answer suggests that the contract state being modified between transactions could be the reason for the difference in gas and to increase gas by 1%. However, the difference between gasRequired
refTime and paymentInfo
refTime is ~8%.