@dedot/api 0.8.1 → 0.9.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/cjs/client/BaseSubstrateClient.js +25 -1
- package/cjs/client/DedotClient.js +4 -0
- package/cjs/client/LegacyClient.js +4 -0
- package/cjs/executor/StorageQueryExecutor.js +8 -15
- package/cjs/executor/v2/StorageQueryExecutorV2.js +7 -33
- package/cjs/extrinsic/extensions/ExtraSignedExtension.js +24 -5
- package/cjs/extrinsic/extensions/FallbackSignedExtension.js +63 -0
- package/cjs/extrinsic/extensions/index.js +1 -0
- package/cjs/extrinsic/extensions/known/index.js +11 -8
- package/cjs/storage/BaseStorageQuery.js +27 -0
- package/cjs/storage/LegacyStorageQuery.js +69 -0
- package/cjs/storage/NewStorageQuery.js +85 -0
- package/cjs/storage/index.js +3 -0
- package/client/BaseSubstrateClient.d.ts +34 -1
- package/client/BaseSubstrateClient.js +25 -1
- package/client/DedotClient.d.ts +3 -1
- package/client/DedotClient.js +4 -0
- package/client/LegacyClient.d.ts +3 -1
- package/client/LegacyClient.js +4 -0
- package/executor/StorageQueryExecutor.d.ts +3 -2
- package/executor/StorageQueryExecutor.js +7 -14
- package/executor/v2/StorageQueryExecutorV2.d.ts +4 -6
- package/executor/v2/StorageQueryExecutorV2.js +7 -33
- package/extrinsic/extensions/ExtraSignedExtension.d.ts +6 -0
- package/extrinsic/extensions/ExtraSignedExtension.js +25 -6
- package/extrinsic/extensions/FallbackSignedExtension.d.ts +33 -0
- package/extrinsic/extensions/FallbackSignedExtension.js +58 -0
- package/extrinsic/extensions/index.d.ts +1 -0
- package/extrinsic/extensions/index.js +1 -0
- package/extrinsic/extensions/known/index.d.ts +11 -0
- package/extrinsic/extensions/known/index.js +11 -8
- package/package.json +10 -10
- package/storage/BaseStorageQuery.d.ts +41 -0
- package/storage/BaseStorageQuery.js +23 -0
- package/storage/LegacyStorageQuery.d.ts +35 -0
- package/storage/LegacyStorageQuery.js +65 -0
- package/storage/NewStorageQuery.d.ts +35 -0
- package/storage/NewStorageQuery.js +81 -0
- package/storage/index.d.ts +3 -0
- package/storage/index.js +3 -0
- package/types.d.ts +17 -1
- package/cjs/extrinsic/extensions/known/CheckNonZeroSender.js +0 -10
- package/cjs/extrinsic/extensions/known/CheckWeight.js +0 -10
- package/cjs/extrinsic/extensions/known/PrevalidateAttests.js +0 -11
- package/cjs/extrinsic/extensions/known/StorageWeightReclaim.js +0 -10
- package/extrinsic/extensions/known/CheckNonZeroSender.d.ts +0 -6
- package/extrinsic/extensions/known/CheckNonZeroSender.js +0 -6
- package/extrinsic/extensions/known/CheckWeight.d.ts +0 -6
- package/extrinsic/extensions/known/CheckWeight.js +0 -6
- package/extrinsic/extensions/known/PrevalidateAttests.d.ts +0 -7
- package/extrinsic/extensions/known/PrevalidateAttests.js +0 -7
- package/extrinsic/extensions/known/StorageWeightReclaim.d.ts +0 -6
- package/extrinsic/extensions/known/StorageWeightReclaim.js +0 -6
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { BaseStorageQuery } from './BaseStorageQuery.js';
|
|
2
|
+
/**
|
|
3
|
+
* @name NewStorageQuery
|
|
4
|
+
* @description
|
|
5
|
+
* Implementation of BaseStorageQuery for the new JSON-RPC API (v2).
|
|
6
|
+
* This service handles storage queries using the chainHead_storage RPC method
|
|
7
|
+
* and chainHead events for subscriptions.
|
|
8
|
+
*
|
|
9
|
+
* It provides:
|
|
10
|
+
* - One-time queries using chainHead_storage
|
|
11
|
+
* - Subscriptions using chainHead 'bestBlock' events
|
|
12
|
+
* - Efficient change detection for subscriptions
|
|
13
|
+
*/
|
|
14
|
+
export class NewStorageQuery extends BaseStorageQuery {
|
|
15
|
+
/**
|
|
16
|
+
* Query multiple storage items in a single call using chainHead_storage
|
|
17
|
+
*
|
|
18
|
+
* @param keys - Array of storage keys to query
|
|
19
|
+
* @param at - Optional block hash to query at (defaults to best block)
|
|
20
|
+
* @returns Promise resolving to a record mapping storage keys to their values
|
|
21
|
+
*/
|
|
22
|
+
async query(keys, at) {
|
|
23
|
+
// Query storage using ChainHead API
|
|
24
|
+
const storageQueries = keys.map(key => ({ type: 'value', key }));
|
|
25
|
+
// Use the provided block hash or skip it in tests
|
|
26
|
+
const rawResults = at
|
|
27
|
+
? await this.client.chainHead.storage(storageQueries, undefined, at)
|
|
28
|
+
: await this.client.chainHead.storage(storageQueries);
|
|
29
|
+
// Create a map of key -> value for easy lookup
|
|
30
|
+
const results = {};
|
|
31
|
+
// Initialize all keys with undefined
|
|
32
|
+
keys.forEach(key => results[key] = undefined);
|
|
33
|
+
// Update with actual values from the response
|
|
34
|
+
rawResults.forEach((result) => {
|
|
35
|
+
results[result.key] = result.value ?? undefined;
|
|
36
|
+
});
|
|
37
|
+
return results;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Subscribe to multiple storage items using chainHead 'bestBlock' events
|
|
41
|
+
*
|
|
42
|
+
* @param keys - Array of storage keys to subscribe to
|
|
43
|
+
* @param callback - Function to call when storage values change
|
|
44
|
+
* @returns Promise resolving to an unsubscribe function
|
|
45
|
+
*/
|
|
46
|
+
async subscribe(keys, callback) {
|
|
47
|
+
// Get the best block
|
|
48
|
+
const best = await this.client.chainHead.bestBlock();
|
|
49
|
+
// Track the latest changes for each key
|
|
50
|
+
const latestChanges = {};
|
|
51
|
+
// Function to pull storage values and call the callback if there are changes
|
|
52
|
+
const pull = async ({ hash }) => {
|
|
53
|
+
// Query storage using ChainHead API
|
|
54
|
+
const storageQueries = keys.map(key => ({ type: 'value', key }));
|
|
55
|
+
const rawResults = await this.client.chainHead.storage(storageQueries, undefined, hash);
|
|
56
|
+
let changed = false;
|
|
57
|
+
// Create a map for easy lookup
|
|
58
|
+
const results = {};
|
|
59
|
+
rawResults.forEach((result) => {
|
|
60
|
+
results[result.key] = result.value ?? undefined;
|
|
61
|
+
});
|
|
62
|
+
keys.forEach((key) => {
|
|
63
|
+
const newValue = results[key];
|
|
64
|
+
if (Object.keys(latestChanges).length > 0 && latestChanges[key] === newValue)
|
|
65
|
+
return;
|
|
66
|
+
changed = true;
|
|
67
|
+
latestChanges[key] = newValue;
|
|
68
|
+
});
|
|
69
|
+
if (!changed)
|
|
70
|
+
return;
|
|
71
|
+
callback({ ...latestChanges });
|
|
72
|
+
};
|
|
73
|
+
// Initial pull
|
|
74
|
+
await pull(best);
|
|
75
|
+
// Subscribe to best block events
|
|
76
|
+
const unsub = this.client.chainHead.on('bestBlock', pull);
|
|
77
|
+
return async () => {
|
|
78
|
+
unsub();
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
}
|
package/storage/index.d.ts
CHANGED
package/storage/index.js
CHANGED
package/types.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { BlockHash, Hash, Metadata, PortableRegistry } from '@dedot/codecs';
|
|
|
2
2
|
import type { ConnectionStatus, JsonRpcProvider, ProviderEvent } from '@dedot/providers';
|
|
3
3
|
import type { AnyShape } from '@dedot/shape';
|
|
4
4
|
import type { IStorage } from '@dedot/storage';
|
|
5
|
-
import type { GenericSubstrateApi, InjectedSigner, RpcVersion, RuntimeApiName, RuntimeApiSpec, Unsub } from '@dedot/types';
|
|
5
|
+
import type { Callback, GenericStorageQuery, GenericSubstrateApi, InjectedSigner, Query, QueryFnResult, RpcVersion, RuntimeApiName, RuntimeApiSpec, Unsub } from '@dedot/types';
|
|
6
6
|
import type { HashFn, HexString, IEventEmitter } from '@dedot/utils';
|
|
7
7
|
import type { AnySignedExtension } from './extrinsic/index.js';
|
|
8
8
|
export type MetadataKey = `RAW_META/${string}`;
|
|
@@ -119,6 +119,22 @@ export interface ISubstrateClient<ChainApi extends GenericSubstrateApi = Generic
|
|
|
119
119
|
* @param signer
|
|
120
120
|
*/
|
|
121
121
|
setSigner(signer?: InjectedSigner): void;
|
|
122
|
+
/**
|
|
123
|
+
* Perform multiple storage queries in parallel
|
|
124
|
+
* @param queries - An array of query objects
|
|
125
|
+
* @param callback - Optional callback function to handle results
|
|
126
|
+
* @returns A promise resolving to an array of results or an Unsub function
|
|
127
|
+
*/
|
|
128
|
+
queryMulti<Fns extends GenericStorageQuery[]>(queries: {
|
|
129
|
+
[K in keyof Fns]: Query<Fns[K]>;
|
|
130
|
+
}): Promise<{
|
|
131
|
+
[K in keyof Fns]: QueryFnResult<Fns[K]>;
|
|
132
|
+
}>;
|
|
133
|
+
queryMulti<Fns extends GenericStorageQuery[]>(queries: {
|
|
134
|
+
[K in keyof Fns]: Query<Fns[K]>;
|
|
135
|
+
}, callback: Callback<{
|
|
136
|
+
[K in keyof Fns]: QueryFnResult<Fns[K]>;
|
|
137
|
+
}>): Promise<Unsub>;
|
|
122
138
|
}
|
|
123
139
|
/**
|
|
124
140
|
* A generic interface for broadcasting transactions
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CheckNonZeroSender = void 0;
|
|
4
|
-
const SignedExtension_js_1 = require("../SignedExtension.js");
|
|
5
|
-
/**
|
|
6
|
-
* @description Check to ensure that the sender is not the zero address
|
|
7
|
-
*/
|
|
8
|
-
class CheckNonZeroSender extends SignedExtension_js_1.SignedExtension {
|
|
9
|
-
}
|
|
10
|
-
exports.CheckNonZeroSender = CheckNonZeroSender;
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CheckWeight = void 0;
|
|
4
|
-
const SignedExtension_js_1 = require("../SignedExtension.js");
|
|
5
|
-
/**
|
|
6
|
-
* @description Block resource (weight) limit check.
|
|
7
|
-
*/
|
|
8
|
-
class CheckWeight extends SignedExtension_js_1.SignedExtension {
|
|
9
|
-
}
|
|
10
|
-
exports.CheckWeight = CheckWeight;
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.PrevalidateAttests = void 0;
|
|
4
|
-
const SignedExtension_js_1 = require("../SignedExtension.js");
|
|
5
|
-
/**
|
|
6
|
-
* @description Validate `attest` calls prior to execution.
|
|
7
|
-
* Needed to avoid a DoS attack since they are otherwise free to place on chain.
|
|
8
|
-
*/
|
|
9
|
-
class PrevalidateAttests extends SignedExtension_js_1.SignedExtension {
|
|
10
|
-
}
|
|
11
|
-
exports.PrevalidateAttests = PrevalidateAttests;
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.StorageWeightReclaim = void 0;
|
|
4
|
-
const SignedExtension_js_1 = require("../SignedExtension.js");
|
|
5
|
-
/**
|
|
6
|
-
* @description Storage weight reclaim mechanism.
|
|
7
|
-
*/
|
|
8
|
-
class StorageWeightReclaim extends SignedExtension_js_1.SignedExtension {
|
|
9
|
-
}
|
|
10
|
-
exports.StorageWeightReclaim = StorageWeightReclaim;
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { SignedExtension } from '../SignedExtension.js';
|
|
2
|
-
/**
|
|
3
|
-
* @description Validate `attest` calls prior to execution.
|
|
4
|
-
* Needed to avoid a DoS attack since they are otherwise free to place on chain.
|
|
5
|
-
*/
|
|
6
|
-
export declare class PrevalidateAttests extends SignedExtension {
|
|
7
|
-
}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { SignedExtension } from '../SignedExtension.js';
|
|
2
|
-
/**
|
|
3
|
-
* @description Validate `attest` calls prior to execution.
|
|
4
|
-
* Needed to avoid a DoS attack since they are otherwise free to place on chain.
|
|
5
|
-
*/
|
|
6
|
-
export class PrevalidateAttests extends SignedExtension {
|
|
7
|
-
}
|