set_code

Call v101 → current #2

Set the new runtime code with safety checks (root only).

View calls on chain

Click items to navigate. Pan and zoom to explore.

Used by: validatorsdevelopers

The Big Picture

This is how Substrate chains upgrade without forking. The runtime is stored on-chain as WASM.

Use Cases

  • Deploy runtime upgrades
  • Add new features
  • Fix bugs

From Chain Metadata

Set the new runtime code. ## Complexity `O(C + S)` where `C` length of `code` and `S` complexity of `can_set_code`

Input Parameters

#NameTypeDescription
0
code
Vec<u8> Veccode (Vec<u8>)

Permissions

Origin
Unknown
Required Role

Requirements

  • Caller is root/sudo
  • Valid WASM code
  • Spec name matches
  • Spec version increases

Effects

Events Emitted

Storage Modified

Postconditions

  • New runtime code deployed
  • CodeUpdated event emitted

Side Effects

  • All chain behavior may change
  • Nodes need compatible software

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 set_code call
const code = 0;

const call = api.tx[stringCamelCase("System")][stringCamelCase("set_code")](
  code
);

Runtime Info

Pallet Index
0
Call Index
2
First Version
v101
Current Version
v393