@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,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,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
package/index.js
ADDED
|
@@ -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
|
+
}
|