0

I'm trying to write a simple contract but run OutOfGas very quickly. My main storage looks like this:

    type Id = u32;

    #[ink(storage)]
    pub struct Example {
        elements: Mapping<Id, Element>,
        ids: Lazy<Vec<Id>>,
    }

    #[derive(scale::Encode, scale::Decode)]
    #[cfg_attr(
        feature = "std",
        derive(scale_info::TypeInfo, ink::storage::traits::StorageLayout)
    )]
    pub struct Element {
        id: Id,
        name: String,
        owner: AccountId,
        max_speed: i32,
        max_inventory_size: u32,
        max_cargo_size: u32,
        max_energy: u32,
        max_health: u32,
        recharge_rate: u32,
    }

When I now try to create one of those elements via a message I'm running out of gas no matter how much I supply. What can be the reason for this? Is the Element struct perhaps too large for a single Cell, so it's super inefficient? What else could be the reason?

Thanks in advance, any input would be appreciated!

4

1 Answer 1

2

I encountered a similar issue when upgrading to the latest version of substrate and ink smart contracts. Let me tell you what worked for me.

Initially, when deploying a smart contract I would have a gasLimit as a number

  const code = new CodePromise(api, abi, wasm);
  const gasLimit = 300000000000n;
  code.tx
    .new(
      { gasLimit, salt: randomAsHex() },
      initialSupply,
      price,
      owner,
      date,
      redeemableAddress,
      purchaseTrackerAddress
    )

But this was giving me the outOfGas error, I would increase this limit until it would exhaust the block limit so that was no good. I did see that I could deploy the contract on the contracts-ui (https://contracts-ui.substrate.io/). Which was great! They declare the gasLimit like this:

    const code = new CodePromise(api, abi, wasm);
  const gasLimit = api.registry.createType("WeightV2", {
    refTime: 300000000000n,
    proofSize: 262144,
  });

  code.tx
    .new(
      { gasLimit, salt: randomAsHex() },
      initialSupply,
      price,
      owner,
      date,
      redeemableAddress,
      purchaseTrackerAddress
    )

I would recommend uploading your contract to the contract UI and it will tell you what refTime and proofSize values you should use.

Hope this solves your problem!

3
  • 1
    My initial problem from the top post was indeed about the weights v2 not being merged, but I found a link back here to my own question while searching for a different OutOfGas problem and your solution works! Thank you :)
    – Tino Rusch
    Mar 7, 2023 at 8:25
  • Super happy it worked!
    – BRNHINLT
    Mar 7, 2023 at 13:06
  • Brillant thing, It solved a big problem of instantiation using json RPC May 24, 2023 at 9:58

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.