Skip to content

Commit e20f2d1

Browse files
committed
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.
1 parent 9c320f6 commit e20f2d1

File tree

11 files changed

+386
-370
lines changed

11 files changed

+386
-370
lines changed

.chain-interactions/accounts/query-accounts.md

Lines changed: 11 additions & 370 deletions
Large diffs are not rendered by default.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<div class="termynal" data-termynal>
2+
<span data-ty="input"><span class="file-path"></span>npx tsx query-account.ts</span>
3+
<span data-ty>Connected to Polkadot Hub</span>
4+
<span data-ty></span>
5+
<span data-ty>Querying account: 5GgbDVeKZwCmMHzn58iFSgSZDTojRMM52arXnuNXto28R7mg</span>
6+
<span data-ty></span>
7+
<span data-ty>Account Information:</span>
8+
<span data-ty>===================</span>
9+
<span data-ty>Nonce: 15</span>
10+
<span data-ty>Consumers: 0</span>
11+
<span data-ty>Providers: 1</span>
12+
<span data-ty>Sufficients: 0</span>
13+
<span data-ty></span>
14+
<span data-ty>Balance Details:</span>
15+
<span data-ty>================</span>
16+
<span data-ty>Free Balance: 59781317040 (5.978131704 PAS)</span>
17+
<span data-ty>Reserved Balance: 0 (0 PAS)</span>
18+
<span data-ty>Frozen Balance: 0 (0 PAS)</span>
19+
<span data-ty></span>
20+
<span data-ty>Total Balance: 59781317040 (5.978131704 PAS)</span>
21+
<span data-ty></span>
22+
<span data-ty>Disconnected from Polkadot Hub</span>
23+
</div>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { DedotClient, WsProvider } from "dedot";
2+
import type { PolkadotAssetHubApi } from "@dedot/chaintypes";
3+
4+
const POLKADOT_HUB_RPC = "INSERT_WS_ENDPOINT";
5+
const ACCOUNT_ADDRESS = "INSERT_ACCOUNT_ADDRESS";
6+
const PAS_UNITS = 10_000_000_000;
7+
8+
async function main() {
9+
// Initialize provider and client with Asset Hub types
10+
const provider = new WsProvider(POLKADOT_HUB_RPC);
11+
const client = await DedotClient.new<PolkadotAssetHubApi>(provider);
12+
13+
console.log("Connected to Polkadot Hub");
14+
15+
console.log(`\nQuerying account: ${ACCOUNT_ADDRESS}\n`);
16+
17+
// Query account information
18+
const accountInfo = await client.query.system.account(ACCOUNT_ADDRESS);
19+
20+
// Display account information
21+
console.log("Account Information:");
22+
console.log("===================");
23+
console.log(`Nonce: ${accountInfo.nonce}`);
24+
console.log(`Consumers: ${accountInfo.consumers}`);
25+
console.log(`Providers: ${accountInfo.providers}`);
26+
console.log(`Sufficients: ${accountInfo.sufficients}`);
27+
28+
console.log("\nBalance Details:");
29+
console.log("================");
30+
console.log(`Free Balance: ${accountInfo.data.free} (${Number(accountInfo.data.free) / PAS_UNITS} PAS)`);
31+
console.log(`Reserved Balance: ${accountInfo.data.reserved} (${Number(accountInfo.data.reserved) / PAS_UNITS} PAS)`);
32+
console.log(`Frozen Balance: ${accountInfo.data.frozen} (${Number(accountInfo.data.frozen) / PAS_UNITS} PAS)`);
33+
34+
const total = Number(accountInfo.data.free) + Number(accountInfo.data.reserved);
35+
console.log(`\nTotal Balance: ${total} (${total / PAS_UNITS} PAS)`);
36+
37+
// Disconnect the client
38+
await client.disconnect();
39+
console.log("\nDisconnected from Polkadot Hub");
40+
}
41+
42+
main().catch(console.error);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<div class="termynal" data-termynal>
2+
<span data-ty="input"><span class="file-path"></span>npx tsx query-account.ts</span>
3+
<span data-ty>Connected to Polkadot Hub</span>
4+
<span data-ty></span>
5+
<span data-ty>Querying account: 5GgbDVeKZwCmMHzn58iFSgSZDTojRMM52arXnuNXto28R7mg</span>
6+
<span data-ty></span>
7+
<span data-ty>Account Information:</span>
8+
<span data-ty>===================</span>
9+
<span data-ty>Nonce: 15</span>
10+
<span data-ty>Consumers: 0</span>
11+
<span data-ty>Providers: 1</span>
12+
<span data-ty>Sufficients: 0</span>
13+
<span data-ty></span>
14+
<span data-ty>Balance Details:</span>
15+
<span data-ty>================</span>
16+
<span data-ty>Free Balance: 59781317040 (5.978131704 PAS)</span>
17+
<span data-ty>Reserved Balance: 0 (0 PAS)</span>
18+
<span data-ty>Frozen Balance: 0 (0 PAS)</span>
19+
<span data-ty></span>
20+
<span data-ty>Total Balance: 59781317040 (5.978131704 PAS)</span>
21+
<span data-ty></span>
22+
<span data-ty>Disconnected</span>
23+
</div>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { createClient } from "polkadot-api";
2+
import { getWsProvider } from "polkadot-api/ws-provider";
3+
import { withPolkadotSdkCompat } from "polkadot-api/polkadot-sdk-compat";
4+
import { polkadotTestNet } from "@polkadot-api/descriptors";
5+
6+
const POLKADOT_HUB_RPC = "INSERT_WS_ENDPOINT";
7+
const ACCOUNT_ADDRESS = "INSERT_ACCOUNT_ADDRESS";
8+
const PAS_UNITS = 10_000_000_000;
9+
10+
11+
async function main() {
12+
try {
13+
// Create the client connection
14+
const client = createClient(
15+
withPolkadotSdkCompat(getWsProvider(POLKADOT_HUB_RPC))
16+
);
17+
18+
// Get the typed API
19+
const api = client.getTypedApi(polkadotTestNet);
20+
console.log("Connected to Polkadot Hub");
21+
22+
console.log(`\nQuerying account: ${ACCOUNT_ADDRESS}\n`);
23+
24+
// Query account information
25+
const accountInfo = await api.query.System.Account.getValue(ACCOUNT_ADDRESS);
26+
27+
// Display account information
28+
console.log("Account Information:");
29+
console.log("===================");
30+
console.log(`Nonce: ${accountInfo.nonce}`);
31+
console.log(`Consumers: ${accountInfo.consumers}`);
32+
console.log(`Providers: ${accountInfo.providers}`);
33+
console.log(`Sufficients: ${accountInfo.sufficients}`);
34+
35+
console.log("\nBalance Details:");
36+
console.log("================");
37+
console.log(`Free Balance: ${accountInfo.data.free} (${Number(accountInfo.data.free) / PAS_UNITS} PAS)`);
38+
console.log(`Reserved Balance: ${accountInfo.data.reserved} (${Number(accountInfo.data.reserved) / PAS_UNITS} PAS)`);
39+
console.log(`Frozen Balance: ${accountInfo.data.frozen} (${Number(accountInfo.data.frozen) / PAS_UNITS} PAS)`);
40+
41+
const total = Number(accountInfo.data.free) + Number(accountInfo.data.reserved);
42+
console.log(`\nTotal Balance: ${total} (${total / PAS_UNITS} PAS)`);
43+
44+
await client.destroy();
45+
console.log("\nDisconnected");
46+
47+
} catch (error) {
48+
console.error("Error:", error);
49+
process.exit(1);
50+
}
51+
}
52+
53+
main();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<div class="termynal" data-termynal>
2+
<span data-ty="input"><span class="file-path"></span>node query-account.js</span>
3+
<span data-ty>Connected to Polkadot Hub</span>
4+
<span data-ty></span>
5+
<span data-ty>Querying account: 5GgbDVeKZwCmMHzn58iFSgSZDTojRMM52arXnuNXto28R7mg</span>
6+
<span data-ty></span>
7+
<span data-ty>Account Information:</span>
8+
<span data-ty>===================</span>
9+
<span data-ty>Nonce: 15</span>
10+
<span data-ty>Consumers: 0</span>
11+
<span data-ty>Providers: 1</span>
12+
<span data-ty>Sufficients: 0</span>
13+
<span data-ty></span>
14+
<span data-ty>Balance Details:</span>
15+
<span data-ty>================</span>
16+
<span data-ty>Free Balance: 59781317040 (5.978131704 PAS)</span>
17+
<span data-ty>Reserved Balance: 0 (0 PAS)</span>
18+
<span data-ty>Frozen Balance: 0 (0 PAS)</span>
19+
<span data-ty></span>
20+
<span data-ty>Total Balance: 59781317040 (5.978131704 PAS)</span>
21+
<span data-ty></span>
22+
<span data-ty>Disconnected</span>
23+
</div>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { ApiPromise, WsProvider } from "@polkadot/api";
2+
3+
const POLKADOT_HUB_RPC = "INSERT_WS_ENDPOINT";
4+
const ACCOUNT_ADDRESS = "INSERT_ACCOUNT_ADDRESS";
5+
const PAS_UNITS = 10_000_000_000;
6+
7+
async function main() {
8+
// Create a WebSocket provider
9+
const wsProvider = new WsProvider(POLKADOT_HUB_RPC);
10+
11+
// Initialize the API
12+
const api = await ApiPromise.create({ provider: wsProvider });
13+
console.log("Connected to Polkadot Hub");
14+
15+
console.log(`\nQuerying account: ${ACCOUNT_ADDRESS}\n`);
16+
17+
// Query account information
18+
const accountInfo = await api.query.system.account(ACCOUNT_ADDRESS);
19+
20+
// Display account information
21+
console.log("Account Information:");
22+
console.log("===================");
23+
console.log(`Nonce: ${accountInfo.nonce.toString()}`);
24+
console.log(`Consumers: ${accountInfo.consumers.toString()}`);
25+
console.log(`Providers: ${accountInfo.providers.toString()}`);
26+
console.log(`Sufficients: ${accountInfo.sufficients.toString()}`);
27+
28+
console.log("\nBalance Details:");
29+
console.log("================");
30+
console.log(`Free Balance: ${accountInfo.data.free.toString()} (${Number(accountInfo.data.free.toBigInt()) / PAS_UNITS} PAS)`);
31+
console.log(`Reserved Balance: ${accountInfo.data.reserved.toString()} (${Number(accountInfo.data.reserved.toBigInt()) / PAS_UNITS} PAS)`);
32+
console.log(`Frozen Balance: ${accountInfo.data.frozen.toString()} (${Number(accountInfo.data.frozen.toBigInt()) / PAS_UNITS} PAS)`);
33+
34+
const total = Number(accountInfo.data.free.toBigInt()) + Number(accountInfo.data.reserved.toBigInt());
35+
console.log(`\nTotal Balance: ${total} (${total / PAS_UNITS} PAS)`);
36+
37+
// Disconnect from the node
38+
await api.disconnect();
39+
console.log("\nDisconnected");
40+
}
41+
42+
main().catch(console.error);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<div class="termynal" data-termynal>
2+
<span data-ty="input"><span class="file-path"></span>python3 query_account.py</span>
3+
<span data-ty>Connected to Polkadot Hub</span>
4+
<span data-ty></span>
5+
<span data-ty>Querying account: 5GgbDVeKZwCmMHzn58iFSgSZDTojRMM52arXnuNXto28R7mg</span>
6+
<span data-ty></span>
7+
<span data-ty>Account Information:</span>
8+
<span data-ty>===================</span>
9+
<span data-ty>Nonce: 15</span>
10+
<span data-ty>Consumers: 0</span>
11+
<span data-ty>Providers: 1</span>
12+
<span data-ty>Sufficients: 0</span>
13+
<span data-ty></span>
14+
<span data-ty>Balance Details:</span>
15+
<span data-ty>================</span>
16+
<span data-ty>Free Balance: 59781317040 (5.978131704 PAS)</span>
17+
<span data-ty>Reserved Balance: 0 (0.0 PAS)</span>
18+
<span data-ty>Frozen Balance: 0 (0.0 PAS)</span>
19+
<span data-ty></span>
20+
<span data-ty>Total Balance: 59781317040 (5.978131704 PAS)</span>
21+
<span data-ty></span>
22+
<span data-ty>Disconnected</span>
23+
</div>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from substrateinterface import SubstrateInterface
2+
3+
POLKADOT_HUB_RPC = "INSERT_WS_ENDPOINT"
4+
ACCOUNT_ADDRESS = "INSERT_ACCOUNT_ADDRESS"
5+
PAS_UNITS = 10_000_000_000
6+
7+
def main():
8+
# Connect to Polkadot Hub
9+
substrate = SubstrateInterface(url=POLKADOT_HUB_RPC)
10+
11+
print("Connected to Polkadot Hub")
12+
13+
print(f"\nQuerying account: {ACCOUNT_ADDRESS}\n")
14+
15+
# Query account information
16+
account_info = substrate.query(
17+
module="System", storage_function="Account", params=[ACCOUNT_ADDRESS]
18+
)
19+
20+
# Display account information
21+
print("Account Information:")
22+
print("===================")
23+
print(f"Nonce: {account_info.value['nonce']}")
24+
print(f"Consumers: {account_info.value['consumers']}")
25+
print(f"Providers: {account_info.value['providers']}")
26+
print(f"Sufficients: {account_info.value['sufficients']}")
27+
28+
print("\nBalance Details:")
29+
print("================")
30+
free_balance = account_info.value["data"]["free"]
31+
reserved_balance = account_info.value["data"]["reserved"]
32+
frozen_balance = account_info.value["data"]["frozen"]
33+
34+
print(f"Free Balance: {free_balance} ({free_balance / PAS_UNITS} PAS)")
35+
print(
36+
f"Reserved Balance: {reserved_balance} ({reserved_balance / PAS_UNITS} PAS)"
37+
)
38+
print(f"Frozen Balance: {frozen_balance} ({frozen_balance / PAS_UNITS} PAS)")
39+
40+
total = free_balance + reserved_balance
41+
print(f"\nTotal Balance: {total} ({total / PAS_UNITS} PAS)")
42+
43+
# Close connection
44+
substrate.close()
45+
print("\nDisconnected")
46+
47+
48+
if __name__ == "__main__":
49+
main()
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<div class="termynal" data-termynal>
2+
<span data-ty="input"><span class="file-path"></span>cargo run --bin query_account</span>
3+
<span data-ty>Connected to Polkadot Hub</span>
4+
<span data-ty></span>
5+
<span data-ty>Querying account: 5GgbDVeKZwCmMHzn58iFSgSZDTojRMM52arXnuNXto28R7mg</span>
6+
<span data-ty></span>
7+
<span data-ty>Account Information:</span>
8+
<span data-ty>===================</span>
9+
<span data-ty>Nonce: 15</span>
10+
<span data-ty>Consumers: 0</span>
11+
<span data-ty>Providers: 1</span>
12+
<span data-ty>Sufficients: 0</span>
13+
<span data-ty></span>
14+
<span data-ty>Balance Details:</span>
15+
<span data-ty>================</span>
16+
<span data-ty>Free Balance: 59781317040 (5.978131704 PAS)</span>
17+
<span data-ty>Reserved Balance: 0 (0 PAS)</span>
18+
<span data-ty>Frozen Balance: 0 (0 PAS)</span>
19+
<span data-ty></span>
20+
<span data-ty>Total Balance: 59781317040 (5.978131704 PAS)</span>
21+
<span data-ty></span>
22+
<span data-ty>Disconnected</span>
23+
</div>

0 commit comments

Comments
 (0)