Skip to content

Commit 4bcb1a6

Browse files
nhussein11eshaben
andauthored
Add query account info (#1322)
* Enhance documentation for querying account information with SDKs * Added comprehensive guide on querying account information using five popular SDKs: Polkadot API (PAPI), Polkadot.js API, Dedot, Python Substrate Interface, and Subxt. * Included prerequisites, environment setup, and runnable examples for each SDK. * Clarified account data fields and provided next steps for users. * Refactor account querying examples for multiple SDKs * Updated the documentation for querying account information using various SDKs: Polkadot API, Polkadot.js, Dedot, Python Substrate Interface, and Subxt. * Replaced lengthy code snippets with references to external files for better readability. * Ensured consistency in output examples across all SDKs. * update account query page and similar pages for consistency --------- Co-authored-by: Erin Shaben <[email protected]>
1 parent 102cab5 commit 4bcb1a6

File tree

16 files changed

+765
-17
lines changed

16 files changed

+765
-17
lines changed
Lines changed: 323 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,323 @@
1-
TODO
1+
---
2+
title: Query Account Information with SDKs
3+
description: Learn how to query account information using five popular SDKs—Polkadot API (PAPI), Polkadot.js API, Dedot, Python Substrate Interface, and Subxt.
4+
categories: Basics, Polkadot Protocol
5+
---
6+
7+
# Query Account Information with SDKs
8+
9+
## Introduction
10+
11+
Querying account information is a fundamental operation when interacting with Polkadot SDK-based blockchains. Account queries allow you to retrieve balances, nonces, account data, and other state information stored on-chain. Each SDK provides different methods for accessing this data efficiently.
12+
13+
This guide demonstrates how to query account information using five popular SDKs:
14+
15+
- **[Polkadot API (PAPI)](/reference/tools/papi/){target=\_blank}**: Modern TypeScript library with type-safe APIs
16+
- **[Polkadot.js API](/reference/tools/polkadot-js-api/){target=\_blank}**: Comprehensive JavaScript library (maintenance mode)
17+
- **[Dedot](/reference/tools/dedot/){target=\_blank}**: Lightweight TypeScript library optimized for performance
18+
- **[Python Substrate Interface](/reference/tools/py-substrate-interface/){target=\_blank}**: Python library for Substrate chains
19+
- **[Subxt](/reference/tools/subxt/){target=\_blank}**: Rust library with compile-time type safety
20+
21+
Select your preferred SDK below to see complete, runnable examples that query account information on Polkadot Hub.
22+
23+
## Prerequisites
24+
25+
- Access to a Polkadot SDK-compatible blockchain endpoint (WebSocket URL)
26+
- An account address to query (can be any valid SS58 address)
27+
28+
## Query Account Information
29+
30+
=== "PAPI"
31+
32+
**Prerequisites**
33+
34+
- [Node.js](https://nodejs.org/){target=\_blank} v18 or higher
35+
- npm, pnpm, or yarn package manager
36+
37+
**Environment Setup**
38+
39+
1. Create and initialize a new project:
40+
41+
```bash
42+
mkdir papi-query-account-example && cd papi-query-account-example && \
43+
npm init -y && npm pkg set type=module
44+
```
45+
46+
2. Install dependencies:
47+
48+
```bash
49+
npm install polkadot-api && \
50+
npm install --save-dev @types/node tsx typescript
51+
```
52+
53+
3. Generate types for Polkadot Hub:
54+
55+
```bash
56+
npx papi add polkadotTestNet -w wss://asset-hub-paseo.dotters.network
57+
```
58+
59+
**Query Account Data**
60+
61+
The following example queries account information including balance, nonce, and other account data.
62+
63+
Create a file named `query-account.ts` and add the following code to it:
64+
65+
```typescript title="query-account.ts"
66+
--8<-- "code/chain-interactions/accounts/query-account/papi/query-account.ts"
67+
```
68+
69+
!!! note
70+
Ensure to replace `INSERT_WS_ENDPOINT` with a valid WebSocket endpoint (e.g., `wss://asset-hub-paseo.dotters.network`) and `INSERT_ADDRESS` with the account address you want to query.
71+
72+
Run the script:
73+
74+
```bash
75+
npx tsx query-account.ts
76+
```
77+
78+
You should see output similar to:
79+
80+
--8<-- "code/chain-interactions/accounts/query-account/papi/query-account-output.html"
81+
82+
83+
=== "Polkadot.js"
84+
85+
!!! warning "Maintenance Mode Only"
86+
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.
87+
88+
**Prerequisites**
89+
90+
- [Node.js](https://nodejs.org/){target=\_blank} v18 or higher
91+
- npm, pnpm, or yarn package manager
92+
93+
**Environment Setup**
94+
95+
1. Create and initialize a new project:
96+
97+
```bash
98+
mkdir pjs-query-account-example && cd pjs-query-account-example && \
99+
npm init -y && npm pkg set type=module
100+
```
101+
102+
2. Install dependencies:
103+
104+
```bash
105+
npm install @polkadot/api
106+
```
107+
108+
**Query Account Data**
109+
110+
The following example queries account information including balance, nonce, and other account data.
111+
112+
Create a file named `query-account.js` and add the following code to it:
113+
114+
```javascript title="query-account.js"
115+
--8<-- "code/chain-interactions/accounts/query-account/pjs/query-account.js"
116+
```
117+
118+
!!! note
119+
Ensure to replace `INSERT_WS_ENDPOINT` with a valid WebSocket endpoint (e.g., `wss://asset-hub-paseo.dotters.network`) and `INSERT_ADDRESS` with the account address you want to query.
120+
121+
Run the script:
122+
123+
```bash
124+
node query-account.js
125+
```
126+
127+
You should see output similar to:
128+
129+
--8<-- "code/chain-interactions/accounts/query-account/pjs/query-account-output.html"
130+
131+
=== "Dedot"
132+
133+
**Prerequisites**
134+
135+
- [Node.js](https://nodejs.org/){target=\_blank} v18 or higher
136+
- npm, pnpm, or yarn package manager
137+
138+
**Environment Setup**
139+
140+
1. Create and initialize a new project:
141+
142+
```bash
143+
mkdir dedot-query-account-example && cd dedot-query-account-example && \
144+
npm init -y && npm pkg set type=module
145+
```
146+
147+
2. Install dependencies:
148+
149+
```bash
150+
npm install dedot && \
151+
npm install --save-dev @dedot/chaintypes @types/node tsx typescript
152+
```
153+
154+
**Query Account Data**
155+
156+
The following example queries account information including balance, nonce, and other account data.
157+
158+
Create a file named `query-account.ts` and add the following code to it:
159+
160+
```typescript title="query-account.ts"
161+
--8<-- "code/chain-interactions/accounts/query-account/dedot/query-account.ts"
162+
```
163+
164+
!!! note
165+
Ensure to replace `INSERT_WS_ENDPOINT` with a valid WebSocket endpoint (e.g., `wss://asset-hub-paseo.dotters.network`) and `INSERT_ADDRESS` with the account address you want to query.
166+
167+
Run the script:
168+
169+
```bash
170+
npx tsx query-account.ts
171+
```
172+
173+
You should see output similar to:
174+
175+
--8<-- "code/chain-interactions/accounts/query-account/dedot/query-account-output.html"
176+
177+
=== "Python Substrate Interface"
178+
179+
**Prerequisites**
180+
181+
- [Python](https://www.python.org/){target=\_blank} 3.8 or higher
182+
- pip package manager
183+
184+
**Environment Setup**
185+
186+
1. Create a new project directory and set up a virtual environment:
187+
188+
```bash
189+
mkdir psi-query-account-example && cd psi-query-account-example && \
190+
python3 -m venv venv && source venv/bin/activate
191+
```
192+
193+
2. Install the substrate-interface package:
194+
195+
```bash
196+
pip install substrate-interface
197+
```
198+
199+
**Query Account Data**
200+
201+
The following example queries account information including balance, nonce, and other account data.
202+
203+
Create a file named `query_account.py` and add the following code to it:
204+
205+
```python title="query_account.py"
206+
--8<-- "code/chain-interactions/accounts/query-account/psi/query_account.py"
207+
```
208+
209+
!!! note
210+
Ensure to replace `INSERT_WS_ENDPOINT` with a valid WebSocket endpoint (e.g., `wss://asset-hub-paseo.dotters.network`) and `INSERT_ADDRESS` with the account address you want to query.
211+
212+
Run the script:
213+
214+
```bash
215+
python query_account.py
216+
```
217+
218+
You should see output similar to:
219+
220+
--8<-- "code/chain-interactions/accounts/query-account/psi/query-account-output.html"
221+
222+
=== "Subxt"
223+
224+
**Prerequisites**
225+
226+
- [Rust](https://rustup.rs/){target=\_blank} toolchain (latest stable)
227+
- Cargo package manager
228+
229+
**Environment Setup**
230+
231+
1. Create a new Rust project:
232+
233+
```bash
234+
cargo new subxt-query-account-example && cd subxt-query-account-example
235+
```
236+
237+
2. Install the Subxt CLI:
238+
239+
```bash
240+
cargo install [email protected]
241+
```
242+
243+
3. Download the Polkadot Hub metadata:
244+
245+
```bash
246+
subxt metadata --url INSERT_WS_ENDPOINT -o polkadot_testnet_metadata.scale
247+
```
248+
249+
4. Update `Cargo.toml` with the required dependencies:
250+
251+
```toml title="Cargo.toml"
252+
[package]
253+
name = "subxt-query-account-example"
254+
version = "0.1.0"
255+
edition = "2021"
256+
257+
[[bin]]
258+
name = "query_account"
259+
path = "src/bin/query_account.rs"
260+
261+
[dependencies]
262+
subxt = { version = "0.44.0" }
263+
tokio = { version = "1.36.0", features = ["macros", "rt"] }
264+
```
265+
266+
**Query Account Data**
267+
268+
The following example queries account information including balance, nonce, and other account data.
269+
270+
Create a file at `src/bin/query_account.rs` and add the following code to it:
271+
272+
```rust title="src/bin/query_account.rs"
273+
--8<-- "code/chain-interactions/accounts/query-account/subxt/query-account.rs"
274+
```
275+
276+
!!! note
277+
Ensure to replace `INSERT_WS_ENDPOINT` with a valid WebSocket endpoint (e.g., `wss://asset-hub-paseo.dotters.network`) and `INSERT_ADDRESS` with the account address you want to query.
278+
279+
Run the script:
280+
281+
```bash
282+
cargo run --bin query_account
283+
```
284+
285+
You should see output similar to:
286+
287+
--8<-- "code/chain-interactions/accounts/query-account/subxt/query-account-output.html"
288+
289+
## Understanding Account Data
290+
291+
When querying account information, you'll receive several key fields:
292+
293+
- **Nonce**: The number of transactions sent from this account, used to prevent replay attacks.
294+
- **Consumers**: The number of modules depending on this account's existence.
295+
- **Providers**: The number of modules providing for this account's existence.
296+
- **Sufficients**: The number of modules that allow this account to exist on its own.
297+
- **Free Balance**: The transferable balance available for transactions.
298+
- **Reserved Balance**: Balance that is locked for specific purposes (staking, governance, etc.).
299+
- **Frozen Balance**: Balance that cannot be used for transfers but may be used for other operations.
300+
301+
The total balance is the sum of free and reserved balances.
302+
303+
## Where to Go Next
304+
305+
<div class="grid cards" markdown>
306+
307+
- <span class="badge guide">Guide</span> **Query On-Chain State**
308+
309+
---
310+
311+
Explore other types of storage queries.
312+
313+
[:octicons-arrow-right-24: Get Started](/chain-interactions/query-data/query-sdks/)
314+
315+
- <span class="badge guide">Guide</span> **Send Transactions**
316+
317+
---
318+
319+
Learn how to construct and submit transactions.
320+
321+
[:octicons-arrow-right-24: Get Started](/chain-interactions/send-transactions/with-sdks/)
322+
323+
</div>

0 commit comments

Comments
 (0)