Skip to content

Commit d59089b

Browse files
Query chain state with SDKs (#1293)
* add query chain state with sdks * update links * add output * add placeholders * add note for replacing INSERT * Add Cargo.toml for subxt query examples * formatting updates * update description --------- Co-authored-by: Erin Shaben <eshaben@icloud.com>
1 parent a116452 commit d59089b

22 files changed

+1120
-1
lines changed

.chain-interactions/query-data/query-sdks.md

Lines changed: 404 additions & 1 deletion
Large diffs are not rendered by default.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<div class="termynal" data-termynal>
2+
<span data-ty="input"><span class="file-path"></span>npx tsx query-asset.ts</span>
3+
<span data-ty>Connected to Polkadot Hub</span>
4+
<span data-ty>Querying asset ID: 1984</span>
5+
<span data-ty></span>
6+
<span data-ty>Asset Metadata:</span>
7+
<span data-ty> Name: Tether USD</span>
8+
<span data-ty> Symbol: USDT</span>
9+
<span data-ty> Decimals: 6</span>
10+
<span data-ty></span>
11+
<span data-ty>Asset Details:</span>
12+
<span data-ty> Owner: 5Gy6UDPQNFG6vBLnwn3VyFSMAisgw1yP2kJuo2Gn8XhUD8V8</span>
13+
<span data-ty> Supply: 77998622834581</span>
14+
<span data-ty> Accounts: 13544</span>
15+
<span data-ty> Min Balance: 10000</span>
16+
<span data-ty> Status: "Live"</span>
17+
<span data-ty></span>
18+
<span data-ty>Querying asset balance for: 14E5nqKAp3oAJcmzgZhUD2RcptBeUBScxKHgJKU4HPNcKVf3</span>
19+
<span data-ty></span>
20+
<span data-ty>No asset balance found for this account</span>
21+
</div>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { DedotClient, WsProvider } from 'dedot';
2+
import { hexToString } from 'dedot/utils';
3+
import type { PolkadotAssetHubApi } from '@dedot/chaintypes';
4+
5+
const ASSET_HUB_RPC = 'INSERT_WS_ENDPOINT';
6+
7+
// USDT on Polkadot Hub
8+
const USDT_ASSET_ID = 1984;
9+
10+
// Example address to query asset balance
11+
const ADDRESS = 'INSERT_ADDRESS';
12+
13+
async function main() {
14+
// Initialize provider and client with Asset Hub types
15+
const provider = new WsProvider(ASSET_HUB_RPC);
16+
const client = await DedotClient.new<PolkadotAssetHubApi>(provider);
17+
18+
console.log('Connected to Polkadot Hub');
19+
console.log(`Querying asset ID: ${USDT_ASSET_ID}\n`);
20+
21+
// Query asset metadata
22+
const assetMetadata = await client.query.assets.metadata(USDT_ASSET_ID);
23+
24+
console.log('Asset Metadata:');
25+
console.log(` Name: ${hexToString(assetMetadata.name)}`);
26+
console.log(` Symbol: ${hexToString(assetMetadata.symbol)}`);
27+
console.log(` Decimals: ${assetMetadata.decimals}`);
28+
29+
// Query asset details
30+
const assetDetails = await client.query.assets.asset(USDT_ASSET_ID);
31+
32+
if (assetDetails) {
33+
console.log('\nAsset Details:');
34+
console.log(` Owner: ${assetDetails.owner.address()}`);
35+
console.log(` Supply: ${assetDetails.supply}`);
36+
console.log(` Accounts: ${assetDetails.accounts}`);
37+
console.log(` Min Balance: ${assetDetails.minBalance}`);
38+
console.log(` Status: ${JSON.stringify(assetDetails.status)}`);
39+
}
40+
41+
// Query account's asset balance
42+
console.log(`\nQuerying asset balance for: ${ADDRESS}`);
43+
const assetAccount = await client.query.assets.account([
44+
USDT_ASSET_ID,
45+
ADDRESS,
46+
]);
47+
48+
if (assetAccount) {
49+
console.log('\nAsset Account:');
50+
console.log(` Balance: ${assetAccount.balance}`);
51+
console.log(` Status: ${JSON.stringify(assetAccount.status)}`);
52+
} else {
53+
console.log('\nNo asset balance found for this account');
54+
}
55+
56+
// Disconnect the client
57+
await client.disconnect();
58+
}
59+
60+
main().catch(console.error);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<div class="termynal" data-termynal>
2+
<span data-ty="input"><span class="file-path"></span>npx tsx query-balance.ts</span>
3+
<span data-ty>Connected to Polkadot Hub</span>
4+
<span data-ty>Querying balance for: 14E5nqKAp3oAJcmzgZhUD2RcptBeUBScxKHgJKU4HPNcKVf3</span>
5+
<span data-ty></span>
6+
<span data-ty>Account Information:</span>
7+
<span data-ty> Nonce: 0</span>
8+
<span data-ty> Free Balance: 0</span>
9+
<span data-ty> Reserved: 0</span>
10+
<span data-ty> Frozen: 0</span>
11+
</div>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { DedotClient, WsProvider } from 'dedot';
2+
import type { PolkadotAssetHubApi } from '@dedot/chaintypes';
3+
4+
const ASSET_HUB_RPC = 'INSERT_WS_ENDPOINT';
5+
6+
// Example address to query (Polkadot Hub address)
7+
const ADDRESS = 'INSERT_ADDRESS';
8+
9+
async function main() {
10+
// Initialize provider and client with Asset Hub types
11+
const provider = new WsProvider(ASSET_HUB_RPC);
12+
const client = await DedotClient.new<PolkadotAssetHubApi>(provider);
13+
14+
console.log('Connected to Polkadot Hub');
15+
console.log(`Querying balance for: ${ADDRESS}\n`);
16+
17+
// Query the system.account storage
18+
const accountInfo = await client.query.system.account(ADDRESS);
19+
20+
// Extract balance information
21+
const { nonce, data } = accountInfo;
22+
const { free, reserved, frozen } = data;
23+
24+
console.log('Account Information:');
25+
console.log(` Nonce: ${nonce}`);
26+
console.log(` Free Balance: ${free}`);
27+
console.log(` Reserved: ${reserved}`);
28+
console.log(` Frozen: ${frozen}`);
29+
30+
// Disconnect the client
31+
await client.disconnect();
32+
}
33+
34+
main().catch(console.error);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<div class="termynal" data-termynal>
2+
<span data-ty="input"><span class="file-path"></span>npx tsx query-asset.ts</span>
3+
<span data-ty>Connected to Polkadot Hub</span>
4+
<span data-ty>Querying asset ID: 1984</span>
5+
<span data-ty></span>
6+
<span data-ty>Asset Metadata:</span>
7+
<span data-ty> Name: Tether USD</span>
8+
<span data-ty> Symbol: USDT</span>
9+
<span data-ty> Decimals: 6</span>
10+
<span data-ty></span>
11+
<span data-ty>Asset Details:</span>
12+
<span data-ty> Owner: 15uPcYeUE2XaMiMJuR6W7QGW2LsLdKXX7F3PxKG8gcizPh3X</span>
13+
<span data-ty> Supply: 77998622834581</span>
14+
<span data-ty> Accounts: 13544</span>
15+
<span data-ty> Min Balance: 10000</span>
16+
<span data-ty> Status: Live</span>
17+
<span data-ty></span>
18+
<span data-ty>Querying asset balance for: 14E5nqKAp3oAJcmzgZhUD2RcptBeUBScxKHgJKU4HPNcKVf3</span>
19+
<span data-ty></span>
20+
<span data-ty>No asset balance found for this account</span>
21+
</div>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { createClient } from 'polkadot-api';
2+
import { getWsProvider } from 'polkadot-api/ws-provider/node';
3+
import { withPolkadotSdkCompat } from 'polkadot-api/polkadot-sdk-compat';
4+
import { pah } from '@polkadot-api/descriptors';
5+
6+
const ASSET_HUB_RPC = 'INSERT_WS_ENDPOINT';
7+
8+
// USDT on Polkadot Hub
9+
const USDT_ASSET_ID = 1984;
10+
11+
// Example address to query asset balance
12+
const ADDRESS = 'INSERT_ADDRESS';
13+
14+
async function main() {
15+
// Create the client connection
16+
const client = createClient(
17+
withPolkadotSdkCompat(getWsProvider(ASSET_HUB_RPC))
18+
);
19+
20+
// Get the typed API for Polkadot Hub
21+
const api = client.getTypedApi(pah);
22+
23+
console.log('Connected to Polkadot Hub');
24+
console.log(`Querying asset ID: ${USDT_ASSET_ID}\n`);
25+
26+
// Query asset metadata
27+
const assetMetadata = await api.query.Assets.Metadata.getValue(USDT_ASSET_ID);
28+
29+
if (assetMetadata) {
30+
console.log('Asset Metadata:');
31+
console.log(` Name: ${assetMetadata.name.asText()}`);
32+
console.log(` Symbol: ${assetMetadata.symbol.asText()}`);
33+
console.log(` Decimals: ${assetMetadata.decimals}`);
34+
}
35+
36+
// Query asset details
37+
const assetDetails = await api.query.Assets.Asset.getValue(USDT_ASSET_ID);
38+
39+
if (assetDetails) {
40+
console.log('\nAsset Details:');
41+
console.log(` Owner: ${assetDetails.owner}`);
42+
console.log(` Supply: ${assetDetails.supply}`);
43+
console.log(` Accounts: ${assetDetails.accounts}`);
44+
console.log(` Min Balance: ${assetDetails.min_balance}`);
45+
console.log(` Status: ${assetDetails.status.type}`);
46+
}
47+
48+
// Query account's asset balance
49+
console.log(`\nQuerying asset balance for: ${ADDRESS}`);
50+
const assetAccount = await api.query.Assets.Account.getValue(
51+
USDT_ASSET_ID,
52+
ADDRESS
53+
);
54+
55+
if (assetAccount) {
56+
console.log('\nAsset Account:');
57+
console.log(` Balance: ${assetAccount.balance}`);
58+
console.log(` Status: ${assetAccount.status.type}`);
59+
} else {
60+
console.log('\nNo asset balance found for this account');
61+
}
62+
63+
// Disconnect the client
64+
client.destroy();
65+
}
66+
67+
main().catch(console.error);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<div class="termynal" data-termynal>
2+
<span data-ty="input"><span class="file-path"></span>npx tsx query-balance.ts</span>
3+
<span data-ty>Connected to Polkadot Hub</span>
4+
<span data-ty>Querying balance for: 14E5nqKAp3oAJcmzgZhUD2RcptBeUBScxKHgJKU4HPNcKVf3</span>
5+
<span data-ty></span>
6+
<span data-ty>Account Information:</span>
7+
<span data-ty> Nonce: 0</span>
8+
<span data-ty> Free Balance: 0</span>
9+
<span data-ty> Reserved: 0</span>
10+
<span data-ty> Frozen: 0</span>
11+
</div>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { createClient } from 'polkadot-api';
2+
import { getWsProvider } from 'polkadot-api/ws-provider/node';
3+
import { withPolkadotSdkCompat } from 'polkadot-api/polkadot-sdk-compat';
4+
import { pah } from '@polkadot-api/descriptors';
5+
6+
const ASSET_HUB_RPC = 'INSERT_WS_ENDPOINT';
7+
8+
// Example address to query (Polkadot Hub address)
9+
const ADDRESS = 'INSERT_ADDRESS';
10+
11+
async function main() {
12+
// Create the client connection
13+
const client = createClient(
14+
withPolkadotSdkCompat(getWsProvider(ASSET_HUB_RPC))
15+
);
16+
17+
// Get the typed API for Polkadot Hub
18+
const api = client.getTypedApi(pah);
19+
20+
console.log('Connected to Polkadot Hub');
21+
console.log(`Querying balance for: ${ADDRESS}\n`);
22+
23+
// Query the System.Account storage
24+
const accountInfo = await api.query.System.Account.getValue(ADDRESS);
25+
26+
// Extract balance information
27+
const { data, nonce } = accountInfo;
28+
const { free, reserved, frozen } = data;
29+
30+
console.log('Account Information:');
31+
console.log(` Nonce: ${nonce}`);
32+
console.log(` Free Balance: ${free}`);
33+
console.log(` Reserved: ${reserved}`);
34+
console.log(` Frozen: ${frozen}`);
35+
36+
// Disconnect the client
37+
client.destroy();
38+
}
39+
40+
main().catch(console.error);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<div class="termynal" data-termynal>
2+
<span data-ty="input"><span class="file-path"></span>node query-asset.js</span>
3+
<span data-ty>Connected to Polkadot Hub</span>
4+
<span data-ty>Querying asset ID: 1984</span>
5+
<span data-ty></span>
6+
<span data-ty>Asset Metadata:</span>
7+
<span data-ty> Name: Tether USD</span>
8+
<span data-ty> Symbol: USDT</span>
9+
<span data-ty> Decimals: 6</span>
10+
<span data-ty></span>
11+
<span data-ty>Asset Details:</span>
12+
<span data-ty> Owner: 15uPcYeUE2XaMiMJuR6W7QGW2LsLdKXX7F3PxKG8gcizPh3X</span>
13+
<span data-ty> Supply: 77998622834581</span>
14+
<span data-ty> Accounts: 13544</span>
15+
<span data-ty> Min Balance: 10000</span>
16+
<span data-ty> Status: Live</span>
17+
<span data-ty></span>
18+
<span data-ty>Querying asset balance for: 14E5nqKAp3oAJcmzgZhUD2RcptBeUBScxKHgJKU4HPNcKVf3</span>
19+
<span data-ty></span>
20+
<span data-ty>No asset balance found for this account</span>
21+
</div>

0 commit comments

Comments
 (0)