Query dApp Staking extrinsics and participating addresses with Python
TL;DR
As a Substrate-based multi-VM blockchain, Astar nodes have all Polkadot or Substrate features. Python Substrate Interface library allows developers to query Substrate-runtime-level metadata from an Astar node and interact with the node's Polkadot or Substrate features, including querying and composing extrinsics using a native Python interface.
In this guide, we will cover:
- How to install Python Substrate Interface
- How to create an API provider instance
- How to query blocks and extrinsics, using an example of querying dApp staking participants’ addresses.
What is Substrate?
Substrate is an open-source software development kit (SDK) that allows teams to quickly build highly customized blockchains. It comes with native support for connecting to Polkadot and Kusama right out of the box.
All Polkadot and Kusama parachains and relay chains are built with Substrate, this include Astar and Shiden networks. Thus, Astar nodes have all the major Polkadot or Substrate features.
What is Substrate Python Interface?
Substrate Python Interface is a Python library that specializes in interfacing with a Substrate node; querying storage, composing extrinsics, SCALE encoding/decoding, and providing additional convenience methods to deal with the features and metadata of the Substrate runtime.
For interface function reference, please read https://polkascan.github.io/py-substrate-interface/.
Instructions
1. Install Substrate Python Interface
-
Before installing Substrate Python Interface, please run the following command to check if you have Python package installer
pip
installed:pip --version
-
If not, please follow the guide at https://pip.pypa.io/en/stable/installation/ to install
pip
. -
After making sure
pip
is installed, you can install Python Substrate Interface library by running the following command in your project directory:pip install substrate-interface
2. Construct an API provider Instance
In order to query and interact with an Astar node, you need to first construct a WsProvider
API provider using the WebSocket endpoint of Astar Network that you wish to interact with.
You can find the list of supported endpoints from our network RPC endpoint list.
# Import Python Substrate Interface
from substrateinterface import SubstrateInterface
# Construct the API provider
ws_provider = SubstrateInterface(
url="wss://rpc.astar.network",
)