State API
The State API provides a way to query the state of the blockchain. It allows you to query the runtime version, metadata, storage, and subscribe to storage changes.
This API is available through the StateApi
class from the polkadart
package.
Sample usage
You will get the following output:
Methods
There are several methods available in the StateApi
class that allow you to query the state of the blockchain. Here are some of the most commonly used methods:
call
Call a contract at a block’s state
getPairs
Returns the keys with prefix, leave empty to get all the keys
getKeysPaged
Returns the keys with prefix with pagination support.
Up to count
keys will be returned.
If startKey
is passed, return next keys in storage in lexicographic order.
getStorage
Returns a storage entry at a specific block’s state.
getStorageHash
Returns the hash of a storage entry at a block’s state.
getStorageSize
Returns the size of a storage entry at a block’s state.
queryStorage
Query historical storage entries (by key) starting from a block given as the second parameter.
NOTE This first returned result contains the initial state of storage for all keys. Subsequent values in the vector represent changes to the previous state (diffs).
queryStorageAt
Query storage entries (by key) starting at block hash given as the second parameter.
getReadProof
Returns proof of storage entries at a specific block’s state.
getMetadata
Returns the runtime metadata
getRuntimeVersion
Get the runtime version.
subscribeRuntimeVersion
Retrieves the runtime version via subscription
subscribeEvents
subscribeStorage
Subscribes to storage changes for the provided keys