set_code

Call v334 → current #5

Updates the code of an existing contract to a different code hash (admin only).

View calls on chain

Click items to navigate. Pan and zoom to explore.

Used by: developers

The Big Picture

This is a privileged upgrade mechanism. It swaps out a contract's code without changing its address or storage. Use with extreme caution - incompatible code can corrupt contract state. Typically gated behind governance or sudo to prevent unauthorized upgrades.

Why This Matters

Contracts may need upgrades for bug fixes or new features. This call enables in-place upgrades without redeploying or migrating. But it's powerful and dangerous - wrong code = broken contract.

Example Scenario

Critical bug found in production contract 0x123.... Governance approves fix. New code uploaded (code_hash=0xfixed...). Root calls set_code(dest=0x123..., code_hash=0xfixed...). ContractCodeUpdated fires. Contract now runs fixed code at same address.

Common Questions

Why is this admin-only?
Changing code changes behavior. Without restrictions, anyone could swap contract logic. Admin gating ensures only authorized parties (governance, sudo) can upgrade.
What about storage compatibility?
The chain doesn't validate storage layout. If new code interprets storage differently, it will read garbage. Ensure upgrade-safe practices (versioned storage, migrations).
Can contracts upgrade themselves?
Not through this call (requires root). Contracts implement self-upgrade by storing code_hash and having an authorized account call set_code.

Use Cases

  • Emergency contract upgrades
  • Governance-approved code updates
  • Fix critical bugs in deployed contracts
  • Administrative migration between versions

From Chain Metadata

Privileged function that changes the code of an existing contract. This takes care of updating refcounts and all other necessary operations. Returns an error if either the `code_hash` or `dest` do not exist.

Input Parameters

#NameTypeDescription
0
dest
MultiAddress dest (MultiAddress)
1
code_hash
H256 code_hash (H256)

Permissions

Origin
Unknown
Required Role

Permission data inferred from metadata. May be incomplete.

Requirements

  • Caller has root/sudo privileges
  • Target contract exists
  • New code_hash exists in PristineCode
  • New code is compatible with existing storage layout

Effects

Events Emitted

Storage Modified

Postconditions

  • Contract's code reference updated
  • ContractCodeUpdated event emitted
  • Old code reference count decremented
  • New code reference count incremented

Side Effects

  • Contract behavior changes immediately
  • Storage interpretation may change if layout differs
  • May break contract if code is incompatible

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 dest = 0 as any /* MultiAddress */;
const code_hash = 0 as any /* H256 */;

const call = api.tx[stringCamelCase("Contracts")][stringCamelCase("set_code")](
  dest,
  code_hash
);

Runtime Info

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