Flatten displayType & encodeType into encodeTypeDef (#2698)

* Flatten displayType & encodeType into encodeTypeDef

* Remove to-be-replaced MetaRegistry tests (hardcoded results)
This commit is contained in:
Jaco Greeff
2020-10-09 15:13:52 +02:00
committed by GitHub
parent 3b1845f0a0
commit 6a0063addd
4 changed files with 19 additions and 73 deletions
@@ -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);
});
});
});
+3 -3
View File
@@ -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}`);