Export Substrate map keys from one immutable state

A large System.Account map can exceed a provider’s response budget in a single read. Paginated raw keys let an exporter bound each request and resume after interruptions.

Obtain the prefix from runtime metadata

With a connected polkadot-js API exposing system.account, request a small page. Use a runtime matching the chosen snapshot to derive its storage layout. Retain the raw keys in the returned order because the next cursor consumes a storage key.

const hash = await api.rpc.chain.getFinalizedHead()
const prefix = api.query.system.account.keyPrefix()
const keys = await api.rpc.state.getKeysPaged(prefix, 100, null, hash)
console.log(hash.toHex(), keys.map(key => key.toHex()))

Resume at the last returned raw key

Pass the last key from page one as startKey in the next getKeysPaged call, using the same prefix and hash. A printable account address is not a replacement for that encoded storage key. Read associated values against the saved hash as well. Stop on an empty page and flag a cursor that fails to advance.

Persist the page before advancing its checkpoint. If the node prunes the pinned state during a long export, resume against an endpoint retaining that state or restart at a new hash with a new export ID.

Sources