Skip to content

Commit 3f676a2

Browse files
committed
add query chain state with sdks
1 parent 9cccc2d commit 3f676a2

File tree

18 files changed

+962
-1
lines changed

18 files changed

+962
-1
lines changed
Lines changed: 333 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,333 @@
1-
TODO
1+
---
2+
title: Query On-Chain State with SDKs
3+
description: Learn how to query on-chain storage data using PAPI, Polkadot.js, Dedot, Python Substrate Interface, and Subxt.
4+
categories: Chain Interactions
5+
---
6+
7+
# Query On-Chain State with SDKs
8+
9+
## Introduction
10+
11+
Polkadot SDK-based blockchains store data in a key-value database that can be queried by external applications. This on-chain state includes account balances, asset information, governance proposals, and any other data the runtime manages.
12+
13+
This guide demonstrates how to query on-chain storage using five popular SDKs:
14+
15+
- **[Polkadot API (PAPI)](/reference/tools/papi/)** - Modern TypeScript library with type-safe APIs
16+
- **[Polkadot.js API](/reference/tools/polkadot-js-api/)** - Comprehensive JavaScript library (maintenance mode)
17+
- **[Dedot](/reference/tools/dedot/)** - Lightweight TypeScript library optimized for performance
18+
- **[Python Substrate Interface](/reference/tools/py-substrate-interface/)** - Python library for Substrate chains
19+
- **[Subxt](/reference/tools/subxt/)** - Rust library with compile-time type safety
20+
21+
Select your preferred SDK below to see complete, runnable examples that query Polkadot Hub for account balances and asset information.
22+
23+
## Query On-Chain Data
24+
25+
=== "PAPI"
26+
27+
[Polkadot API (PAPI)](/reference/tools/papi/) is a modern, type-safe TypeScript library optimized for light-client functionality.
28+
29+
**Prerequisites**
30+
31+
- [Node.js](https://nodejs.org/){target=\_blank} v18 or higher
32+
- npm, pnpm, or yarn package manager
33+
34+
**Environment Setup**
35+
36+
1. Create and initialize a new project:
37+
38+
```bash
39+
mkdir papi-query-example && cd papi-query-example && \
40+
npm init -y && npm pkg set type=module
41+
```
42+
43+
2. Install dependencies:
44+
45+
```bash
46+
npm install polkadot-api && \
47+
npm install --save-dev @types/node tsx typescript
48+
```
49+
50+
3. Generate types for Polkadot Hub:
51+
52+
```bash
53+
npx papi add pah -n polkadot_asset_hub
54+
```
55+
56+
**Query Account Balance**
57+
58+
The following example queries the `System.Account` storage to retrieve an account's native token balance.
59+
60+
Create a file named `query-balance.ts`:
61+
62+
```typescript title="query-balance.ts"
63+
--8<-- "code/chain-interactions/query-data/query-sdks/papi/query-balance.ts"
64+
```
65+
66+
Run the script:
67+
68+
```bash
69+
npx tsx query-balance.ts
70+
```
71+
72+
**Query Asset Information**
73+
74+
The following example queries the `Assets` pallet to retrieve metadata and balance information for USDT (asset ID 1984).
75+
76+
Create a file named `query-asset.ts`:
77+
78+
```typescript title="query-asset.ts"
79+
--8<-- "code/chain-interactions/query-data/query-sdks/papi/query-asset.ts"
80+
```
81+
82+
Run the script:
83+
84+
```bash
85+
npx tsx query-asset.ts
86+
```
87+
88+
=== "Polkadot.js"
89+
90+
!!! warning "Maintenance Mode Only"
91+
The Polkadot.js API is no longer actively developed. New projects should use [PAPI](/reference/tools/papi/){target=\_blank} or [Dedot](/reference/tools/dedot/){target=\_blank} as actively maintained alternatives.
92+
93+
[Polkadot.js API](/reference/tools/polkadot-js-api/) is a comprehensive JavaScript library with extensive ecosystem support.
94+
95+
**Prerequisites**
96+
97+
- [Node.js](https://nodejs.org/){target=\_blank} v18 or higher
98+
- npm, pnpm, or yarn package manager
99+
100+
**Environment Setup**
101+
102+
1. Create and initialize a new project:
103+
104+
```bash
105+
mkdir pjs-query-example && cd pjs-query-example && \
106+
npm init -y && npm pkg set type=module
107+
```
108+
109+
2. Install dependencies:
110+
111+
```bash
112+
npm install @polkadot/api
113+
```
114+
115+
**Query Account Balance**
116+
117+
The following example queries the `System.Account` storage to retrieve an account's native token balance.
118+
119+
Create a file named `query-balance.js`:
120+
121+
```javascript title="query-balance.js"
122+
--8<-- "code/chain-interactions/query-data/query-sdks/pjs/query-balance.js"
123+
```
124+
125+
Run the script:
126+
127+
```bash
128+
node query-balance.js
129+
```
130+
131+
**Query Asset Information**
132+
133+
The following example queries the `Assets` pallet to retrieve metadata and balance information for USDT (asset ID 1984).
134+
135+
Create a file named `query-asset.js`:
136+
137+
```javascript title="query-asset.js"
138+
--8<-- "code/chain-interactions/query-data/query-sdks/pjs/query-asset.js"
139+
```
140+
141+
Run the script:
142+
143+
```bash
144+
node query-asset.js
145+
```
146+
147+
=== "Dedot"
148+
149+
[Dedot](/reference/tools/dedot/) is a next-generation TypeScript client that's lightweight, tree-shakable, and maintains API compatibility with Polkadot.js.
150+
151+
**Prerequisites**
152+
153+
- [Node.js](https://nodejs.org/){target=\_blank} v18 or higher
154+
- npm, pnpm, or yarn package manager
155+
156+
**Environment Setup**
157+
158+
1. Create and initialize a new project:
159+
160+
```bash
161+
mkdir dedot-query-example && cd dedot-query-example && \
162+
npm init -y && npm pkg set type=module
163+
```
164+
165+
2. Install dependencies:
166+
167+
```bash
168+
npm install dedot && \
169+
npm install --save-dev @dedot/chaintypes @types/node tsx typescript
170+
```
171+
172+
**Query Account Balance**
173+
174+
The following example queries the `System.Account` storage to retrieve an account's native token balance.
175+
176+
Create a file named `query-balance.ts`:
177+
178+
```typescript title="query-balance.ts"
179+
--8<-- "code/chain-interactions/query-data/query-sdks/dedot/query-balance.ts"
180+
```
181+
182+
Run the script:
183+
184+
```bash
185+
npx tsx query-balance.ts
186+
```
187+
188+
**Query Asset Information**
189+
190+
The following example queries the `Assets` pallet to retrieve metadata and balance information for USDT (asset ID 1984).
191+
192+
Create a file named `query-asset.ts`:
193+
194+
```typescript title="query-asset.ts"
195+
--8<-- "code/chain-interactions/query-data/query-sdks/dedot/query-asset.ts"
196+
```
197+
198+
Run the script:
199+
200+
```bash
201+
npx tsx query-asset.ts
202+
```
203+
204+
=== "Python"
205+
206+
[Python Substrate Interface](/reference/tools/py-substrate-interface/) provides a Python library for interacting with Substrate-based chains.
207+
208+
**Prerequisites**
209+
210+
- [Python](https://www.python.org/){target=\_blank} 3.8 or higher
211+
- pip package manager
212+
213+
**Environment Setup**
214+
215+
1. Create a new project directory and set up a virtual environment:
216+
217+
```bash
218+
mkdir psi-query-example && cd psi-query-example && \
219+
python3 -m venv venv && source venv/bin/activate
220+
```
221+
222+
2. Install the substrate-interface package:
223+
224+
```bash
225+
pip install substrate-interface
226+
```
227+
228+
**Query Account Balance**
229+
230+
The following example queries the `System.Account` storage to retrieve an account's native token balance.
231+
232+
Create a file named `query_balance.py`:
233+
234+
```python title="query_balance.py"
235+
--8<-- "code/chain-interactions/query-data/query-sdks/psi/query_balance.py"
236+
```
237+
238+
Run the script:
239+
240+
```bash
241+
python query_balance.py
242+
```
243+
244+
**Query Asset Information**
245+
246+
The following example queries the `Assets` pallet to retrieve metadata and balance information for USDT (asset ID 1984).
247+
248+
Create a file named `query_asset.py`:
249+
250+
```python title="query_asset.py"
251+
--8<-- "code/chain-interactions/query-data/query-sdks/psi/query_asset.py"
252+
```
253+
254+
Run the script:
255+
256+
```bash
257+
python query_asset.py
258+
```
259+
260+
=== "Subxt"
261+
262+
[Subxt](/reference/tools/subxt/) is a Rust library that provides compile-time type safety through code generation from chain metadata.
263+
264+
**Prerequisites**
265+
266+
- [Rust](https://rustup.rs/){target=\_blank} toolchain (latest stable)
267+
- Cargo package manager
268+
269+
**Environment Setup**
270+
271+
1. Create a new Rust project:
272+
273+
```bash
274+
cargo new subxt-query-example && cd subxt-query-example
275+
```
276+
277+
2. Install the Subxt CLI:
278+
279+
```bash
280+
cargo install subxt-cli@{{ dependencies.crates.subxt_cli.version }}
281+
```
282+
283+
3. Download the Polkadot Hub metadata:
284+
285+
```bash
286+
subxt metadata --url wss://polkadot-asset-hub-rpc.polkadot.io -o asset_hub_metadata.scale
287+
```
288+
289+
4. Update `Cargo.toml` with the required dependencies:
290+
291+
```toml title="Cargo.toml"
292+
--8<-- "code/chain-interactions/query-data/query-sdks/subxt/Cargo.toml"
293+
```
294+
295+
**Query Account Balance**
296+
297+
The following example queries the `System.Account` storage to retrieve an account's native token balance.
298+
299+
Create a file at `src/bin/query_balance.rs`:
300+
301+
```rust title="src/bin/query_balance.rs"
302+
--8<-- "code/chain-interactions/query-data/query-sdks/subxt/src/bin/query_balance.rs"
303+
```
304+
305+
Run the script:
306+
307+
```bash
308+
cargo run --bin query_balance
309+
```
310+
311+
**Query Asset Information**
312+
313+
The following example queries the `Assets` pallet to retrieve metadata and balance information for USDT (asset ID 1984).
314+
315+
Create a file at `src/bin/query_asset.rs`:
316+
317+
```rust title="src/bin/query_asset.rs"
318+
--8<-- "code/chain-interactions/query-data/query-sdks/subxt/src/bin/query_asset.rs"
319+
```
320+
321+
Run the script:
322+
323+
```bash
324+
cargo run --bin query_asset
325+
```
326+
327+
## Where to Go Next
328+
329+
Now that you understand how to query on-chain state, explore these related topics:
330+
331+
- **[Runtime API Calls](/chain-interactions/query-data/runtime-api-calls/)** - Execute runtime APIs for specialized queries
332+
- **[Send Transactions](/chain-interactions/send-transactions/with-sdks/)** - Learn to construct and submit transactions
333+
- **[SDK Reference Pages](/reference/tools/papi/)** - Detailed documentation for each SDK
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "dedot-query-example",
3+
"version": "1.0.0",
4+
"type": "module",
5+
"scripts": {
6+
"query-balance": "npx tsx query-balance.ts",
7+
"query-asset": "npx tsx query-asset.ts"
8+
},
9+
"dependencies": {
10+
"dedot": "^0.7.0"
11+
},
12+
"devDependencies": {
13+
"@dedot/chaintypes": "^0.7.0",
14+
"@types/node": "^22.12.0",
15+
"tsx": "^4.19.0",
16+
"typescript": "^5.7.3"
17+
}
18+
}

0 commit comments

Comments
 (0)