@polkadot-api/substrate-client 0.0.1-ff4e99b70602ea882d2c55b6adafc6b6ec8a0da2.1.0 → 0.0.1
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/README.md +210 -0
- package/dist/index.d.mts +25 -92
- package/dist/index.d.ts +25 -92
- package/dist/index.js +116 -89
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +115 -88
- package/dist/index.mjs.map +1 -1
- package/dist/min/index.d.ts +25 -92
- package/dist/min/index.js +1 -1
- package/dist/min/index.js.map +1 -1
- package/package.json +4 -5
package/README.md
CHANGED
|
@@ -1 +1,211 @@
|
|
|
1
1
|
# @polkadot-api/substrate-client
|
|
2
|
+
|
|
3
|
+
This TypeScript package provides low-level bindings to the [Substrate JSON-RPC Interface](https://paritytech.github.io/json-rpc-interface-spec/introduction.html), enabling interaction with Substrate-based blockchains.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
Start by creating a `SubstrateClient` object with the exported function `createClient`. To create one, you need a `ConnectProvider` provider defined in [@polkadot-api/json-rpc-provider](https://github.com/polkadot-api/polkadot-api/tree/main/packages/json-rpc-provider) for establishing a connection to a specific blockchain client.
|
|
8
|
+
|
|
9
|
+
For instance, you can use [@polkadot-api/sc-provider](https://github.com/polkadot-api/polkadot-api/tree/main/packages/sc-provider) to get a substrate-connect provider for connecting to the Polkadot relay chain through a light client:
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { getScProvider, WellKnownChain } from "@polkadot-api/sc-provider"
|
|
13
|
+
import { createClient } from "@polkadot-api/substrate-client"
|
|
14
|
+
|
|
15
|
+
const scProvider = getScProvider()
|
|
16
|
+
const { relayChain } = scProvider(WellKnownChain.polkadot)
|
|
17
|
+
|
|
18
|
+
const client = createClient(relayChain)
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
### Request
|
|
22
|
+
|
|
23
|
+
Invoke any method defined in the [JSON-RPC Spec](https://paritytech.github.io/json-rpc-interface-spec/introduction.html) using `client.request(method, params, abortSignal?)`. This returns a promise resolving with the response from the JSON-RPC server.
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
const genesisHash = await client.request("chainSpec_v1_genesisHash", [])
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
All promise-returning functions exported by this package accept an [AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) for operation cancellation.
|
|
30
|
+
|
|
31
|
+
### ChainHead
|
|
32
|
+
|
|
33
|
+
Operations within the [`chainHead` group of functions](https://paritytech.github.io/json-rpc-interface-spec/api/chainHead.html) involve subscriptions and interdependencies between methods. The client has a function that simplifies the interaction with these group.
|
|
34
|
+
|
|
35
|
+
Calling `client.chainHead(withRuntime, onFollowEvent, onFollowError)` will start a `chainHead_unstable_follow` subscription, and will return a handle to perform operations with the chainHead.
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
const chainHead = client.chainHead(
|
|
39
|
+
true,
|
|
40
|
+
(event) => {
|
|
41
|
+
// ...
|
|
42
|
+
},
|
|
43
|
+
(error) => {
|
|
44
|
+
// ...
|
|
45
|
+
},
|
|
46
|
+
)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
The handle provides one method per each of the functions defined inside `chainHead`: `chainHead_unstable_body`, `chainHead_unstable_call`, `chainHead_unstable_header`, `chainHead_unstable_storage`, and `chainHead_unstable_unpin`.
|
|
50
|
+
|
|
51
|
+
The JSON-RPC Spec for chainHead specifies that these functions return an `operationId`, and that the resolved response for the call will come through the `chainHead_unstable_follow` subscription, linking it through this `operationId`.
|
|
52
|
+
|
|
53
|
+
**`substrate-client`'s chainHead is an abstraction over this**: The events emitted through the `client.chainHead()` callback are only the ones initiated from the JSON-RPC Server. The promise returned by any of the `chainHead`'s handle functions will resolve with the respective event.
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
const chainHead = client.chainHead(
|
|
57
|
+
true,
|
|
58
|
+
async (event) => {
|
|
59
|
+
if (event.type === "newBlock") {
|
|
60
|
+
const body = await chainHead.body(event.blockHash)
|
|
61
|
+
// body is a string[] containing the SCALE-encoded values within the body
|
|
62
|
+
processBody(body)
|
|
63
|
+
|
|
64
|
+
chainHead.unpin([event.blockHash])
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
(error) => {
|
|
68
|
+
// ...
|
|
69
|
+
},
|
|
70
|
+
)
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
#### header
|
|
74
|
+
|
|
75
|
+
Calls `chainHead_unstable_call` and returns a promise that resolves with the SCALE-encoded header of the block
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
const header = await chainHead.header(blockHash)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
#### body
|
|
82
|
+
|
|
83
|
+
Calls `chainHead_unstable_body` and returns a promise that will resolve with an array of strings containing the SCALE-encoded extrinsics found in the block
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
const body = await chainHead.body(blockHash)
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
#### call
|
|
90
|
+
|
|
91
|
+
Calls `chainHead_unstable_header` and returns a promise that resolves with the encoded output of the runtime function call
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
const result = await chainHead.call(blockHash, fnName, callParameters)
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
#### storage
|
|
98
|
+
|
|
99
|
+
Calls `chainHead_unstable_storage` and returns a promise that resolves with the value returned by the JSON-RPC server, which depends on the `type` parameter. See the [JSON-RPC spec for chainHead_unstable_storage](https://paritytech.github.io/json-rpc-interface-spec/api/chainHead_unstable_storage.html) for the details on the usage.
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
// string with the SCALE-encoded value
|
|
103
|
+
const value = await chainHead.storage(blockHash, "value", key, childTrie)
|
|
104
|
+
|
|
105
|
+
// string with the hash value
|
|
106
|
+
const hash = await chainHead.storage(blockHash, "hash", key, childTrie)
|
|
107
|
+
|
|
108
|
+
// string with the merkle value
|
|
109
|
+
const items = await chainHead.storage(
|
|
110
|
+
blockHash,
|
|
111
|
+
"closestDescendantMerkleValue",
|
|
112
|
+
key,
|
|
113
|
+
childTrie,
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
// array of key-value pairs
|
|
117
|
+
const items = await chainHead.storage(
|
|
118
|
+
blockHash,
|
|
119
|
+
"descendantsValues",
|
|
120
|
+
key,
|
|
121
|
+
childTrie,
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
// array of key-hash pairs
|
|
125
|
+
const hashes = await chainHead.storage(
|
|
126
|
+
blockHash,
|
|
127
|
+
"descendantsHashes",
|
|
128
|
+
key,
|
|
129
|
+
childTrie,
|
|
130
|
+
)
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
#### storageSubscription
|
|
134
|
+
|
|
135
|
+
While `storage` only can resolve for one specific item, the JSON-RPC specification allows to resolve multiple items within the same call. For this case, substrate-client also offers a lower-level version called `chainHead.storageSubscription(hash, inputs, childTrie, onItems, onError, onDone, onDiscardedItems)` that emits the storage items as they get resolved by the JSON-RPC server:
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
const abort = chainHead.storageSubscription(
|
|
139
|
+
hash,
|
|
140
|
+
[
|
|
141
|
+
{ key, type },
|
|
142
|
+
/* ... each item */
|
|
143
|
+
],
|
|
144
|
+
null,
|
|
145
|
+
(items) => {
|
|
146
|
+
// items is an array of { key, value?, hash?, closestDescendantMerkleValue? }
|
|
147
|
+
},
|
|
148
|
+
onError,
|
|
149
|
+
onDone,
|
|
150
|
+
(nDiscardedItems) => {
|
|
151
|
+
// amount of discarded items, as defined by the JSON-RPC spec.
|
|
152
|
+
},
|
|
153
|
+
)
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
`storageSubscription` returns a function to cancel the operation.
|
|
157
|
+
|
|
158
|
+
#### unpin
|
|
159
|
+
|
|
160
|
+
Calls `chainHead_unstable_unpin` and returns a promise that will resolve after the operation is done.
|
|
161
|
+
|
|
162
|
+
```ts
|
|
163
|
+
chainHead.unpin(blockHashes)
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
#### unfollow
|
|
167
|
+
|
|
168
|
+
To close the chainHead subscription, call `chainHead.unfollow()`.
|
|
169
|
+
|
|
170
|
+
### Transaction
|
|
171
|
+
|
|
172
|
+
[`transaction` group of functions](https://paritytech.github.io/json-rpc-interface-spec/api/transaction.html) also deals with subscriptions through `submitAndWatch`. SubstrateClient also abstracts over this:
|
|
173
|
+
|
|
174
|
+
```ts
|
|
175
|
+
const cancelRequest = client.transaction(
|
|
176
|
+
transaction, // SCALE-encoded transaction
|
|
177
|
+
(event) => {
|
|
178
|
+
// ...
|
|
179
|
+
},
|
|
180
|
+
(error) => {
|
|
181
|
+
// ...
|
|
182
|
+
},
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
// call `cancelRequest()` to abort the transaction (`transaction_unstable_stop`)
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
The `event` emitted through the callback are fully typed, and can be discriminated through `event.type`
|
|
189
|
+
|
|
190
|
+
```ts
|
|
191
|
+
switch (event.type) {
|
|
192
|
+
case "validated":
|
|
193
|
+
break
|
|
194
|
+
case "broadcasted":
|
|
195
|
+
const { numPeers } = event
|
|
196
|
+
break
|
|
197
|
+
case "bestChainBlockIncluded":
|
|
198
|
+
case "finalized":
|
|
199
|
+
const { block } = event
|
|
200
|
+
break
|
|
201
|
+
case "dropped":
|
|
202
|
+
case "error":
|
|
203
|
+
case "invalid":
|
|
204
|
+
const { error } = event
|
|
205
|
+
break
|
|
206
|
+
}
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
### Destroy
|
|
210
|
+
|
|
211
|
+
Call `client.destroy()` to disconnect from the provider.
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export { ConnectProvider, Provider } from '@polkadot-api/json-rpc-provider';
|
|
1
|
+
import { JsonRpcProvider } from '@polkadot-api/json-rpc-provider';
|
|
3
2
|
|
|
4
3
|
interface IRpcError {
|
|
5
4
|
code: number;
|
|
@@ -24,7 +23,7 @@ interface Subscriber<T> {
|
|
|
24
23
|
error: (e: Error) => void;
|
|
25
24
|
}
|
|
26
25
|
|
|
27
|
-
type FollowSubscriptionCb<T> = (subscriptionId: string, cb: Subscriber<T>) => UnsubscribeFn;
|
|
26
|
+
type FollowSubscriptionCb<T> = (methodName: string, subscriptionId: string, cb: Subscriber<T>) => UnsubscribeFn;
|
|
28
27
|
type ClientRequestCb<T, TT> = {
|
|
29
28
|
onSuccess: (result: T, followSubscription: FollowSubscriptionCb<TT>) => void;
|
|
30
29
|
onError: (e: Error) => void;
|
|
@@ -35,6 +34,16 @@ interface Client {
|
|
|
35
34
|
request: ClientRequest<any, any>;
|
|
36
35
|
}
|
|
37
36
|
|
|
37
|
+
declare class DestroyedError extends Error {
|
|
38
|
+
constructor();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
type FollowInnerSubscriptionCb<T> = (subscriptionId: string, cb: Subscriber<T>) => UnsubscribeFn;
|
|
42
|
+
type ClientInnerRequestCb<T, TT> = {
|
|
43
|
+
onSuccess: (result: T, followSubscription: FollowInnerSubscriptionCb<TT>) => void;
|
|
44
|
+
onError: (e: Error) => void;
|
|
45
|
+
};
|
|
46
|
+
type ClientInnerRequest<T, TT> = (method: string, params: Array<any>, cb?: ClientInnerRequestCb<T, TT>) => UnsubscribeFn;
|
|
38
47
|
interface StorageItemInput {
|
|
39
48
|
key: string;
|
|
40
49
|
type: "value" | "hash" | "closestDescendantMerkleValue" | "descendantsValues" | "descendantsHashes";
|
|
@@ -55,7 +64,7 @@ interface Runtime {
|
|
|
55
64
|
}
|
|
56
65
|
interface Initialized {
|
|
57
66
|
type: "initialized";
|
|
58
|
-
|
|
67
|
+
finalizedBlockHashes: string[];
|
|
59
68
|
}
|
|
60
69
|
type InitializedWithRuntime$1 = Initialized & {
|
|
61
70
|
finalizedBlockRuntime: Runtime;
|
|
@@ -99,11 +108,12 @@ interface FollowResponse {
|
|
|
99
108
|
storageSubscription: (hash: string, inputs: Array<StorageItemInput>, childTrie: string | null, onItems: (items: Array<StorageItemResponse>) => void, onError: (e: Error) => void, onDone: () => void, onDiscardedItems: (nDiscarded: number) => void) => () => void;
|
|
100
109
|
header: (hash: string) => Promise<string>;
|
|
101
110
|
unpin: (hashes: Array<string>) => Promise<void>;
|
|
102
|
-
_request: <Reply, Notification>(method: string, params: any[], cb?:
|
|
111
|
+
_request: <Reply, Notification>(method: string, params: any[], cb?: ClientInnerRequestCb<Reply, Notification>) => UnsubscribeFn;
|
|
103
112
|
}
|
|
104
113
|
interface ChainHead {
|
|
105
114
|
(withRuntime: false, cb: (event: FollowEventWithoutRuntime) => void, onError: (error: Error) => void): FollowResponse;
|
|
106
115
|
(withRuntime: true, cb: (event: FollowEventWithRuntime) => void, onError: (error: Error) => void): FollowResponse;
|
|
116
|
+
(withRuntime: boolean, cb: (event: FollowEventWithoutRuntime | FollowEventWithRuntime) => void, onError: (error: Error) => void): FollowResponse;
|
|
107
117
|
}
|
|
108
118
|
|
|
109
119
|
declare class StopError extends Error {
|
|
@@ -130,10 +140,13 @@ interface RuntimeRpc {
|
|
|
130
140
|
transactionVersion: number;
|
|
131
141
|
apis: Record<string, number>;
|
|
132
142
|
}
|
|
133
|
-
|
|
143
|
+
type InitializedRpc = {
|
|
134
144
|
event: "initialized";
|
|
135
145
|
finalizedBlockHash: string;
|
|
136
|
-
}
|
|
146
|
+
} | {
|
|
147
|
+
event: "initialized";
|
|
148
|
+
finalizedBlockHashes: string[];
|
|
149
|
+
};
|
|
137
150
|
type InitializedWithRuntime = InitializedRpc & {
|
|
138
151
|
finalizedBlockRuntime: RuntimeRpc;
|
|
139
152
|
};
|
|
@@ -200,97 +213,17 @@ type OperationEventsRpc = OperationBodyDoneRpc | OperationCallDoneRpc | Operatio
|
|
|
200
213
|
type FollowEventRpc = FollowEventWithRuntimeRpc | FollowEventWithoutRuntimeRpc | OperationEventsRpc | StopRpc;
|
|
201
214
|
declare function getChainHead(request: ClientRequest<string, FollowEventRpc>): ChainHead;
|
|
202
215
|
|
|
203
|
-
|
|
204
|
-
type: "validated";
|
|
205
|
-
}
|
|
206
|
-
interface TxBroadcasted {
|
|
207
|
-
type: "broadcasted";
|
|
208
|
-
numPeers: number;
|
|
209
|
-
}
|
|
210
|
-
interface TxBestChainBlockIncluded {
|
|
211
|
-
type: "bestChainBlockIncluded";
|
|
212
|
-
block: {
|
|
213
|
-
hash: string;
|
|
214
|
-
index: number;
|
|
215
|
-
} | null;
|
|
216
|
-
}
|
|
217
|
-
interface TxFinalized {
|
|
218
|
-
type: "finalized";
|
|
219
|
-
block: {
|
|
220
|
-
hash: string;
|
|
221
|
-
index: number;
|
|
222
|
-
};
|
|
223
|
-
}
|
|
224
|
-
interface TxInvalid {
|
|
225
|
-
type: "invalid";
|
|
226
|
-
error: string;
|
|
227
|
-
}
|
|
228
|
-
interface TxDropped {
|
|
229
|
-
type: "dropped";
|
|
230
|
-
broadcasted: boolean;
|
|
231
|
-
error: string;
|
|
232
|
-
}
|
|
233
|
-
interface TxError {
|
|
234
|
-
type: "error";
|
|
235
|
-
error: string;
|
|
236
|
-
}
|
|
237
|
-
type TxEvent = TxValidated | TxBroadcasted | TxBestChainBlockIncluded | TxFinalized | TxInvalid | TxDropped | TxError;
|
|
238
|
-
type Transaction = (tx: string, next: (event: TxEvent) => void, error: (e: Error) => void) => UnsubscribeFn;
|
|
239
|
-
|
|
240
|
-
interface TxValidatedRpc {
|
|
241
|
-
event: "validated";
|
|
242
|
-
}
|
|
243
|
-
interface TxBroadcastedRpc {
|
|
244
|
-
event: "broadcasted";
|
|
245
|
-
numPeers: number;
|
|
246
|
-
}
|
|
247
|
-
interface TxBestChainBlockIncludedRpc {
|
|
248
|
-
event: "bestChainBlockIncluded";
|
|
249
|
-
block: {
|
|
250
|
-
hash: string;
|
|
251
|
-
index: number;
|
|
252
|
-
} | null;
|
|
253
|
-
}
|
|
254
|
-
interface TxFinalizedRpc {
|
|
255
|
-
event: "finalized";
|
|
256
|
-
block: {
|
|
257
|
-
hash: string;
|
|
258
|
-
index: number;
|
|
259
|
-
};
|
|
260
|
-
}
|
|
261
|
-
interface TxInvalidRpc {
|
|
262
|
-
event: "invalid";
|
|
263
|
-
error: string;
|
|
264
|
-
}
|
|
265
|
-
interface TxDroppedRpc {
|
|
266
|
-
event: "dropped";
|
|
267
|
-
broadcasted: boolean;
|
|
268
|
-
error: string;
|
|
269
|
-
}
|
|
270
|
-
interface TxErrorRpc {
|
|
271
|
-
event: "error";
|
|
272
|
-
error: string;
|
|
273
|
-
}
|
|
274
|
-
type TxEventRpc = TxValidatedRpc | TxBroadcastedRpc | TxBestChainBlockIncludedRpc | TxFinalizedRpc | TxInvalidRpc | TxDroppedRpc | TxErrorRpc;
|
|
216
|
+
type Transaction = (tx: string, error: (e: Error) => void) => UnsubscribeFn;
|
|
275
217
|
|
|
276
|
-
|
|
277
|
-
interface ITxError {
|
|
278
|
-
type: ErrorEvents["event"];
|
|
279
|
-
error: string;
|
|
280
|
-
}
|
|
281
|
-
declare class TransactionError extends Error implements ITxError {
|
|
282
|
-
type: "error" | "invalid" | "dropped";
|
|
283
|
-
error: string;
|
|
284
|
-
constructor(e: ErrorEvents);
|
|
285
|
-
}
|
|
286
|
-
declare const getTransaction: (request: ClientRequest<string, TxEventRpc>) => Transaction;
|
|
218
|
+
declare const getTransaction: (request: ClientRequest<string, any>, rpcMethods: Promise<Set<string>> | Set<string>) => (tx: string, error: (e: Error) => void) => () => void;
|
|
287
219
|
|
|
288
220
|
interface SubstrateClient {
|
|
289
221
|
chainHead: ChainHead;
|
|
290
222
|
transaction: Transaction;
|
|
291
223
|
destroy: UnsubscribeFn;
|
|
224
|
+
request: <T>(method: string, params: any[], abortSignal?: AbortSignal) => Promise<T>;
|
|
292
225
|
_request: <Reply, Notification>(method: string, params: any[], cb?: ClientRequestCb<Reply, Notification>) => UnsubscribeFn;
|
|
293
226
|
}
|
|
294
|
-
declare const createClient: (provider:
|
|
227
|
+
declare const createClient: (provider: JsonRpcProvider) => SubstrateClient;
|
|
295
228
|
|
|
296
|
-
export { AbortablePromiseFn, BestBlockChanged, ChainHead, Client, ClientRequest, ClientRequestCb, DisjointError, Finalized, FollowEventWithRuntime, FollowEventWithoutRuntime, FollowResponse, FollowSubscriptionCb, IRpcError, Initialized, InitializedWithRuntime$1 as InitializedWithRuntime, NewBlock, NewBlockWithRuntime$1 as NewBlockWithRuntime, OperationError, OperationInaccessibleError, OperationLimitError, RpcError, Runtime, StopError, StorageItemInput, StorageItemResponse, StorageResult, SubstrateClient, Transaction,
|
|
229
|
+
export { type AbortablePromiseFn, type BestBlockChanged, type ChainHead, type Client, type ClientInnerRequest, type ClientInnerRequestCb, type ClientRequest, type ClientRequestCb, DestroyedError, DisjointError, type Finalized, type FollowEventWithRuntime, type FollowEventWithoutRuntime, type FollowInnerSubscriptionCb, type FollowResponse, type FollowSubscriptionCb, type IRpcError, type Initialized, type InitializedWithRuntime$1 as InitializedWithRuntime, type NewBlock, type NewBlockWithRuntime$1 as NewBlockWithRuntime, OperationError, OperationInaccessibleError, OperationLimitError, RpcError, type Runtime, StopError, type StorageItemInput, type StorageItemResponse, type StorageResult, type SubstrateClient, type Transaction, type UnsubscribeFn, createClient, getChainHead, getTransaction };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export { ConnectProvider, Provider } from '@polkadot-api/json-rpc-provider';
|
|
1
|
+
import { JsonRpcProvider } from '@polkadot-api/json-rpc-provider';
|
|
3
2
|
|
|
4
3
|
interface IRpcError {
|
|
5
4
|
code: number;
|
|
@@ -24,7 +23,7 @@ interface Subscriber<T> {
|
|
|
24
23
|
error: (e: Error) => void;
|
|
25
24
|
}
|
|
26
25
|
|
|
27
|
-
type FollowSubscriptionCb<T> = (subscriptionId: string, cb: Subscriber<T>) => UnsubscribeFn;
|
|
26
|
+
type FollowSubscriptionCb<T> = (methodName: string, subscriptionId: string, cb: Subscriber<T>) => UnsubscribeFn;
|
|
28
27
|
type ClientRequestCb<T, TT> = {
|
|
29
28
|
onSuccess: (result: T, followSubscription: FollowSubscriptionCb<TT>) => void;
|
|
30
29
|
onError: (e: Error) => void;
|
|
@@ -35,6 +34,16 @@ interface Client {
|
|
|
35
34
|
request: ClientRequest<any, any>;
|
|
36
35
|
}
|
|
37
36
|
|
|
37
|
+
declare class DestroyedError extends Error {
|
|
38
|
+
constructor();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
type FollowInnerSubscriptionCb<T> = (subscriptionId: string, cb: Subscriber<T>) => UnsubscribeFn;
|
|
42
|
+
type ClientInnerRequestCb<T, TT> = {
|
|
43
|
+
onSuccess: (result: T, followSubscription: FollowInnerSubscriptionCb<TT>) => void;
|
|
44
|
+
onError: (e: Error) => void;
|
|
45
|
+
};
|
|
46
|
+
type ClientInnerRequest<T, TT> = (method: string, params: Array<any>, cb?: ClientInnerRequestCb<T, TT>) => UnsubscribeFn;
|
|
38
47
|
interface StorageItemInput {
|
|
39
48
|
key: string;
|
|
40
49
|
type: "value" | "hash" | "closestDescendantMerkleValue" | "descendantsValues" | "descendantsHashes";
|
|
@@ -55,7 +64,7 @@ interface Runtime {
|
|
|
55
64
|
}
|
|
56
65
|
interface Initialized {
|
|
57
66
|
type: "initialized";
|
|
58
|
-
|
|
67
|
+
finalizedBlockHashes: string[];
|
|
59
68
|
}
|
|
60
69
|
type InitializedWithRuntime$1 = Initialized & {
|
|
61
70
|
finalizedBlockRuntime: Runtime;
|
|
@@ -99,11 +108,12 @@ interface FollowResponse {
|
|
|
99
108
|
storageSubscription: (hash: string, inputs: Array<StorageItemInput>, childTrie: string | null, onItems: (items: Array<StorageItemResponse>) => void, onError: (e: Error) => void, onDone: () => void, onDiscardedItems: (nDiscarded: number) => void) => () => void;
|
|
100
109
|
header: (hash: string) => Promise<string>;
|
|
101
110
|
unpin: (hashes: Array<string>) => Promise<void>;
|
|
102
|
-
_request: <Reply, Notification>(method: string, params: any[], cb?:
|
|
111
|
+
_request: <Reply, Notification>(method: string, params: any[], cb?: ClientInnerRequestCb<Reply, Notification>) => UnsubscribeFn;
|
|
103
112
|
}
|
|
104
113
|
interface ChainHead {
|
|
105
114
|
(withRuntime: false, cb: (event: FollowEventWithoutRuntime) => void, onError: (error: Error) => void): FollowResponse;
|
|
106
115
|
(withRuntime: true, cb: (event: FollowEventWithRuntime) => void, onError: (error: Error) => void): FollowResponse;
|
|
116
|
+
(withRuntime: boolean, cb: (event: FollowEventWithoutRuntime | FollowEventWithRuntime) => void, onError: (error: Error) => void): FollowResponse;
|
|
107
117
|
}
|
|
108
118
|
|
|
109
119
|
declare class StopError extends Error {
|
|
@@ -130,10 +140,13 @@ interface RuntimeRpc {
|
|
|
130
140
|
transactionVersion: number;
|
|
131
141
|
apis: Record<string, number>;
|
|
132
142
|
}
|
|
133
|
-
|
|
143
|
+
type InitializedRpc = {
|
|
134
144
|
event: "initialized";
|
|
135
145
|
finalizedBlockHash: string;
|
|
136
|
-
}
|
|
146
|
+
} | {
|
|
147
|
+
event: "initialized";
|
|
148
|
+
finalizedBlockHashes: string[];
|
|
149
|
+
};
|
|
137
150
|
type InitializedWithRuntime = InitializedRpc & {
|
|
138
151
|
finalizedBlockRuntime: RuntimeRpc;
|
|
139
152
|
};
|
|
@@ -200,97 +213,17 @@ type OperationEventsRpc = OperationBodyDoneRpc | OperationCallDoneRpc | Operatio
|
|
|
200
213
|
type FollowEventRpc = FollowEventWithRuntimeRpc | FollowEventWithoutRuntimeRpc | OperationEventsRpc | StopRpc;
|
|
201
214
|
declare function getChainHead(request: ClientRequest<string, FollowEventRpc>): ChainHead;
|
|
202
215
|
|
|
203
|
-
|
|
204
|
-
type: "validated";
|
|
205
|
-
}
|
|
206
|
-
interface TxBroadcasted {
|
|
207
|
-
type: "broadcasted";
|
|
208
|
-
numPeers: number;
|
|
209
|
-
}
|
|
210
|
-
interface TxBestChainBlockIncluded {
|
|
211
|
-
type: "bestChainBlockIncluded";
|
|
212
|
-
block: {
|
|
213
|
-
hash: string;
|
|
214
|
-
index: number;
|
|
215
|
-
} | null;
|
|
216
|
-
}
|
|
217
|
-
interface TxFinalized {
|
|
218
|
-
type: "finalized";
|
|
219
|
-
block: {
|
|
220
|
-
hash: string;
|
|
221
|
-
index: number;
|
|
222
|
-
};
|
|
223
|
-
}
|
|
224
|
-
interface TxInvalid {
|
|
225
|
-
type: "invalid";
|
|
226
|
-
error: string;
|
|
227
|
-
}
|
|
228
|
-
interface TxDropped {
|
|
229
|
-
type: "dropped";
|
|
230
|
-
broadcasted: boolean;
|
|
231
|
-
error: string;
|
|
232
|
-
}
|
|
233
|
-
interface TxError {
|
|
234
|
-
type: "error";
|
|
235
|
-
error: string;
|
|
236
|
-
}
|
|
237
|
-
type TxEvent = TxValidated | TxBroadcasted | TxBestChainBlockIncluded | TxFinalized | TxInvalid | TxDropped | TxError;
|
|
238
|
-
type Transaction = (tx: string, next: (event: TxEvent) => void, error: (e: Error) => void) => UnsubscribeFn;
|
|
239
|
-
|
|
240
|
-
interface TxValidatedRpc {
|
|
241
|
-
event: "validated";
|
|
242
|
-
}
|
|
243
|
-
interface TxBroadcastedRpc {
|
|
244
|
-
event: "broadcasted";
|
|
245
|
-
numPeers: number;
|
|
246
|
-
}
|
|
247
|
-
interface TxBestChainBlockIncludedRpc {
|
|
248
|
-
event: "bestChainBlockIncluded";
|
|
249
|
-
block: {
|
|
250
|
-
hash: string;
|
|
251
|
-
index: number;
|
|
252
|
-
} | null;
|
|
253
|
-
}
|
|
254
|
-
interface TxFinalizedRpc {
|
|
255
|
-
event: "finalized";
|
|
256
|
-
block: {
|
|
257
|
-
hash: string;
|
|
258
|
-
index: number;
|
|
259
|
-
};
|
|
260
|
-
}
|
|
261
|
-
interface TxInvalidRpc {
|
|
262
|
-
event: "invalid";
|
|
263
|
-
error: string;
|
|
264
|
-
}
|
|
265
|
-
interface TxDroppedRpc {
|
|
266
|
-
event: "dropped";
|
|
267
|
-
broadcasted: boolean;
|
|
268
|
-
error: string;
|
|
269
|
-
}
|
|
270
|
-
interface TxErrorRpc {
|
|
271
|
-
event: "error";
|
|
272
|
-
error: string;
|
|
273
|
-
}
|
|
274
|
-
type TxEventRpc = TxValidatedRpc | TxBroadcastedRpc | TxBestChainBlockIncludedRpc | TxFinalizedRpc | TxInvalidRpc | TxDroppedRpc | TxErrorRpc;
|
|
216
|
+
type Transaction = (tx: string, error: (e: Error) => void) => UnsubscribeFn;
|
|
275
217
|
|
|
276
|
-
|
|
277
|
-
interface ITxError {
|
|
278
|
-
type: ErrorEvents["event"];
|
|
279
|
-
error: string;
|
|
280
|
-
}
|
|
281
|
-
declare class TransactionError extends Error implements ITxError {
|
|
282
|
-
type: "error" | "invalid" | "dropped";
|
|
283
|
-
error: string;
|
|
284
|
-
constructor(e: ErrorEvents);
|
|
285
|
-
}
|
|
286
|
-
declare const getTransaction: (request: ClientRequest<string, TxEventRpc>) => Transaction;
|
|
218
|
+
declare const getTransaction: (request: ClientRequest<string, any>, rpcMethods: Promise<Set<string>> | Set<string>) => (tx: string, error: (e: Error) => void) => () => void;
|
|
287
219
|
|
|
288
220
|
interface SubstrateClient {
|
|
289
221
|
chainHead: ChainHead;
|
|
290
222
|
transaction: Transaction;
|
|
291
223
|
destroy: UnsubscribeFn;
|
|
224
|
+
request: <T>(method: string, params: any[], abortSignal?: AbortSignal) => Promise<T>;
|
|
292
225
|
_request: <Reply, Notification>(method: string, params: any[], cb?: ClientRequestCb<Reply, Notification>) => UnsubscribeFn;
|
|
293
226
|
}
|
|
294
|
-
declare const createClient: (provider:
|
|
227
|
+
declare const createClient: (provider: JsonRpcProvider) => SubstrateClient;
|
|
295
228
|
|
|
296
|
-
export { AbortablePromiseFn, BestBlockChanged, ChainHead, Client, ClientRequest, ClientRequestCb, DisjointError, Finalized, FollowEventWithRuntime, FollowEventWithoutRuntime, FollowResponse, FollowSubscriptionCb, IRpcError, Initialized, InitializedWithRuntime$1 as InitializedWithRuntime, NewBlock, NewBlockWithRuntime$1 as NewBlockWithRuntime, OperationError, OperationInaccessibleError, OperationLimitError, RpcError, Runtime, StopError, StorageItemInput, StorageItemResponse, StorageResult, SubstrateClient, Transaction,
|
|
229
|
+
export { type AbortablePromiseFn, type BestBlockChanged, type ChainHead, type Client, type ClientInnerRequest, type ClientInnerRequestCb, type ClientRequest, type ClientRequestCb, DestroyedError, DisjointError, type Finalized, type FollowEventWithRuntime, type FollowEventWithoutRuntime, type FollowInnerSubscriptionCb, type FollowResponse, type FollowSubscriptionCb, type IRpcError, type Initialized, type InitializedWithRuntime$1 as InitializedWithRuntime, type NewBlock, type NewBlockWithRuntime$1 as NewBlockWithRuntime, OperationError, OperationInaccessibleError, OperationLimitError, RpcError, type Runtime, StopError, type StorageItemInput, type StorageItemResponse, type StorageResult, type SubstrateClient, type Transaction, type UnsubscribeFn, createClient, getChainHead, getTransaction };
|