3

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%.

2
  • if you just perform a single contracts call, and not a batch transaction, is the gasRequired enough (it should be).
    – Pierre
    May 22 at 16:07
  • Also if you could write js snippets that I could actually use to repro in the polkadot js app, this might help me investigate that further.
    – Pierre
    May 22 at 16:08

1 Answer 1

1

Are your calls failing with OutOfGas when submitted individually or only when batched?

The difference may be due to the different runtime apis called under the hood:

  • transactionPaymentApi.queryInfo for tx.paymentInfo
  • contractsApi.call for contract.query

There may be extra costs for batching that are not estimated by contractsApi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.