UNPKG

7.91 kBTypeScriptView Raw
1/// <reference types="bn.js" />
2import type { Observable } from 'rxjs';
3import type { AugmentedCall, QueryableCalls } from '@polkadot/api-base/types';
4import type { RpcInterface } from '@polkadot/rpc-core/types';
5import type { Metadata, Text } from '@polkadot/types';
6import type { Hash, RuntimeVersion } from '@polkadot/types/interfaces';
7import type { DecoratedMeta } from '@polkadot/types/metadata/decorate/types';
8import type { AnyFunction, Codec, DefinitionCallNamed, DefinitionRpc, DefinitionRpcSub, DefinitionsCall, DefinitionsCallEntry, DetectCodec, Registry, RegistryTypes } from '@polkadot/types/types';
9import type { HexString } from '@polkadot/util/types';
10import type { ApiDecoration, ApiInterfaceRx, ApiOptions, ApiTypes, DecoratedErrors, DecoratedEvents, DecoratedRpc, DecorateMethod, QueryableConsts, QueryableStorage, QueryableStorageMulti, SubmittableExtrinsics } from '../types/index.js';
11import type { AllDerives } from '../util/decorate.js';
12import type { VersionedRegistry } from './types.js';
13import { BehaviorSubject } from 'rxjs';
14import { RpcCore } from '@polkadot/rpc-core';
15import { BN } from '@polkadot/util';
16import { Events } from './Events.js';
17interface FullDecoration<ApiType extends ApiTypes> {
18 createdAt?: Uint8Array | undefined;
19 decoratedApi: ApiDecoration<ApiType>;
20 decoratedMeta: DecoratedMeta;
21}
22export declare abstract class Decorate<ApiType extends ApiTypes> extends Events {
23 #private;
24 protected __phantom: BN;
25 protected _type: ApiTypes;
26 protected _call: QueryableCalls<ApiType>;
27 protected _consts: QueryableConsts<ApiType>;
28 protected _derive?: ReturnType<Decorate<ApiType>['_decorateDerive']>;
29 protected _errors: DecoratedErrors<ApiType>;
30 protected _events: DecoratedEvents<ApiType>;
31 protected _extrinsics?: SubmittableExtrinsics<ApiType>;
32 protected _extrinsicType: number;
33 protected _genesisHash?: Hash;
34 protected _isConnected: BehaviorSubject<boolean>;
35 protected _isReady: boolean;
36 protected _query: QueryableStorage<ApiType>;
37 protected _queryMulti?: QueryableStorageMulti<ApiType>;
38 protected _rpc?: DecoratedRpc<ApiType, RpcInterface>;
39 protected _rpcCore: RpcCore & RpcInterface;
40 protected _runtimeMap: Record<HexString, string>;
41 protected _runtimeChain?: Text;
42 protected _runtimeMetadata?: Metadata;
43 protected _runtimeVersion?: RuntimeVersion;
44 protected _rx: ApiInterfaceRx;
45 protected readonly _options: ApiOptions;
46 /**
47 * This is the one and only method concrete children classes need to implement.
48 * It's a higher-order function, which takes one argument
49 * `method: Method extends (...args: any[]) => Observable<any>`
50 * (and one optional `options`), and should return the user facing method.
51 * For example:
52 * - For ApiRx, `decorateMethod` should just be identity, because the input
53 * function is already an Observable
54 * - For ApiPromise, `decorateMethod` should return a function that takes all
55 * the parameters from `method`, adds an optional `callback` argument, and
56 * returns a Promise.
57 *
58 * We could easily imagine other user-facing interfaces, which are simply
59 * implemented by transforming the Observable to Stream/Iterator/Kefir/Bacon
60 * via `decorateMethod`.
61 */
62 protected _decorateMethod: DecorateMethod<ApiType>;
63 /**
64 * @description Create an instance of the class
65 *
66 * @param options Options object to create API instance or a Provider instance
67 *
68 * @example
69 * <BR>
70 *
71 * ```javascript
72 * import Api from '@polkadot/api/promise';
73 *
74 * const api = new Api().isReady();
75 *
76 * api.rpc.subscribeNewHeads((header) => {
77 * console.log(`new block #${header.number.toNumber()}`);
78 * });
79 * ```
80 */
81 constructor(options: ApiOptions, type: ApiTypes, decorateMethod: DecorateMethod<ApiType>);
82 abstract at(blockHash: Uint8Array | string, knownVersion?: RuntimeVersion): Promise<ApiDecoration<ApiType>>;
83 /**
84 * @description Return the current used registry
85 */
86 get registry(): Registry;
87 /**
88 * @description Creates an instance of a type as registered
89 */
90 createType<T extends Codec = Codec, K extends string = string>(type: K, ...params: unknown[]): DetectCodec<T, K>;
91 /**
92 * @description Register additional user-defined of chain-specific types in the type registry
93 */
94 registerTypes(types?: RegistryTypes): void;
95 /**
96 * @returns `true` if the API operates with subscriptions
97 */
98 get hasSubscriptions(): boolean;
99 /**
100 * @returns `true` if the API decorate multi-key queries
101 */
102 get supportMulti(): boolean;
103 protected _emptyDecorated(registry: Registry, blockHash?: Uint8Array): ApiDecoration<ApiType>;
104 protected _createDecorated(registry: VersionedRegistry<ApiType>, fromEmpty: boolean, decoratedApi: ApiDecoration<ApiType> | null, blockHash?: Uint8Array): FullDecoration<ApiType>;
105 protected _injectMetadata(registry: VersionedRegistry<ApiType>, fromEmpty?: boolean): void;
106 /**
107 * @deprecated
108 * backwards compatible endpoint for metadata injection, may be removed in the future (However, it is still useful for testing injection)
109 */
110 injectMetadata(metadata: Metadata, fromEmpty?: boolean, registry?: Registry): void;
111 private _decorateFunctionMeta;
112 protected _filterRpc(methods: string[], additional: Record<string, Record<string, DefinitionRpc | DefinitionRpcSub>>): void;
113 protected _filterRpcMethods(exposed: string[]): void;
114 private _rpcSubmitter;
115 protected _decorateRpc<ApiType extends ApiTypes>(rpc: RpcCore & RpcInterface, decorateMethod: DecorateMethod<ApiType>, input?: Partial<DecoratedRpc<ApiType, RpcInterface>>): DecoratedRpc<ApiType, RpcInterface>;
116 protected _addRuntimeDef(result: DefinitionsCall, additional?: DefinitionsCall): void;
117 protected _getRuntimeDefs(registry: Registry, specName: Text, chain?: Text | string): [string, DefinitionsCallEntry[]][];
118 protected _decorateCalls<ApiType extends ApiTypes>({ registry, runtimeVersion: { apis, specName, specVersion } }: VersionedRegistry<any>, decorateMethod: DecorateMethod<ApiType>, blockHash?: Uint8Array | string | null): QueryableCalls<ApiType>;
119 protected _decorateCall<ApiType extends ApiTypes>(registry: Registry, def: DefinitionCallNamed, stateCall: (method: string, bytes: Uint8Array) => Observable<Codec>, decorateMethod: DecorateMethod<ApiType>): AugmentedCall<ApiType>;
120 protected _decorateMulti<ApiType extends ApiTypes>(decorateMethod: DecorateMethod<ApiType>): QueryableStorageMulti<ApiType>;
121 protected _decorateMultiAt<ApiType extends ApiTypes>(atApi: ApiDecoration<ApiType>, decorateMethod: DecorateMethod<ApiType>, blockHash: Uint8Array | string): QueryableStorageMulti<ApiType>;
122 protected _decorateExtrinsics<ApiType extends ApiTypes>({ tx }: DecoratedMeta, decorateMethod: DecorateMethod<ApiType>): SubmittableExtrinsics<ApiType>;
123 private _decorateExtrinsicEntry;
124 protected _decorateStorage<ApiType extends ApiTypes>({ query, registry }: DecoratedMeta, decorateMethod: DecorateMethod<ApiType>, blockHash?: Uint8Array): QueryableStorage<ApiType>;
125 private _decorateStorageEntry;
126 private _decorateStorageEntryAt;
127 private _queueStorage;
128 private _decorateStorageCall;
129 private _retrieveMulti;
130 private _retrieveMapKeys;
131 private _retrieveMapKeysPaged;
132 private _retrieveMapEntries;
133 private _retrieveMapEntriesPaged;
134 protected _decorateDeriveRx(decorateMethod: DecorateMethod<ApiType>): AllDerives<'rxjs'>;
135 protected _decorateDerive(decorateMethod: DecorateMethod<ApiType>): AllDerives<ApiType>;
136 /**
137 * Put the `this.onCall` function of ApiRx here, because it is needed by
138 * `api._rx`.
139 */
140 protected _rxDecorateMethod: <Method extends AnyFunction>(method: Method) => Method;
141}
142export {};