@dedot/api 0.0.1-alpha.32

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.
Files changed (202) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +1 -0
  3. package/chaintypes/index.d.ts +1 -0
  4. package/chaintypes/index.js +1 -0
  5. package/chaintypes/substrate/consts.d.ts +2132 -0
  6. package/chaintypes/substrate/consts.js +2 -0
  7. package/chaintypes/substrate/errors.d.ts +3490 -0
  8. package/chaintypes/substrate/errors.js +2 -0
  9. package/chaintypes/substrate/events.d.ts +5187 -0
  10. package/chaintypes/substrate/events.js +2 -0
  11. package/chaintypes/substrate/index.d.ts +22 -0
  12. package/chaintypes/substrate/index.js +2 -0
  13. package/chaintypes/substrate/json-rpc.d.ts +3 -0
  14. package/chaintypes/substrate/json-rpc.js +2 -0
  15. package/chaintypes/substrate/query.d.ts +3829 -0
  16. package/chaintypes/substrate/query.js +2 -0
  17. package/chaintypes/substrate/runtime.d.ts +850 -0
  18. package/chaintypes/substrate/runtime.js +2 -0
  19. package/chaintypes/substrate/tx.d.ts +14269 -0
  20. package/chaintypes/substrate/tx.js +2 -0
  21. package/chaintypes/substrate/types.d.ts +30457 -0
  22. package/chaintypes/substrate/types.js +2 -0
  23. package/cjs/chaintypes/index.js +17 -0
  24. package/cjs/chaintypes/substrate/consts.js +3 -0
  25. package/cjs/chaintypes/substrate/errors.js +3 -0
  26. package/cjs/chaintypes/substrate/events.js +3 -0
  27. package/cjs/chaintypes/substrate/index.js +18 -0
  28. package/cjs/chaintypes/substrate/json-rpc.js +3 -0
  29. package/cjs/chaintypes/substrate/query.js +3 -0
  30. package/cjs/chaintypes/substrate/runtime.js +3 -0
  31. package/cjs/chaintypes/substrate/tx.js +3 -0
  32. package/cjs/chaintypes/substrate/types.js +3 -0
  33. package/cjs/client/BaseSubstrateClient.js +257 -0
  34. package/cjs/client/Dedot.js +250 -0
  35. package/cjs/client/DedotClient.js +186 -0
  36. package/cjs/client/index.js +18 -0
  37. package/cjs/executor/ConstantExecutor.js +20 -0
  38. package/cjs/executor/ErrorExecutor.js +46 -0
  39. package/cjs/executor/EventExecutor.js +52 -0
  40. package/cjs/executor/Executor.js +42 -0
  41. package/cjs/executor/RuntimeApiExecutor.js +121 -0
  42. package/cjs/executor/StorageQueryExecutor.js +115 -0
  43. package/cjs/executor/TxExecutor.js +55 -0
  44. package/cjs/executor/index.js +26 -0
  45. package/cjs/executor/v2/RuntimeApiExecutorV2.js +21 -0
  46. package/cjs/executor/v2/StorageQueryExecutorV2.js +65 -0
  47. package/cjs/executor/v2/TxExecutorV2.js +20 -0
  48. package/cjs/extrinsic/extensions/ExtraSignedExtension.js +87 -0
  49. package/cjs/extrinsic/extensions/SignedExtension.js +41 -0
  50. package/cjs/extrinsic/extensions/index.js +19 -0
  51. package/cjs/extrinsic/extensions/known/ChargeAssetTxPayment.js +43 -0
  52. package/cjs/extrinsic/extensions/known/ChargeTransactionPayment.js +16 -0
  53. package/cjs/extrinsic/extensions/known/CheckGenesis.js +19 -0
  54. package/cjs/extrinsic/extensions/known/CheckMortality.js +90 -0
  55. package/cjs/extrinsic/extensions/known/CheckNonZeroSender.js +10 -0
  56. package/cjs/extrinsic/extensions/known/CheckNonce.js +33 -0
  57. package/cjs/extrinsic/extensions/known/CheckSpecVersion.js +19 -0
  58. package/cjs/extrinsic/extensions/known/CheckTxVersion.js +19 -0
  59. package/cjs/extrinsic/extensions/known/CheckWeight.js +10 -0
  60. package/cjs/extrinsic/extensions/known/PrevalidateAttests.js +11 -0
  61. package/cjs/extrinsic/extensions/known/index.js +25 -0
  62. package/cjs/extrinsic/index.js +18 -0
  63. package/cjs/extrinsic/submittable/BaseSubmittableExtrinsic.js +70 -0
  64. package/cjs/extrinsic/submittable/SubmittableExtrinsic.js +47 -0
  65. package/cjs/extrinsic/submittable/SubmittableExtrinsicV2.js +204 -0
  66. package/cjs/extrinsic/submittable/SubmittableResult.js +33 -0
  67. package/cjs/extrinsic/submittable/errors.js +16 -0
  68. package/cjs/extrinsic/submittable/fakeSigner.js +13 -0
  69. package/cjs/extrinsic/submittable/index.js +20 -0
  70. package/cjs/extrinsic/submittable/utils.js +20 -0
  71. package/cjs/index.js +21 -0
  72. package/cjs/json-rpc/JsonRpcClient.js +165 -0
  73. package/cjs/json-rpc/group/ChainHead/BlockUsage.js +24 -0
  74. package/cjs/json-rpc/group/ChainHead/ChainHead.js +603 -0
  75. package/cjs/json-rpc/group/ChainHead/error.js +59 -0
  76. package/cjs/json-rpc/group/ChainHead/index.js +18 -0
  77. package/cjs/json-rpc/group/ChainSpec.js +19 -0
  78. package/cjs/json-rpc/group/JsonRpcGroup.js +89 -0
  79. package/cjs/json-rpc/group/Transaction.js +25 -0
  80. package/cjs/json-rpc/group/TransactionWatch.js +17 -0
  81. package/cjs/json-rpc/group/index.js +21 -0
  82. package/cjs/json-rpc/index.js +20 -0
  83. package/cjs/json-rpc/scaledResponses.js +15 -0
  84. package/cjs/json-rpc/subscriptionsInfo.js +20 -0
  85. package/cjs/package.json +1 -0
  86. package/cjs/proxychain.js +27 -0
  87. package/cjs/storage/QueryableStorage.js +161 -0
  88. package/cjs/storage/index.js +17 -0
  89. package/cjs/types.js +2 -0
  90. package/client/BaseSubstrateClient.d.ts +65 -0
  91. package/client/BaseSubstrateClient.js +252 -0
  92. package/client/Dedot.d.ts +131 -0
  93. package/client/Dedot.js +246 -0
  94. package/client/DedotClient.d.ts +61 -0
  95. package/client/DedotClient.js +182 -0
  96. package/client/index.d.ts +2 -0
  97. package/client/index.js +2 -0
  98. package/executor/ConstantExecutor.d.ts +10 -0
  99. package/executor/ConstantExecutor.js +16 -0
  100. package/executor/ErrorExecutor.d.ts +10 -0
  101. package/executor/ErrorExecutor.js +42 -0
  102. package/executor/EventExecutor.d.ts +10 -0
  103. package/executor/EventExecutor.js +48 -0
  104. package/executor/Executor.d.ts +149 -0
  105. package/executor/Executor.js +38 -0
  106. package/executor/RuntimeApiExecutor.d.ts +27 -0
  107. package/executor/RuntimeApiExecutor.js +117 -0
  108. package/executor/StorageQueryExecutor.d.ts +14 -0
  109. package/executor/StorageQueryExecutor.js +111 -0
  110. package/executor/TxExecutor.d.ts +10 -0
  111. package/executor/TxExecutor.js +51 -0
  112. package/executor/index.d.ts +10 -0
  113. package/executor/index.js +10 -0
  114. package/executor/v2/RuntimeApiExecutorV2.d.ts +14 -0
  115. package/executor/v2/RuntimeApiExecutorV2.js +17 -0
  116. package/executor/v2/StorageQueryExecutorV2.d.ts +17 -0
  117. package/executor/v2/StorageQueryExecutorV2.js +61 -0
  118. package/executor/v2/TxExecutorV2.d.ts +11 -0
  119. package/executor/v2/TxExecutorV2.js +16 -0
  120. package/extrinsic/extensions/ExtraSignedExtension.d.ts +14 -0
  121. package/extrinsic/extensions/ExtraSignedExtension.js +60 -0
  122. package/extrinsic/extensions/SignedExtension.d.ts +40 -0
  123. package/extrinsic/extensions/SignedExtension.js +37 -0
  124. package/extrinsic/extensions/index.d.ts +3 -0
  125. package/extrinsic/extensions/index.js +3 -0
  126. package/extrinsic/extensions/known/ChargeAssetTxPayment.d.ts +14 -0
  127. package/extrinsic/extensions/known/ChargeAssetTxPayment.js +39 -0
  128. package/extrinsic/extensions/known/ChargeTransactionPayment.d.ts +6 -0
  129. package/extrinsic/extensions/known/ChargeTransactionPayment.js +12 -0
  130. package/extrinsic/extensions/known/CheckGenesis.d.ts +10 -0
  131. package/extrinsic/extensions/known/CheckGenesis.js +15 -0
  132. package/extrinsic/extensions/known/CheckMortality.d.ts +15 -0
  133. package/extrinsic/extensions/known/CheckMortality.js +86 -0
  134. package/extrinsic/extensions/known/CheckNonZeroSender.d.ts +6 -0
  135. package/extrinsic/extensions/known/CheckNonZeroSender.js +6 -0
  136. package/extrinsic/extensions/known/CheckNonce.d.ts +10 -0
  137. package/extrinsic/extensions/known/CheckNonce.js +29 -0
  138. package/extrinsic/extensions/known/CheckSpecVersion.d.ts +9 -0
  139. package/extrinsic/extensions/known/CheckSpecVersion.js +15 -0
  140. package/extrinsic/extensions/known/CheckTxVersion.d.ts +9 -0
  141. package/extrinsic/extensions/known/CheckTxVersion.js +15 -0
  142. package/extrinsic/extensions/known/CheckWeight.d.ts +6 -0
  143. package/extrinsic/extensions/known/CheckWeight.js +6 -0
  144. package/extrinsic/extensions/known/PrevalidateAttests.d.ts +7 -0
  145. package/extrinsic/extensions/known/PrevalidateAttests.js +7 -0
  146. package/extrinsic/extensions/known/index.d.ts +3 -0
  147. package/extrinsic/extensions/known/index.js +22 -0
  148. package/extrinsic/index.d.ts +2 -0
  149. package/extrinsic/index.js +2 -0
  150. package/extrinsic/submittable/BaseSubmittableExtrinsic.d.ts +17 -0
  151. package/extrinsic/submittable/BaseSubmittableExtrinsic.js +66 -0
  152. package/extrinsic/submittable/SubmittableExtrinsic.d.ts +12 -0
  153. package/extrinsic/submittable/SubmittableExtrinsic.js +43 -0
  154. package/extrinsic/submittable/SubmittableExtrinsicV2.d.ts +15 -0
  155. package/extrinsic/submittable/SubmittableExtrinsicV2.js +200 -0
  156. package/extrinsic/submittable/SubmittableResult.d.ts +19 -0
  157. package/extrinsic/submittable/SubmittableResult.js +29 -0
  158. package/extrinsic/submittable/errors.d.ts +11 -0
  159. package/extrinsic/submittable/errors.js +12 -0
  160. package/extrinsic/submittable/fakeSigner.d.ts +2 -0
  161. package/extrinsic/submittable/fakeSigner.js +10 -0
  162. package/extrinsic/submittable/index.d.ts +4 -0
  163. package/extrinsic/submittable/index.js +4 -0
  164. package/extrinsic/submittable/utils.d.ts +10 -0
  165. package/extrinsic/submittable/utils.js +15 -0
  166. package/index.d.ts +6 -0
  167. package/index.js +5 -0
  168. package/json-rpc/JsonRpcClient.d.ts +50 -0
  169. package/json-rpc/JsonRpcClient.js +160 -0
  170. package/json-rpc/group/ChainHead/BlockUsage.d.ts +11 -0
  171. package/json-rpc/group/ChainHead/BlockUsage.js +20 -0
  172. package/json-rpc/group/ChainHead/ChainHead.d.ts +70 -0
  173. package/json-rpc/group/ChainHead/ChainHead.js +599 -0
  174. package/json-rpc/group/ChainHead/error.d.ts +47 -0
  175. package/json-rpc/group/ChainHead/error.js +48 -0
  176. package/json-rpc/group/ChainHead/index.d.ts +2 -0
  177. package/json-rpc/group/ChainHead/index.js +2 -0
  178. package/json-rpc/group/ChainSpec.d.ts +10 -0
  179. package/json-rpc/group/ChainSpec.js +15 -0
  180. package/json-rpc/group/JsonRpcGroup.d.ts +74 -0
  181. package/json-rpc/group/JsonRpcGroup.js +85 -0
  182. package/json-rpc/group/Transaction.d.ts +11 -0
  183. package/json-rpc/group/Transaction.js +21 -0
  184. package/json-rpc/group/TransactionWatch.d.ts +10 -0
  185. package/json-rpc/group/TransactionWatch.js +13 -0
  186. package/json-rpc/group/index.d.ts +5 -0
  187. package/json-rpc/group/index.js +5 -0
  188. package/json-rpc/index.d.ts +4 -0
  189. package/json-rpc/index.js +4 -0
  190. package/json-rpc/scaledResponses.d.ts +2 -0
  191. package/json-rpc/scaledResponses.js +12 -0
  192. package/json-rpc/subscriptionsInfo.d.ts +2 -0
  193. package/json-rpc/subscriptionsInfo.js +17 -0
  194. package/package.json +54 -0
  195. package/proxychain.d.ts +14 -0
  196. package/proxychain.js +23 -0
  197. package/storage/QueryableStorage.d.ts +35 -0
  198. package/storage/QueryableStorage.js +157 -0
  199. package/storage/index.d.ts +1 -0
  200. package/storage/index.js +1 -0
  201. package/types.d.ts +120 -0
  202. package/types.js +1 -0
@@ -0,0 +1,29 @@
1
+ export class SubmittableResult {
2
+ status;
3
+ events;
4
+ dispatchInfo;
5
+ dispatchError;
6
+ txHash;
7
+ txIndex;
8
+ constructor({ events, status, txHash, txIndex }) {
9
+ this.events = events || [];
10
+ this.status = status;
11
+ this.txHash = txHash;
12
+ this.txIndex = txIndex;
13
+ [this.dispatchInfo, this.dispatchError] = this._extractDispatchInfo();
14
+ }
15
+ _extractDispatchInfo() {
16
+ for (const { event } of this.events) {
17
+ const { pallet, palletEvent } = event;
18
+ if (pallet === 'System' && palletEvent.name === 'ExtrinsicFailed') {
19
+ const { dispatchInfo, dispatchError } = palletEvent.data;
20
+ return [dispatchInfo, dispatchError];
21
+ }
22
+ else if (pallet === 'System' && palletEvent.name === 'ExtrinsicSuccess') {
23
+ const { dispatchInfo } = palletEvent.data;
24
+ return [dispatchInfo, undefined];
25
+ }
26
+ }
27
+ return [undefined, undefined];
28
+ }
29
+ }
@@ -0,0 +1,11 @@
1
+ import type { Result } from '@dedot/codecs';
2
+ import { DedotError } from '@dedot/utils';
3
+ import type { SpRuntimeTransactionValidityTransactionValidityError, SpRuntimeTransactionValidityValidTransaction } from '../../chaintypes/index.js';
4
+ /**
5
+ * This throws out if the extrinsic is invalid
6
+ * after verifying via `call.taggedTransactionQueue.validateTransaction`
7
+ */
8
+ export declare class InvalidTxError extends DedotError {
9
+ data: Result<SpRuntimeTransactionValidityValidTransaction, SpRuntimeTransactionValidityTransactionValidityError>;
10
+ constructor(message: string, data: Result<SpRuntimeTransactionValidityValidTransaction, SpRuntimeTransactionValidityTransactionValidityError>);
11
+ }
@@ -0,0 +1,12 @@
1
+ import { DedotError } from '@dedot/utils';
2
+ /**
3
+ * This throws out if the extrinsic is invalid
4
+ * after verifying via `call.taggedTransactionQueue.validateTransaction`
5
+ */
6
+ export class InvalidTxError extends DedotError {
7
+ data;
8
+ constructor(message, data) {
9
+ super(message);
10
+ this.data = data;
11
+ }
12
+ }
@@ -0,0 +1,2 @@
1
+ import type { Signer } from '@polkadot/types/types';
2
+ export declare const fakeSigner: Signer;
@@ -0,0 +1,10 @@
1
+ import { u8aToHex } from '@dedot/utils';
2
+ const FAKE_SIGNATURE = new Uint8Array(64 * 8).fill(0);
3
+ export const fakeSigner = {
4
+ signPayload: async () => {
5
+ return {
6
+ id: Date.now(),
7
+ signature: u8aToHex(FAKE_SIGNATURE),
8
+ };
9
+ },
10
+ };
@@ -0,0 +1,4 @@
1
+ export * from './errors.js';
2
+ export * from './SubmittableResult.js';
3
+ export * from './SubmittableExtrinsic.js';
4
+ export * from './SubmittableExtrinsicV2.js';
@@ -0,0 +1,4 @@
1
+ export * from './errors.js';
2
+ export * from './SubmittableResult.js';
3
+ export * from './SubmittableExtrinsic.js';
4
+ export * from './SubmittableExtrinsicV2.js';
@@ -0,0 +1,10 @@
1
+ import { IKeyringPair } from '@polkadot/types/types';
2
+ import type { AddressOrPair } from '@dedot/types';
3
+ import { HexString } from '@dedot/utils';
4
+ export declare function isKeyringPair(account: AddressOrPair): account is IKeyringPair;
5
+ /**
6
+ * Sign a raw message
7
+ * @param signerPair
8
+ * @param raw
9
+ */
10
+ export declare function signRaw(signerPair: IKeyringPair, raw: HexString): Uint8Array;
@@ -0,0 +1,15 @@
1
+ import { blake2AsU8a, hexToU8a, isFunction } from '@dedot/utils';
2
+ export function isKeyringPair(account) {
3
+ return isFunction(account.sign);
4
+ }
5
+ /**
6
+ * Sign a raw message
7
+ * @param signerPair
8
+ * @param raw
9
+ */
10
+ export function signRaw(signerPair, raw) {
11
+ const u8a = hexToU8a(raw);
12
+ // Ref: https://github.com/paritytech/polkadot-sdk/blob/943697fa693a4da6ef481ef93df522accb7d0583/substrate/primitives/runtime/src/generic/unchecked_extrinsic.rs#L234-L238
13
+ const toSignRaw = u8a.length > 256 ? blake2AsU8a(u8a, 256) : u8a;
14
+ return signerPair.sign(toSignRaw, { withType: true });
15
+ }
package/index.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ export type * from './types.js';
2
+ export * from './storage/index.js';
3
+ export * from './extrinsic/index.js';
4
+ export * from './executor/index.js';
5
+ export * from './json-rpc/index.js';
6
+ export * from './client/index.js';
package/index.js ADDED
@@ -0,0 +1,5 @@
1
+ export * from './storage/index.js';
2
+ export * from './extrinsic/index.js';
3
+ export * from './executor/index.js';
4
+ export * from './json-rpc/index.js';
5
+ export * from './client/index.js';
@@ -0,0 +1,50 @@
1
+ import type { ConnectionStatus, JsonRpcProvider, ProviderEvent } from '@dedot/providers';
2
+ import type { GenericSubstrateApi, RpcVersion } from '@dedot/types';
3
+ import { EventEmitter } from '@dedot/utils';
4
+ import type { SubstrateApi } from '../chaintypes/index.js';
5
+ import type { IJsonRpcClient, JsonRpcClientOptions } from '../types.js';
6
+ export declare const isJsonRpcProvider: (provider: any) => provider is JsonRpcProvider;
7
+ export declare class JsonRpcClient<ChainApi extends GenericSubstrateApi = SubstrateApi[RpcVersion], Events extends string = ProviderEvent> extends EventEmitter<Events> implements IJsonRpcClient<ChainApi, Events> {
8
+ #private;
9
+ constructor(options: JsonRpcClientOptions | JsonRpcProvider);
10
+ /**
11
+ * Factory method to create a new JsonRpcClient instance
12
+ *
13
+ * @param options
14
+ */
15
+ static create<ChainApi extends GenericSubstrateApi = SubstrateApi[RpcVersion]>(options: JsonRpcClientOptions | JsonRpcProvider): Promise<JsonRpcClient<ChainApi>>;
16
+ /**
17
+ * Alias for __JsonRpcClient.create__
18
+ *
19
+ * @param options
20
+ */
21
+ static new<ChainApi extends GenericSubstrateApi = SubstrateApi[RpcVersion]>(options: JsonRpcClientOptions | JsonRpcProvider): Promise<JsonRpcClient<ChainApi>>;
22
+ get options(): JsonRpcClientOptions;
23
+ /**
24
+ * @description Check connection status of the api instance
25
+ */
26
+ get status(): ConnectionStatus;
27
+ /**
28
+ * @description Get the JSON-RPC provider instance
29
+ */
30
+ get provider(): JsonRpcProvider;
31
+ connect(): Promise<this>;
32
+ disconnect(): Promise<void>;
33
+ /**
34
+ * @description Entry-point for executing JSON-RPCs to blockchain node.
35
+ *
36
+ * ```typescript
37
+ * const client = new JsonRpcClient('wss://rpc.polkadot.io');
38
+ * await client.connect();
39
+ *
40
+ * // Subscribe to new heads
41
+ * client.rpc.chain_subscribeNewHeads((header) => {
42
+ * console.log(header);
43
+ * });
44
+ *
45
+ * // Execute arbitrary rpc method: `module_rpc_name`
46
+ * const result = await client.rpc.module_rpc_name();
47
+ * ```
48
+ */
49
+ get rpc(): ChainApi['rpc'];
50
+ }
@@ -0,0 +1,160 @@
1
+ import { assert, EventEmitter, isFunction, isString } from '@dedot/utils';
2
+ import { scaledResponses } from './scaledResponses.js';
3
+ import { subscriptionsInfo } from './subscriptionsInfo.js';
4
+ export const isJsonRpcProvider = (provider) => {
5
+ return (provider &&
6
+ isString(provider.status) &&
7
+ isFunction(provider.send) &&
8
+ isFunction(provider.subscribe) &&
9
+ isFunction(provider.connect) &&
10
+ isFunction(provider.disconnect));
11
+ };
12
+ export class JsonRpcClient extends EventEmitter {
13
+ #options;
14
+ #provider;
15
+ constructor(options) {
16
+ super();
17
+ if (isJsonRpcProvider(options)) {
18
+ this.#options = { provider: options };
19
+ this.#provider = options;
20
+ }
21
+ else {
22
+ this.#options = options;
23
+ this.#provider = options.provider;
24
+ }
25
+ assert(this.#provider, 'A JsonRpcProvider is required');
26
+ }
27
+ /**
28
+ * Factory method to create a new JsonRpcClient instance
29
+ *
30
+ * @param options
31
+ */
32
+ static async create(options) {
33
+ return new JsonRpcClient(options).connect();
34
+ }
35
+ /**
36
+ * Alias for __JsonRpcClient.create__
37
+ *
38
+ * @param options
39
+ */
40
+ static async new(options) {
41
+ return JsonRpcClient.create(options);
42
+ }
43
+ get options() {
44
+ return this.#options;
45
+ }
46
+ /**
47
+ * @description Check connection status of the api instance
48
+ */
49
+ get status() {
50
+ return this.provider.status;
51
+ }
52
+ /**
53
+ * @description Get the JSON-RPC provider instance
54
+ */
55
+ get provider() {
56
+ return this.#provider;
57
+ }
58
+ connect() {
59
+ return this.#doConnect();
60
+ }
61
+ async #doConnect() {
62
+ this.provider.on('connected', this.#onConnected);
63
+ this.provider.on('disconnected', this.#onDisconnected);
64
+ this.provider.on('reconnecting', this.#onReconnecting);
65
+ this.provider.on('error', this.#onError);
66
+ return new Promise((resolve, reject) => {
67
+ // @ts-ignore
68
+ this.once('connected', () => {
69
+ resolve(this);
70
+ });
71
+ if (this.status === 'connected') {
72
+ this.#onConnected().catch(reject);
73
+ }
74
+ else {
75
+ this.provider.connect().catch(reject);
76
+ }
77
+ });
78
+ }
79
+ #onConnected = async () => {
80
+ // @ts-ignore
81
+ this.emit('connected');
82
+ };
83
+ #onDisconnected = async () => {
84
+ // @ts-ignore
85
+ this.emit('disconnected');
86
+ };
87
+ #onReconnecting = async () => {
88
+ // @ts-ignore
89
+ this.emit('reconnecting');
90
+ };
91
+ #onError = async (e) => {
92
+ // @ts-ignore
93
+ this.emit('error', e);
94
+ };
95
+ async disconnect() {
96
+ await this.#provider.disconnect();
97
+ this.clearEvents();
98
+ }
99
+ /**
100
+ * @description Entry-point for executing JSON-RPCs to blockchain node.
101
+ *
102
+ * ```typescript
103
+ * const client = new JsonRpcClient('wss://rpc.polkadot.io');
104
+ * await client.connect();
105
+ *
106
+ * // Subscribe to new heads
107
+ * client.rpc.chain_subscribeNewHeads((header) => {
108
+ * console.log(header);
109
+ * });
110
+ *
111
+ * // Execute arbitrary rpc method: `module_rpc_name`
112
+ * const result = await client.rpc.module_rpc_name();
113
+ * ```
114
+ */
115
+ get rpc() {
116
+ return new Proxy(this, {
117
+ get(target, property, receiver) {
118
+ const rpcMethod = property.toString();
119
+ return target.#doExecute(rpcMethod);
120
+ },
121
+ });
122
+ }
123
+ #doExecute(rpcName) {
124
+ const subscriptionInfo = this.options.subscriptions?.[rpcName] || subscriptionsInfo[rpcName];
125
+ const isSubscription = !!subscriptionInfo;
126
+ const fnRpc = async (...args) => {
127
+ const result = await this.provider.send(rpcName, args);
128
+ return this.#tryDecode(rpcName, result);
129
+ };
130
+ const fnSubRpc = async (...args) => {
131
+ const inArgs = args.slice();
132
+ const callback = inArgs.pop();
133
+ assert(isFunction(callback), 'A callback is required for subscription');
134
+ const onNewMessage = (error, result, subscription) => {
135
+ if (error) {
136
+ console.error(error);
137
+ return;
138
+ }
139
+ callback(this.#tryDecode(rpcName, result), subscription);
140
+ };
141
+ const [subname, unsubscribe] = subscriptionInfo;
142
+ const subscription = await this.provider.subscribe({ subname, subscribe: rpcName, params: inArgs, unsubscribe }, onNewMessage);
143
+ return async () => {
144
+ await subscription.unsubscribe();
145
+ };
146
+ };
147
+ return isSubscription ? fnSubRpc : fnRpc;
148
+ }
149
+ #tryDecode(rpcName, raw) {
150
+ if (raw === null) {
151
+ // We use `undefined` to represent Option::None in the client
152
+ return undefined;
153
+ }
154
+ const $maybeCodec = this.options.scaledResponses?.[rpcName] || scaledResponses[rpcName];
155
+ if ($maybeCodec) {
156
+ return $maybeCodec.tryDecode(raw);
157
+ }
158
+ return raw;
159
+ }
160
+ }
@@ -0,0 +1,11 @@
1
+ import { BlockHash } from '@dedot/codecs';
2
+ /**
3
+ * A helper to track block hash usage
4
+ */
5
+ export declare class BlockUsage {
6
+ #private;
7
+ use(blockHash: BlockHash): void;
8
+ release(blockHash: BlockHash): void;
9
+ usage(blockHash: BlockHash): number;
10
+ clear(): void;
11
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * A helper to track block hash usage
3
+ */
4
+ export class BlockUsage {
5
+ #usages = {};
6
+ use(blockHash) {
7
+ this.#usages[blockHash] = (this.#usages[blockHash] || 0) + 1;
8
+ }
9
+ release(blockHash) {
10
+ if (!this.#usages[blockHash])
11
+ return;
12
+ this.#usages[blockHash] -= 1;
13
+ }
14
+ usage(blockHash) {
15
+ return this.#usages[blockHash] || 0;
16
+ }
17
+ clear() {
18
+ this.#usages = {};
19
+ }
20
+ }
@@ -0,0 +1,70 @@
1
+ import { BlockHash, Option } from '@dedot/codecs';
2
+ import type { ChainHeadRuntimeVersion, OperationId, StorageQuery, StorageResult } from '@dedot/types/json-rpc';
3
+ import { Deferred, HexString } from '@dedot/utils';
4
+ import type { IJsonRpcClient } from '../../../types.js';
5
+ import { JsonRpcGroup, type JsonRpcGroupOptions } from '../JsonRpcGroup.js';
6
+ export type OperationHandler<T = any> = {
7
+ operationId: OperationId;
8
+ defer: Deferred<T>;
9
+ storageResults?: Array<StorageResult>;
10
+ hash: BlockHash;
11
+ };
12
+ export type PinnedBlock = {
13
+ hash: BlockHash;
14
+ number: number;
15
+ parent: BlockHash;
16
+ runtime?: ChainHeadRuntimeVersion;
17
+ };
18
+ export type ChainHeadEvent = 'newBlock' | 'bestBlock' | 'finalizedBlock' | 'bestChainChanged';
19
+ export declare const MIN_FINALIZED_QUEUE_SIZE = 10;
20
+ export declare class ChainHead extends JsonRpcGroup<ChainHeadEvent> {
21
+ #private;
22
+ constructor(client: IJsonRpcClient, options?: Partial<JsonRpcGroupOptions>);
23
+ runtimeVersion(): Promise<ChainHeadRuntimeVersion>;
24
+ bestRuntimeVersion(): Promise<ChainHeadRuntimeVersion>;
25
+ finalizedHash(): Promise<BlockHash>;
26
+ bestHash(): Promise<BlockHash>;
27
+ bestBlock(): Promise<PinnedBlock>;
28
+ finalizedBlock(): Promise<PinnedBlock>;
29
+ findBlock(hash: BlockHash): PinnedBlock | undefined;
30
+ isPinned(hash: BlockHash): boolean;
31
+ /**
32
+ * chainHead_unfollow
33
+ */
34
+ unfollow(): Promise<void>;
35
+ /**
36
+ * chainHead_follow
37
+ */
38
+ follow(): Promise<void>;
39
+ /**
40
+ * chainHead_body
41
+ */
42
+ body(at?: BlockHash): Promise<Array<HexString>>;
43
+ /**
44
+ * chainHead_call
45
+ */
46
+ call(func: string, params?: HexString, at?: BlockHash): Promise<HexString>;
47
+ /**
48
+ * chainHead_header
49
+ */
50
+ header(at?: BlockHash): Promise<Option<HexString>>;
51
+ /**
52
+ * chainHead_storage
53
+ */
54
+ storage(items: Array<StorageQuery>, childTrie?: string | null, at?: BlockHash): Promise<Array<StorageResult>>;
55
+ /**
56
+ * chainHead_stopOperation
57
+ * @protected
58
+ */
59
+ protected stopOperation(operationId: OperationId): Promise<void>;
60
+ /**
61
+ * chainHead_continue
62
+ * @protected
63
+ */
64
+ protected continue(operationId: OperationId): Promise<void>;
65
+ /**
66
+ * chainHead_unpin
67
+ * @protected
68
+ */
69
+ protected unpin(hashes: BlockHash | BlockHash[]): Promise<void>;
70
+ }