state_getStorage
Description
Retrieves a storage value from the Moonbase Alpha 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 |
Quick Examples
- cURL
- JavaScript (polkadot.js)
- Python (py-substrate-interface)
# Read runtime :code (Wasm) at latest
curl -X POST https://api-moonbase-alpha.n.dwellir.com/YOUR_API_KEY -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","id":1,"method":"state_getStorage","params":["0x3a636f6465"]}'
# Read the same key at a specific block
curl -X POST https://api-moonbase-alpha.n.dwellir.com/YOUR_API_KEY -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","id":1,"method":"state_getStorage","params":["0x3a636f6465","0x9d44ea4103132b9ab5addc69bfe96065e0b0d58c44591b9f72bf448ca3877549"]}'
import { ApiPromise, WsProvider } from '@polkadot/api';
async function main() {
const api = await ApiPromise.create({ provider: new WsProvider('${S.wsBase}') });
// High-level query: System.Account for a known AccountId (Moonbase uses 20-byte AccountId)
const account = await api.query.system.account('${S.accountId}');
console.log('Account nonce:', account.nonce.toString());
console.log('Free balance:', account.data.free.toString());
// Raw storage query: runtime :code key
const raw = await api.rpc.state.getStorage('${S.storageCodeKey}');
console.log('Runtime code length:', raw?.toU8a().length ?? 0);
// Historical query
const at = await api.rpc.chain.getBlockHash(${S.blockNumber});
const historical = await api.rpc.state.getStorage('${S.storageCodeKey}', at);
console.log('Historical code length:', historical?.toU8a().length ?? 0);
await api.disconnect();
}
main().catch(console.error);
from substrateinterface import SubstrateInterface
api = SubstrateInterface(
url='${S.wsBase}',
ss58_format=1287,
type_registry_preset='substrate-node-template'
)
# High-level System.Account
info = api.query('System', 'Account', params=['${S.accountId}'])
print('Nonce:', info.value['nonce'])
print('Free:', info.value['data']['free'])
# Raw :code key
raw = api.rpc_request('state_getStorage', ['${S.storageCodeKey}'])['result']
print('Code (hex, truncated):', raw[:66] + '…')
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 | twox128("System") + twox128("Account") | Account information |
:code | 0x3a636f6465 | Runtime Wasm blob |
For Account maps, the key suffix uses
blake2_128_concat(accountId)
where accountId is 20 bytes on Moonbase Alpha.
Notes
- Responses are SCALE-encoded; prefer high-level SDKs to decode types correctly.
- Use
state_getKeysPaged
to enumerate keys for large prefixes efficiently. - Historical queries require a block hash; prefer finalized hashes for consistency.