polkadot-api 0.7.2 → 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/dist/index.d.mts CHANGED
@@ -4,6 +4,8 @@ import { HexString, Binary, SS58String, Enum, BlockHeader } from '@polkadot-api/
4
4
  export { AccountId, Binary, Codec, Enum, EnumVariant, FixedSizeBinary, GetEnum, HexString, ResultPayload, SS58String, _Enum } from '@polkadot-api/substrate-bindings';
5
5
  import { ChainSpecData } from '@polkadot-api/substrate-client';
6
6
  import { Observable } from 'rxjs';
7
+ import { DescriptorValues } from '@polkadot-api/codegen';
8
+ export { DescriptorValues } from '@polkadot-api/codegen';
7
9
  import { PolkadotSigner } from '@polkadot-api/polkadot-signer';
8
10
  export { PolkadotSigner } from '@polkadot-api/polkadot-signer';
9
11
 
@@ -23,13 +25,6 @@ type TxDescriptor<Args extends {} | undefined> = {
23
25
  };
24
26
  type RuntimeDescriptor<Args extends Array<any>, T> = [Args, T];
25
27
  type DescriptorEntry<T> = Record<string, Record<string, T>>;
26
- type PalletDescriptors = Record<string, [
27
- Record<string, number>,
28
- Record<string, number>,
29
- Record<string, number>,
30
- Record<string, number>,
31
- Record<string, number>
32
- ]>;
33
28
  type PalletsTypedef<St extends DescriptorEntry<StorageDescriptor<any, any, any>>, Tx extends DescriptorEntry<TxDescriptor<any>>, Ev extends DescriptorEntry<PlainDescriptor<any>>, Err extends DescriptorEntry<PlainDescriptor<any>>, Ct extends DescriptorEntry<PlainDescriptor<any>>> = {
34
29
  __storage: St;
35
30
  __tx: Tx;
@@ -37,14 +32,10 @@ type PalletsTypedef<St extends DescriptorEntry<StorageDescriptor<any, any, any>>
37
32
  __error: Err;
38
33
  __const: Ct;
39
34
  };
40
- type ApisDescriptors = DescriptorEntry<number>;
41
35
  type ApisTypedef<T extends DescriptorEntry<RuntimeDescriptor<any, any>>> = T;
42
- type DescriptorsValue = {
43
- pallets: PalletDescriptors;
44
- apis: ApisDescriptors;
45
- };
36
+
46
37
  type ChainDefinition = {
47
- descriptors: Promise<DescriptorsValue> & {
38
+ descriptors: Promise<DescriptorValues> & {
48
39
  pallets: PalletsTypedef<any, any, any, any, any>;
49
40
  apis: ApisTypedef<any>;
50
41
  };
@@ -77,11 +68,11 @@ type ErrorsFromPalletsDef<T extends PalletsTypedef<any, any, any, any, any>> = E
77
68
  type ConstFromPalletsDef<T extends PalletsTypedef<any, any, any, any, any>> = ExtractPlain<T["__const"]>;
78
69
 
79
70
  declare const enum OpType {
80
- Storage = 0,
81
- Tx = 1,
82
- Event = 2,
83
- Error = 3,
84
- Const = 4
71
+ Storage = "storage",
72
+ Tx = "tx",
73
+ Event = "events",
74
+ Error = "errors",
75
+ Const = "constants"
85
76
  }
86
77
  declare class Runtime {
87
78
  private _ctx;
@@ -91,7 +82,7 @@ declare class Runtime {
91
82
  /**
92
83
  * @access package - Internal implementation detail. Do not use.
93
84
  */
94
- static _create(ctx: RuntimeContext, checksums: string[], descriptors: DescriptorsValue): Runtime;
85
+ static _create(ctx: RuntimeContext, checksums: string[], descriptors: DescriptorValues): Runtime;
95
86
  /**
96
87
  * @access package - Internal implementation detail. Do not use.
97
88
  */
@@ -106,10 +97,28 @@ declare class Runtime {
106
97
  _getApiChecksum(name: string, method: string): string;
107
98
  }
108
99
  type RuntimeApi = Observable<Runtime> & {
100
+ /**
101
+ * @returns Promise that resolves in the `Runtime` as soon as it's
102
+ * loaded.
103
+ */
109
104
  latest: () => Promise<Runtime>;
110
105
  };
111
106
  interface IsCompatible {
107
+ /**
108
+ * `isCompatible` enables you to check whether or not the call you're trying
109
+ * to make is compatible with the descriptors you generated on dev time.
110
+ * In this case the function waits for `Runtime` to load, and returns
111
+ * asynchronously.
112
+ *
113
+ * @returns Promise that resolves with the result of the compatibility
114
+ * check.
115
+ */
112
116
  (): Promise<boolean>;
117
+ /**
118
+ * Passing the runtime makes the function to return synchronously.
119
+ *
120
+ * @returns Result of the compatibility check.
121
+ */
113
122
  (runtime: Runtime): boolean;
114
123
  }
115
124
  declare const compatibilityHelper: (runtimeApi: RuntimeApi, getDescriptorChecksum: (runtime: Runtime) => string) => (getChecksum: (ctx: RuntimeContext) => string | null) => {
@@ -121,8 +130,21 @@ declare const compatibilityHelper: (runtimeApi: RuntimeApi, getDescriptorChecksu
121
130
  type CompatibilityHelper = ReturnType<typeof compatibilityHelper>;
122
131
 
123
132
  interface ConstantEntry<T> {
133
+ /**
134
+ * Constants are simple key-value structures found in the runtime metadata.
135
+ *
136
+ * @returns Promise that will resolve in the value of the constant.
137
+ */
124
138
  (): Promise<T>;
139
+ /**
140
+ * @param runtime Runtime from got with `typedApi.runtime`
141
+ * @returns Synchronously returns value of the constant.
142
+ */
125
143
  (runtime: Runtime): T;
144
+ /**
145
+ * `isCompatible` enables you to check whether or not the call you're trying
146
+ * to make is compatible with the descriptors you generated on dev time.
147
+ */
126
148
  isCompatible: IsCompatible;
127
149
  }
128
150
 
@@ -150,9 +172,30 @@ type EvPull<T> = () => Promise<Array<{
150
172
  }>>;
151
173
  type EvFilter<T> = (collection: SystemEvent["event"][]) => Array<T>;
152
174
  type EvClient<T> = {
175
+ /**
176
+ * Multicast and stateful Observable watching for new events (matching the
177
+ * event kind chosen) in the latest known `finalized` block.
178
+ *
179
+ * @param filter Optional filter function to only emit events complying
180
+ * with the function.
181
+ */
153
182
  watch: EvWatch<T>;
183
+ /**
184
+ * Fetch (Promise-based) all events (matching the event kind chosen) available
185
+ * in the latest known `finalized` block.
186
+ */
154
187
  pull: EvPull<T>;
188
+ /**
189
+ * Filter a bunch of `SystemEvent` and return the decoded `payload` of every
190
+ * of them.
191
+ *
192
+ * @param collection Array of `SystemEvent` to filter.
193
+ */
155
194
  filter: EvFilter<T>;
195
+ /**
196
+ * `isCompatible` enables you to check whether or not the call you're trying
197
+ * to make is compatible with the descriptors you generated on dev time.
198
+ */
156
199
  isCompatible: IsCompatible;
157
200
  };
158
201
  type SystemEvent = {
@@ -173,12 +216,31 @@ type CallOptions$1 = Partial<{
173
216
  }>;
174
217
  type WithCallOptions$1<Args extends Array<any>> = Args["length"] extends 0 ? [options?: CallOptions$1] : [...args: Args, options?: CallOptions$1];
175
218
  interface RuntimeCall<Args extends Array<any>, Payload> {
219
+ /**
220
+ * Get `Payload` (Promise-based) for the runtime call.
221
+ *
222
+ * @param args All keys needed for that runtime call.
223
+ * At the end, optionally set which block to target (latest
224
+ * known finalized is the default) and an AbortSignal.
225
+ */
176
226
  (...args: WithCallOptions$1<Args>): Promise<Payload>;
227
+ /**
228
+ * `isCompatible` enables you to check whether or not the call you're trying
229
+ * to make is compatible with the descriptors you generated on dev time.
230
+ */
177
231
  isCompatible: IsCompatible;
178
232
  }
179
233
 
180
234
  type CallOptions = Partial<{
235
+ /**
236
+ * `at` could be a blockHash, `best`, or `finalized` (default)
237
+ */
181
238
  at: string;
239
+ /**
240
+ * `signal` allows you to abort an ongoing Promise. See [MDN
241
+ * docs](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) for
242
+ * more information
243
+ */
182
244
  signal: AbortSignal;
183
245
  }>;
184
246
  type WithCallOptions<Args extends Array<any>> = [
@@ -187,15 +249,73 @@ type WithCallOptions<Args extends Array<any>> = [
187
249
  ];
188
250
  type PossibleParents<A extends Array<any>> = A extends [...infer Left, any] ? Left | PossibleParents<Left> : [];
189
251
  type StorageEntryWithoutKeys<Payload> = {
252
+ /**
253
+ * `isCompatible` enables you to check whether or not the call you're trying
254
+ * to make is compatible with the descriptors you generated on dev time.
255
+ */
190
256
  isCompatible: IsCompatible;
257
+ /**
258
+ * Get `Payload` (Promise-based) for the storage entry.
259
+ *
260
+ * @param options Optionally set which block to target (latest known
261
+ * finalized is the default) and an AbortSignal.
262
+ */
191
263
  getValue: (options?: CallOptions) => Promise<Payload>;
264
+ /**
265
+ * Watch changes in `Payload` (observable-based) for the storage entry.
266
+ *
267
+ * @param bestOrFinalized Optionally choose which block to query and watch
268
+ * changes, `best` or `finalized` (default)
269
+ */
192
270
  watchValue: (bestOrFinalized?: "best" | "finalized") => Observable<Payload>;
193
271
  };
194
272
  type StorageEntryWithKeys<Args extends Array<any>, Payload> = {
273
+ /**
274
+ * `isCompatible` enables you to check whether or not the call you're trying
275
+ * to make is compatible with the descriptors you generated on dev time.
276
+ */
195
277
  isCompatible: IsCompatible;
278
+ /**
279
+ * Get `Payload` (Promise-based) for the storage entry with a specific set of
280
+ * `Args`.
281
+ *
282
+ * @param args All keys needed for that storage entry.
283
+ * At the end, optionally set which block to target (latest
284
+ * known finalized is the default) and an AbortSignal.
285
+ */
196
286
  getValue: (...args: [...WithCallOptions<Args>]) => Promise<Payload>;
287
+ /**
288
+ * Watch changes in `Payload` (observable-based) for the storage entry.
289
+ *
290
+ * @param args All keys needed for that storage entry.
291
+ * At the end, optionally choose which block to query and
292
+ * watch changes, `best` or `finalized` (default)
293
+ */
197
294
  watchValue: (...args: [...Args, bestOrFinalized?: "best" | "finalized"]) => Observable<Payload>;
295
+ /**
296
+ * Get an Array of `Payload` (Promise-based) for the storage entry with
297
+ * several sets of `Args`.
298
+ *
299
+ * @param keys Array of sets of keys needed for the storage entry.
300
+ * @param options Optionally set which block to target (latest known
301
+ * finalized is the default) and an AbortSignal.
302
+ */
198
303
  getValues: (keys: Array<[...Args]>, options?: CallOptions) => Promise<Array<Payload>>;
304
+ /**
305
+ * Get an Array of `Payload` (Promise-based) for the storage entry with a
306
+ * subset of `Args`.
307
+ *
308
+ * @param args Subset of keys needed for the storage entry.
309
+ * At the end, optionally set which block to target (latest
310
+ * known finalized is the default) and an AbortSignal.
311
+ * @example
312
+ *
313
+ * // this is a query with 3 keys
314
+ * typedApi.query.Pallet.Query.getEntries({ at: "best" }) // no keys
315
+ * typedApi.query.Pallet.Query.getEntries(arg1, { at: "finalized" }) // 1/3 keys
316
+ * typedApi.query.Pallet.Query.getEntries(arg1, arg2, { at: "0x12345678" }) // 2/3 keys
317
+ *
318
+ */
199
319
  getEntries: (...args: WithCallOptions<PossibleParents<Args>>) => Promise<Array<{
200
320
  keyArgs: Args;
201
321
  value: NonNullable<Payload>;
@@ -225,10 +345,23 @@ type TxInBestBlocksFound = {
225
345
  found: true;
226
346
  } & TxEventsPayload;
227
347
  type TxEventsPayload = {
348
+ /**
349
+ * Verify if extrinsic was successful, i.e. check if `System.ExtrinsicSuccess`
350
+ * is found.
351
+ */
228
352
  ok: boolean;
353
+ /**
354
+ * Array of all events emitted by the tx. Ordered as they are emitted
355
+ * on-chain.
356
+ */
229
357
  events: Array<SystemEvent$1["event"]>;
358
+ /**
359
+ * Block information where the tx is found. `hash` of the block, `number` of
360
+ * the block, `index` of the tx in the block.
361
+ */
230
362
  block: {
231
363
  hash: string;
364
+ number: number;
232
365
  index: number;
233
366
  };
234
367
  };
@@ -237,41 +370,125 @@ type TxFinalized = {
237
370
  txHash: HexString;
238
371
  } & TxEventsPayload;
239
372
  type TxOptions<Asset> = Partial<void extends Asset ? {
373
+ /**
374
+ * Block to target the transaction against. Default: `"finalized"`
375
+ */
240
376
  at: HexString | "best" | "finalized";
377
+ /**
378
+ * Tip in fundamental units. Default: `0`
379
+ */
241
380
  tip: bigint;
381
+ /**
382
+ * Mortality of the transaction. Default: `{ mortal: true, period: 64 }`
383
+ */
242
384
  mortality: {
243
385
  mortal: false;
244
386
  } | {
245
387
  mortal: true;
246
388
  period: number;
247
389
  };
390
+ /**
391
+ * Custom nonce for the transaction. Default: retrieve from latest known
392
+ * finalized block.
393
+ */
248
394
  nonce: number;
249
395
  } : {
396
+ /**
397
+ * Block to target the transaction against. Default: `"finalized"`
398
+ */
250
399
  at: HexString | "best" | "finalized";
400
+ /**
401
+ * Tip in fundamental units. Default: `0`
402
+ */
251
403
  tip: bigint;
404
+ /**
405
+ * Mortality of the transaction. Default: `{ mortal: true, period: 64 }`
406
+ */
252
407
  mortality: {
253
408
  mortal: false;
254
409
  } | {
255
410
  mortal: true;
256
411
  period: number;
257
412
  };
413
+ /**
414
+ * Asset information to pay fees, tip, etc. By default it'll use the
415
+ * native token of the chain.
416
+ */
258
417
  asset: Asset;
418
+ /**
419
+ * Custom nonce for the transaction. Default: retrieve from latest known
420
+ * finalized block.
421
+ */
259
422
  nonce: number;
260
423
  }>;
261
424
  type TxFinalizedPayload = Omit<TxFinalized, "type">;
262
425
  type TxPromise<Asset> = (from: PolkadotSigner, txOptions?: TxOptions<Asset>) => Promise<TxFinalizedPayload>;
263
426
  type TxObservable<Asset> = (from: PolkadotSigner, txOptions?: TxOptions<Asset>) => Observable<TxEvent>;
264
427
  interface TxCall {
428
+ /**
429
+ * SCALE-encoded callData of the transaction.
430
+ *
431
+ * @returns Promise resolving in the encoded data.
432
+ */
265
433
  (): Promise<Binary>;
434
+ /**
435
+ * SCALE-encoded callData of the transaction.
436
+ *
437
+ * @param runtime Runtime from got with `typedApi.runtime`
438
+ * @returns Synchronously returns encoded data.
439
+ */
266
440
  (runtime: Runtime): Binary;
267
441
  }
268
442
  type TxSignFn<Asset> = (from: PolkadotSigner, txOptions?: TxOptions<Asset>) => Promise<HexString>;
269
443
  type Transaction<Arg extends {} | undefined, Pallet extends string, Name extends string, Asset> = {
444
+ /**
445
+ * Pack the transaction, sends it to the signer, and return the signature
446
+ * asynchronously. If the signer fails (or the user cancels the signature)
447
+ * it'll throw an error.
448
+ *
449
+ * @param from `PolkadotSigner`-compliant signer.
450
+ * @param txOptions Optionally pass any number of txOptions.
451
+ * @returns Encoded `SignedExtrinsic` ready for broadcasting.
452
+ */
270
453
  sign: TxSignFn<Asset>;
454
+ /**
455
+ * Observable-based all-in-one transaction submitting. It will sign,
456
+ * broadcast, and track the transaction. The observable is singlecast, i.e.
457
+ * it will sign, broadcast, etc at every subscription. It will complete once
458
+ * the transaction is found in a `finalizedBlock`.
459
+ *
460
+ * @param from `PolkadotSigner`-compliant signer.
461
+ * @param txOptions Optionally pass any number of txOptions.
462
+ * @returns Observable to the transaction.
463
+ */
271
464
  signSubmitAndWatch: TxObservable<Asset>;
465
+ /**
466
+ * Pack the transaction, sends it to the signer, broadcast, and track the
467
+ * transaction. The promise will resolve as soon as the transaction in found
468
+ * in a `finalizedBlock`. If the signer fails (or the user cancels the
469
+ * signature), or the transaction becomes invalid it'll throw an error.
470
+ *
471
+ * @param from `PolkadotSigner`-compliant signer.
472
+ * @param txOptions Optionally pass any number of txOptions.
473
+ * @returns Finalized transaction information.
474
+ */
272
475
  signAndSubmit: TxPromise<Asset>;
476
+ /**
477
+ * SCALE-encoded callData of the transaction.
478
+ */
273
479
  getEncodedData: TxCall;
480
+ /**
481
+ * Estimate fees against the latest known `finalizedBlock`
482
+ *
483
+ * @param from Public key or address from the potencial sender.
484
+ * @param txOptions Optionally pass any number of txOptions.
485
+ * @returns Fees in fundamental units.
486
+ */
274
487
  getEstimatedFees: (from: Uint8Array | SS58String, txOptions?: TxOptions<Asset>) => Promise<bigint>;
488
+ /**
489
+ * PAPI way of expressing an extrinsic with arguments.
490
+ * It's useful to pass as a parameter to extrinsics that accept calls.
491
+ */
275
492
  decodedCall: Enum<{
276
493
  [P in Pallet]: Enum<{
277
494
  [N in Name]: Arg;
@@ -279,7 +496,18 @@ type Transaction<Arg extends {} | undefined, Pallet extends string, Name extends
279
496
  }>;
280
497
  };
281
498
  interface TxEntry<Arg extends {} | undefined, Pallet extends string, Name extends string, Asset> {
499
+ /**
500
+ * Synchronously create the transaction object ready to sign, submit, estimate
501
+ * fees, etc.
502
+ *
503
+ * @param args All parameters required by the transaction.
504
+ * @returns Transaction object.
505
+ */
282
506
  (...args: Arg extends undefined ? [] : [data: Arg]): Transaction<Arg, Pallet, Name, Asset>;
507
+ /**
508
+ * `isCompatible` enables you to check whether or not the call you're trying
509
+ * to make is compatible with the descriptors you generated on dev time.
510
+ */
283
511
  isCompatible: IsCompatible;
284
512
  }
285
513
 
@@ -331,8 +559,8 @@ type TypedApi<D extends ChainDefinition> = {
331
559
  };
332
560
  interface PolkadotClient {
333
561
  /**
334
- * Retrieve the ChainSpecData as it comes from the
335
- * [JSON-RPC spec](https://paritytech.github.io/json-rpc-interface-spec/api/chainSpec.html)
562
+ * Retrieve the ChainSpecData as it comes from the [JSON-RPC
563
+ * spec](https://paritytech.github.io/json-rpc-interface-spec/api/chainSpec.html)
336
564
  */
337
565
  getChainSpecData: () => Promise<ChainSpecData>;
338
566
  /**
@@ -417,6 +645,10 @@ interface PolkadotClient {
417
645
  * generated by `papi` CLI.
418
646
  */
419
647
  getTypedApi: <D extends ChainDefinition>(descriptors: D) => TypedApi<D>;
648
+ /**
649
+ * This will `unfollow` the provider, disconnect and error every subscription.
650
+ * After calling it nothing can be done with the client.
651
+ */
420
652
  destroy: () => void;
421
653
  /**
422
654
  * This API is meant as an "escape hatch" to allow access to debug endpoints
@@ -461,4 +693,4 @@ type FixedSizeArray<L extends number, T> = Array<T> & {
461
693
  */
462
694
  declare function createClient(provider: JsonRpcProvider): PolkadotClient;
463
695
 
464
- export { type ApisDescriptors, type ApisTypedef, type AssetDescriptor, type ChainDefinition, type ConstFromPalletsDef, type DescriptorEntry, type DescriptorsValue, type ErrorsFromPalletsDef, type EventPhase, type EventsFromPalletsDef, type FixedSizeArray, type PalletDescriptors, type PalletsTypedef, type PlainDescriptor, type PolkadotClient, type QueryFromPalletsDef, type RuntimeDescriptor, type StorageDescriptor, type Transaction, type TxBestBlocksState, type TxBroadcastEvent, type TxBroadcasted, type TxCall, type TxDescriptor, type TxEntry, type TxEvent, type TxEventsPayload, type TxFinalized, type TxFinalizedPayload, type TxFromPalletsDef, type TxInBestBlocksFound, type TxInBestBlocksNotFound, type TxObservable, type TxOptions, type TxPromise, type TxSignFn, type TxSigned, type TypedApi, createClient, createTxEntry, submit, submit$ };
696
+ export { type ApisTypedef, type AssetDescriptor, type ChainDefinition, type ConstFromPalletsDef, type DescriptorEntry, type ErrorsFromPalletsDef, type EventPhase, type EventsFromPalletsDef, type FixedSizeArray, type PalletsTypedef, type PlainDescriptor, type PolkadotClient, type QueryFromPalletsDef, type RuntimeDescriptor, type StorageDescriptor, type Transaction, type TxBestBlocksState, type TxBroadcastEvent, type TxBroadcasted, type TxCall, type TxDescriptor, type TxEntry, type TxEvent, type TxEventsPayload, type TxFinalized, type TxFinalizedPayload, type TxFromPalletsDef, type TxInBestBlocksFound, type TxInBestBlocksNotFound, type TxObservable, type TxOptions, type TxPromise, type TxSignFn, type TxSigned, type TypedApi, createClient, createTxEntry, submit, submit$ };