@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.
- package/LICENSE +201 -0
- package/README.md +1 -0
- package/chaintypes/index.d.ts +1 -0
- package/chaintypes/index.js +1 -0
- package/chaintypes/substrate/consts.d.ts +2132 -0
- package/chaintypes/substrate/consts.js +2 -0
- package/chaintypes/substrate/errors.d.ts +3490 -0
- package/chaintypes/substrate/errors.js +2 -0
- package/chaintypes/substrate/events.d.ts +5187 -0
- package/chaintypes/substrate/events.js +2 -0
- package/chaintypes/substrate/index.d.ts +22 -0
- package/chaintypes/substrate/index.js +2 -0
- package/chaintypes/substrate/json-rpc.d.ts +3 -0
- package/chaintypes/substrate/json-rpc.js +2 -0
- package/chaintypes/substrate/query.d.ts +3829 -0
- package/chaintypes/substrate/query.js +2 -0
- package/chaintypes/substrate/runtime.d.ts +850 -0
- package/chaintypes/substrate/runtime.js +2 -0
- package/chaintypes/substrate/tx.d.ts +14269 -0
- package/chaintypes/substrate/tx.js +2 -0
- package/chaintypes/substrate/types.d.ts +30457 -0
- package/chaintypes/substrate/types.js +2 -0
- package/cjs/chaintypes/index.js +17 -0
- package/cjs/chaintypes/substrate/consts.js +3 -0
- package/cjs/chaintypes/substrate/errors.js +3 -0
- package/cjs/chaintypes/substrate/events.js +3 -0
- package/cjs/chaintypes/substrate/index.js +18 -0
- package/cjs/chaintypes/substrate/json-rpc.js +3 -0
- package/cjs/chaintypes/substrate/query.js +3 -0
- package/cjs/chaintypes/substrate/runtime.js +3 -0
- package/cjs/chaintypes/substrate/tx.js +3 -0
- package/cjs/chaintypes/substrate/types.js +3 -0
- package/cjs/client/BaseSubstrateClient.js +257 -0
- package/cjs/client/Dedot.js +250 -0
- package/cjs/client/DedotClient.js +186 -0
- package/cjs/client/index.js +18 -0
- package/cjs/executor/ConstantExecutor.js +20 -0
- package/cjs/executor/ErrorExecutor.js +46 -0
- package/cjs/executor/EventExecutor.js +52 -0
- package/cjs/executor/Executor.js +42 -0
- package/cjs/executor/RuntimeApiExecutor.js +121 -0
- package/cjs/executor/StorageQueryExecutor.js +115 -0
- package/cjs/executor/TxExecutor.js +55 -0
- package/cjs/executor/index.js +26 -0
- package/cjs/executor/v2/RuntimeApiExecutorV2.js +21 -0
- package/cjs/executor/v2/StorageQueryExecutorV2.js +65 -0
- package/cjs/executor/v2/TxExecutorV2.js +20 -0
- package/cjs/extrinsic/extensions/ExtraSignedExtension.js +87 -0
- package/cjs/extrinsic/extensions/SignedExtension.js +41 -0
- package/cjs/extrinsic/extensions/index.js +19 -0
- package/cjs/extrinsic/extensions/known/ChargeAssetTxPayment.js +43 -0
- package/cjs/extrinsic/extensions/known/ChargeTransactionPayment.js +16 -0
- package/cjs/extrinsic/extensions/known/CheckGenesis.js +19 -0
- package/cjs/extrinsic/extensions/known/CheckMortality.js +90 -0
- package/cjs/extrinsic/extensions/known/CheckNonZeroSender.js +10 -0
- package/cjs/extrinsic/extensions/known/CheckNonce.js +33 -0
- package/cjs/extrinsic/extensions/known/CheckSpecVersion.js +19 -0
- package/cjs/extrinsic/extensions/known/CheckTxVersion.js +19 -0
- package/cjs/extrinsic/extensions/known/CheckWeight.js +10 -0
- package/cjs/extrinsic/extensions/known/PrevalidateAttests.js +11 -0
- package/cjs/extrinsic/extensions/known/index.js +25 -0
- package/cjs/extrinsic/index.js +18 -0
- package/cjs/extrinsic/submittable/BaseSubmittableExtrinsic.js +70 -0
- package/cjs/extrinsic/submittable/SubmittableExtrinsic.js +47 -0
- package/cjs/extrinsic/submittable/SubmittableExtrinsicV2.js +204 -0
- package/cjs/extrinsic/submittable/SubmittableResult.js +33 -0
- package/cjs/extrinsic/submittable/errors.js +16 -0
- package/cjs/extrinsic/submittable/fakeSigner.js +13 -0
- package/cjs/extrinsic/submittable/index.js +20 -0
- package/cjs/extrinsic/submittable/utils.js +20 -0
- package/cjs/index.js +21 -0
- package/cjs/json-rpc/JsonRpcClient.js +165 -0
- package/cjs/json-rpc/group/ChainHead/BlockUsage.js +24 -0
- package/cjs/json-rpc/group/ChainHead/ChainHead.js +603 -0
- package/cjs/json-rpc/group/ChainHead/error.js +59 -0
- package/cjs/json-rpc/group/ChainHead/index.js +18 -0
- package/cjs/json-rpc/group/ChainSpec.js +19 -0
- package/cjs/json-rpc/group/JsonRpcGroup.js +89 -0
- package/cjs/json-rpc/group/Transaction.js +25 -0
- package/cjs/json-rpc/group/TransactionWatch.js +17 -0
- package/cjs/json-rpc/group/index.js +21 -0
- package/cjs/json-rpc/index.js +20 -0
- package/cjs/json-rpc/scaledResponses.js +15 -0
- package/cjs/json-rpc/subscriptionsInfo.js +20 -0
- package/cjs/package.json +1 -0
- package/cjs/proxychain.js +27 -0
- package/cjs/storage/QueryableStorage.js +161 -0
- package/cjs/storage/index.js +17 -0
- package/cjs/types.js +2 -0
- package/client/BaseSubstrateClient.d.ts +65 -0
- package/client/BaseSubstrateClient.js +252 -0
- package/client/Dedot.d.ts +131 -0
- package/client/Dedot.js +246 -0
- package/client/DedotClient.d.ts +61 -0
- package/client/DedotClient.js +182 -0
- package/client/index.d.ts +2 -0
- package/client/index.js +2 -0
- package/executor/ConstantExecutor.d.ts +10 -0
- package/executor/ConstantExecutor.js +16 -0
- package/executor/ErrorExecutor.d.ts +10 -0
- package/executor/ErrorExecutor.js +42 -0
- package/executor/EventExecutor.d.ts +10 -0
- package/executor/EventExecutor.js +48 -0
- package/executor/Executor.d.ts +149 -0
- package/executor/Executor.js +38 -0
- package/executor/RuntimeApiExecutor.d.ts +27 -0
- package/executor/RuntimeApiExecutor.js +117 -0
- package/executor/StorageQueryExecutor.d.ts +14 -0
- package/executor/StorageQueryExecutor.js +111 -0
- package/executor/TxExecutor.d.ts +10 -0
- package/executor/TxExecutor.js +51 -0
- package/executor/index.d.ts +10 -0
- package/executor/index.js +10 -0
- package/executor/v2/RuntimeApiExecutorV2.d.ts +14 -0
- package/executor/v2/RuntimeApiExecutorV2.js +17 -0
- package/executor/v2/StorageQueryExecutorV2.d.ts +17 -0
- package/executor/v2/StorageQueryExecutorV2.js +61 -0
- package/executor/v2/TxExecutorV2.d.ts +11 -0
- package/executor/v2/TxExecutorV2.js +16 -0
- package/extrinsic/extensions/ExtraSignedExtension.d.ts +14 -0
- package/extrinsic/extensions/ExtraSignedExtension.js +60 -0
- package/extrinsic/extensions/SignedExtension.d.ts +40 -0
- package/extrinsic/extensions/SignedExtension.js +37 -0
- package/extrinsic/extensions/index.d.ts +3 -0
- package/extrinsic/extensions/index.js +3 -0
- package/extrinsic/extensions/known/ChargeAssetTxPayment.d.ts +14 -0
- package/extrinsic/extensions/known/ChargeAssetTxPayment.js +39 -0
- package/extrinsic/extensions/known/ChargeTransactionPayment.d.ts +6 -0
- package/extrinsic/extensions/known/ChargeTransactionPayment.js +12 -0
- package/extrinsic/extensions/known/CheckGenesis.d.ts +10 -0
- package/extrinsic/extensions/known/CheckGenesis.js +15 -0
- package/extrinsic/extensions/known/CheckMortality.d.ts +15 -0
- package/extrinsic/extensions/known/CheckMortality.js +86 -0
- package/extrinsic/extensions/known/CheckNonZeroSender.d.ts +6 -0
- package/extrinsic/extensions/known/CheckNonZeroSender.js +6 -0
- package/extrinsic/extensions/known/CheckNonce.d.ts +10 -0
- package/extrinsic/extensions/known/CheckNonce.js +29 -0
- package/extrinsic/extensions/known/CheckSpecVersion.d.ts +9 -0
- package/extrinsic/extensions/known/CheckSpecVersion.js +15 -0
- package/extrinsic/extensions/known/CheckTxVersion.d.ts +9 -0
- package/extrinsic/extensions/known/CheckTxVersion.js +15 -0
- package/extrinsic/extensions/known/CheckWeight.d.ts +6 -0
- package/extrinsic/extensions/known/CheckWeight.js +6 -0
- package/extrinsic/extensions/known/PrevalidateAttests.d.ts +7 -0
- package/extrinsic/extensions/known/PrevalidateAttests.js +7 -0
- package/extrinsic/extensions/known/index.d.ts +3 -0
- package/extrinsic/extensions/known/index.js +22 -0
- package/extrinsic/index.d.ts +2 -0
- package/extrinsic/index.js +2 -0
- package/extrinsic/submittable/BaseSubmittableExtrinsic.d.ts +17 -0
- package/extrinsic/submittable/BaseSubmittableExtrinsic.js +66 -0
- package/extrinsic/submittable/SubmittableExtrinsic.d.ts +12 -0
- package/extrinsic/submittable/SubmittableExtrinsic.js +43 -0
- package/extrinsic/submittable/SubmittableExtrinsicV2.d.ts +15 -0
- package/extrinsic/submittable/SubmittableExtrinsicV2.js +200 -0
- package/extrinsic/submittable/SubmittableResult.d.ts +19 -0
- package/extrinsic/submittable/SubmittableResult.js +29 -0
- package/extrinsic/submittable/errors.d.ts +11 -0
- package/extrinsic/submittable/errors.js +12 -0
- package/extrinsic/submittable/fakeSigner.d.ts +2 -0
- package/extrinsic/submittable/fakeSigner.js +10 -0
- package/extrinsic/submittable/index.d.ts +4 -0
- package/extrinsic/submittable/index.js +4 -0
- package/extrinsic/submittable/utils.d.ts +10 -0
- package/extrinsic/submittable/utils.js +15 -0
- package/index.d.ts +6 -0
- package/index.js +5 -0
- package/json-rpc/JsonRpcClient.d.ts +50 -0
- package/json-rpc/JsonRpcClient.js +160 -0
- package/json-rpc/group/ChainHead/BlockUsage.d.ts +11 -0
- package/json-rpc/group/ChainHead/BlockUsage.js +20 -0
- package/json-rpc/group/ChainHead/ChainHead.d.ts +70 -0
- package/json-rpc/group/ChainHead/ChainHead.js +599 -0
- package/json-rpc/group/ChainHead/error.d.ts +47 -0
- package/json-rpc/group/ChainHead/error.js +48 -0
- package/json-rpc/group/ChainHead/index.d.ts +2 -0
- package/json-rpc/group/ChainHead/index.js +2 -0
- package/json-rpc/group/ChainSpec.d.ts +10 -0
- package/json-rpc/group/ChainSpec.js +15 -0
- package/json-rpc/group/JsonRpcGroup.d.ts +74 -0
- package/json-rpc/group/JsonRpcGroup.js +85 -0
- package/json-rpc/group/Transaction.d.ts +11 -0
- package/json-rpc/group/Transaction.js +21 -0
- package/json-rpc/group/TransactionWatch.d.ts +10 -0
- package/json-rpc/group/TransactionWatch.js +13 -0
- package/json-rpc/group/index.d.ts +5 -0
- package/json-rpc/group/index.js +5 -0
- package/json-rpc/index.d.ts +4 -0
- package/json-rpc/index.js +4 -0
- package/json-rpc/scaledResponses.d.ts +2 -0
- package/json-rpc/scaledResponses.js +12 -0
- package/json-rpc/subscriptionsInfo.d.ts +2 -0
- package/json-rpc/subscriptionsInfo.js +17 -0
- package/package.json +54 -0
- package/proxychain.d.ts +14 -0
- package/proxychain.js +23 -0
- package/storage/QueryableStorage.d.ts +35 -0
- package/storage/QueryableStorage.js +157 -0
- package/storage/index.d.ts +1 -0
- package/storage/index.js +1 -0
- package/types.d.ts +120 -0
- package/types.js +1 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./substrate/index.js"), exports);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Generated by @dedot/codegen
|
|
3
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
+
if (k2 === undefined) k2 = k;
|
|
5
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
+
}
|
|
9
|
+
Object.defineProperty(o, k2, desc);
|
|
10
|
+
}) : (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
o[k2] = m[k];
|
|
13
|
+
}));
|
|
14
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
15
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
16
|
+
};
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
__exportStar(require("./types.js"), exports);
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseSubstrateClient = exports.ensurePresence = void 0;
|
|
4
|
+
const codecs_1 = require("@dedot/codecs");
|
|
5
|
+
const storage_1 = require("@dedot/storage");
|
|
6
|
+
const utils_1 = require("@dedot/utils");
|
|
7
|
+
const index_js_1 = require("../executor/index.js");
|
|
8
|
+
const index_js_2 = require("../json-rpc/index.js");
|
|
9
|
+
const proxychain_js_1 = require("../proxychain.js");
|
|
10
|
+
const SUPPORTED_METADATA_VERSIONS = [15, 14];
|
|
11
|
+
const MetadataApiHash = (0, utils_1.calcRuntimeApiHash)('Metadata'); // 0x37e397fc7c91f5e4
|
|
12
|
+
const MESSAGE = 'Make sure to call `.connect()` method first before using the API interfaces.';
|
|
13
|
+
function ensurePresence(value) {
|
|
14
|
+
return (0, utils_1.ensurePresence)(value, MESSAGE);
|
|
15
|
+
}
|
|
16
|
+
exports.ensurePresence = ensurePresence;
|
|
17
|
+
/**
|
|
18
|
+
* @name BaseSubstrateClient
|
|
19
|
+
* @description Base & shared abstraction for Substrate API Clients
|
|
20
|
+
*/
|
|
21
|
+
class BaseSubstrateClient extends index_js_2.JsonRpcClient {
|
|
22
|
+
rpcVersion;
|
|
23
|
+
_options;
|
|
24
|
+
_registry;
|
|
25
|
+
_metadata;
|
|
26
|
+
_genesisHash;
|
|
27
|
+
_runtimeVersion;
|
|
28
|
+
_localCache;
|
|
29
|
+
constructor(rpcVersion, options) {
|
|
30
|
+
super(options);
|
|
31
|
+
this.rpcVersion = rpcVersion;
|
|
32
|
+
this._options = this.normalizeOptions(options);
|
|
33
|
+
}
|
|
34
|
+
/// --- Internal logics
|
|
35
|
+
normalizeOptions(options) {
|
|
36
|
+
const defaultOptions = { throwOnUnknownApi: true };
|
|
37
|
+
if ((0, index_js_2.isJsonRpcProvider)(options)) {
|
|
38
|
+
return { ...defaultOptions, provider: options };
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
return { ...defaultOptions, ...options };
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
async initializeLocalCache() {
|
|
45
|
+
if (!this._options.cacheMetadata)
|
|
46
|
+
return;
|
|
47
|
+
// Initialize local cache
|
|
48
|
+
if (this._options.cacheStorage) {
|
|
49
|
+
this._localCache = this._options.cacheStorage;
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
try {
|
|
53
|
+
this._localCache = new storage_1.LocalStorage();
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
throw new Error('localStorage is not available for caching, please provide a cacheStorage option');
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
async setupMetadata(preloadMetadata) {
|
|
60
|
+
let metadata = preloadMetadata;
|
|
61
|
+
let shouldUpdateCache = !!metadata;
|
|
62
|
+
if (!metadata) {
|
|
63
|
+
metadata = this.getMetadataFromOptions(this._runtimeVersion);
|
|
64
|
+
}
|
|
65
|
+
const metadataKey = this.currentMetadataKey;
|
|
66
|
+
try {
|
|
67
|
+
if (this._localCache && this._options.cacheMetadata) {
|
|
68
|
+
if (!metadata) {
|
|
69
|
+
try {
|
|
70
|
+
const cachedRawMetadata = await this._localCache.get(metadataKey);
|
|
71
|
+
if (cachedRawMetadata) {
|
|
72
|
+
metadata = codecs_1.$Metadata.tryDecode(cachedRawMetadata);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
catch (e) {
|
|
76
|
+
console.error('Cannot decode raw metadata, try fetching fresh metadata from chain.', e);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
finally {
|
|
82
|
+
if (!metadata) {
|
|
83
|
+
metadata = await this.fetchMetadata();
|
|
84
|
+
if (this._options.cacheMetadata)
|
|
85
|
+
shouldUpdateCache = true;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (shouldUpdateCache && this._localCache) {
|
|
89
|
+
await this._localCache.set(metadataKey, (0, utils_1.u8aToHex)(codecs_1.$Metadata.tryEncode(metadata)));
|
|
90
|
+
}
|
|
91
|
+
if (!metadata) {
|
|
92
|
+
throw new Error('Cannot load metadata');
|
|
93
|
+
}
|
|
94
|
+
this.setMetadata(metadata);
|
|
95
|
+
}
|
|
96
|
+
setMetadata(metadata) {
|
|
97
|
+
this._metadata = metadata;
|
|
98
|
+
this._registry = new codecs_1.PortableRegistry(metadata.latest, this.options.hasher);
|
|
99
|
+
}
|
|
100
|
+
getMetadataKey(runtime) {
|
|
101
|
+
return `RAW_META/${this._genesisHash || '0x'}/${runtime?.specVersion || '---'}`;
|
|
102
|
+
}
|
|
103
|
+
get currentMetadataKey() {
|
|
104
|
+
return this.getMetadataKey(this._runtimeVersion);
|
|
105
|
+
}
|
|
106
|
+
async shouldPreloadMetadata() {
|
|
107
|
+
if (this._options.metadata && Object.keys(this._options.metadata).length) {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
if (!this._options.cacheMetadata || !this._localCache) {
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
// TODO improve this
|
|
114
|
+
const keys = await this._localCache.keys();
|
|
115
|
+
return !keys.some((k) => k.startsWith('RAW_META/'));
|
|
116
|
+
}
|
|
117
|
+
getMetadataFromOptions(runtime) {
|
|
118
|
+
if (!runtime || !this.options.metadata)
|
|
119
|
+
return;
|
|
120
|
+
const key = this.getMetadataKey(runtime);
|
|
121
|
+
if (this.options.metadata[key]) {
|
|
122
|
+
return codecs_1.$Metadata.tryDecode(this.options.metadata[key]);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
async fetchMetadata(hash, runtime) {
|
|
126
|
+
// First try finding metadata from the provided option
|
|
127
|
+
const optionMetadata = this.getMetadataFromOptions(runtime);
|
|
128
|
+
if (optionMetadata)
|
|
129
|
+
return optionMetadata;
|
|
130
|
+
// If there is no runtime, we assume that the node supports Metadata Api V2
|
|
131
|
+
const supportedV2 = runtime ? runtime.apis[MetadataApiHash] === 2 : true;
|
|
132
|
+
if (supportedV2) {
|
|
133
|
+
// It makes sense to call metadata.metadataVersions to fetch the list of supported metadata versions first
|
|
134
|
+
// But for now, this approach could potentially help save/reduce one rpc call to the server in case the node support v15
|
|
135
|
+
// Question: Why not having a `metadata.metadataLatest` to fetch the latest version?
|
|
136
|
+
for (const version of SUPPORTED_METADATA_VERSIONS) {
|
|
137
|
+
try {
|
|
138
|
+
const rawMetadata = await this.callAt(hash).metadata.metadataAtVersion(version);
|
|
139
|
+
if (!rawMetadata)
|
|
140
|
+
continue;
|
|
141
|
+
return codecs_1.$Metadata.tryDecode(rawMetadata);
|
|
142
|
+
}
|
|
143
|
+
catch { }
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
try {
|
|
147
|
+
return codecs_1.$Metadata.tryDecode(await this.callAt(hash).metadata.metadata());
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
return await this.rpc.state_getMetadata();
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
cleanUp() {
|
|
154
|
+
this._registry = undefined;
|
|
155
|
+
this._metadata = undefined;
|
|
156
|
+
this._genesisHash = undefined;
|
|
157
|
+
this._runtimeVersion = undefined;
|
|
158
|
+
this._localCache = undefined;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* @description Clear local cache
|
|
162
|
+
*/
|
|
163
|
+
async clearCache() {
|
|
164
|
+
await this._localCache?.clear();
|
|
165
|
+
}
|
|
166
|
+
async doConnect() {
|
|
167
|
+
this.on('connected', this.onConnected);
|
|
168
|
+
this.on('disconnected', this.onDisconnected);
|
|
169
|
+
return new Promise((resolve) => {
|
|
170
|
+
this.once('ready', () => {
|
|
171
|
+
resolve(this);
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
onConnected = async () => {
|
|
176
|
+
await this.initialize();
|
|
177
|
+
};
|
|
178
|
+
onDisconnected = async () => { };
|
|
179
|
+
async initialize() {
|
|
180
|
+
await this.initializeLocalCache();
|
|
181
|
+
await this.doInitialize();
|
|
182
|
+
this.emit('ready');
|
|
183
|
+
}
|
|
184
|
+
async doInitialize() {
|
|
185
|
+
throw new Error('Unimplemented!');
|
|
186
|
+
}
|
|
187
|
+
async beforeDisconnect() { }
|
|
188
|
+
async afterDisconnect() {
|
|
189
|
+
this.cleanUp();
|
|
190
|
+
}
|
|
191
|
+
toSubstrateRuntimeVersion(runtimeVersion) {
|
|
192
|
+
return {
|
|
193
|
+
...runtimeVersion,
|
|
194
|
+
apis: runtimeVersion.apis.reduce((o, [name, version]) => {
|
|
195
|
+
o[name] = version;
|
|
196
|
+
return o;
|
|
197
|
+
}, {}),
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
/// --- Public APIs ---
|
|
201
|
+
/**
|
|
202
|
+
* @description Connect to blockchain node
|
|
203
|
+
*/
|
|
204
|
+
async connect() {
|
|
205
|
+
const [api, _] = await Promise.all([this.doConnect(), super.connect()]);
|
|
206
|
+
return api;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* @description Disconnect to blockchain node
|
|
210
|
+
*/
|
|
211
|
+
async disconnect() {
|
|
212
|
+
await this.beforeDisconnect();
|
|
213
|
+
await super.disconnect();
|
|
214
|
+
await this.afterDisconnect();
|
|
215
|
+
}
|
|
216
|
+
get options() {
|
|
217
|
+
return this._options;
|
|
218
|
+
}
|
|
219
|
+
get metadata() {
|
|
220
|
+
return ensurePresence(this._metadata);
|
|
221
|
+
}
|
|
222
|
+
get registry() {
|
|
223
|
+
return ensurePresence(this._registry);
|
|
224
|
+
}
|
|
225
|
+
get genesisHash() {
|
|
226
|
+
return ensurePresence(this._genesisHash);
|
|
227
|
+
}
|
|
228
|
+
get runtimeVersion() {
|
|
229
|
+
return ensurePresence(this._runtimeVersion);
|
|
230
|
+
}
|
|
231
|
+
get consts() {
|
|
232
|
+
return (0, proxychain_js_1.newProxyChain)({ executor: new index_js_1.ConstantExecutor(this) });
|
|
233
|
+
}
|
|
234
|
+
get errors() {
|
|
235
|
+
return (0, proxychain_js_1.newProxyChain)({ executor: new index_js_1.ErrorExecutor(this) });
|
|
236
|
+
}
|
|
237
|
+
get events() {
|
|
238
|
+
return (0, proxychain_js_1.newProxyChain)({ executor: new index_js_1.EventExecutor(this) });
|
|
239
|
+
}
|
|
240
|
+
get query() {
|
|
241
|
+
throw new Error('Unimplemented!');
|
|
242
|
+
}
|
|
243
|
+
get call() {
|
|
244
|
+
return this.callAt();
|
|
245
|
+
}
|
|
246
|
+
// For internal use with caution
|
|
247
|
+
callAt(hash) {
|
|
248
|
+
throw new Error('Unimplemented!');
|
|
249
|
+
}
|
|
250
|
+
get tx() {
|
|
251
|
+
throw new Error('Unimplemented!');
|
|
252
|
+
}
|
|
253
|
+
at(hash) {
|
|
254
|
+
throw new Error('Unimplemented!');
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
exports.BaseSubstrateClient = BaseSubstrateClient;
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Dedot = void 0;
|
|
4
|
+
const codecs_1 = require("@dedot/codecs");
|
|
5
|
+
const index_js_1 = require("../executor/index.js");
|
|
6
|
+
const proxychain_js_1 = require("../proxychain.js");
|
|
7
|
+
const BaseSubstrateClient_js_1 = require("./BaseSubstrateClient.js");
|
|
8
|
+
const KEEP_ALIVE_INTERVAL = 10_000; // in ms
|
|
9
|
+
/**
|
|
10
|
+
* @name Dedot
|
|
11
|
+
* @description Promised-based API Client for Polkadot & Substrate
|
|
12
|
+
*
|
|
13
|
+
* Initialize API instance and interact with substrate-based network
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import { Dedot } from 'dedot';
|
|
16
|
+
* import type { PolkadotApi } from '@dedot/chaintypes/polkadot';
|
|
17
|
+
*
|
|
18
|
+
* const run = async () => {
|
|
19
|
+
* const api = await Dedot.new<PolkadotApi>('wss://rpc.polkadot.io');
|
|
20
|
+
*
|
|
21
|
+
* // Call rpc `state_getMetadata` to fetch raw scale-encoded metadata and decode it.
|
|
22
|
+
* const metadata = await api.rpc.state.getMetadata();
|
|
23
|
+
* console.log('Metadata:', metadata);
|
|
24
|
+
*
|
|
25
|
+
* // Query on-chain storage
|
|
26
|
+
* const address = '14...';
|
|
27
|
+
* const balance = await api.query.system.account(address);
|
|
28
|
+
* console.log('Balance:', balance);
|
|
29
|
+
*
|
|
30
|
+
*
|
|
31
|
+
* // Subscribe to on-chain storage changes
|
|
32
|
+
* const unsub = await api.query.system.number((blockNumber) => {
|
|
33
|
+
* console.log(`Current block number: ${blockNumber}`);
|
|
34
|
+
* });
|
|
35
|
+
*
|
|
36
|
+
* // Get pallet constants
|
|
37
|
+
* const ss58Prefix = api.consts.system.ss58Prefix;
|
|
38
|
+
* console.log('Polkadot ss58Prefix:', ss58Prefix)
|
|
39
|
+
*
|
|
40
|
+
* // await unsub();
|
|
41
|
+
* // await api.disconnect();
|
|
42
|
+
* }
|
|
43
|
+
*
|
|
44
|
+
* run().catch(console.error);
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
class Dedot extends BaseSubstrateClient_js_1.BaseSubstrateClient {
|
|
48
|
+
#runtimeSubscriptionUnsub;
|
|
49
|
+
#healthTimer;
|
|
50
|
+
#apiAtCache = {};
|
|
51
|
+
/**
|
|
52
|
+
* Use factory methods (`create`, `new`) to create `Dedot` instances.
|
|
53
|
+
*
|
|
54
|
+
* @param options
|
|
55
|
+
*/
|
|
56
|
+
constructor(options) {
|
|
57
|
+
super('legacy', options);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Factory method to create a new Dedot instance
|
|
61
|
+
*
|
|
62
|
+
* @param options
|
|
63
|
+
*/
|
|
64
|
+
static async create(options) {
|
|
65
|
+
return new Dedot(options).connect();
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Alias for __Dedot.create__
|
|
69
|
+
*
|
|
70
|
+
* @param options
|
|
71
|
+
*/
|
|
72
|
+
static async new(options) {
|
|
73
|
+
return Dedot.create(options);
|
|
74
|
+
}
|
|
75
|
+
onDisconnected = async () => {
|
|
76
|
+
await this.#unsubscribeUpdates();
|
|
77
|
+
};
|
|
78
|
+
async beforeDisconnect() {
|
|
79
|
+
await this.#unsubscribeUpdates();
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Initialize APIs before usage
|
|
83
|
+
*/
|
|
84
|
+
async doInitialize() {
|
|
85
|
+
let [genesisHash, runtimeVersion, metadata] = await Promise.all([
|
|
86
|
+
this.rpc.chain_getBlockHash(0),
|
|
87
|
+
this.#getRuntimeVersion(),
|
|
88
|
+
(await this.shouldPreloadMetadata()) ? this.fetchMetadata() : Promise.resolve(undefined),
|
|
89
|
+
]);
|
|
90
|
+
this._genesisHash = genesisHash;
|
|
91
|
+
this._runtimeVersion = runtimeVersion;
|
|
92
|
+
await this.setupMetadata(metadata);
|
|
93
|
+
this.#subscribeUpdates();
|
|
94
|
+
}
|
|
95
|
+
cleanUp() {
|
|
96
|
+
super.cleanUp();
|
|
97
|
+
this.#apiAtCache = {};
|
|
98
|
+
this.#healthTimer = undefined;
|
|
99
|
+
this.#runtimeSubscriptionUnsub = undefined;
|
|
100
|
+
}
|
|
101
|
+
#subscribeRuntimeUpgrades() {
|
|
102
|
+
if (this.#runtimeSubscriptionUnsub)
|
|
103
|
+
return;
|
|
104
|
+
this.rpc
|
|
105
|
+
.state_subscribeRuntimeVersion(async (runtimeVersion) => {
|
|
106
|
+
if (runtimeVersion.specVersion !== this.runtimeVersion?.specVersion) {
|
|
107
|
+
this._runtimeVersion = this.toSubstrateRuntimeVersion(runtimeVersion);
|
|
108
|
+
const newMetadata = await this.fetchMetadata(undefined, this._runtimeVersion);
|
|
109
|
+
await this.setupMetadata(newMetadata);
|
|
110
|
+
}
|
|
111
|
+
})
|
|
112
|
+
.then((unsub) => {
|
|
113
|
+
this.#runtimeSubscriptionUnsub = unsub;
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
async #getRuntimeVersion(at) {
|
|
117
|
+
return this.toSubstrateRuntimeVersion(await this.rpc.state_getRuntimeVersion(at));
|
|
118
|
+
}
|
|
119
|
+
#subscribeHealth() {
|
|
120
|
+
this.#unsubscribeHealth();
|
|
121
|
+
this.#healthTimer = setInterval(() => {
|
|
122
|
+
this.rpc.system_health().catch(console.error);
|
|
123
|
+
}, KEEP_ALIVE_INTERVAL);
|
|
124
|
+
}
|
|
125
|
+
#unsubscribeHealth() {
|
|
126
|
+
if (!this.#healthTimer) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
clearInterval(this.#healthTimer);
|
|
130
|
+
this.#healthTimer = undefined;
|
|
131
|
+
}
|
|
132
|
+
async #unsubscribeRuntimeUpdates() {
|
|
133
|
+
if (!this.#runtimeSubscriptionUnsub) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
await this.#runtimeSubscriptionUnsub();
|
|
137
|
+
this.#runtimeSubscriptionUnsub = undefined;
|
|
138
|
+
}
|
|
139
|
+
#subscribeUpdates() {
|
|
140
|
+
this.#subscribeRuntimeUpgrades();
|
|
141
|
+
this.#subscribeHealth();
|
|
142
|
+
}
|
|
143
|
+
async #unsubscribeUpdates() {
|
|
144
|
+
await this.#unsubscribeRuntimeUpdates();
|
|
145
|
+
this.#unsubscribeHealth();
|
|
146
|
+
}
|
|
147
|
+
/// --- Public APIs ---
|
|
148
|
+
/**
|
|
149
|
+
* @description Entry-point for inspecting constants (parameter types) for all pallets (modules).
|
|
150
|
+
*
|
|
151
|
+
* ```typescript
|
|
152
|
+
* const ss58Prefix = api.consts.system.ss58Prefix;
|
|
153
|
+
* console.log('ss58Prefix:', ss58Prefix)
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
get consts() {
|
|
157
|
+
return (0, proxychain_js_1.newProxyChain)({ executor: new index_js_1.ConstantExecutor(this) });
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* @description Entry-point for executing query to on-chain storage.
|
|
161
|
+
*
|
|
162
|
+
* ```typescript
|
|
163
|
+
* const balance = await api.query.system.account(<address>);
|
|
164
|
+
* console.log('Balance:', balance);
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
get query() {
|
|
168
|
+
return (0, proxychain_js_1.newProxyChain)({ executor: new index_js_1.StorageQueryExecutor(this) });
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* @description Entry-point for inspecting errors from metadata
|
|
172
|
+
*/
|
|
173
|
+
get errors() {
|
|
174
|
+
return (0, proxychain_js_1.newProxyChain)({ executor: new index_js_1.ErrorExecutor(this) });
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* @description Entry-point for inspecting events from metadata
|
|
178
|
+
*/
|
|
179
|
+
get events() {
|
|
180
|
+
return (0, proxychain_js_1.newProxyChain)({ executor: new index_js_1.EventExecutor(this) });
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* @description Entry-point for executing runtime api
|
|
184
|
+
*
|
|
185
|
+
* ```typescript
|
|
186
|
+
* // Get account nonce
|
|
187
|
+
* const nonce = await api.call.accountNonceApi.accountNonce(<address>);
|
|
188
|
+
*
|
|
189
|
+
* // Query transaction payment info
|
|
190
|
+
* const tx = api.tx.balances.transferKeepAlive(<address>, 2_000_000_000_000n);
|
|
191
|
+
* const queryInfo = await api.call.transactionPaymentApi.queryInfo(tx.toU8a(), tx.length);
|
|
192
|
+
* ```
|
|
193
|
+
*/
|
|
194
|
+
get call() {
|
|
195
|
+
return this.callAt();
|
|
196
|
+
}
|
|
197
|
+
// For internal use with caution
|
|
198
|
+
callAt(hash) {
|
|
199
|
+
return (0, proxychain_js_1.newProxyChain)({ executor: new index_js_1.RuntimeApiExecutor(this, hash) });
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* @description Entry-point for executing on-chain transactions
|
|
203
|
+
*
|
|
204
|
+
* ```typescript
|
|
205
|
+
* // Make a transfer balance transaction
|
|
206
|
+
* api.tx.balances.transferKeepAlive(<address>, <amount>)
|
|
207
|
+
* .signAndSend(<keyPair|address>, { signer }, ({ status }) => {
|
|
208
|
+
* console.log('Transaction status', status.tag);
|
|
209
|
+
* });
|
|
210
|
+
* ```
|
|
211
|
+
*/
|
|
212
|
+
get tx() {
|
|
213
|
+
return (0, proxychain_js_1.newProxyChain)({ executor: new index_js_1.TxExecutor(this) });
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Create a new API instance at a specific block hash
|
|
217
|
+
* This is useful when we want to inspect the state of the chain at a specific block hash
|
|
218
|
+
*
|
|
219
|
+
* @param hash
|
|
220
|
+
*/
|
|
221
|
+
async at(hash) {
|
|
222
|
+
if (this.#apiAtCache[hash])
|
|
223
|
+
return this.#apiAtCache[hash];
|
|
224
|
+
const targetVersion = await this.#getRuntimeVersion(hash);
|
|
225
|
+
let metadata = this.metadata;
|
|
226
|
+
let registry = this.registry;
|
|
227
|
+
if (targetVersion.specVersion !== this.runtimeVersion.specVersion) {
|
|
228
|
+
metadata = await this.fetchMetadata(hash, targetVersion);
|
|
229
|
+
registry = new codecs_1.PortableRegistry(metadata.latest, this.options.hasher);
|
|
230
|
+
}
|
|
231
|
+
const api = {
|
|
232
|
+
rpcVersion: 'legacy',
|
|
233
|
+
atBlockHash: hash,
|
|
234
|
+
options: this.options,
|
|
235
|
+
genesisHash: this.genesisHash,
|
|
236
|
+
runtimeVersion: targetVersion,
|
|
237
|
+
metadata,
|
|
238
|
+
registry,
|
|
239
|
+
rpc: this.rpc,
|
|
240
|
+
};
|
|
241
|
+
api.consts = (0, proxychain_js_1.newProxyChain)({ executor: new index_js_1.ConstantExecutor(api) });
|
|
242
|
+
api.query = (0, proxychain_js_1.newProxyChain)({ executor: new index_js_1.StorageQueryExecutor(api) });
|
|
243
|
+
api.call = (0, proxychain_js_1.newProxyChain)({ executor: new index_js_1.RuntimeApiExecutor(api) });
|
|
244
|
+
api.events = (0, proxychain_js_1.newProxyChain)({ executor: new index_js_1.EventExecutor(api) });
|
|
245
|
+
api.errors = (0, proxychain_js_1.newProxyChain)({ executor: new index_js_1.ErrorExecutor(api) });
|
|
246
|
+
this.#apiAtCache[hash] = api;
|
|
247
|
+
return api;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
exports.Dedot = Dedot;
|