Flatten displayType & encodeType into encodeTypeDef (#2698)
* Flatten displayType & encodeType into encodeTypeDef * Remove to-be-replaced MetaRegistry tests (hardcoded results)
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
import { CodecArg, Constructor, Registry, TypeDef } from '@polkadot/types/types';
|
||||
import { ContractABIArgBasePre, ContractABIContract, ContractABIContractPre, ContractABIEvent, ContractABIEventPre, ContractABIFn, ContractABIFnArg, ContractABIMessage, ContractABIMessageBase, ContractABIMessagePre, ContractABI, ContractABIMessageCommon, ContractABIPre, ContractABIRange, ContractABIRangePre, ContractABIStorage, ContractABIStorageLayout, ContractABIStorageLayoutPre, ContractABIStoragePre, ContractABIStorageStruct, ContractABIStorageStructPre, ContractABITypePre } from './types';
|
||||
|
||||
import { Compact, u32, createClass, encodeType } from '@polkadot/types';
|
||||
import { Compact, u32, createClass, encodeTypeDef } from '@polkadot/types';
|
||||
import { assert, hexToU8a, isNumber, isString, isNull, isObject, isUndefined, stringCamelCase, isHex, hexToNumber } from '@polkadot/util';
|
||||
|
||||
import MetaRegistry from './MetaRegistry';
|
||||
@@ -43,7 +43,7 @@ function createArgClass (registry: Registry, args: ContractABIFnArg[], baseDef:
|
||||
registry,
|
||||
JSON.stringify(
|
||||
args.reduce((base: Record<string, any>, { name, type }): Record<string, any> => {
|
||||
base[name] = type.displayName || encodeType(type);
|
||||
base[name] = type.displayName || encodeTypeDef(type);
|
||||
|
||||
return base;
|
||||
}, baseDef)
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
// Copyright 2017-2020 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { TypeRegistry } from '@polkadot/types';
|
||||
|
||||
import MetaRegistry from './MetaRegistry';
|
||||
|
||||
import erc20Abi from '../test/contracts/Erc20.json';
|
||||
import erc20Cmp from '../test/compare/erc20.test.json';
|
||||
import sharedVecAbi from '../test/contracts/SharedVecV2.json';
|
||||
import sharedVecCmp from '../test/compare/shared-vec.test.json';
|
||||
import test001Abi from '../test/abi/test001.json';
|
||||
import test001Cmp from '../test/compare/test001.cmp.json';
|
||||
|
||||
function compare (meta: MetaRegistry, other: any): void {
|
||||
try {
|
||||
expect(meta.typeDefs).toEqual(other);
|
||||
} catch (error) {
|
||||
console.error(JSON.stringify(meta.typeDefs));
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
describe('MetaRegistry', (): void => {
|
||||
const registry = new TypeRegistry();
|
||||
|
||||
describe('construction', (): void => {
|
||||
it('initializes from a contract ABI (ERC20)', (): void => {
|
||||
compare(new MetaRegistry(registry, erc20Abi), erc20Cmp);
|
||||
});
|
||||
|
||||
it('initializes from a contract ABI (SharedVec)', (): void => {
|
||||
compare(new MetaRegistry(registry, sharedVecAbi), sharedVecCmp);
|
||||
});
|
||||
|
||||
it('initializes from a contract ABI (Other, test001)', (): void => {
|
||||
compare(new MetaRegistry(registry, test001Abi), test001Cmp);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -4,7 +4,7 @@
|
||||
import { MetaRegistryItem, MetaRegistryJson, MetaTypeDefClikeEnum, MetaType, MetaTypeDefEnum, MetaTypeDefEnumVariant, MetaTypeDefEnumVariantStruct, MetaTypeDefEnumVariantTupleStruct, MetaTypeDefEnumVariantUnit, MetaTypeDefStruct, MetaTypeDefStructField, MetaTypeDefTupleStruct, MetaTypeDefUnion, MetaTypeIdCustom, MetaTypeIdVec, MetaTypeIdVecFixed, MetaTypeInfo, Registry, StringIndex, TypeDef, TypeDefInfo, TypeIndex } from '@polkadot/types/types';
|
||||
|
||||
import { assert } from '@polkadot/util';
|
||||
import { displayType, withTypeString } from '@polkadot/types';
|
||||
import { encodeTypeDef, withTypeString } from '@polkadot/types';
|
||||
|
||||
const builtinMap: [(id: unknown | unknown[] | { 'array.type': unknown } | { 'slice.type': unknown }) => boolean, MetaTypeInfo][] = [
|
||||
[(id: any) => typeof id === 'string', MetaTypeInfo.BuiltinPlain],
|
||||
@@ -238,7 +238,7 @@ export default class MetaRegistry extends MetadataRegistryLookup {
|
||||
|
||||
assert(!typeIndex || vecTypeIndex !== typeIndex, `MetaRegistry: self-referencing registry type at index ${typeIndex}`);
|
||||
|
||||
const type = displayType(this.typeDefFromMetaTypeAt(vecTypeIndex));
|
||||
const type = encodeTypeDef(this.typeDefFromMetaTypeAt(vecTypeIndex));
|
||||
|
||||
assert(type && type.length > 0, `MetaRegistry: Invalid builtin Vec type found at index ${typeIndex}`);
|
||||
|
||||
@@ -255,7 +255,7 @@ export default class MetaRegistry extends MetadataRegistryLookup {
|
||||
assert(!vecLength || vecLength <= 256, 'MetaRegistry: Only support for [Type; <length>], where length <= 256');
|
||||
assert(!typeIndex || vecTypeIndex !== typeIndex, `MetaRegistry: self-referencing registry type at index ${typeIndex}`);
|
||||
|
||||
const type = displayType(this.typeDefFromMetaTypeAt(vecTypeIndex));
|
||||
const type = encodeTypeDef(this.typeDefFromMetaTypeAt(vecTypeIndex));
|
||||
|
||||
assert(type && type.length > 0, `MetaRegistry: Invalid vector type found at index ${typeIndex}`);
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ function encodeWithParams (typeDef: Pick<TypeDef, any>, outer = typeDef.displayN
|
||||
outer,
|
||||
params || sub,
|
||||
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
||||
(param: TypeDef) => displayType(param)
|
||||
(param: TypeDef) => encodeTypeDef(param)
|
||||
);
|
||||
default:
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
||||
@@ -55,7 +55,7 @@ function encodeSubTypes (sub: TypeDef[], asEnum?: boolean): string {
|
||||
return {
|
||||
...result,
|
||||
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
||||
[type.name as string]: encodeType(type)
|
||||
[type.name as string]: encodeTypeDef(type)
|
||||
};
|
||||
},
|
||||
{}
|
||||
@@ -106,7 +106,7 @@ function encodeTuple (typeDef: Pick<TypeDef, any>): string {
|
||||
return `(${
|
||||
sub
|
||||
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
||||
.map((type: TypeDef): string => encodeType(type))
|
||||
.map((type: TypeDef): string => encodeTypeDef(type))
|
||||
.join(', ')
|
||||
})`;
|
||||
}
|
||||
@@ -148,38 +148,25 @@ const encoders: Record<TypeDefInfo, (typeDef: TypeDef) => string> = {
|
||||
[TypeDefInfo.VecFixed]: (typeDef: TypeDef): string => encodeVecFixed(typeDef)
|
||||
};
|
||||
|
||||
export function encodeType (typeDef: Pick<TypeDef, any>): string {
|
||||
export function encodeTypeDef (typeDef: Pick<TypeDef, any>): string {
|
||||
assert(!isUndefined(typeDef.info), `Invalid type definition with no instance info, ${JSON.stringify(typeDef)}`);
|
||||
|
||||
if (SPECIAL_TYPES.includes(typeDef.displayName)) {
|
||||
return typeDef.displayName as string;
|
||||
} else if (typeDef.displayName || [TypeDefInfo.Enum, TypeDefInfo.Struct].includes(typeDef.info)) {
|
||||
return encodeWithParams(typeDef);
|
||||
}
|
||||
|
||||
const encoder = encoders[(typeDef as TypeDef).info];
|
||||
|
||||
assert(encoder, `Cannot encode type: ${typeDef.toString()}`);
|
||||
assert(encoder, `Cannot encode type: ${JSON.stringify(typeDef)}`);
|
||||
|
||||
return encoder(typeDef as TypeDef);
|
||||
}
|
||||
|
||||
export function displayType (typeDef: Pick<TypeDef, any>): string {
|
||||
if (typeDef.displayName) {
|
||||
return encodeWithParams(typeDef);
|
||||
}
|
||||
|
||||
switch (typeDef.info) {
|
||||
case TypeDefInfo.Struct:
|
||||
case TypeDefInfo.Enum:
|
||||
return encodeWithParams(typeDef);
|
||||
|
||||
default:
|
||||
return encodeType(typeDef);
|
||||
}
|
||||
}
|
||||
|
||||
export function withTypeString (typeDef: Pick<TypeDef, any>): Pick<TypeDef, any> {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
const type = SPECIAL_TYPES.includes(typeDef.displayName)
|
||||
? typeDef.displayName
|
||||
: encodeType(typeDef);
|
||||
|
||||
return {
|
||||
...typeDef,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
type
|
||||
type: encodeTypeDef(typeDef)
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user