@parity/product-sdk-chain-client 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/types.ts ADDED
@@ -0,0 +1,73 @@
1
+ import type { ChainDefinition, PolkadotClient, TypedApi } from "polkadot-api";
2
+
3
+ /** Supported chain environments for the Polkadot ecosystem. */
4
+ export type Environment = "polkadot" | "kusama" | "paseo" | "local" | "westend";
5
+
6
+ /**
7
+ * Configuration for {@link createChainClient}.
8
+ *
9
+ * Provide named chain descriptors and their RPC endpoints.
10
+ * TypeScript enforces that `rpcs` has the same keys as `chains`.
11
+ *
12
+ * Note: The SDK routes all connections through the host provider. The `rpcs`
13
+ * field is currently unused but kept for API compatibility.
14
+ *
15
+ * @typeParam TChains - Record mapping user-chosen chain names to PAPI descriptors.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * import { createChainClient } from "@parity/product-sdk-chain-client";
20
+ * import { paseo_asset_hub } from "./descriptors/paseo-asset-hub";
21
+ * import { bulletin } from "./descriptors/bulletin";
22
+ *
23
+ * const client = await createChainClient({
24
+ * chains: { assetHub: paseo_asset_hub, bulletin },
25
+ * rpcs: {
26
+ * assetHub: ["wss://sys.ibp.network/asset-hub-paseo"],
27
+ * bulletin: ["wss://paseo-bulletin-rpc.polkadot.io"],
28
+ * },
29
+ * });
30
+ * ```
31
+ */
32
+ export interface ChainClientConfig<
33
+ TChains extends Record<string, ChainDefinition> = Record<string, ChainDefinition>,
34
+ > {
35
+ /** Named chain descriptors (PAPI `ChainDefinition` objects). */
36
+ chains: TChains;
37
+ /** RPC endpoints per chain name (currently unused - connections route through host). */
38
+ rpcs: { [K in keyof TChains]: readonly string[] };
39
+ }
40
+
41
+ /**
42
+ * A connected chain client returned by {@link createChainClient}.
43
+ *
44
+ * Each key from your config maps to a fully-typed PAPI {@link TypedApi}.
45
+ * Access raw `PolkadotClient` instances via `.raw` for advanced use cases
46
+ * like creating an `InkSdk` for contract interactions.
47
+ *
48
+ * @typeParam TChains - The chain descriptor record used to create this client.
49
+ *
50
+ * @example
51
+ * ```ts
52
+ * // Typed API access — fully typed from your descriptors
53
+ * const account = await client.assetHub.query.System.Account.getValue(addr);
54
+ *
55
+ * // Raw client for advanced use (e.g., InkSdk for contracts)
56
+ * import { createInkSdk } from "@polkadot-api/sdk-ink";
57
+ * const inkSdk = createInkSdk(client.raw.assetHub, { atBest: true });
58
+ * ```
59
+ */
60
+ export type ChainClient<TChains extends Record<string, ChainDefinition>> = {
61
+ [K in string & keyof TChains]: TypedApi<TChains[K]>;
62
+ } & {
63
+ /** Raw `PolkadotClient` instances, keyed by chain name. Use for advanced APIs like `createInkSdk`. */
64
+ raw: { [K in string & keyof TChains]: PolkadotClient };
65
+ /** Destroy all connections managed by this client. */
66
+ destroy: () => void;
67
+ };
68
+
69
+ /** Internal per-chain state stored in the HMR-safe cache. */
70
+ export interface ChainEntry {
71
+ client: PolkadotClient;
72
+ api: Map<ChainDefinition, unknown>;
73
+ }