From df9b6959d16fec643dc4b2ff85869ee26ce651c3 Mon Sep 17 00:00:00 2001 From: Jaco Date: Wed, 6 Oct 2021 11:36:18 +0300 Subject: [PATCH] Expose findCall/findError on api.at (#4027) * Expose findCall/findError on api.at * Move findError/findCall --- packages/api/src/base/Decorate.ts | 23 +++++++++++++++++++++-- packages/api/src/base/index.ts | 16 +--------------- packages/api/src/types/index.ts | 5 ++++- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/packages/api/src/base/Decorate.ts b/packages/api/src/base/Decorate.ts index 775fffcf56..36c876fe24 100644 --- a/packages/api/src/base/Decorate.ts +++ b/packages/api/src/base/Decorate.ts @@ -7,7 +7,7 @@ import type { Option, Raw, StorageKey, Text, u64 } from '@polkadot/types'; import type { Call, Hash, RuntimeVersion } from '@polkadot/types/interfaces'; import type { DecoratedMeta } from '@polkadot/types/metadata/decorate/types'; import type { StorageEntry } from '@polkadot/types/primitive/types'; -import type { AnyFunction, AnyTuple, CallFunction, Codec, DefinitionRpc, DefinitionRpcSub, DetectCodec, IMethod, IStorageKey, Registry, RegistryTypes } from '@polkadot/types/types'; +import type { AnyFunction, AnyTuple, CallFunction, Codec, DefinitionRpc, DefinitionRpcSub, DetectCodec, IMethod, IStorageKey, Registry, RegistryError, RegistryTypes } from '@polkadot/types/types'; import type { SubmittableExtrinsic } from '../submittable/types'; import type { ApiDecoration, ApiInterfaceRx, ApiOptions, ApiTypes, DecoratedErrors, DecoratedEvents, DecoratedRpc, DecoratedRpcSection, DecorateMethod, PaginationOptions, QueryableConsts, QueryableModuleStorage, QueryableModuleStorageAt, QueryableStorage, QueryableStorageAt, QueryableStorageEntry, QueryableStorageEntryAt, QueryableStorageMulti, QueryableStorageMultiArg, SubmittableExtrinsicFunction, SubmittableExtrinsics, SubmittableModuleExtrinsics } from '../types'; import type { VersionedRegistry } from './types'; @@ -18,7 +18,7 @@ import { decorateDerive, ExactDerive } from '@polkadot/api-derive'; import { memo, RpcCore } from '@polkadot/rpc-core'; import { WsProvider } from '@polkadot/rpc-provider'; import { expandMetadata, Metadata, TypeRegistry, unwrapStorageType } from '@polkadot/types'; -import { arrayChunk, arrayFlatten, assert, BN, BN_ZERO, compactStripLength, logger, u8aToHex } from '@polkadot/util'; +import { arrayChunk, arrayFlatten, assert, BN, BN_ZERO, compactStripLength, logger, u8aToHex, u8aToU8a } from '@polkadot/util'; import { createSubmittable } from '../submittable'; import { augmentObject } from '../util/augmentObject'; @@ -172,6 +172,20 @@ export abstract class Decorate extends Events { return this.#registry.createType(type, ...params); } + /** + * @description Finds the definition for a specific [[CallFunction]] based on the index supplied + */ + public findCall (callIndex: Uint8Array | string): CallFunction { + return this.registry.findMetaCall(u8aToU8a(callIndex)); + } + + /** + * @description Finds the definition for a specific [[RegistryError]] based on the index supplied + */ + public findError (errorIndex: Uint8Array | string): RegistryError { + return this.registry.findMetaError(u8aToU8a(errorIndex)); + } + /** * @description Register additional user-defined of chain-specific types in the type registry */ @@ -226,6 +240,11 @@ export abstract class Decorate extends Events { augmentObject('query', storage, decoratedApi.query, fromEmpty); augmentObject('query', storageRx, decoratedApi.rx.query, fromEmpty); + decoratedApi.findCall = (callIndex: Uint8Array | string): CallFunction => + registry.registry.findMetaCall(u8aToU8a(callIndex)); + decoratedApi.findError = (errorIndex: Uint8Array | string): RegistryError => + registry.registry.findMetaError(u8aToU8a(errorIndex)); + return { decoratedApi, decoratedMeta: registry.decoratedMeta diff --git a/packages/api/src/base/index.ts b/packages/api/src/base/index.ts index 01cb37df2b..6a43fa841b 100644 --- a/packages/api/src/base/index.ts +++ b/packages/api/src/base/index.ts @@ -1,7 +1,7 @@ // Copyright 2017-2021 @polkadot/api authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { CallFunction, RegistryError, SignerPayloadRawBase } from '@polkadot/types/types'; +import type { SignerPayloadRawBase } from '@polkadot/types/types'; import type { ApiOptions, ApiTypes, DecorateMethod, Signer } from '../types'; import { assert, isString, u8aToHex, u8aToU8a } from '@polkadot/util'; @@ -55,20 +55,6 @@ export abstract class ApiBase extends Getters return this._rpcCore.disconnect(); } - /** - * @description Finds the definition for a specific [[CallFunction]] based on the index supplied - */ - public findCall (callIndex: Uint8Array | string): CallFunction { - return this.registry.findMetaCall(u8aToU8a(callIndex)); - } - - /** - * @description Finds the definition for a specific [[RegistryError]] based on the index supplied - */ - public findError (errorIndex: Uint8Array | string): RegistryError { - return this.registry.findMetaError(u8aToU8a(errorIndex)); - } - /** * @description Set an external signer which will be used to sign extrinsic when account passed in is not KeyringPair */ diff --git a/packages/api/src/types/index.ts b/packages/api/src/types/index.ts index eb9f010ae7..b52ebdc13e 100644 --- a/packages/api/src/types/index.ts +++ b/packages/api/src/types/index.ts @@ -11,7 +11,7 @@ import type { ProviderInterface, ProviderInterfaceEmitted } from '@polkadot/rpc- import type { ExtDef } from '@polkadot/types/extrinsic/signedExtensions/types'; import type { Hash, RuntimeVersion } from '@polkadot/types/interfaces'; import type { Metadata } from '@polkadot/types/metadata'; -import type { DefinitionRpc, DefinitionRpcSub, RegisteredTypes, Registry, SignatureOptions, Signer } from '@polkadot/types/types'; +import type { CallFunction, DefinitionRpc, DefinitionRpcSub, RegisteredTypes, Registry, RegistryError, SignatureOptions, Signer } from '@polkadot/types/types'; import type { BN } from '@polkadot/util'; import type { ApiBase } from '../base'; import type { DeriveAllSections } from '../util/decorate'; @@ -118,4 +118,7 @@ export interface ApiDecoration { rx: { query: QueryableStorage<'rxjs'>; } + + findCall (callIndex: Uint8Array | string): CallFunction; + findError (errorIndex: Uint8Array | string): RegistryError; }