Nonce
Storage Plain v334 → currentA running counter used for contract address derivation.
Explore chainThe Big Picture
Part of the deterministic address formula. Along with deployer, code_hash, and salt, the nonce ensures uniqueness. Incremented on each instantiation.
Use Cases
- Understand address derivation
- Debug address collisions
From Chain Metadata
This is a **monotonic** counter incremented on contract instantiation. This is used in order to generate unique trie ids for contracts. The trie id of a new contract is calculated from hash(account_id, nonce). The nonce is required because otherwise the following sequence would lead to a possible collision of storage: 1. Create a new contract. 2. Terminate the contract. 3. Immediately recreate the contract with the same account_id. This is bad because the contents of a trie are deleted lazily and there might be storage of the old instantiation still in it when the new contract is created. Please note that we can't replace the counter by the block number because the sequence above can happen in the same block. We also can't keep the account counter in memory only because storage is the only way to communicate across different extrinsics in the same block. # Note Do not use it to determine the number of contracts. It won't be decremented if a contract is destroyed.
Purpose & Usage
Purpose
Ensures unique contract addresses even with identical parameters.
Common Query Patterns
- Query for current value
Stored Value
Relationships
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 });
// Query Nonce storage (no keys - plain value)
const result = await api.query
[stringCamelCase("Contracts")]
[stringCamelCase("Nonce")]();
console.log("Nonce:", result.toHuman());Runtime Info
- Pallet
- Contracts
- Storage Kind
- Plain
- First Version
- v334
- Current Version
- v393