Split @polkadot/types-create (#4330)

* Split @polkadot/types-create

* Remove floating Type class

* Adjust

* Adjust detectPackage

* Fix types-known exports
This commit is contained in:
Jaco
2021-12-15 16:05:13 +02:00
committed by GitHub
parent 890dd391dc
commit f02d3af3d2
56 changed files with 288 additions and 189 deletions
+2 -1
View File
@@ -6,7 +6,8 @@ import type { ChainProperties, ContractConstructorSpecLatest, ContractEventSpecL
import type { Codec, Registry } from '@polkadot/types/types';
import type { AbiConstructor, AbiEvent, AbiMessage, AbiParam, DecodedEvent, DecodedMessage } from '../types';
import { TypeDefInfo, TypeRegistry } from '@polkadot/types';
import { TypeRegistry } from '@polkadot/types';
import { TypeDefInfo } from '@polkadot/types-create';
import { assert, assertReturn, compactAddLength, compactStripLength, isNumber, isObject, isString, logger, stringCamelCase, stringify, u8aConcat, u8aToHex } from '@polkadot/util';
import { v0ToLatest, v1ToLatest } from './toLatest';
+3
View File
@@ -29,6 +29,9 @@
"@polkadot/rpc-core": "6.12.2-2",
"@polkadot/rpc-provider": "6.12.2-2",
"@polkadot/types": "6.12.2-2",
"@polkadot/types-codec": "6.12.2-2",
"@polkadot/types-create": "6.12.2-2",
"@polkadot/types-known": "6.12.2-2",
"@polkadot/util": "^8.1.2",
"@polkadot/util-crypto": "^8.1.2",
"eventemitter3": "^4.0.7",
+4 -1
View File
@@ -4,8 +4,11 @@
import { packageInfo as coreInfo } from '@polkadot/rpc-core/packageInfo';
import { packageInfo as providerInfo } from '@polkadot/rpc-provider/packageInfo';
import { packageInfo as typesInfo } from '@polkadot/types/packageInfo';
import { packageInfo as codecInfo } from '@polkadot/types-codec/packageInfo';
import { packageInfo as createInfo } from '@polkadot/types-create/packageInfo';
import { packageInfo as knownInfo } from '@polkadot/types-known/packageInfo';
import { detectPackage } from '@polkadot/util';
import { packageInfo } from './packageInfo';
detectPackage(packageInfo, typeof __dirname !== 'undefined' && __dirname, [coreInfo, providerInfo, typesInfo]);
detectPackage(packageInfo, typeof __dirname !== 'undefined' && __dirname, [coreInfo, providerInfo, typesInfo, codecInfo, createInfo, knownInfo]);
+1
View File
@@ -37,6 +37,7 @@
"@polkadot/rpc-provider": "6.12.2-2",
"@polkadot/types": "6.12.2-2",
"@polkadot/types-codec": "6.12.2-2",
"@polkadot/types-create": "6.12.2-2",
"@polkadot/types-support": "6.12.2-2",
"@polkadot/util": "^8.1.2",
"handlebars": "^4.7.7",
@@ -21,6 +21,7 @@ try {
'^@polkadot/rpc-core(.*)': './packages/rpc-core/src\\1',
'^@polkadot/rpc-provider(.*)': './packages/rpc-provider/src\\1',
'^@polkadot/types-codec(.*)': './packages/types-codec/src\\1',
'^@polkadot/types-create(.*)': './packages/types-create/src\\1',
'^@polkadot/types-known(.*)': './packages/types-known/src\\1',
'^@polkadot/types-support(.*)': './packages/types-support/src\\1',
'^@polkadot/types(.*)': './packages/types/src\\1'
@@ -17,6 +17,7 @@ try {
['module-resolver', {
alias: {
'^@polkadot/types-codec(.*)': './packages/types-codec/src\\1',
'^@polkadot/types-create(.*)': './packages/types-create/src\\1',
'^@polkadot/types-known(.*)': './packages/types-known/src\\1',
'^@polkadot/types-support(.*)': './packages/types-support/src\\1',
'^@polkadot/types(.*)': './packages/types/src\\1'
@@ -18,6 +18,7 @@ try {
alias: {
'^@polkadot/typegen(.*)': './packages/typegen/src\\1',
'^@polkadot/types-codec(.*)': './packages/types-codec/src\\1',
'^@polkadot/types-create(.*)': './packages/types-create/src\\1',
'^@polkadot/types-known(.*)': './packages/types-known/src\\1',
'^@polkadot/types-support(.*)': './packages/types-support/src\\1',
'^@polkadot/types(.*)': './packages/types/src\\1'
@@ -17,6 +17,7 @@ try {
['module-resolver', {
alias: {
'^@polkadot/types-codec(.*)': './packages/types-codec/src\\1',
'^@polkadot/types-create(.*)': './packages/types-create/src\\1',
'^@polkadot/types-known(.*)': './packages/types-known/src\\1',
'^@polkadot/types-support(.*)': './packages/types-support/src\\1',
'^@polkadot/types(.*)': './packages/types/src\\1'
@@ -17,6 +17,7 @@ try {
['module-resolver', {
alias: {
'^@polkadot/types-codec(.*)': './packages/types-codec/src\\1',
'^@polkadot/types-create(.*)': './packages/types-create/src\\1',
'^@polkadot/types-known(.*)': './packages/types-known/src\\1',
'^@polkadot/types-support(.*)': './packages/types-support/src\\1',
'^@polkadot/types(.*)': './packages/types/src\\1'
+1 -1
View File
@@ -3,7 +3,7 @@
import type { PortableType, SiLookupTypeId, SiPath, SiTypeParameter } from '@polkadot/types/interfaces';
import type { PortableRegistry } from '@polkadot/types/metadata';
import type { TypeDef } from '@polkadot/types/types';
import type { TypeDef } from '@polkadot/types-create/types';
import type { HexString } from '@polkadot/util/types';
import Handlebars from 'handlebars';
+3 -3
View File
@@ -1,16 +1,16 @@
// Copyright 2017-2021 @polkadot/typegen authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { TypeDef } from '@polkadot/types/create/types';
import type { Registry } from '@polkadot/types/types';
import type { TypeDef } from '@polkadot/types-create/types';
import type { ModuleTypes } from '../util/imports';
import Handlebars from 'handlebars';
import path from 'path';
import { getTypeDef, TypeRegistry } from '@polkadot/types/create';
import { TypeDefInfo } from '@polkadot/types/create/types';
import { TypeRegistry } from '@polkadot/types/create';
import * as defaultDefinitions from '@polkadot/types/interfaces/definitions';
import { getTypeDef, TypeDefInfo } from '@polkadot/types-create';
import { assert, isString, stringify, stringPascalCase } from '@polkadot/util';
import { createImports, exportInterface, exportType, formatType, readTemplate, setImports, TypeImports, writeFile } from '../util';
+2 -3
View File
@@ -1,14 +1,13 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { TypeDef } from '@polkadot/types/create/types';
import type { Constructor, Registry } from '@polkadot/types/types';
import type { TypeDef } from '@polkadot/types-create/types';
import { getTypeDef } from '@polkadot/types/create';
import { TypeDefInfo } from '@polkadot/types/create/types';
import { GenericAccountId, GenericLookupSource, GenericVote } from '@polkadot/types/generic';
import { AllConvictions } from '@polkadot/types/interfaces/democracy/definitions';
import { AbstractInt, bool, Compact, Enum, Null, Option, Struct, Tuple, UInt, Vec } from '@polkadot/types-codec';
import { getTypeDef, TypeDefInfo } from '@polkadot/types-create';
import { isChildClass, stringify } from '@polkadot/util';
import { formatType } from './formatting';
+2 -3
View File
@@ -3,14 +3,13 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import type { TypeDef } from '@polkadot/types/create/types';
import type { Registry } from '@polkadot/types/types';
import type { CodecRegistry } from '@polkadot/types-codec/types';
import type { TypeDef } from '@polkadot/types-create/types';
import Handlebars from 'handlebars';
import { getTypeDef, paramsNotation } from '@polkadot/types/create';
import { TypeDefInfo } from '@polkadot/types/create/types';
import { getTypeDef, paramsNotation, TypeDefInfo } from '@polkadot/types-create';
import { isString, stringify } from '@polkadot/util';
import { readTemplate } from './file';
+1 -2
View File
@@ -2,11 +2,10 @@
// SPDX-License-Identifier: Apache-2.0
import * as codecClasses from '@polkadot/types/codec';
import { getTypeDef } from '@polkadot/types/create';
import { TypeDefInfo } from '@polkadot/types/create/types';
import * as extrinsicClasses from '@polkadot/types/extrinsic';
import * as genericClasses from '@polkadot/types/generic';
import * as primitiveClasses from '@polkadot/types/primitive';
import { getTypeDef, TypeDefInfo } from '@polkadot/types-create';
export interface ModuleTypes {
types: Record<string, unknown>;
+29
View File
@@ -0,0 +1,29 @@
{
"author": "Jaco Greeff <jacogr@gmail.com>",
"bugs": "https://github.com/polkadot-js/api/issues",
"contributors": [],
"description": "Type creator helpers",
"engines": {
"node": ">=14.0.0"
},
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/types-create#readme",
"license": "Apache-2.0",
"maintainers": [],
"name": "@polkadot/types-create",
"repository": {
"directory": "packages/types-create",
"type": "git",
"url": "https://github.com/polkadot-js/api.git"
},
"sideEffects": [
"./detectPackage.js",
"./detectPackage.cjs"
],
"type": "module",
"version": "6.12.2-2",
"main": "index.js",
"dependencies": {
"@babel/runtime": "^7.16.3",
"@polkadot/util": "^8.1.2"
}
}
+8
View File
@@ -0,0 +1,8 @@
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
export { packageInfo } from './packageInfo';
export { TypeDefInfo } from './types';
export * from './create';
export * from './util';
@@ -1,15 +1,14 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-create authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Codec, CodecClass, CodecRegistry, U8aBitLength, UIntBitLength } from '@polkadot/types-codec/types';
import type { DetectCodec, LookupRegistry } from '../types';
import type { TypeDef } from './types';
import type { CreateRegistry, TypeDef } from '../types';
import { BTreeMap, BTreeSet, Bytes, CodecSet, Compact, DoNotConstruct, Enum, HashMap, Int, Null, Option, Range, RangeInclusive, Result, Struct, Tuple, U8aFixed, UInt, Vec, VecFixed, WrapperOpaque } from '@polkadot/types-codec';
import { assert, isNumber, stringify } from '@polkadot/util';
import { getTypeDef } from './getTypeDef';
import { TypeDefInfo } from './types';
import { TypeDefInfo } from '../types';
import { getTypeDef } from '../util/getTypeDef';
function getTypeDefType ({ lookupName, type }: TypeDef): string {
return lookupName || type;
@@ -143,7 +142,7 @@ const infoMapping: Record<TypeDefInfo, (registry: CodecRegistry, value: TypeDef)
),
[TypeDefInfo.Si]: (registry: CodecRegistry, value: TypeDef): CodecClass<Codec> =>
getTypeClass(registry, (registry as LookupRegistry).lookup.getTypeDef(value.type)),
getTypeClass(registry, (registry as CreateRegistry).lookup.getTypeDef(value.type)),
[TypeDefInfo.Struct]: (registry: CodecRegistry, value: TypeDef): CodecClass<Codec> =>
Struct.with(getTypeClassMap(value), value.alias),
@@ -199,18 +198,14 @@ export function constructTypeClass<T extends Codec = Codec> (registry: CodecRegi
// Returns the type Class for construction
export function getTypeClass<T extends Codec = Codec> (registry: CodecRegistry, typeDef: TypeDef): CodecClass<T> {
return (registry as LookupRegistry).getUnsafe(typeDef.type, false, typeDef) as CodecClass<T>;
}
export function createClass<T extends Codec = Codec, K extends string = string> (registry: CodecRegistry, type: K): CodecClass<DetectCodec<T, K>> {
return createClassUnsafe(registry, type);
return (registry as CreateRegistry).getUnsafe(typeDef.type, false, typeDef) as CodecClass<T>;
}
export function createClassUnsafe<T extends Codec = Codec, K extends string = string> (registry: CodecRegistry, type: K): CodecClass<T> {
return getTypeClass(
registry,
registry.isLookupType(type)
? (registry as LookupRegistry).lookup.getTypeDef(type)
? (registry as CreateRegistry).lookup.getTypeDef(type)
: getTypeDef(type)
);
}
@@ -0,0 +1,5 @@
// Copyright 2017-2021 @polkadot/types-create authors & contributors
// SPDX-License-Identifier: Apache-2.0
export * from './class';
export * from './type';
@@ -1,14 +1,13 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-create authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Codec, CodecClass, CodecRegistry } from '@polkadot/types-codec/types';
import type { DetectCodec } from '../types';
import type { CreateOptions } from './types';
import type { Codec, CodecClass, CodecRegistry, IU8a } from '@polkadot/types-codec/types';
import type { CreateOptions } from '../types';
import { Bytes, Option } from '@polkadot/types-codec';
import { assert, isHex, isU8a, u8aEq, u8aToHex, u8aToU8a } from '@polkadot/util';
import { createClass } from './createClass';
import { createClassUnsafe } from './class';
// With isPedantic, actually check that the encoding matches that supplied. This
// is much slower, but verifies that we have the correct types defined
@@ -51,7 +50,7 @@ function initType<T extends Codec> (registry: CodecRegistry, Type: CodecClass, p
checkPedantic(created, params, isPedantic);
if (blockHash) {
created.createdAtHash = createType(registry, 'Hash', blockHash);
created.createdAtHash = createTypeUnsafe<IU8a>(registry, 'Hash', [blockHash]);
}
return created as T;
@@ -60,12 +59,12 @@ function initType<T extends Codec> (registry: CodecRegistry, Type: CodecClass, p
// An unsafe version of the `createType` below. It's unsafe because the `type`
// argument here can be any string, which, when it cannot parse, will yield a
// runtime error.
export function createTypeUnsafe<T extends Codec = Codec, K extends string = string> (registry: CodecRegistry, type: K, params: unknown[] = [], options: CreateOptions = {}): DetectCodec<T, K> {
export function createTypeUnsafe<T extends Codec = Codec, K extends string = string> (registry: CodecRegistry, type: K, params: unknown[] = [], options: CreateOptions = {}): T {
let Clazz: CodecClass | null = null;
let firstError: Error | null = null;
try {
Clazz = createClass(registry, type);
Clazz = createClassUnsafe(registry, type);
return initType(registry, Clazz, params, options);
} catch (error) {
@@ -74,7 +73,7 @@ export function createTypeUnsafe<T extends Codec = Codec, K extends string = str
if (Clazz && Clazz.__fallbackType) {
try {
Clazz = createClass(registry, Clazz.__fallbackType as unknown as K);
Clazz = createClassUnsafe(registry, Clazz.__fallbackType as unknown as K);
return initType(registry, Clazz, params, options);
} catch {
@@ -84,13 +83,3 @@ export function createTypeUnsafe<T extends Codec = Codec, K extends string = str
throw firstError;
}
/**
* Create an instance of a `type` with a given `params`.
* @param type - A recognizable string representing the type to create an
* instance from
* @param params - The value to instantiate the type with
*/
export function createType<T extends Codec = Codec, K extends string = string> (registry: CodecRegistry, type: K, ...params: unknown[]): DetectCodec<T, K> {
return createTypeUnsafe(registry, type, params);
}
@@ -0,0 +1,9 @@
// Copyright 2017-2021 @polkadot/types-create authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { packageInfo as codecInfo } from '@polkadot/types-codec';
import { detectPackage } from '@polkadot/util';
import { packageInfo } from './packageInfo';
detectPackage(packageInfo, typeof __dirname !== 'undefined' && __dirname, [codecInfo]);
+6
View File
@@ -0,0 +1,6 @@
// Copyright 2017-2021 @polkadot/types-create authors & contributors
// SPDX-License-Identifier: Apache-2.0
import './detectPackage';
export * from './bundle';
+6
View File
@@ -0,0 +1,6 @@
// Copyright 2017-2021 @polkadot/types-create authors & contributors
// SPDX-License-Identifier: Apache-2.0
// Auto-generated by @polkadot/dev, do not edit
export const packageInfo = { name: '@polkadot/types-create', version: '6.12.2-2' };
+67
View File
@@ -0,0 +1,67 @@
// Copyright 2017-2021 @polkadot/types-create authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Codec, CodecClass, CodecRegistry, ICompact, INumber } from '@polkadot/types-codec/types';
export type { CodecCreateOptions as CreateOptions } from '@polkadot/types-codec/types';
export enum TypeDefInfo {
BTreeMap,
BTreeSet,
Compact,
DoNotConstruct,
Enum,
HashMap,
Int,
Linkage,
Null,
Option,
Plain,
Range,
Result,
Set,
Si,
Struct,
Tuple,
UInt,
Vec,
VecFixed,
WrapperOpaque
}
export interface TypeDef {
alias?: Map<string, string>;
displayName?: string;
docs?: string[];
fallbackType?: string;
info: TypeDefInfo;
index?: number;
isFromSi?: boolean;
length?: number;
lookupIndex?: number;
lookupName?: string;
lookupNameRoot?: string;
name?: string;
namespace?: string;
sub?: TypeDef | TypeDef[];
type: string;
typeName?: string;
}
export interface ILookup {
getSiType (lookupId: ICompact<INumber> | string | number): {
def: {
asTuple: ICompact<INumber>[]
}
},
getTypeDef (lookupId: ICompact<INumber> | string | number): TypeDef;
}
export interface CreateRegistry extends CodecRegistry {
lookup: ILookup;
createLookupType (lookupId: ICompact<INumber> | number): string;
setLookup (lookup: ILookup): void;
getUnsafe <T extends Codec = Codec, K extends string = string> (name: K, withUnknown?: boolean, knownTypeDef?: TypeDef): CodecClass<T> | undefined;
}
@@ -1,12 +1,12 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-create authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { CodecRegistry } from '@polkadot/types-codec/types';
import type { TypeDef } from './types';
import type { TypeDef } from '@polkadot/types-create/types';
import { assert, isNumber, isUndefined, objectSpread, stringify } from '@polkadot/util';
import { TypeDefInfo } from './types';
import { TypeDefInfo } from '../types';
const stringIdentity = <T extends { toString: () => string }> (value: T): string => value.toString();
@@ -1,12 +1,12 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-create authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { TypeDef } from './types';
import type { TypeDef } from '@polkadot/types-create/types';
import { sanitize } from '@polkadot/types-codec';
import { assert, isNumber, isString, objectSpread } from '@polkadot/util';
import { TypeDefInfo } from './types';
import { TypeDefInfo } from '../types';
import { typeSplit } from './typeSplit';
interface TypeDefOptions {
+6
View File
@@ -0,0 +1,6 @@
// Copyright 2017-2021 @polkadot/types-create authors & contributors
// SPDX-License-Identifier: Apache-2.0
export * from './encodeTypes';
export * from './getTypeDef';
export * from './typeSplit';
@@ -1,4 +1,4 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-create authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { assert } from '@polkadot/util';
+12
View File
@@ -0,0 +1,12 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"baseUrl": "..",
"outDir": "./build",
"rootDir": "./src",
"resolveJsonModule": true
},
"references": [
{ "path": "../types-codec" }
]
}
+1 -1
View File
@@ -1,6 +1,6 @@
// Copyright 2017-2021 @polkadot/types-known authors & contributors
// SPDX-License-Identifier: Apache-2.0
export * from '@polkadot/types/known';
export { getSpecAlias, getSpecExtensions, getSpecHasher, getSpecRpc, getSpecTypes, getUpgradeVersion } from '@polkadot/types';
export { packageInfo } from './packageInfo';
+1
View File
@@ -25,6 +25,7 @@
"dependencies": {
"@babel/runtime": "^7.16.5",
"@polkadot/types-codec": "6.12.2-2",
"@polkadot/types-create": "6.12.2-2",
"@polkadot/util": "^8.1.2",
"@polkadot/util-crypto": "^8.1.2",
"rxjs": "^7.4.0"
+3 -1
View File
@@ -7,9 +7,11 @@ import rpcDefinitions from './interfaces/jsonrpc';
export * from './codec';
export * from './create';
export * from './index.types';
export * from './known';
export * from './metadata';
export { TypeDefInfo } from './create/types';
export { TypeDefInfo } from '@polkadot/types-create';
export { convertSiV0toV1 } from './metadata/PortableRegistry';
export { packageInfo } from './packageInfo';
export { unwrapStorageType } from './primitive/StorageKey';
@@ -1,7 +1,8 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { TypeDefInfo } from '../types';
import { TypeDefInfo } from '@polkadot/types-create';
import { createClass, getTypeClass, TypeRegistry } from '.';
describe('createClass', (): void => {
@@ -1,9 +1,10 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { encodeTypeDef } from './encodeTypes';
import { TypeDefInfo } from '@polkadot/types-create';
import { TypeRegistry } from './registry';
import { TypeDefInfo } from './types';
import { encodeTypeDef } from '.';
describe('encodeTypeDef', (): void => {
const registry = new TypeRegistry();
+2 -1
View File
@@ -1,7 +1,8 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { TypeDefInfo } from './types';
import { TypeDefInfo } from '@polkadot/types-create';
import { getTypeDef, TypeRegistry } from '.';
describe('getTypeDef', (): void => {
+21 -5
View File
@@ -1,10 +1,26 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
export * from './createClass';
export * from './createType';
export * from './encodeTypes';
export * from './getTypeDef';
import type { Codec, CodecClass, CodecRegistry } from '@polkadot/types-codec/types';
import type { DetectCodec } from '../types';
import { createClassUnsafe, createTypeUnsafe } from '@polkadot/types-create';
export * from '@polkadot/types-create';
export * from './lazy';
export * from './registry';
export * from './typeSplit';
export function createClass<T extends Codec = Codec, K extends string = string> (registry: CodecRegistry, type: K): CodecClass<DetectCodec<T, K>> {
return createClassUnsafe(registry, type);
}
/**
* Create an instance of a `type` with a given `params`.
* @param type - A recognizable string representing the type to create an
* instance from
* @param params - The value to instantiate the type with
*/
export function createType<T extends Codec = Codec, K extends string = string> (registry: CodecRegistry, type: K, ...params: unknown[]): DetectCodec<T, K> {
return createTypeUnsafe(registry, type, params);
}
+5 -6
View File
@@ -2,12 +2,13 @@
// SPDX-License-Identifier: Apache-2.0
import type { Codec, CodecClass, CodecRegistry, IU8a } from '@polkadot/types-codec/types';
import type { CreateOptions, TypeDef } from '@polkadot/types-create/types';
import type { ExtDef } from '../extrinsic/signedExtensions/types';
import type { ChainProperties, DispatchErrorModule, EventMetadataLatest, Hash, MetadataLatest, SiField, SiLookupTypeId, SiVariant } from '../interfaces/types';
import type { CallFunction, CodecHasher, Definitions, DetectCodec, RegisteredTypes, Registry, RegistryError, RegistryTypes } from '../types';
import type { CreateOptions, TypeDef } from './types';
import { DoNotConstruct, Json, Raw } from '@polkadot/types-codec';
import { constructTypeClass, createClassUnsafe, createTypeUnsafe } from '@polkadot/types-create';
import { assert, assertReturn, BN_ZERO, formatBalance, isFunction, isString, isU8a, lazyMethod, logger, objectSpread, stringCamelCase, stringify } from '@polkadot/util';
import { blake2AsU8a } from '@polkadot/util-crypto';
@@ -19,8 +20,6 @@ import { decorateConstants, filterCallsSome, filterEventsSome } from '../metadat
import { createCallFunction } from '../metadata/decorate/extrinsics';
import { Metadata } from '../metadata/Metadata';
import { PortableRegistry } from '../metadata/PortableRegistry';
import { constructTypeClass, createClassUnsafe } from './createClass';
import { createTypeUnsafe } from './createType';
import { lazyVariants } from './lazy';
const l = logger('registry');
@@ -266,8 +265,8 @@ export class TypeRegistry implements Registry {
/**
* @describe Creates an instance of the class
*/
public createClass <T extends Codec = Codec, K extends string = string, R = DetectCodec<T, K>> (type: K): CodecClass<R> {
return this.createClassUnsafe(type) as unknown as CodecClass<R>;
public createClass <T extends Codec = Codec, K extends string = string> (type: K): CodecClass<DetectCodec<T, K>> {
return this.createClassUnsafe(type) as unknown as CodecClass<DetectCodec<T, K>>;
}
/**
@@ -280,7 +279,7 @@ export class TypeRegistry implements Registry {
/**
* @description Creates an instance of a type as registered
*/
public createType <T extends Codec = Codec, K extends string = string, R = DetectCodec<T, K>> (type: K, ...params: unknown[]): R {
public createType <T extends Codec = Codec, K extends string = string> (type: K, ...params: unknown[]): DetectCodec<T, K> {
return this.createTypeUnsafe(type, params);
}
+2 -42
View File
@@ -2,46 +2,6 @@
// SPDX-License-Identifier: Apache-2.0
export type { CodecCreateOptions as CreateOptions } from '@polkadot/types-codec/types';
export type { TypeDef } from '@polkadot/types-create/types';
export enum TypeDefInfo {
BTreeMap,
BTreeSet,
Compact,
DoNotConstruct,
Enum,
HashMap,
Int,
Linkage,
Null,
Option,
Plain,
Range,
Result,
Set,
Si,
Struct,
Tuple,
UInt,
Vec,
VecFixed,
WrapperOpaque
}
export interface TypeDef {
alias?: Map<string, string>;
displayName?: string;
docs?: string[];
fallbackType?: string;
info: TypeDefInfo;
index?: number;
isFromSi?: boolean;
length?: number;
lookupIndex?: number;
lookupName?: string;
lookupNameRoot?: string;
name?: string;
namespace?: string;
sub?: TypeDef | TypeDef[];
type: string;
typeName?: string;
}
export { TypeDefInfo } from '@polkadot/types-create';
+2 -1
View File
@@ -2,8 +2,9 @@
// SPDX-License-Identifier: Apache-2.0
import { packageInfo as codecInfo } from '@polkadot/types-codec';
import { packageInfo as createInfo } from '@polkadot/types-create';
import { detectPackage } from '@polkadot/util';
import { packageInfo } from './packageInfo';
detectPackage(packageInfo, typeof __dirname !== 'undefined' && __dirname, [codecInfo]);
detectPackage(packageInfo, typeof __dirname !== 'undefined' && __dirname, [codecInfo, createInfo]);
+3 -3
View File
@@ -2,10 +2,10 @@
// SPDX-License-Identifier: Apache-2.0
import type { AnyJson, Codec, CodecClass, CodecRegistry } from '@polkadot/types-codec/types';
import type { TypeDef } from '../create/types';
import type { CreateRegistry, TypeDef } from '@polkadot/types-create/types';
import type { EventMetadataLatest } from '../interfaces/metadata';
import type { EventId } from '../interfaces/system';
import type { IEvent, IEventData, InterfaceTypes, LookupRegistry } from '../types';
import type { IEvent, IEventData, InterfaceTypes } from '../types';
import { Null, Struct, Tuple } from '@polkadot/types-codec';
import { objectSpread } from '@polkadot/util';
@@ -57,7 +57,7 @@ export class GenericEventData extends Tuple implements IEventData {
this.#meta = meta;
this.#method = method;
this.#section = section;
this.#typeDef = fields.map(({ type }) => (registry as LookupRegistry).lookup.getTypeDef(type));
this.#typeDef = fields.map(({ type }) => (registry as CreateRegistry).lookup.getTypeDef(type));
}
/**
+1 -1
View File
@@ -1,10 +1,10 @@
// Copyright 2017-2021 @polkadot/types-known authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Text } from '@polkadot/types-codec';
import type { BN } from '@polkadot/util';
import type { ExtDef } from '../extrinsic/signedExtensions/types';
import type { Hash } from '../interfaces';
import type { Text } from '../primitive';
import type { ChainUpgradeVersion, CodecHasher, DefinitionRpc, DefinitionRpcSub, OverrideModuleType, OverrideVersionedType, Registry, RegistryTypes } from '../types';
import { bnToBn, isNull, isUndefined, objectSpread } from '@polkadot/util';
+1 -1
View File
@@ -1,4 +1,4 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-known authors & contributors
// SPDX-License-Identifier: Apache-2.0
// We want predictive ordering (manually managed)
@@ -3,18 +3,15 @@
import type { Vec } from '@polkadot/types-codec';
import type { CodecRegistry } from '@polkadot/types-codec/types';
import type { ILookup, TypeDef } from '@polkadot/types-create/types';
import type { PortableType } from '../../interfaces/metadata';
import type { SiField, SiLookupTypeId, SiPath, SiType, SiTypeDefArray, SiTypeDefBitSequence, SiTypeDefCompact, SiTypeDefComposite, SiTypeDefSequence, SiTypeDefTuple, SiTypeDefVariant, SiTypeParameter, SiVariant } from '../../interfaces/scaleInfo';
import type { Text, Type } from '../../primitive';
import type { ILookup, TypeDef } from '../../types';
import { sanitize, Struct } from '@polkadot/types-codec';
import { sanitize, Struct, u32 } from '@polkadot/types-codec';
import { getTypeDef, TypeDefInfo, withTypeString } from '@polkadot/types-create';
import { assert, isNumber, isString, objectSpread, stringCamelCase, stringify, stringPascalCase } from '@polkadot/util';
import { withTypeString } from '../../create/encodeTypes';
import { getTypeDef } from '../../create/getTypeDef';
import { u32 } from '../../primitive';
import { TypeDefInfo } from '../../types';
import { assertUnreachable } from './util';
// Just a placeholder for a type.unrwapOr()
@@ -2,9 +2,9 @@
// SPDX-License-Identifier: Apache-2.0
import type { CodecRegistry, ICompact, INumber } from '@polkadot/types-codec/types';
import type { CreateRegistry } from '@polkadot/types-create/types';
import type { StorageEntryMetadataLatest, StorageHasher } from '../../../interfaces/metadata';
import type { StorageEntry } from '../../../primitive/types';
import type { LookupRegistry } from '../../../types';
import { Raw } from '@polkadot/types-codec';
import { assert, compactAddLength, compactStripLength, isUndefined, objectSpread, stringCamelCase, stringLowerFirst, u8aConcat, u8aToU8a } from '@polkadot/util';
@@ -104,7 +104,7 @@ function createWStorageFn (registry: CodecRegistry, itemFn: CreateItemFn, option
return hashers.length === 1
? createKey(registry, itemFn, { args, hashers, keys: [key] })
: createKey(registry, itemFn, { args, hashers, keys: (registry as LookupRegistry).lookup.getSiType(key).def.asTuple });
: createKey(registry, itemFn, { args, hashers, keys: (registry as CreateRegistry).lookup.getSiType(key).def.asTuple });
};
}
@@ -162,7 +162,7 @@ function extendPrefixedMap (registry: CodecRegistry, itemFn: CreateItemFn, stora
const { hashers, key } = type.asMap;
const keysVec = hashers.length === 1
? [key]
: (registry as LookupRegistry).lookup.getSiType(key).def.asTuple;
: (registry as CreateRegistry).lookup.getSiType(key).def.asTuple;
return new Raw(registry, createKeyRaw(registry, itemFn, { args, hashers: hashers.slice(0, args.length), keys: keysVec.slice(0, args.length) }));
}
@@ -1,11 +1,9 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { TypeDef } from '../../types';
import type { TypeDef } from '@polkadot/types-create/types';
// we are attempting to avoid circular refs, hence the path import
import { getTypeDef } from '../../create/getTypeDef';
import { TypeDefInfo } from '../../create/types';
import { getTypeDef, TypeDefInfo } from '@polkadot/types-create';
type Extracted = string | Extracted[];
@@ -1,8 +1,8 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { ILookup } from '@polkadot/types-create/types';
import type { SiLookupTypeId } from '../../interfaces';
import type { ILookup } from '../../types';
export function getSiName (lookup: ILookup, type: SiLookupTypeId): string {
const typeDef = lookup.getTypeDef(type);
+1 -2
View File
@@ -1,11 +1,10 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Text } from '@polkadot/types-codec';
import type { Text, Type } from '@polkadot/types-codec';
import type { CodecRegistry } from '@polkadot/types-codec/types';
import type { ErrorMetadataV13, EventMetadataV13, ExtrinsicMetadataV13, ExtrinsicMetadataV14, FunctionMetadataV13, MetadataV13, MetadataV14, ModuleConstantMetadataV13, ModuleMetadataV13, PalletCallMetadataV14, PalletConstantMetadataV14, PalletErrorMetadataV14, PalletEventMetadataV14, PalletMetadataV14, PalletStorageMetadataV14, StorageEntryMetadataV14, StorageEntryTypeV14, StorageHasherV13, StorageMetadataV13 } from '../../interfaces/metadata';
import type { SiVariant } from '../../interfaces/scaleInfo';
import type { Type } from '../../primitive/Type';
import type { OverrideModuleType, Registry } from '../../types';
import { stringCamelCase } from '@polkadot/util';
+4 -3
View File
@@ -2,10 +2,11 @@
// SPDX-License-Identifier: Apache-2.0
import type { AnyJson, AnyTuple, Codec, CodecRegistry, ICompact, INumber } from '@polkadot/types-codec/types';
import type { CreateRegistry } from '@polkadot/types-create/types';
import type { StorageEntryMetadataLatest, StorageEntryTypeLatest, StorageHasher } from '../interfaces/metadata';
import type { AllHashers } from '../interfaces/metadata/definitions';
import type { SiLookupTypeId } from '../interfaces/scaleInfo';
import type { InterfaceTypes, IStorageKey, LookupRegistry } from '../types';
import type { InterfaceTypes, IStorageKey } from '../types';
import type { StorageEntry } from './types';
import { Bytes } from '@polkadot/types-codec';
@@ -44,7 +45,7 @@ export function unwrapStorageSi (type: StorageEntryTypeLatest): SiLookupTypeId {
/** @internal */
export function unwrapStorageType (registry: CodecRegistry, type: StorageEntryTypeLatest, isOptional?: boolean): keyof InterfaceTypes {
const outputType = getSiName((registry as LookupRegistry).lookup, unwrapStorageSi(type));
const outputType = getSiName((registry as CreateRegistry).lookup, unwrapStorageSi(type));
return isOptional
? `Option<${outputType}>` as unknown as keyof InterfaceTypes
@@ -118,7 +119,7 @@ function decodeArgsFromMeta <A extends AnyTuple> (registry: CodecRegistry, value
const { hashers, key } = meta.type.asMap;
const keys = hashers.length === 1
? [key]
: (registry as LookupRegistry).lookup.getSiType(key).def.asTuple;
: (registry as CreateRegistry).lookup.getSiType(key).def.asTuple;
return decodeHashers(registry, value, hashers.map((h, i) => [h, keys[i]]));
}
-28
View File
@@ -1,28 +0,0 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { CodecRegistry } from '@polkadot/types-codec/types';
import { sanitize, Text } from '@polkadot/types-codec';
/**
* @name Type
* @description
* This is a extended version of Text, specifically to handle types. Here we rely fully
* on what Text provides us, however we also adjust the types received from the runtime,
* i.e. we remove the `T::` prefixes found in some types for consistency across implementation.
*/
export class Type extends Text {
constructor (registry: CodecRegistry, value: Text | Uint8Array | string = '') {
super(registry, value);
this.setOverride(sanitize(this.toString()));
}
/**
* @description Returns the base runtime type name for this instance
*/
public override toRawType (): string {
return 'Type';
}
}
+1 -1
View File
@@ -4,7 +4,7 @@
/**
* @summary Type definitions that are used in the system
*/
export { BitVec, bool, Bool, Bytes, Type, i8, I8, i16, I16, i32, I32, i64, I64, i128, I128, i256, I256, Null, Text, u8, U8, u16, U16, u32, U32, u64, U64, u128, U128, u256, U256, usize, USize } from '@polkadot/types-codec';
export { BitVec, bool, Bool, Bytes, i8, I8, i16, I16, i32, I32, i64, I64, i128, I128, i256, I256, Null, Text, Type, u8, U8, u16, U16, u32, U32, u64, U64, u128, U128, u256, U256, usize, USize } from '@polkadot/types-codec';
export { Data } from './Data';
export { StorageKey } from './StorageKey';
+3 -21
View File
@@ -2,9 +2,9 @@
// SPDX-License-Identifier: Apache-2.0
import type { Observable } from 'rxjs';
import type { CodecRegistry, ICompact, INumber, RegistryTypes } from '@polkadot/types-codec/types';
import type { RegistryTypes } from '@polkadot/types-codec/types';
import type { CreateRegistry, TypeDef } from '@polkadot/types-create/types';
import type { BN } from '@polkadot/util';
import type { TypeDef } from '../create/types';
import type { ExtDef } from '../extrinsic/signedExtensions/types';
import type { MetadataLatest } from '../interfaces/metadata';
import type { SiField, SiLookupTypeId } from '../interfaces/scaleInfo';
@@ -96,26 +96,8 @@ export interface RegisteredTypes {
typesSpec?: Record<string, RegistryTypes>;
}
export interface ILookup {
getSiType (lookupId: ICompact<INumber> | string | number): {
def: {
asTuple: ICompact<INumber>[]
}
},
getTypeDef (lookupId: ICompact<INumber> | string | number): TypeDef;
}
export interface LookupRegistry extends CodecRegistry {
lookup: ILookup;
createLookupType (lookupId: ICompact<INumber> | number): string;
setLookup (lookup: ILookup): void;
getUnsafe <T extends Codec = Codec, K extends string = string> (name: K, withUnknown?: boolean, knownTypeDef?: TypeDef): CodecClass<T> | undefined;
}
// Note the commented interfaces here are directly from CodecRegistry
export interface Registry extends LookupRegistry {
export interface Registry extends CreateRegistry {
// readonly chainDecimals: number[];
// readonly chainSS58: number | undefined;
// readonly chainTokens: string[];
+1
View File
@@ -8,6 +8,7 @@
},
"references": [
{ "path": "../types-codec" },
{ "path": "../types-create" },
{ "path": "../types-support" }
]
}