3

I am running tests of my !ink smart contracts using @727-ventures/typechain-polkadot module. But when I am running tests, sometimes getting OutOfGas Error. The strange thing is that not all cases are failing. I tried to put gasLimit manually but didn't work. Here is the code for calling smart contract.

const MAX_CALL_WEIGHT = new BN(5_000_000_000_000_000).isub(BN_ONE)
const PROOFSIZE = new BN(1_000_000_000)
const gasLimit = api?.registry.createType('WeightV2', {
  refTime: MAX_CALL_WEIGHT,
  proofSize: PROOFSIZE,
}) as WeightV2

const result = await contract.query.func(...args, { gasLimit })
const gasRequired = preview.gasRequired as WeightV2
const newGasLimit = api?.registry.createType('WeightV2', gasRequired) as WeightV2
const res = await contract.tx.func(...args, { gasLimit: newGasLimit })

This code is running well sometimes but getting error sometimes.

1 Answer 1

2

Looking at the code you provided, couple of things needs to be modified:

  1. The line
const gasRequired = preview.gasRequired as WeightV2

should probably be

const gasRequired = result.gasRequired as WeightV2

But that can be destructured as well.

  1. As per the docs, you should also include storageDeposit in the options

  2. Options should be the first parameter passed to the .func method

Also, you don't need to create the type again, as result.gasRequired should be of the correct type already.

So, something like this should work:

const MAX_CALL_WEIGHT = new BN(5_000_000_000_000_000).isub(BN_ONE);
const PROOFSIZE = new BN(1_000_000_000);
const gasLimit = api?.registry.createType('WeightV2', {
  refTime: MAX_CALL_WEIGHT,
  proofSize: PROOFSIZE,
});
const storageDepositLimit = null;
const {storageDeposit, gasRequired} = await contract.query.func({ gasLimit, storageDepositLimit }, ...args);
const res = await contract.tx.func({ gasLimit: gasRequired, storageDepositLimit: storageDeposit }, ...args);

3
  • The query result only have value, gasRequired, gasConsumed value. And I have set storageDepositLimit as null but the result was same.
    – Wolf
    Jun 27, 2023 at 1:51
  • What are the polkadot.js and typechain-polkadot versions you're using? And, maybe a silly question, but what account is signing the tx, and does it have enough funds to pay for gas?
    – codespool
    Jun 27, 2023 at 8:18
  • The typechain-polkadot version is the latest, v1.1.0 And of course account has enough fund to run the tx.
    – Wolf
    Jun 27, 2023 at 12:53

Your Answer

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

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