5

I am deploying flipper ink! smart contract to shibuya, but when I tried to read the value using get() I am getting this error:

{ Err: { Module: { index: 70 error: 0x02000000 } } }

I also tried deploying it to rococo and getting similar error:

{ Err: { Module: { index: 40 error: 0x02000000 } } }

any idea what is the error and why ?

2 Answers 2

6

This is because of Shibuya has updated to Weights v2.

If you are getting this error on polkadot.js we are working on a fix for it https://github.com/polkadot-js/apps/pull/8538.

If you are getting this from your code you can fix like this:

Weights v2 requires refTime and proofSize. refTime is equal to v1 weight.

Both values can be fetched by sending the txn as a query and using the gasRequired value.

const { gasRequired, result, output } = await contract.query.flip(
  address,
  {
    gasLimit: api.registry.createType('WeightV2', {
      refTime,
      proofSize,
    }) as WeightV2,
    storageDepositLimit,
  }
)

const gasLimit = api.registry.createType('WeightV2', gasRequired) as WeightV2

// Send the transaction, like elsewhere this is a normal extrinsic
// with the same rules as applied in the API (As with the read example,
// additional params, if required can follow)
await contract.tx
  .flip({
    gasLimit: gasLimit,
    storageDepositLimit
  })
  .signAndSend(account, async (res) => {
    if (res.status.isInBlock) {
      console.log('in a block')
    } else if (res.status.isFinalized) {
      console.log('finalized')
    }
  })
2
  • What should the initial values of refTime and proofSize be? Whenever I run a query like this I get { refTime: '0', proofSize: '0' } returned as gasRequired.
    – forgetso
    Commented Jan 31, 2023 at 15:49
  • any call with { value: 0, gasLimit: -1 } fails after deploying with Shibuya, any way to fix this?
    – mastro
    Commented Feb 13, 2023 at 16:15
4

It looks like the same issue still occurs in 2023, moreover I realised that now interacting with Smart Contracts on https://polkadot.js.org/apps/ is broken as well. The only way to interact with them is by using https://contracts-ui.substrate.io/ which apparently has some kind of workaround for that WeightV2 issue. Nevertheless, as a fulfilment of the reply from Adam Wierzbicki, you need to add following parts to make your code work.

First of all import these at the top of the code block

import { BN, BN_ONE } from "@polkadot/util";
import type { WeightV2 } from '@polkadot/types/interfaces'

Somewhere below define two new variables

const MAX_CALL_WEIGHT = new BN(5_000_000_000_000).isub(BN_ONE);
const PROOFSIZE = new BN(1_000_000);

Execute query on the given function to retrieve the required gas limit

const storageDepositLimit = null;

const { gasRequired } = await contract.query.flip(
  account.address,
  {
    gasLimit: api?.registry.createType('WeightV2', {
      refTime: MAX_CALL_WEIGHT,
      proofSize: PROOFSIZE,
    }) as WeightV2,
    storageDepositLimit,
  }
);

Define the gas limit variable and execute transaction call

const gasLimit = api?.registry.createType('WeightV2', gasRequired) as WeightV2
   
await contract.tx
  .flip({
    gasLimit,
    storageDepositLimit
  })
  .signAndSend(alice, async (res) => {
    if (res.status.isInBlock) {
      console.log('in a block')
    } else if (res.status.isFinalized) {
      console.log('finalized')
    }
  });

If you need to only read some data from the Smart contract you can query it like in the code block below

const { result, output } = await contract.query.get(
  alice.address,
  {
    gasLimit: api?.registry.createType('WeightV2', {
      refTime: MAX_CALL_WEIGHT,
      proofSize: PROOFSIZE,
    }) as WeightV2,
    storageDepositLimit,
  }
);
   
// The actual result from RPC as `ContractExecResult`
console.log(result.toHuman());
    
// check if the call was successful
if (result.isOk) {
  // output the return value
  console.log("Success", output?.toHuman());
} else {
  console.error("Error", result.asErr);
}
2
  • This worked like a charm! Thank you!
    – Snowmead
    Commented May 18, 2023 at 20:18
  • My pleasure to help :) As far as I know the issue has been already addressed in the Polkadot.js dashboard, so probably they should release new version of library as well.
    – Pawel
    Commented May 19, 2023 at 7:56

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.