diff --git a/packages/types/src/create/createClass.ts b/packages/types/src/create/createClass.ts index 284954ed58..20074954f9 100644 --- a/packages/types/src/create/createClass.ts +++ b/packages/types/src/create/createClass.ts @@ -175,16 +175,9 @@ const infoMapping: Record C WrapperOpaque.with(getSubType(value)) }; -// Returns the type Class for construction -export function getTypeClass (registry: Registry, typeDef: TypeDef): Constructor { - let Type = registry.get(typeDef.type); - - if (Type) { - return Type as Constructor; - } - +export function constructTypeClass (registry: Registry, typeDef: TypeDef): Constructor { try { - Type = infoMapping[typeDef.info](registry, typeDef); + const Type = infoMapping[typeDef.info](registry, typeDef); assert(Type, 'No class created'); @@ -201,6 +194,11 @@ export function getTypeClass (registry: Registry, typeD } } +// Returns the type Class for construction +export function getTypeClass (registry: Registry, typeDef: TypeDef): Constructor { + return registry.get(typeDef.type, false, typeDef) as Constructor; +} + export function createClass (registry: Registry, type: K): DetectConstructor { return getTypeClass( registry, diff --git a/packages/types/src/create/registry.ts b/packages/types/src/create/registry.ts index 0cd29c1dec..e4b79fcb4a 100644 --- a/packages/types/src/create/registry.ts +++ b/packages/types/src/create/registry.ts @@ -4,7 +4,7 @@ import type { ExtDef } from '../extrinsic/signedExtensions/types'; import type { ChainProperties, CodecHash, DispatchErrorModule, Hash, MetadataLatest, SiField, SiLookupTypeId, SiVariant } from '../interfaces/types'; import type { CallFunction, Codec, CodecHasher, Constructor, DetectCodec, DetectConstructor, RegisteredTypes, Registry, RegistryError, RegistryTypes } from '../types'; -import type { CreateOptions } from './types'; +import type { CreateOptions, TypeDef } from './types'; import { assert, assertReturn, BN_ZERO, formatBalance, isFunction, isString, isU8a, lazyMethod, logger, objectSpread, stringCamelCase, stringify } from '@polkadot/util'; import { blake2AsU8a } from '@polkadot/util-crypto'; @@ -20,7 +20,7 @@ import { decorateConstants, filterCallsSome, filterEventsSome } from '../metadat import { createCallFunction } from '../metadata/decorate/extrinsics'; import { Metadata } from '../metadata/Metadata'; import { PortableRegistry } from '../metadata/PortableRegistry'; -import { createClass } from './createClass'; +import { constructTypeClass, createClass } from './createClass'; import { createTypeUnsafe } from './createType'; import { lazyVariants } from './lazy'; @@ -316,7 +316,7 @@ export class TypeRegistry implements Registry { ); } - public get (name: K, withUnknown?: boolean): DetectConstructor | undefined { + public get (name: K, withUnknown?: boolean, knownTypeDef?: TypeDef): DetectConstructor | undefined { let Type = this.#classes.get(name); // we have not already created the type, attempt it @@ -327,8 +327,8 @@ export class TypeRegistry implements Registry { // we have a definition, so create the class now (lazily) if (definition) { BaseType = createClass(this, definition); - - this.#classes.set(name, BaseType); + } else if (knownTypeDef) { + BaseType = constructTypeClass(this, knownTypeDef); } else if (withUnknown) { l.warn(`Unable to resolve type ${name}, it will fail on construction`); diff --git a/packages/types/src/types/registry.ts b/packages/types/src/types/registry.ts index 642cdd82af..88e26c9326 100644 --- a/packages/types/src/types/registry.ts +++ b/packages/types/src/types/registry.ts @@ -3,7 +3,7 @@ import type { Observable } from 'rxjs'; import type { BN } from '@polkadot/util'; -import type { CreateOptions } from '../create/types'; +import type { CreateOptions, TypeDef } from '../create/types'; import type { ExtDef } from '../extrinsic/signedExtensions/types'; import type { MetadataLatest } from '../interfaces/metadata'; import type { CodecHash, Hash } from '../interfaces/runtime'; @@ -126,7 +126,7 @@ export interface Registry { createClass (type: K): DetectConstructor; createType (type: K, ...params: unknown[]): DetectCodec; createTypeUnsafe (type: K, params: unknown[], options?: CreateOptions): DetectCodec; - get (name: K, withUnknown?: boolean): DetectConstructor | undefined; + get (name: K, withUnknown?: boolean, knownTypeDef?: TypeDef): DetectConstructor | undefined; getChainProperties (): ChainProperties | undefined; getClassName (clazz: Constructor): string | undefined; getDefinition (typeName: string): string | undefined;