instantiate_with_code

Call v334 → current #7

Deploys new WASM contract code and instantiates a contract from it in one operation.

View calls on chain

Click items to navigate. Pan and zoom to explore.

Used by: developers

The Big Picture

This is the all-in-one deployment call: upload your WASM code and create a contract instance simultaneously. The contract address is derived deterministically from deployer, code hash, salt, and input data. The constructor runs immediately, initializing contract state.

Why This Matters

When deploying a new contract for the first time, this is the simplest path. One transaction handles both code upload and instantiation. Use this for fresh deployments where the code isn't already on-chain.

Example Scenario

You compiled a token contract to WASM. Call instantiate_with_code with code=your_wasm_bytes, data=constructor_args(name, symbol, decimals), salt=unique_bytes, value=0. CodeStored fires with code_hash, then Instantiated fires with contract address. Your token contract is live.

Common Questions

What's the salt for?
Salt ensures unique contract addresses. Same deployer + same code + same data + different salt = different addresses. Use random bytes or a nonce.
How much deposit do I need?
Deposit covers code storage (DepositPerByte * code_size) plus contract state. Query DefaultDepositLimit for the maximum allowed deposit.
Can I reuse the code for multiple contracts?
Yes. After this call, use 'instantiate' with the code_hash to create more instances without re-uploading.

Use Cases

  • Deploy new smart contracts to the chain
  • Launch dApps with custom on-chain logic
  • Create token contracts, DAOs, or other programmable entities
  • One-step deployment without separate upload

From Chain Metadata

Instantiates a new contract from the supplied `code` optionally transferring some balance. This dispatchable has the same effect as calling [`Self::upload_code`] + [`Self::instantiate`]. Bundling them together provides efficiency gains. Please also check the documentation of [`Self::upload_code`].

Input Parameters

#NameTypeDescription
0
value
Compact<u64> CptValue in RAO (÷10⁹ for TAO) (RAO -> TAO (/ 10^9))
1
gas_limit
Weight gas_limit (Weight)
2
storage_deposit_limit
Option storage_deposit_limit (Option)
3
code
Vec<u8> Veccode (Vec<u8>)
4
data
Vec<u8> Vecdata (Vec<u8>)
5
salt
Vec<u8> Vecsalt (Vec<u8>)

Permissions

Origin
Unknown
Required Role

Permission data inferred from metadata. May be incomplete.

Requirements

  • Caller has sufficient balance for storage deposits
  • WASM code is valid and passes validation
  • Code size is within MaxCodeLen limit
  • Salt produces unique contract address

Effects

Postconditions

  • Code stored in PristineCode
  • CodeInfoOf populated with code metadata
  • Contract instantiated at deterministic address
  • Instantiated event emitted
  • CodeStored event emitted

Side Effects

  • Storage deposit charged for code and contract state
  • Contract constructor executed with provided data
  • Nonce incremented for address derivation

Code Examples

import { ApiPromise, WsProvider } from "@polkadot/api";
import { stringCamelCase } from "@polkadot/util";

const provider = new WsProvider("wss://entrypoint-finney.opentensor.ai:443");
const api = await ApiPromise.create({ provider });

// Build instantiate_with_code call
const value = 0;
const gas_limit = 0 as any /* Weight */;
const storage_deposit_limit = 0 as any /* Option */;
const code = 0;
const data = 0;
const salt = 0;

const call = api.tx[stringCamelCase("Contracts")][stringCamelCase("instantiate_with_code")](
  value,
  gas_limit,
  storage_deposit_limit,
  code,
  data,
  salt
);

Runtime Info

Pallet Index
29
Call Index
7
First Version
v334
Current Version
v393