1

I'm trying to read the flipper contract data from a contract in a local node, following the code in here. This is my code:

// Required imports
const { ApiPromise, WsProvider } = require("@polkadot/api");
const { ContractPromise } = require("@polkadot/api-contract");
const { keyring } = require("@polkadot/ui-keyring");
keyring.loadAll({ ss58Format: 42, type: "sr25519" });

const abi = require("./metadata.json");

async function main() {
    const provider = new WsProvider("ws://127.0.0.1:9944");
    const api = await ApiPromise.create({ provider });

    const contractAddress = "5HDxbYh52Wb34ATHwWhct1FQ4gnavcK8kAyMAVRdHUxgDhYo";
    const contract = new ContractPromise(api, abi, contractAddress);

    const alicePair = keyring.createFromUri("//Alice");

    const value = 0;
    const gasLimit = 3000n * 1000000n;
    const { gasConsumed, result, output } = await contract.query.get(
        alicePair.address,
        { value, gasLimit }
    );
    console.log(result.toHuman());
    console.log(gasConsumed.toHuman());

    if (result.isOk) {
        console.log("Success", output.toHuman());
    } else {
        console.error("Error", result.asErr);
    }
}

main()
    .catch(console.error)
    .finally(() => process.exit());

After running this file with node, this is the output:

{ Err: { Module: { index: '8', error: '1', message: 'OutOfGas' } } }
3,000,000,000
Error Type {
  registry: TypeRegistry { createdAtHash: undefined },
  createdAtHash: undefined,
  initialU8aLength: undefined,
  isOther: [Getter],
  isCannotLookup: [Getter],
  isBadOrigin: [Getter],
  isModule: [Getter],
  asOther: [Getter],
  asCannotLookup: [Getter],
  asBadOrigin: [Getter],
  asModule: [Getter]
}

What caused the error and how to fix it?

1 Answer 1

4

The error is telling you that the gas limit you have provided is too low. You supplied a limit of 3 * 10^9 which is 3 milliseconds of execution (1 gas = 1 pico second). This could be too less if the message you are calling is doing some heavy lifting. It really depends on your contract so we cannot really tell.

You are calling the message through a node RPC in order to retrieve some value. You should set the gas_limit to the maximum allowed for an RPC execution and see where this leads. The default for that is 5 * 10^12 (5 seconds). If you still see an out of gas error you should inspect your contract and find out why it consumes such a heavy amount of gas.

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.