dedot 0.0.1-next.f5bf4fc2.1 → 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.
Files changed (119) hide show
  1. package/README.md +211 -64
  2. package/bin/dedot +3 -0
  3. package/chaintypes/index.d.ts +1 -0
  4. package/chaintypes/index.js +1 -0
  5. package/cjs/{extrinsic → chaintypes}/index.js +1 -2
  6. package/cjs/{extrinsic/extensions → codecs}/index.js +1 -3
  7. package/cjs/{executor → contracts}/index.js +1 -8
  8. package/cjs/index.js +16 -7
  9. package/cjs/runtime-specs/index.js +17 -0
  10. package/cjs/{client → shape}/index.js +1 -1
  11. package/cjs/types/index.js +17 -0
  12. package/cjs/types/json-rpc/index.js +17 -0
  13. package/cjs/utils/index.js +17 -0
  14. package/codecs/index.d.ts +1 -0
  15. package/codecs/index.js +1 -0
  16. package/contracts/index.d.ts +1 -0
  17. package/contracts/index.js +1 -0
  18. package/index.d.ts +3 -6
  19. package/index.js +3 -5
  20. package/package.json +77 -33
  21. package/runtime-specs/index.d.ts +1 -0
  22. package/runtime-specs/index.js +1 -0
  23. package/shape/index.d.ts +1 -0
  24. package/shape/index.js +1 -0
  25. package/types/index.d.ts +1 -0
  26. package/types/index.js +1 -0
  27. package/types/json-rpc/index.d.ts +1 -0
  28. package/types/json-rpc/index.js +1 -0
  29. package/utils/index.d.ts +1 -0
  30. package/utils/index.js +1 -0
  31. package/cjs/client/Dedot.js +0 -436
  32. package/cjs/executor/ConstantExecutor.js +0 -22
  33. package/cjs/executor/ErrorExecutor.js +0 -47
  34. package/cjs/executor/EventExecutor.js +0 -53
  35. package/cjs/executor/Executor.js +0 -39
  36. package/cjs/executor/RpcExecutor.js +0 -110
  37. package/cjs/executor/RuntimeApiExecutor.js +0 -108
  38. package/cjs/executor/StorageQueryExecutor.js +0 -33
  39. package/cjs/executor/TxExecutor.js +0 -153
  40. package/cjs/extrinsic/SubmittableResult.js +0 -33
  41. package/cjs/extrinsic/extensions/ExtraSignedExtension.js +0 -88
  42. package/cjs/extrinsic/extensions/SignedExtension.js +0 -41
  43. package/cjs/extrinsic/extensions/known/ChargeAssetTxPayment.js +0 -44
  44. package/cjs/extrinsic/extensions/known/ChargeTransactionPayment.js +0 -16
  45. package/cjs/extrinsic/extensions/known/CheckGenesis.js +0 -19
  46. package/cjs/extrinsic/extensions/known/CheckMortality.js +0 -71
  47. package/cjs/extrinsic/extensions/known/CheckNonZeroSender.js +0 -10
  48. package/cjs/extrinsic/extensions/known/CheckNonce.js +0 -34
  49. package/cjs/extrinsic/extensions/known/CheckSpecVersion.js +0 -19
  50. package/cjs/extrinsic/extensions/known/CheckTxVersion.js +0 -19
  51. package/cjs/extrinsic/extensions/known/CheckWeight.js +0 -10
  52. package/cjs/extrinsic/extensions/known/PrevalidateAttests.js +0 -11
  53. package/cjs/extrinsic/extensions/known/index.js +0 -25
  54. package/cjs/packageInfo.js +0 -5
  55. package/cjs/proxychain.js +0 -20
  56. package/cjs/storage/QueryableStorage.js +0 -99
  57. package/cjs/types.js +0 -2
  58. package/client/Dedot.d.ts +0 -181
  59. package/client/Dedot.js +0 -432
  60. package/client/index.d.ts +0 -1
  61. package/client/index.js +0 -1
  62. package/executor/ConstantExecutor.d.ts +0 -11
  63. package/executor/ConstantExecutor.js +0 -18
  64. package/executor/ErrorExecutor.d.ts +0 -11
  65. package/executor/ErrorExecutor.js +0 -43
  66. package/executor/EventExecutor.d.ts +0 -11
  67. package/executor/EventExecutor.js +0 -49
  68. package/executor/Executor.d.ts +0 -150
  69. package/executor/Executor.js +0 -35
  70. package/executor/RpcExecutor.d.ts +0 -14
  71. package/executor/RpcExecutor.js +0 -106
  72. package/executor/RuntimeApiExecutor.d.ts +0 -12
  73. package/executor/RuntimeApiExecutor.js +0 -104
  74. package/executor/StorageQueryExecutor.d.ts +0 -10
  75. package/executor/StorageQueryExecutor.js +0 -29
  76. package/executor/TxExecutor.d.ts +0 -47
  77. package/executor/TxExecutor.js +0 -147
  78. package/executor/index.d.ts +0 -8
  79. package/executor/index.js +0 -8
  80. package/extrinsic/SubmittableResult.d.ts +0 -19
  81. package/extrinsic/SubmittableResult.js +0 -29
  82. package/extrinsic/extensions/ExtraSignedExtension.d.ts +0 -14
  83. package/extrinsic/extensions/ExtraSignedExtension.js +0 -61
  84. package/extrinsic/extensions/SignedExtension.d.ts +0 -40
  85. package/extrinsic/extensions/SignedExtension.js +0 -37
  86. package/extrinsic/extensions/index.d.ts +0 -3
  87. package/extrinsic/extensions/index.js +0 -3
  88. package/extrinsic/extensions/known/ChargeAssetTxPayment.d.ts +0 -14
  89. package/extrinsic/extensions/known/ChargeAssetTxPayment.js +0 -40
  90. package/extrinsic/extensions/known/ChargeTransactionPayment.d.ts +0 -6
  91. package/extrinsic/extensions/known/ChargeTransactionPayment.js +0 -12
  92. package/extrinsic/extensions/known/CheckGenesis.d.ts +0 -10
  93. package/extrinsic/extensions/known/CheckGenesis.js +0 -15
  94. package/extrinsic/extensions/known/CheckMortality.d.ts +0 -15
  95. package/extrinsic/extensions/known/CheckMortality.js +0 -67
  96. package/extrinsic/extensions/known/CheckNonZeroSender.d.ts +0 -6
  97. package/extrinsic/extensions/known/CheckNonZeroSender.js +0 -6
  98. package/extrinsic/extensions/known/CheckNonce.d.ts +0 -10
  99. package/extrinsic/extensions/known/CheckNonce.js +0 -30
  100. package/extrinsic/extensions/known/CheckSpecVersion.d.ts +0 -9
  101. package/extrinsic/extensions/known/CheckSpecVersion.js +0 -15
  102. package/extrinsic/extensions/known/CheckTxVersion.d.ts +0 -9
  103. package/extrinsic/extensions/known/CheckTxVersion.js +0 -15
  104. package/extrinsic/extensions/known/CheckWeight.d.ts +0 -6
  105. package/extrinsic/extensions/known/CheckWeight.js +0 -6
  106. package/extrinsic/extensions/known/PrevalidateAttests.d.ts +0 -7
  107. package/extrinsic/extensions/known/PrevalidateAttests.js +0 -7
  108. package/extrinsic/extensions/known/index.d.ts +0 -3
  109. package/extrinsic/extensions/known/index.js +0 -22
  110. package/extrinsic/index.d.ts +0 -2
  111. package/extrinsic/index.js +0 -2
  112. package/packageInfo.d.ts +0 -4
  113. package/packageInfo.js +0 -2
  114. package/proxychain.d.ts +0 -7
  115. package/proxychain.js +0 -16
  116. package/storage/QueryableStorage.d.ts +0 -16
  117. package/storage/QueryableStorage.js +0 -95
  118. package/types.d.ts +0 -54
  119. package/types.js +0 -1
package/index.d.ts CHANGED
@@ -1,6 +1,3 @@
1
- import { Dedot } from './client';
2
- export type * from './types';
3
- export * from './executor';
4
- export * from './extrinsic';
5
- export { Dedot };
6
- export { WsProvider } from '@polkadot/rpc-provider';
1
+ export * as $ from '@dedot/shape';
2
+ export * from '@dedot/api';
3
+ export * from '@dedot/providers';
package/index.js CHANGED
@@ -1,5 +1,3 @@
1
- import { Dedot } from './client';
2
- export * from './executor';
3
- export * from './extrinsic';
4
- export { Dedot };
5
- export { WsProvider } from '@polkadot/rpc-provider';
1
+ export * as $ from '@dedot/shape';
2
+ export * from '@dedot/api';
3
+ export * from '@dedot/providers';
package/package.json CHANGED
@@ -1,54 +1,98 @@
1
1
  {
2
2
  "name": "dedot",
3
- "version": "0.0.1-next.f5bf4fc2.1+f5bf4fc",
3
+ "version": "0.1.0",
4
4
  "description": "A delightful JavaScript/TypeScript client for Polkadot & Substrate",
5
5
  "author": "Thang X. Vu <thang@coongcrafts.io>",
6
6
  "homepage": "https://github.com/dedotdev/dedot",
7
7
  "repository": {
8
- "directory": "packages/api",
8
+ "directory": "packages/dedot",
9
9
  "type": "git",
10
10
  "url": "https://github.com/dedotdev/dedot.git"
11
11
  },
12
12
  "main": "./cjs/index.js",
13
- "type": "module",
13
+ "bin": {
14
+ "dedot": "./bin/dedot",
15
+ "djs": "./bin/dedot"
16
+ },
14
17
  "sideEffects": false,
18
+ "type": "module",
19
+ "scripts": {
20
+ "build": "tsc --project tsconfig.build.json && tsc --project tsconfig.build.cjs.json && cp -R ./bin ./dist",
21
+ "clean": "rm -rf ./dist && rm -rf ./tsconfig.tsbuildinfo ./tsconfig.build.tsbuildinfo"
22
+ },
15
23
  "dependencies": {
16
- "@dedot/chaintypes": "0.0.1-next.f5bf4fc2.1+f5bf4fc",
17
- "@dedot/codecs": "0.0.1-next.f5bf4fc2.1+f5bf4fc",
18
- "@dedot/shape": "0.0.1-next.f5bf4fc2.1+f5bf4fc",
19
- "@dedot/specs": "0.0.1-next.f5bf4fc2.1+f5bf4fc",
20
- "@dedot/storage": "0.0.1-next.f5bf4fc2.1+f5bf4fc",
21
- "@dedot/utils": "0.0.1-next.f5bf4fc2.1+f5bf4fc",
22
- "@polkadot/rpc-provider": "^10.11.2",
23
- "@polkadot/util": "^12.6.2"
24
+ "@dedot/api": "0.1.0",
25
+ "@dedot/cli": "0.1.0",
26
+ "@dedot/codecs": "0.1.0",
27
+ "@dedot/contracts": "0.1.0",
28
+ "@dedot/providers": "0.1.0",
29
+ "@dedot/runtime-specs": "0.1.0",
30
+ "@dedot/shape": "0.1.0",
31
+ "@dedot/types": "0.1.0",
32
+ "@dedot/utils": "0.1.0"
24
33
  },
25
- "scripts": {
26
- "build": "tsc --project tsconfig.build.json && tsc --project tsconfig.build.cjs.json",
27
- "clean": "rm -rf ./dist && rm -rf ./tsconfig.tsbuildinfo ./tsconfig.build.tsbuildinfo",
28
- "test": "vitest --watch=false"
34
+ "exports": {
35
+ ".": {
36
+ "types": "./index.d.ts",
37
+ "import": "./index.js",
38
+ "require": "./cjs/index.js",
39
+ "default": "./index.js"
40
+ },
41
+ "./chaintypes": {
42
+ "types": "./chaintypes/index.d.ts",
43
+ "import": "./chaintypes/index.js",
44
+ "require": "./cjs/chaintypes/index.js",
45
+ "default": "./chaintypes/index.js"
46
+ },
47
+ "./codecs": {
48
+ "types": "./codecs/index.d.ts",
49
+ "import": "./codecs/index.js",
50
+ "require": "./cjs/codecs/index.js",
51
+ "default": "./codecs/index.js"
52
+ },
53
+ "./types": {
54
+ "types": "./types/index.d.ts",
55
+ "import": "./types/index.js",
56
+ "require": "./cjs/types/index.js",
57
+ "default": "./types/index.js"
58
+ },
59
+ "./types/json-rpc": {
60
+ "types": "./types/json-rpc/index.d.ts",
61
+ "import": "./types/json-rpc/index.js",
62
+ "require": "./cjs/types/json-rpc/index.js",
63
+ "default": "./types/json-rpc/index.js"
64
+ },
65
+ "./runtime-specs": {
66
+ "types": "./runtime-specs/index.d.ts",
67
+ "import": "./runtime-specs/index.js",
68
+ "require": "./cjs/runtime-specs/index.js",
69
+ "default": "./runtime-specs/index.js"
70
+ },
71
+ "./utils": {
72
+ "types": "./utils/index.d.ts",
73
+ "import": "./utils/index.js",
74
+ "require": "./cjs/utils/index.js",
75
+ "default": "./utils/index.js"
76
+ },
77
+ "./shape": {
78
+ "types": "./shape/index.d.ts",
79
+ "import": "./shape/index.js",
80
+ "require": "./cjs/shape/index.js",
81
+ "default": "./shape/index.js"
82
+ },
83
+ "./contracts": {
84
+ "types": "./contracts/index.d.ts",
85
+ "import": "./contracts/index.js",
86
+ "require": "./cjs/contracts/index.js",
87
+ "default": "./contracts/index.js"
88
+ }
29
89
  },
30
90
  "publishConfig": {
31
91
  "access": "public",
32
92
  "directory": "dist"
33
93
  },
34
94
  "license": "Apache-2.0",
35
- "devDependencies": {
36
- "@polkadot/keyring": "^12.6.2",
37
- "@polkadot/types": "^10.11.2"
38
- },
39
- "gitHead": "f5bf4fc22dadbbac59819df38cd0c9dccddc8dfd",
95
+ "gitHead": "c8220a4ab4b171fe04adb49759318bde865d7c17",
40
96
  "module": "./index.js",
41
- "types": "./index.d.ts",
42
- "exports": {
43
- ".": {
44
- "import": {
45
- "types": "./index.d.ts",
46
- "default": "./index.js"
47
- },
48
- "require": {
49
- "types": "./index.d.ts",
50
- "default": "./cjs/index.js"
51
- }
52
- }
53
- }
97
+ "types": "./index.d.ts"
54
98
  }
@@ -0,0 +1 @@
1
+ export * from '@dedot/runtime-specs';
@@ -0,0 +1 @@
1
+ export * from '@dedot/runtime-specs';
@@ -0,0 +1 @@
1
+ export * from '@dedot/shape';
package/shape/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from '@dedot/shape';
@@ -0,0 +1 @@
1
+ export * from '@dedot/types';
package/types/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from '@dedot/types';
@@ -0,0 +1 @@
1
+ export * from '@dedot/types/json-rpc';
@@ -0,0 +1 @@
1
+ export * from '@dedot/types/json-rpc';
@@ -0,0 +1 @@
1
+ export * from '@dedot/utils';
package/utils/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from '@dedot/utils';
@@ -1,436 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Dedot = exports.SUPPORTED_METADATA_VERSIONS = exports.CATCH_ALL_METADATA_KEY = exports.KEEP_ALIVE_INTERVAL = void 0;
4
- const rpc_provider_1 = require("@polkadot/rpc-provider");
5
- const codecs_1 = require("@dedot/codecs");
6
- const executor_1 = require("../executor");
7
- const proxychain_1 = require("../proxychain");
8
- const util_1 = require("@polkadot/util");
9
- const storage_1 = require("@dedot/storage");
10
- const utils_1 = require("@dedot/utils");
11
- exports.KEEP_ALIVE_INTERVAL = 10_000; // in ms
12
- exports.CATCH_ALL_METADATA_KEY = `RAW_META/ALL`;
13
- exports.SUPPORTED_METADATA_VERSIONS = [15, 14];
14
- /**
15
- * @name Dedot
16
- * @description Promised-based API Client for Polkadot & Substrate
17
- *
18
- * ### Initialize API instance and interact with substrate-based network
19
- * ```typescript
20
- * import { Dedot } from 'dedot';
21
- * import { PolkadotApi } from '@dedot/chaintypes/polkadot';
22
- *
23
- * const run = async () => {
24
- * const api = await Dedot.new<PolkadotApi>('wss://rpc.polkadot.io');
25
- *
26
- * // Call rpc `state_getMetadata` to fetch raw scale-encoded metadata and decode it.
27
- * const metadata = await api.rpc.state.getMetadata();
28
- * console.log('Metadata:', metadata);
29
- *
30
- * // Query on-chain storage
31
- * const address = '14...';
32
- * const balance = await api.query.system.account(address);
33
- * console.log('Balance:', balance);
34
- *
35
- *
36
- * // Subscribe to on-chain storage changes
37
- * const unsub = await api.query.system.number((blockNumber) => {
38
- * console.log(`Current block number: ${blockNumber}`);
39
- * });
40
- *
41
- * // Get pallet constants
42
- * const ss58Prefix = api.consts.system.ss58Prefix;
43
- * console.log('Polkadot ss58Prefix:', ss58Prefix)
44
- *
45
- * // await unsub();
46
- * // await api.disconnect();
47
- * }
48
- *
49
- * run().catch(console.error);
50
- * ```
51
- */
52
- class Dedot {
53
- #provider;
54
- #options;
55
- #registry;
56
- #metadata;
57
- #metadataLatest;
58
- #genesisHash;
59
- #runtimeVersion;
60
- #chainProperties;
61
- #runtimeChain;
62
- #localCache;
63
- #runtimeSubscriptionUnsub;
64
- #healthTimer;
65
- /**
66
- * Use factory methods (`create`, `new`) to create `Dedot` instances.
67
- *
68
- * @param options
69
- * @protected
70
- */
71
- constructor(options) {
72
- this.#options = this.#normalizeOptions(options);
73
- this.#provider = this.#getProvider();
74
- }
75
- /**
76
- * Factory method to create a new Dedot instance
77
- *
78
- * @param options
79
- */
80
- static async create(options) {
81
- const api = new Dedot(options);
82
- if (api.provider instanceof rpc_provider_1.WsProvider) {
83
- await api.provider.isReady;
84
- }
85
- await api.init();
86
- return api;
87
- }
88
- /**
89
- * Alias for __Dedot.create__
90
- * @param options
91
- */
92
- static async new(options) {
93
- return Dedot.create(options);
94
- }
95
- /**
96
- * Initialize APIs before usage
97
- */
98
- async init() {
99
- await this.#initializeLocalCache();
100
- // Fetching node information
101
- let [genesisHash, runtimeVersion, chainName, chainProps, metadata] = await Promise.all([
102
- this.rpc.chain.getBlockHash(0),
103
- this.rpc.state.getRuntimeVersion(),
104
- this.rpc.system.chain(),
105
- this.rpc.system.properties(),
106
- (await this.#shouldLoadPreloadMetadata()) ? this.#fetchMetadata() : Promise.resolve(undefined),
107
- ]);
108
- this.#genesisHash = genesisHash;
109
- this.#runtimeVersion = runtimeVersion;
110
- this.#chainProperties = chainProps;
111
- this.#runtimeChain = chainName;
112
- await this.#setupMetadata(metadata);
113
- this.#subscribeUpdates();
114
- }
115
- async #initializeLocalCache() {
116
- if (!this.#options.cacheMetadata)
117
- return;
118
- // Initialize local cache
119
- if (this.#options.cacheStorage) {
120
- this.#localCache = this.#options.cacheStorage;
121
- return;
122
- }
123
- try {
124
- this.#localCache = new storage_1.LocalStorage();
125
- }
126
- catch {
127
- throw new Error('localStorage is not available for caching, please provide a cacheStorage option');
128
- }
129
- }
130
- async #setupMetadata(preloadMetadata) {
131
- let metadata = preloadMetadata;
132
- const metadataKey = this.currentMetadataKey;
133
- const optMetadataBundles = this.#options.metadata;
134
- let shouldUpdateCache = !!metadata;
135
- if (optMetadataBundles && !metadata) {
136
- const optRawMetadata = optMetadataBundles[exports.CATCH_ALL_METADATA_KEY] || optMetadataBundles[metadataKey];
137
- if (optRawMetadata) {
138
- metadata = codecs_1.$Metadata.tryDecode(optRawMetadata);
139
- }
140
- }
141
- try {
142
- if (this.#localCache && this.#options.cacheMetadata) {
143
- if (!metadata) {
144
- try {
145
- const cachedRawMetadata = await this.#localCache.get(metadataKey);
146
- if (cachedRawMetadata) {
147
- metadata = codecs_1.$Metadata.tryDecode(cachedRawMetadata);
148
- }
149
- }
150
- catch (e) {
151
- console.error('Cannot decode raw metadata, try fetching fresh metadata from chain.', e);
152
- }
153
- }
154
- }
155
- }
156
- finally {
157
- if (!metadata) {
158
- metadata = await this.#fetchMetadata();
159
- if (this.#options.cacheMetadata)
160
- shouldUpdateCache = true;
161
- }
162
- }
163
- if (shouldUpdateCache && this.#localCache) {
164
- await this.#localCache.set(metadataKey, (0, util_1.u8aToHex)(codecs_1.$Metadata.tryEncode(metadata)));
165
- }
166
- if (!metadata) {
167
- throw new Error('Cannot load metadata');
168
- }
169
- this.setMetadata(metadata);
170
- }
171
- async #fetchMetadata() {
172
- // It makes sense to call metadata.metadataVersions to fetch the list of supported metadata versions first
173
- // But for now, this approach could potentially help save/reduce one rpc call to the server in case the node support v15
174
- // Question: Why not having a `metadata.metadataLatest` to fetch the latest version?
175
- for (const version of exports.SUPPORTED_METADATA_VERSIONS) {
176
- try {
177
- const rawMetadata = await this.call.metadata.metadataAtVersion(version);
178
- if (!rawMetadata)
179
- continue;
180
- return codecs_1.$Metadata.tryDecode(rawMetadata);
181
- }
182
- catch { }
183
- }
184
- try {
185
- return codecs_1.$Metadata.tryDecode(await this.call.metadata.metadata());
186
- }
187
- catch {
188
- return await this.rpc.state.getMetadata();
189
- }
190
- }
191
- #subscribeRuntimeUpgrades() {
192
- // Disable runtime upgrades subscriptions if using a catch all metadata
193
- if (this.#runtimeSubscriptionUnsub || !this.hasSubscriptions || this.hasCatchAllMetadata) {
194
- return;
195
- }
196
- this.rpc.state
197
- .subscribeRuntimeVersion(async (runtimeVersion) => {
198
- if (runtimeVersion.specVersion !== this.#runtimeVersion?.specVersion) {
199
- this.#runtimeVersion = runtimeVersion;
200
- const newMetadata = await this.#fetchMetadata();
201
- await this.#setupMetadata(newMetadata);
202
- }
203
- })
204
- .then((unsub) => {
205
- this.#runtimeSubscriptionUnsub = unsub;
206
- });
207
- }
208
- #subscribeHealth() {
209
- this.#unsubscribeHealth();
210
- this.#healthTimer = setInterval(() => {
211
- this.rpc.system.health().catch(console.error);
212
- }, exports.KEEP_ALIVE_INTERVAL);
213
- }
214
- #unsubscribeHealth() {
215
- if (!this.#healthTimer) {
216
- return;
217
- }
218
- clearInterval(this.#healthTimer);
219
- this.#healthTimer = undefined;
220
- }
221
- async #unsubscribeRuntimeUpdates() {
222
- if (!this.#runtimeSubscriptionUnsub) {
223
- return;
224
- }
225
- await this.#runtimeSubscriptionUnsub();
226
- this.#runtimeSubscriptionUnsub = undefined;
227
- }
228
- #subscribeUpdates() {
229
- this.#subscribeRuntimeUpgrades();
230
- this.#subscribeHealth();
231
- }
232
- async #unsubscribeUpdates() {
233
- await this.#unsubscribeRuntimeUpdates();
234
- this.#unsubscribeHealth();
235
- }
236
- async #shouldLoadPreloadMetadata() {
237
- if (this.#options.metadata && Object.keys(this.#options.metadata).length) {
238
- return false;
239
- }
240
- if (!this.#options.cacheMetadata || !this.#localCache) {
241
- return true;
242
- }
243
- // TODO improve this
244
- const keys = await this.#localCache.keys();
245
- return !keys.some((k) => k.startsWith('RAW_META/'));
246
- }
247
- #normalizeOptions(options) {
248
- if (typeof options === 'string') {
249
- return { endpoint: options };
250
- }
251
- else {
252
- let { metadata } = options || {};
253
- if (metadata && typeof metadata === 'string') {
254
- metadata = {
255
- [exports.CATCH_ALL_METADATA_KEY]: (0, util_1.hexAddPrefix)(metadata),
256
- };
257
- }
258
- return { ...options, metadata };
259
- }
260
- }
261
- #getProvider() {
262
- const { provider, endpoint } = this.#options;
263
- if (provider) {
264
- (0, utils_1.assert)(provider.hasSubscriptions, 'Only supports RPC Provider can make subscription requests');
265
- return provider;
266
- }
267
- if (endpoint) {
268
- if (endpoint.startsWith('ws://') || endpoint.startsWith('wss://')) {
269
- return new rpc_provider_1.WsProvider(endpoint);
270
- }
271
- else {
272
- throw new Error('Invalid RPC network endpoint, a valid endpoint should start with `wss://`, `ws://`');
273
- }
274
- }
275
- // TODO support light-client
276
- return new rpc_provider_1.WsProvider();
277
- }
278
- /**
279
- * @description Entry-point for executing RPCs to blockchain node.
280
- *
281
- * ```typescript
282
- * // Subscribe to new heads
283
- * api.rpc.chain.subscribeNewHeads((header) => {
284
- * console.log(header);
285
- * });
286
- *
287
- * // Execute arbitrary rpc method: `module_rpc_name`
288
- * const result = await api.rpc.module.rpc_name();
289
- * ```
290
- */
291
- get rpc() {
292
- // TODO add executable carrier to support calling arbitrary rpc methods via api.rpc(<method>)
293
- return (0, proxychain_1.newProxyChain)({ executor: new executor_1.RpcExecutor(this) });
294
- }
295
- /**
296
- * @description Entry-point for inspecting constants (parameter types) for all pallets (modules).
297
- *
298
- * ```typescript
299
- * const ss58Prefix = api.consts.system.ss58Prefix;
300
- * console.log('ss58Prefix:', ss58Prefix)
301
- * ```
302
- */
303
- get consts() {
304
- return (0, proxychain_1.newProxyChain)({ executor: new executor_1.ConstantExecutor(this) });
305
- }
306
- /**
307
- * @description Entry-point for executing query to on-chain storage.
308
- *
309
- * ```typescript
310
- * const balance = await api.query.system.account(<address>);
311
- * console.log('Balance:', balance);
312
- * ```
313
- */
314
- get query() {
315
- return (0, proxychain_1.newProxyChain)({ executor: new executor_1.StorageQueryExecutor(this) });
316
- }
317
- queryAt(blockHash) {
318
- return (0, proxychain_1.newProxyChain)({ executor: new executor_1.StorageQueryExecutor(this, blockHash) });
319
- }
320
- /**
321
- * @description Entry-point for inspecting errors from metadata
322
- */
323
- get errors() {
324
- return (0, proxychain_1.newProxyChain)({ executor: new executor_1.ErrorExecutor(this) });
325
- }
326
- /**
327
- * @description Entry-point for inspecting events from metadata
328
- */
329
- get events() {
330
- return (0, proxychain_1.newProxyChain)({ executor: new executor_1.EventExecutor(this) });
331
- }
332
- get call() {
333
- return (0, proxychain_1.newProxyChain)({ executor: new executor_1.RuntimeApiExecutor(this) });
334
- }
335
- callAt(blockHash) {
336
- return (0, proxychain_1.newProxyChain)({ executor: new executor_1.RuntimeApiExecutor(this, blockHash) });
337
- }
338
- get tx() {
339
- return (0, proxychain_1.newProxyChain)({ executor: new executor_1.TxExecutor(this) });
340
- }
341
- /**
342
- * @description Current provider for api connection
343
- */
344
- get provider() {
345
- return this.#provider;
346
- }
347
- /**
348
- * @description Codec registry
349
- */
350
- get registry() {
351
- return this.#registry;
352
- }
353
- /**
354
- * @description Check if metadata is present
355
- */
356
- get hasMetadata() {
357
- return !!this.#metadata && !!this.#metadataLatest;
358
- }
359
- get metadata() {
360
- return this.#metadata;
361
- }
362
- get metadataLatest() {
363
- return this.#metadataLatest;
364
- }
365
- /**
366
- * Setup metadata for
367
- * @param metadata
368
- */
369
- setMetadata(metadata) {
370
- this.#metadata = metadata;
371
- this.#metadataLatest = metadata.latest;
372
- this.#registry = new codecs_1.PortableRegistry(this.#metadataLatest);
373
- }
374
- /**
375
- * @description Disconnect to blockchain node
376
- */
377
- async disconnect() {
378
- await this.#unsubscribeUpdates();
379
- await this.#provider.disconnect();
380
- }
381
- /**
382
- * @description Clear local cache
383
- */
384
- async clearCache() {
385
- await this.#localCache?.clear();
386
- }
387
- /**
388
- * @description Check if current provider can make subscription request (e.: via WebSocket)
389
- */
390
- get hasSubscriptions() {
391
- return this.provider.hasSubscriptions;
392
- }
393
- /**
394
- * @description Check if it's connected to the blockchain node
395
- */
396
- get isConnected() {
397
- return this.provider.isConnected;
398
- }
399
- /**
400
- * @description Check if the api instance is using a catch-all metadata
401
- */
402
- get hasCatchAllMetadata() {
403
- return !!this.#options.metadata && !!this.#options.metadata[exports.CATCH_ALL_METADATA_KEY];
404
- }
405
- get currentMetadataKey() {
406
- return `RAW_META/${this.#genesisHash || '0x'}/${this.#runtimeVersion?.specVersion || '---'}`;
407
- }
408
- /**
409
- * @description Genesis hash of connected blockchain node
410
- */
411
- get genesisHash() {
412
- return this.#genesisHash;
413
- }
414
- /**
415
- * @description Runtime version of connected blockchain node
416
- */
417
- get runtimeVersion() {
418
- return this.#runtimeVersion;
419
- }
420
- /**
421
- * @description Chain properties of connected blockchain node
422
- */
423
- get chainProperties() {
424
- return this.#chainProperties;
425
- }
426
- /**
427
- * @description Runtime chain name of connected blockchain node
428
- */
429
- get runtimeChain() {
430
- return this.#runtimeChain;
431
- }
432
- get options() {
433
- return this.#options;
434
- }
435
- }
436
- exports.Dedot = Dedot;
@@ -1,22 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ConstantExecutor = void 0;
4
- const util_1 = require("@polkadot/util");
5
- const Executor_1 = require("./Executor");
6
- /**
7
- * @name ConstantExecutor
8
- *
9
- * Find & decode the constant value from metadata
10
- */
11
- class ConstantExecutor extends Executor_1.Executor {
12
- execute(pallet, constantName) {
13
- const targetPallet = this.getPallet(pallet);
14
- const constantDef = targetPallet.constants.find((one) => (0, util_1.stringCamelCase)(one.name) === constantName);
15
- if (!constantDef) {
16
- throw new Error(`Constant ${constantName} not found in pallet ${pallet}`);
17
- }
18
- const $codec = this.registry.findCodec(constantDef.typeId);
19
- return $codec.tryDecode(constantDef.value);
20
- }
21
- }
22
- exports.ConstantExecutor = ConstantExecutor;