state_getStorage
Description
Retrieves a storage value from the Polkadot state at a given key. Storage keys are constructed from pallet name, storage item name, and optional parameters. The returned value is SCALE-encoded and needs to be decoded according to the storage item's type.
Parameters
Parameter | Type | Required | Description |
---|---|---|---|
storageKey | string | Yes | Hex-encoded storage key |
blockHash | string | No | Block hash to query state at. If omitted, uses latest block |
Returns
Field | Type | Description |
---|---|---|
result | string | Hex-encoded SCALE-encoded storage value, or null if not exists |
Request Example
{
"jsonrpc": "2.0",
"method": "state_getStorage",
"params": [
"0x26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da9"
],
"id": 1
}
Response Example
{
"jsonrpc": "2.0",
"result": "0x0000000000000000000000000000000000000000000000000000000000000000",
"id": 1
}
Storage Key Construction
Storage keys follow the pattern:
twox128(pallet_name) + twox128(storage_item_name) + hasher(key_params)
Common Storage Keys
Storage | Key Prefix | Description |
---|---|---|
System.Account | 0x26aa394eea5630e07c48ae0c9558cef7 | Account information |
Balances.TotalIssuance | 0xc2261276cc9d1f8598ea4b6a74b15c2f | Total token supply |
Staking.Validators | 0x5f3e4907f716ac89b6347d15ececedca | Validator preferences |
Staking.Nominators | 0x5f3e4907f716ac89b6347d15ececedca | Nominator information |
Code Examples
JavaScript
import { xxhashAsHex } from '@polkadot/util-crypto';
// Helper to construct storage key
const getStorageKey = (module, item, ...params) => {
const moduleHash = xxhashAsHex(module, 128);
const itemHash = xxhashAsHex(item, 128);
// Add parameter hashing as needed
return moduleHash + itemHash.slice(2);
};
// Query account balance
const getAccountInfo = async (address) => {
// Construct storage key for System.Account
const storageKey = '0x26aa394eea5630e07c48ae0c9558cef7' +
'9c1fc3b50141589e85322024b6e379f5' +
// Add blake2_128_concat hash of address
address.slice(2);
const response = await fetch('https://api-polkadot.n.dwellir.com', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
jsonrpc: '2.0',
method: 'state_getStorage',
params: [storageKey],
id: 1
})
});
const data = await response.json();
return data.result;
};
Python
from substrateinterface import SubstrateInterface
import json
# Using substrate-interface for easier key construction
substrate = SubstrateInterface(
url="https://api-polkadot.n.dwellir.com",
extra_headers={"Authorization": "Bearer YOUR_API_KEY"}
)
# Query account info
def get_account_info(address):
result = substrate.query(
module='System',
storage_function='Account',
params=[address]
)
return {
'nonce': result['nonce'],
'free': result['data']['free'],
'reserved': result['data']['reserved']
}
# Direct RPC call
def get_storage_raw(storage_key):
url = "https://api-polkadot.n.dwellir.com"
headers = {
"Authorization": "Bearer YOUR_API_KEY",
"Content-Type": "application/json"
}
payload = {
"jsonrpc": "2.0",
"method": "state_getStorage",
"params": [storage_key],
"id": 1
}
response = requests.post(url, headers=headers, data=json.dumps(payload))
return response.json()["result"]
TypeScript (@polkadot/api)
import { ApiPromise, WsProvider } from '@polkadot/api';
async function queryStorage() {
const provider = new WsProvider('wss://api-polkadot.n.dwellir.com');
const api = await ApiPromise.create({ provider });
// High-level query
const account = await api.query.system.account('ADDRESS');
console.log('Account:', account.toHuman());
// Raw storage query
const storageKey = api.query.system.account.key('ADDRESS');
const rawValue = await api.rpc.state.getStorage(storageKey);
console.log('Raw storage:', rawValue.toHex());
// Query at specific block
const blockHash = await api.rpc.chain.getBlockHash(1000000);
const historicalValue = await api.rpc.state.getStorage(
storageKey,
blockHash
);
await api.disconnect();
}
Common Storage Queries
Account Balance
// Storage key for System.Account
const key = '0x26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da9' +
// Add account address hash
accountHash;
Total Issuance
// Storage key for Balances.TotalIssuance
const key = '0xc2261276cc9d1f8598ea4b6a74b15c2f57c875e4cff74148e4628f264b974c80';
Validator Count
// Storage key for Staking.ValidatorCount
const key = '0x5f3e4907f716ac89b6347d15ececedca9e6291df5932f5f9e8b539b69d9bc699';
Related Methods
state_getMetadata
- Get metadata for decodingchain_getBlock
- Get block datachain_getBlockHash
- Get block hash for queriessystem_health
- Check node status
Notes
- Storage values are SCALE-encoded and require proper decoding
- Use high-level APIs when possible for automatic encoding/decoding
- Storage keys can be complex for map-type storage items
- Null result indicates the storage item doesn't exist