1
let polkaAPI = require("@polkadot/api")
let key = require("@polkadot/keyring")
let Key1 = new key.Keyring( { type:'sr25519'})
let UserKey = Key1.addFromMnemonic("mneomics keys ")
let polkaProvider = new polkaAPI.WsProvider("wss://rococo-contracts-rpc.polkadot.io/" )
let PAPI = await polkaAPI.ApiPromise.create( { provider : polkaProvider } )
let nonce = await PAPI.query.system.account(UserKey.address)
let bal = await PAPI.query.balances.account(UserKey.address)
INC_CON = new PCON.ContractPromise(PAPI , inc_meta, "5DVAK9xZSEiJzWCdZGNkUjWVuXHJEgtZDgELiqYZEKwaHz2m");
let x = await 
INC_CON.query.get( UserKey.address , { gasLimit : gl  } )
x.result.toHuman()

When i run this code on polkadot js it give following errors

{ Err: { Module: { index: '40', error: '0x02000000' } } }
1
  • Try running the same call from polkadot apps and the error will be correctly formatted for you using this code.
    – forgetso
    Jan 25 at 11:06

2 Answers 2

3

To understand the error and get a nice message from this error when you only have the index and error, we have to take a look into the code of Substrate.

The index number is the index of the pallet in the runtime from which the error originated. The error is likewise the index of that pallet's errors which is the exact one we're looking for. Both of these indices start counting from 0.

So taking your case error { Err: { Module: { index: '40', error: '0x02000000' } } } first thing we have to check is from which pallet is coming from.

In your case you are connecting to wss://rococo-contracts-rpc.polkadot.io/ which is a Substrate parachain for smart contracts deployed on Rococo, we can see the code here.

If we check the runtime of this parachain, we find that the pallet with the index 40 is the Pallet Contracts

// Smart Contracts.
Contracts: pallet_contracts::{Pallet, Call, Storage, Event<T>} = 40,

Now that we know where this error is coming from, we check the code of the pallet Contracts to find out which is the exact one we're looking for. 0x02000000 is 2 in hex which is OutOfGas:

/// The executed contract exhausted its gas limit.
    OutOfGas,
1

I receive a the same error when running similar code with my own abi JSON. The error decodes to contracts.OutOfGas, which is from the substrate contracts pallet. Note 0x02 probably corresponds to the index of the error.

import { Abi, ContractPromise } from '@polkadot/api-contract'
import { ApiPromise } from '@polkadot/api'
import { Keyring } from '@polkadot/keyring'
import { WsProvider } from '@polkadot/rpc-provider'
import abiJson from 'abi.json'
import { cryptoWaitReady } from '@polkadot/util-crypto'
import { DispatchError, WeightV2 } from '@polkadot/types/interfaces'

async function getUserBalance() {
    const abi = new Abi(abiJson)
    await cryptoWaitReady()
    const Key1 = new Keyring({ type: 'sr25519' })
    const UserKey = Key1.addFromMnemonic(
        '...'
    )

    const polkaProvider = new WsProvider('wss://rococo-contracts-rpc.polkadot.io/')
    const api = await ApiPromise.create({ provider: polkaProvider })
    // @ts-ignore
    const maximumBlockWeight = api.consts.system.blockWeights.maxBlock as unknown as WeightV2
    const maxGas = maximumBlockWeight.refTime.toNumber() * 0.9
    // const gl = api.registry.createType('WeightV2', {
    //     refTime: maxGas,
    //     proofSize: maxGas,
    // }) as WeightV2
    const gl = 2000000000000

    const contract = new ContractPromise(api, abi, '5DVAK9xZSEiJzWCdZGNkUjWVuXHJEgtZDgELiqYZEKwaHz2m')
    const response = await contract.query.getProviderStakeDefault(UserKey.address, { gasLimit: gl })
    return response.result
}

export function getDispatchError(dispatchError: DispatchError): string {
    let message: string = dispatchError.type

    if (dispatchError.isModule) {
        try {
            const mod = dispatchError.asModule
            const error = dispatchError.registry.findMetaError(mod)

            message = `${error.section}.${error.name}`
        } catch (error) {
            // swallow
        }
    } else if (dispatchError.isToken) {
        message = `${dispatchError.type}.${dispatchError.asToken.type}`
    }

    return message
}

getUserBalance().then((x) => {
    console.log(getDispatchError(x.asErr))
    process.exit()
})

You can try to use the commented out code to change the gas limit into a WeightV2 and see if that works. Is your contract deployed on the rococo contracts parachain?

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.