upload_code

Call v334 → current #3

Uploads WASM contract code without instantiating a contract.

View calls on chain

Click items to navigate. Pan and zoom to explore.

Used by: developers

The Big Picture

Sometimes you want to upload code without immediately creating a contract - maybe for governance approval, or to let others instantiate, or to calculate addresses before deployment. This call stores the code on-chain, returning its hash for later use.

Why This Matters

Separating upload from instantiation gives flexibility. Upload code, have it audited, then instantiate later. Or upload once and let a factory contract create instances. Or calculate your contract address before it exists (counterfactual instantiation).

Example Scenario

You want to propose a contract upgrade via governance. Call upload_code with the new WASM. CodeStored event fires with code_hash=0xdef.... Now governance can vote on calling set_code with that hash, without you holding the code bytes in memory during the vote.

Common Questions

How much deposit is required?
DepositPerByte * code_size. Larger code = larger deposit. The deposit is returned when code is removed.
Can anyone instantiate my uploaded code?
Yes, code is public. Anyone with the code_hash can call instantiate. This is by design - think of code as a template, instances as deployments.
What's determinism mode?
Controls whether code must be deterministic (same inputs = same outputs). Most contracts use deterministic mode. Non-deterministic allows randomness but limits some features.

Use Cases

  • Pre-upload code before instantiation
  • Share code for others to instantiate
  • Separate upload and instantiation for better control
  • Prepare code for deterministic address calculation

From Chain Metadata

Upload new `code` without instantiating a contract from it. If the code does not already exist a deposit is reserved from the caller and unreserved only when [`Self::remove_code`] is called. The size of the reserve depends on the size of the supplied `code`. If the code already exists in storage it will still return `Ok` and upgrades the in storage version to the current [`InstructionWeights::version`](InstructionWeights). `determinism`: If this is set to any other value but [`Determinism::Enforced`] then the only way to use this code is to delegate call into it from an offchain execution. Set to [`Determinism::Enforced`] if in doubt.

Input Parameters

#NameTypeDescription
0
code
Vec<u8> Veccode (Vec<u8>)
1
storage_deposit_limit
Option storage_deposit_limit (Option)
2
determinism
Determinism determinism (Determinism)

Permissions

Origin
Unknown
Required Role

Permission data inferred from metadata. May be incomplete.

Requirements

  • Caller has sufficient balance for code storage deposit
  • WASM code is valid and passes validation
  • Code size is within MaxCodeLen limit
  • Code hash is not already stored (unless determinism mode differs)

Effects

Events Emitted

Storage Modified

Postconditions

  • Code stored in PristineCode
  • CodeInfoOf populated with metadata
  • CodeStored event emitted
  • Storage deposit held for code

Side Effects

  • Deposit locked until code is removed
  • Code available for any account to instantiate

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 upload_code call
const code = 0;
const storage_deposit_limit = 0 as any /* Option */;
const determinism = 0 as any /* Determinism */;

const call = api.tx[stringCamelCase("Contracts")][stringCamelCase("upload_code")](
  code,
  storage_deposit_limit,
  determinism
);

Runtime Info

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