state_getMetadata - Moonbeam RPC Method
Get runtime metadata on Moonbeam. Essential for decoding storage, building extrinsics, and discovering available pallets and calls on the cross-chain connected EVM platform on Polkadot with $65M+ TVL and 100+ projects.
Returns the runtime metadata for Moonbeam as a SCALE-encoded hex string. Metadata describes all available pallets, storage items, calls, events, errors, and type definitions — everything needed to interact with the chain programmatically.
Why Moonbeam? Build on the cross-chain connected EVM platform on Polkadot with $65M+ TVL and 100+ projects with full EVM compatibility on Polkadot, native XCM cross-chain messaging, 10K+ TPS, 24% staking APR, and $0.015 transaction costs.
When to Use This Method
state_getMetadata is essential for cross-chain dApp developers, Polkadot builders, and teams requiring multi-chain interoperability:
- Runtime Introspection — Discover available pallets, calls, and storage items on Moonbeam
- Extrinsic Building — Get call signatures and type information for constructing transactions for cross-chain DeFi, multi-chain dApps, and Ethereum-to-Polkadot bridging via XCM, Axelar, LayerZero, and Wormhole
- Storage Key Generation — Build correct storage keys from metadata type definitions
- Client Generation — Auto-generate typed APIs and SDKs from the runtime metadata
- Upgrade Awareness — Detect metadata changes after runtime upgrades
Code Examples
Common Use Cases
1. Discover Available Pallets and Calls
Explore what functionality is available on Moonbeam:
async function explorePallets(api) {
const metadata = await api.rpc.state.getMetadata();
const pallets = metadata.asLatest.pallets;
for (const pallet of pallets) {
const name = pallet.name.toString();
const hasCalls = pallet.calls.isSome;
const hasStorage = pallet.storage.isSome;
const hasEvents = pallet.events.isSome;
console.log(`${name}: calls=${hasCalls} storage=${hasStorage} events=${hasEvents}`);
}
}2. Build Storage Keys from Metadata
Generate correct storage keys for querying chain state:
import { xxhashAsHex } from '@polkadot/util-crypto';
function buildStorageKey(palletName, storageName) {
const palletHash = xxhashAsHex(palletName, 128);
const storageHash = xxhashAsHex(storageName, 128);
return palletHash + storageHash.slice(2); // Concatenate without duplicate 0x
}
// Example: Build key for System.Account storage
const key = buildStorageKey('System', 'Account');
console.log('Storage prefix key:', key);3. Metadata Version Tracking
Track metadata changes across runtime upgrades on Moonbeam:
async function compareMetadataVersions(api, blockA, blockB) {
const hashA = await api.rpc.chain.getBlockHash(blockA);
const hashB = await api.rpc.chain.getBlockHash(blockB);
const metaA = await api.rpc.state.getMetadata(hashA);
const metaB = await api.rpc.state.getMetadata(hashB);
const palletsA = new Set(metaA.asLatest.pallets.map(p => p.name.toString()));
const palletsB = new Set(metaB.asLatest.pallets.map(p => p.name.toString()));
const added = [...palletsB].filter(p => !palletsA.has(p));
const removed = [...palletsA].filter(p => !palletsB.has(p));
console.log('Added pallets:', added);
console.log('Removed pallets:', removed);
}Error Handling
| Error Code | Description | Solution |
|---|---|---|
| -32603 | Internal error | Node may be syncing — retry with backoff |
| -32602 | Invalid params | Verify block hash is valid hex with 0x prefix |
| -32601 | Method not found | Verify the node supports this RPC method |
| -32005 | Rate limit exceeded | Cache metadata locally — it only changes on runtime upgrades |
Related Methods
state_getRuntimeVersion— Get runtime version (check before re-fetching metadata)state_getStorage— Query storage using keys derived from metadatastate_call— Call runtime APIs described in metadata
state_getStorage
Read a storage value by key on Moonbeam. Query account balances, pallet state, and any on-chain data using SCALE-encoded storage keys for cross-chain DeFi, multi-chain dApps, and Ethereum-to-Polkadot bridging via XCM, Axelar, LayerZero, and Wormhole.
state_getRuntimeVersion
Get the runtime version on Moonbeam. Essential for compatibility checking, upgrade detection, and transaction construction on the cross-chain connected EVM platform on Polkadot with $65M+ TVL and 100+ projects.