instantiate_with_code
Call v334 → current #7Deploys new WASM contract code and instantiates a contract from it in one operation.
View calls on chainCall Workflow
Click items to navigate. Pan and zoom to explore.
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
| # | 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 | Vec<u8> Vec | code (Vec<u8>) |
| 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
- 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
Events Emitted
Storage Modified
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
// ----------------------------------------------------------------------
// 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_with_code 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 = Binary.fromOpaque(new Uint8Array(0));
const data = Binary.fromOpaque(new Uint8Array(0));
const salt = Binary.fromOpaque(new Uint8Array(0));
const tx = api.tx.Contracts.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
- v411