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> Vec code (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 { 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 set_code call (typed, named args)
const code = Binary.fromOpaque(new Uint8Array(0));

const tx = api.tx.System.set_code({
  code,
});

Runtime Info

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