Skip to content

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.

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
}

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

Future<List<KeyValue>> getPairs(StorageKey prefix, {BlockHash? at})

Returns the keys with prefix, leave empty to get all the keys

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.

Future<StorageData?> getStorage(StorageKey key, {BlockHash? at})

Returns a storage entry at a specific block’s state.

Future<BlockHash?> getStorageHash(StorageKey key, {BlockHash? at})

Returns the hash of a storage entry at a block’s state.

Future<int?> getStorageSize(StorageKey key, {BlockHash? at})

Returns the size of a storage entry at a block’s state.

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).

Future<List<StorageChangeSet>> queryStorageAt(List<StorageKey> keys, {BlockHash? at})

Query storage entries (by key) starting at block hash given as the second parameter.

Future<ReadProof> getReadProof(List<StorageKey> keys, {BlockHash? at})

Returns proof of storage entries at a specific block’s state.

Future<RuntimeMetadata> getMetadata({BlockHash? at})

Returns the runtime metadata

Future<RuntimeVersion> getRuntimeVersion({BlockHash? at})

Get the runtime version.

Future<StreamSubscription<RuntimeVersion>> subscribeRuntimeVersion(Function(RuntimeVersion) onData)

Retrieves the runtime version via subscription

Future<StreamSubscription<Events>> subscribeEvents(BlockHash at, Function(Events) onData)
Future<StreamSubscription<StorageChangeSet>> subscribeStorage(List<Uint8List> storageKeys, Function(StorageChangeSet) onData)

Subscribes to storage changes for the provided keys