This repository was archived by the owner on Sep 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdynamic.eg.ts
55 lines (49 loc) · 1.83 KB
/
dynamic.eg.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* @title Dynamic Usage
* @description You may want to write code whose target chain is unknown before
* runtime. A common example of this is block explorers. Use cases such as this
* are "dynamic" in that they require one to read the chain's metadata at runtime
* in order to derive the means of interacting with that chain. Dynamic usage of
* Capi entails a subtly-different DX compared to that of chain-specific (codegen)
* usage. There are three key differences:
*
* 1. We manually initialize the `ChainRune`.
* 2. We manually access bindings.
* 3. Chain-specifics are untyped (be wary to supply the correct data, as the checker is on vacation).
*/
import { $, ChainRune, WsConnection } from "capi"
/// We could also initialize a `ChainRune` with `WsConnection` and an RPC node WebSocket URL.
const chain = ChainRune.from(WsConnection.bind("wss://rpc.polkadot.io"))
/// Create a binding to the `System` pallet.
const System = chain.pallet("System")
/// Create a binding to the `Account` storage map.
const Account = System.storage("Account")
/// Read the first ten entries of the `Account` storage map.
/// Note how the lack of partial key is communicated via `null`.
const entries = await Account.entries({ limit: 10 }).run()
/// The result should contain a `[Uint8Array, AccountInfo]` tuple of length 10.
console.log("Entries page:", entries)
$.assert(
$.sizedArray(
$.tuple(
$.sizedUint8Array(32),
$.object(
$.field("nonce", $.u32),
$.field("consumers", $.u32),
$.field("providers", $.u32),
$.field("sufficients", $.u32),
$.field(
"data",
$.object(
$.field("free", $.u128),
$.field("reserved", $.u128),
$.field("frozen", $.u128),
$.field("flags", $.u128),
),
),
),
),
10,
),
entries,
)