diff --git a/CHANGELOG.md b/CHANGELOG.md index 001ad527dc..22fd0f59f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - Add `api.rpc.chain.subscribeAllHeads` (not just best) (Thanks to https://github.com/jak-pan) - Add `api.rpc.engine.*` for manual seal support +- Registry now exposes `.createType(, ...args)` as well - Support types & interfaces required for lazy reaping & offchain phragmen - Support JS `BigInt` inputs in any `UInt` and `Int` object, `api.tx.balances.transfer(..., 9876543210123456789n)` - Expand `.toHuman` on `ExtrinsicEra`, `SubmittableResult` & `Vote` diff --git a/packages/api/src/base/Decorate.ts b/packages/api/src/base/Decorate.ts index e7145ba4d6..0471af832a 100644 --- a/packages/api/src/base/Decorate.ts +++ b/packages/api/src/base/Decorate.ts @@ -17,7 +17,7 @@ import DecoratedMeta from '@polkadot/metadata/Decorated'; import getHasher from '@polkadot/metadata/Decorated/storage/fromMetadata/getHasher'; import RpcCore from '@polkadot/rpc-core'; import { WsProvider } from '@polkadot/rpc-provider'; -import { Metadata, Null, Option, Text, TypeRegistry, u64, Vec } from '@polkadot/types'; +import { createType, Metadata, Null, Option, Text, TypeRegistry, u64, Vec } from '@polkadot/types'; import Linkage, { LinkageResult } from '@polkadot/types/codec/Linkage'; import { DEFAULT_VERSION as EXTRINSIC_DEFAULT_VERSION } from '@polkadot/types/extrinsic/constants'; import StorageKey, { StorageEntry } from '@polkadot/types/primitive/StorageKey'; @@ -136,9 +136,19 @@ export default abstract class Decorate extends Events this._rx.registry = this.registry; } - public abstract createType (type: K, ...params: any[]): InterfaceTypes[K]; + /** + * @description Creates an instance of a type as registered + */ + public createType (type: K, ...params: any[]): InterfaceTypes[K] { + return createType(this.registry, type, ...params); + } - public abstract registerTypes (types?: RegistryTypes): void; + /** + * @description Register additional user-defined of chain-specific types in the type registry + */ + public registerTypes (types?: RegistryTypes): void { + types && this.registry.register(types); + } /** * @returns `true` if the API operates with subscriptions diff --git a/packages/api/src/base/index.ts b/packages/api/src/base/index.ts index 0124480664..ceb1dddea3 100644 --- a/packages/api/src/base/index.ts +++ b/packages/api/src/base/index.ts @@ -5,10 +5,10 @@ import { Constants } from '@polkadot/metadata/Decorated/types'; import { RpcInterface } from '@polkadot/rpc-core/types'; import { Hash, RuntimeVersion } from '@polkadot/types/interfaces'; -import { CallFunction, InterfaceTypes, RegistryError, RegistryTypes, SignerPayloadRawBase } from '@polkadot/types/types'; +import { CallFunction, RegistryError, SignerPayloadRawBase } from '@polkadot/types/types'; import { ApiInterfaceRx, ApiOptions, ApiTypes, DecoratedRpc, DecorateMethod, QueryableStorage, QueryableStorageMulti, SubmittableExtrinsics, Signer } from '../types'; -import { Metadata, createType } from '@polkadot/types'; +import { Metadata } from '@polkadot/types'; import { assert, isString, isUndefined, u8aToHex, u8aToU8a } from '@polkadot/util'; import Init from './Init'; @@ -233,13 +233,6 @@ export default abstract class ApiBase extends Init (type: K, ...params: any[]): InterfaceTypes[K] => { - return createType(this.registry, type, ...params); - } - /** * @description Disconnect from the underlying provider, halting all network traffic */ @@ -261,13 +254,6 @@ export default abstract class ApiBase extends Init (type: K, ...params: any[]): InterfaceTypes[K] { + return createType(this, type, ...params); + } + // find a specific call public findMetaCall (callIndex: Uint8Array): CallFunction { const hexIndex = u8aToHex(callIndex); diff --git a/packages/types/src/types/registry.ts b/packages/types/src/types/registry.ts index b377a085d7..96f70791c0 100644 --- a/packages/types/src/types/registry.ts +++ b/packages/types/src/types/registry.ts @@ -92,6 +92,7 @@ export interface Registry { // keep this as a generic Codec, however the actual impl. returns the correct findMetaEvent (eventIndex: Uint8Array): Constructor; + createType (type: K, ...params: any[]): InterfaceTypes[K]; get (name: string, withUnknown?: boolean): Constructor | undefined; getChainProperties (): ChainProperties | undefined; getDefinition (name: string): string | undefined;