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
Section titled “Sample usage”import 'package:polkadart/apis/apis.dart';import 'package:polkadart/polkadart.dart' show Provider, StateApi;
void main() async { final provider = Provider.fromUri(Uri.parse('wss://rpc.polkadot.io')); final stateApi = StateApi(provider); final runtimeVersion = await stateApi.getRuntimeVersion();
print(runtimeVersion.toJson());}
You will get the following output:
{ specName: polkadot, implName: parity-polkadot, authoringVersion: 0, specVersion: 1003003, implVersion: 0, apis: [...], transactionVersion: 26, stateVersion: 1}
Methods
Section titled “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:
Future<Uint8List> call(String method, Uint8List bytes, {BlockHash? at})
Call a contract at a block’s state
getPairs
Section titled “getPairs”Future<List<KeyValue>> getPairs(StorageKey prefix, {BlockHash? at})
Returns the keys with prefix, leave empty to get all the keys
getKeysPaged
Section titled “getKeysPaged”Future<List<StorageKey>> getKeysPaged({required StorageKey key, required int count, StorageKey? startKey, BlockHash? at})
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
Section titled “getStorage”Future<StorageData?> getStorage(StorageKey key, {BlockHash? at})
Returns a storage entry at a specific block’s state.
getStorageHash
Section titled “getStorageHash”Future<BlockHash?> getStorageHash(StorageKey key, {BlockHash? at})
Returns the hash of a storage entry at a block’s state.
getStorageSize
Section titled “getStorageSize”Future<int?> getStorageSize(StorageKey key, {BlockHash? at})
Returns the size of a storage entry at a block’s state.
queryStorage
Section titled “queryStorage”Future<List<StorageChangeSet>> queryStorage(List<StorageKey> keys, BlockHash fromBlock,{BlockHash? toBlock})
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
Section titled “queryStorageAt”Future<List<StorageChangeSet>> queryStorageAt(List<StorageKey> keys, {BlockHash? at})
Query storage entries (by key) starting at block hash given as the second parameter.
getReadProof
Section titled “getReadProof”Future<ReadProof> getReadProof(List<StorageKey> keys, {BlockHash? at})
Returns proof of storage entries at a specific block’s state.
getMetadata
Section titled “getMetadata”Future<RuntimeMetadata> getMetadata({BlockHash? at})
Returns the runtime metadata
getRuntimeVersion
Section titled “getRuntimeVersion”Future<RuntimeVersion> getRuntimeVersion({BlockHash? at})
Get the runtime version.
subscribeRuntimeVersion
Section titled “subscribeRuntimeVersion”Future<StreamSubscription<RuntimeVersion>> subscribeRuntimeVersion(Function(RuntimeVersion) onData)
Retrieves the runtime version via subscription
subscribeEvents
Section titled “subscribeEvents”Future<StreamSubscription<Events>> subscribeEvents(BlockHash at, Function(Events) onData)
subscribeStorage
Section titled “subscribeStorage”Future<StreamSubscription<StorageChangeSet>> subscribeStorage(List<Uint8List> storageKeys, Function(StorageChangeSet) onData)
Subscribes to storage changes for the provided keys