0

I could find the JS code to deploy an ink contract here. The code however, seems to be using a fixed gas limit to deploy the contract.

Is there a way to estimate the gas limit that is required to deploy the contract before actually deploying it?

Update:

I managed to find the instantiate methods under the contractsApi runtime calls that would provide the gas limit and storage deposit charge for contract deployment. However, I am not sure what data I am supposed to provide for data and salt params. enter image description here

3
  • Salt is simply to avoid hitting the duplicate contract error. You can put 0x00. Try putting the .wasm as the data. You'll get a gas output in the result
    – forgetso
    Jan 26, 2023 at 16:36
  • Got it about the salt. I assume something different than the contract wasm needs to go into the data field tho, since the wasm is already provided for code:CodeSource
    – Non-named
    Jan 27, 2023 at 8:28
  • Good point. I have no idea what data is then! :(
    – forgetso
    Jan 28, 2023 at 10:23

3 Answers 3

1

For the polkadot-js docs which you linked: I think either 0 or -1 for gasLimit means "unlimited"/"automatically choose it". So you don't necessarily have to put a value there.

How it's done in practice: You can dry-run a contract call, the dry-run will return the consumed Gas and you can then put this value as the gasLimit for the real thing.

This is what Contracts UI or polkadot-js shows you as "Estimates:

enter image description here

Those values are then used by default for executing the contract call.

0

Contract deployment gas depends on several factors like contract size, functions, variables, external calls or contract interactions. So gas estimation is complex task. However, you can estimate gas through test deployment in test network by increasing limit until successfully deployed, with this you can estimate gas, but how effective is this I don't know. gasLimit defined on that code is max limit not fixed one.

3
  • That seems really tedious task to do. I was looking for something like eth_estimateGas on the EVM side
    – Non-named
    Jan 26, 2023 at 5:51
  • @Bukks I think this might help you link
    – Ganesh11
    Feb 7, 2023 at 6:05
  • Don't think this holds for deployment
    – Non-named
    Feb 8, 2023 at 10:54
0

From the link you provided:

Getting accurate gas and storage deposit costs is possible by calling the instantiate RPC, which dry runs the instantiation and returns the outcome. For the scope of this tutorial we will use hardcoded values.

Use the gas returned from the runtime section in the polkadot apps UI:

12: contractsApi.instantiate: ContractInstantiateResult
{
  gasConsumed: {
    refTime: 2,875,200,210
    proofSize: 94,053
  }
  gasRequired: {
    refTime: 8,013,742,080
    proofSize: 262,144
  }
  storageDeposit: {
    Charge: 0
  }
  debugMessage: 
  result: {
    Err: {
      Module: {
        index: 8
        error: 0x19000000
      }
    }
  }
}

The error here is DuplicateContract. You can avoid duplicates by adding unique salt.

1
  • That link seems to be dead tho :(
    – Non-named
    Jan 26, 2023 at 5:50

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.