instantiate
Call v334 → current #8Creates a new contract instance from previously uploaded code.
View calls on chainCall Workflow
Click items to navigate. Pan and zoom to explore.
The Big Picture
Once code is uploaded (via upload_code or instantiate_with_code), you can create many contract instances from it without re-uploading. This is more gas-efficient and enables patterns like contract factories where one contract deploys others using known code hashes.
Why This Matters
Code upload is expensive (storage deposit). If you're deploying many instances of the same contract (e.g., one per user), upload once, then use instantiate repeatedly. Saves deposits and gas.
Example Scenario
You uploaded a multisig wallet contract earlier (code_hash=0xabc...). Now user Alice wants her own instance. Call instantiate with code_hash=0xabc..., data=constructor(alice_as_owner), salt=random. Instantiated fires with Alice's unique contract address.
Common Questions
- How do I get the code_hash?
- From the CodeStored event when code was uploaded, or by hashing the WASM yourself. Query CodeInfoOf to verify a hash exists on-chain.
- What if code_hash doesn't exist?
- Call fails with CodeNotFound error. Upload the code first using upload_code or instantiate_with_code.
- Is the address deterministic?
- Yes: hash(deployer, code_hash, input_data, salt). Same inputs = same address. Use this for counterfactual deployments.
Use Cases
- Deploy multiple instances of the same contract code
- Factory pattern - contracts creating contracts
- Efficient deployment when code already exists
- Upgrade patterns using known code hashes
From Chain Metadata
Instantiates a contract from a previously deployed wasm binary. This function is identical to [`Self::instantiate_with_code`] but without the code deployment step. Instead, the `code_hash` of an on-chain deployed wasm binary must be supplied.
Input Parameters
| # | Name | Type | Description |
|---|---|---|---|
| 0 | value | Compact<u64> Cpt BalanceOf<T> | value: Balance in chain native currency (RAO for TAO; ÷10⁹) (RAO -> TAO (/ 10^9)) |
| 1 | gas_limit | Weight | gas_limit: Substrate dispatch weight (computational cost) |
| 2 | storage_deposit_limit | Option Option<<BalanceOf<T> as codec::HasCompact>::Type> | storage_deposit_limit (Option) |
| 3 | code_hash | H256 CodeHash<T> | code_hash: Contract code hash |
| 4 | data | Vec<u8> Vec | data (Vec<u8>) |
| 5 | salt | Vec<u8> Vec | salt (Vec<u8>) |
Permissions
Permission data inferred from metadata. May be incomplete.
Requirements
- Code with given code_hash exists in PristineCode
- Caller has sufficient balance for storage deposits
- Salt produces unique contract address
- Gas limit sufficient for constructor execution
Effects
Events Emitted
Storage Modified
Postconditions
- New contract instantiated at deterministic address
- Instantiated event emitted
- Contract constructor executed
- Storage deposit charged
Side Effects
- Contract state initialized by constructor
- Nonce incremented
- Code reference count updated
Code Examples
// ----------------------------------------------------------------------
// HEADS UP: 1 arg below has a complex type with no usable default.
// Look for `undefined as any` and replace it with real value
// before running — the snippet compiles, but will fail at runtime as-is.
// ----------------------------------------------------------------------
import { createClient, Binary } from "polkadot-api";
import { getWsProvider } from "polkadot-api/ws";
import { sub } from "@polkadot-api/descriptors"; // generated by: npx papi add sub -w wss://entrypoint-finney.opentensor.ai:443
const client = createClient(getWsProvider("wss://entrypoint-finney.opentensor.ai:443"));
const api = client.getTypedApi(sub);
// Build instantiate call (typed, named args)
const value = 0n;
const gas_limit = undefined as any /* Weight — replace with real value */;
const storage_deposit_limit = undefined;
const code_hash = "0x0000000000000000000000000000000000000000000000000000000000000000";
const data = Binary.fromOpaque(new Uint8Array(0));
const salt = Binary.fromOpaque(new Uint8Array(0));
const tx = api.tx.Contracts.instantiate({
value,
gas_limit,
storage_deposit_limit,
code_hash,
data,
salt,
});Runtime Info
- Pallet Index
- 29
- Call Index
- 8
- First Version
- v334
- Current Version
- v411