diff --git a/packages/api-contract/src/ContractRegistry.ts b/packages/api-contract/src/ContractRegistry.ts index 2f998eabfa..b94e8f95b3 100644 --- a/packages/api-contract/src/ContractRegistry.ts +++ b/packages/api-contract/src/ContractRegistry.ts @@ -45,7 +45,7 @@ export default class ContractRegistry { let typeDef = this.typeDefs[offset]; if (!typeDef) { - typeDef = this.extractType(this.getAbiType(id), id) as TypeDef; + typeDef = this.extractType(this.getAbiType(id), id); this.typeDefs[offset] = typeDef; @@ -61,9 +61,9 @@ export default class ContractRegistry { return typeDef; } - private extractType (inkType: MtType, id: MtLookupTypeId): Pick { + private extractType (inkType: MtType, id: MtLookupTypeId): TypeDef { const path = [...inkType.path]; - let typeDef; + let typeDef: Omit; if (inkType.path.join('::').startsWith('ink_env::types::') || inkType.def.isPrimitive) { typeDef = this.extractPrimitive(inkType); @@ -100,7 +100,7 @@ export default class ContractRegistry { }); } - private extractVariant ({ variants }: MtTypeDefVariant, id: MtLookupTypeId): Pick { + private extractVariant ({ variants }: MtTypeDefVariant, id: MtLookupTypeId): Omit { const { params, path } = this.getAbiType(id); const specialVariant = path[0].toString(); @@ -125,7 +125,7 @@ export default class ContractRegistry { }; } - private extractVariantSub (variants: MtVariant[]): Pick[] { + private extractVariantSub (variants: MtVariant[]): TypeDef[] { const isAllUnitVariants = variants.every(({ fields }) => fields.length === 0); if (isAllUnitVariants) { @@ -141,13 +141,15 @@ export default class ContractRegistry { })); } - return variants.map(({ fields, name }) => ({ - ...this.extractFields(fields), - name: name.toString() - })); + return variants.map(({ fields, name }) => + withTypeString({ + ...this.extractFields(fields), + name: name.toString() + }) + ); } - private extractFields (fields: MtField[]): Pick { + private extractFields (fields: MtField[]): Omit { const [isStruct, isTuple] = fields.reduce(([isAllNamed, isAllUnnamed], { name }) => ([ isAllNamed && name.isSome, isAllUnnamed && name.isNone @@ -177,7 +179,7 @@ export default class ContractRegistry { : { info, sub }; } - private extractArray ({ len: length, type }: MtTypeDefArray): Pick { + private extractArray ({ len: length, type }: MtTypeDefArray): Omit { assert(!length || length.toNumber() <= 256, 'ContractRegistry: Only support for [Type; ], where length > 256'); return { @@ -187,7 +189,7 @@ export default class ContractRegistry { }; } - private extractSequence ({ type }: MtTypeDefSequence, id: MtLookupTypeId): Pick { + private extractSequence ({ type }: MtTypeDefSequence, id: MtLookupTypeId): Omit { assert(!!type, `ContractRegistry: Invalid sequence type found at id ${id.toString()}`); return { @@ -196,7 +198,7 @@ export default class ContractRegistry { }; } - private extractTuple (ids: MtTypeDefTuple): Pick { + private extractTuple (ids: MtTypeDefTuple): Omit { return ids.length === 1 ? this.typeDefAt(ids[0]) : { @@ -205,7 +207,7 @@ export default class ContractRegistry { }; } - private extractPrimitive (inkType: MtType): Pick { + private extractPrimitive (inkType: MtType): TypeDef { if (inkType.def.isPrimitive) { return { info: TypeDefInfo.Plain, diff --git a/packages/api-contract/test/compare/multisigPlain.test.json b/packages/api-contract/test/compare/multisigPlain.test.json index dea01e154c..7f8395152c 100644 --- a/packages/api-contract/test/compare/multisigPlain.test.json +++ b/packages/api-contract/test/compare/multisigPlain.test.json @@ -488,7 +488,8 @@ { "info": 10, "sub": [], - "name": "Confirmed" + "name": "Confirmed", + "type": "()" }, { "info": 6, @@ -509,7 +510,8 @@ { "info": 10, "sub": [], - "name": "Confirmed" + "name": "Confirmed", + "type": "()" }, { "info": 6, @@ -677,4 +679,4 @@ }, "type": "Option>" } -] \ No newline at end of file +] diff --git a/packages/types/src/codec/utils/encodeTypes.ts b/packages/types/src/codec/utils/encodeTypes.ts index f39798abf5..b387387ae3 100644 --- a/packages/types/src/codec/utils/encodeTypes.ts +++ b/packages/types/src/codec/utils/encodeTypes.ts @@ -5,11 +5,9 @@ import { TypeDef, TypeDefInfo } from '../../create/types'; import { assert, isNumber, isUndefined } from '@polkadot/util'; -export const SPECIAL_TYPES = ['AccountId', 'AccountIndex', 'Address', 'Balance']; +const stringIdentity = string }> (value: T): string => value.toString(); -const identity = (value: T): T => value; - -export function paramsNotation (outer: string, inner?: string | any[], transform: (_: any) => string = identity): string { +export function paramsNotation (outer: string, inner?: T | T[], transform: (_: T) => string = stringIdentity): string { return `${outer}${ inner ? `<${(Array.isArray(inner) ? inner : [inner]).map(transform).join(', ')}>` @@ -18,7 +16,7 @@ export function paramsNotation (outer: string, inner?: string | any[], transform } // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -function encodeWithParams (typeDef: Pick, outer = typeDef.displayName || typeDef.type): string { +function encodeWithParams (typeDef: TypeDef, outer = typeDef.displayName || typeDef.type): string { const { info, params, sub } = typeDef; switch (info) { @@ -34,7 +32,6 @@ function encodeWithParams (typeDef: Pick, outer = typeDef.displayN return paramsNotation(outer, params || sub, (param) => encodeTypeDef(param)); default: - // eslint-disable-next-line @typescript-eslint/no-unsafe-return return outer; } } @@ -57,10 +54,10 @@ function encodeSubTypes (sub: TypeDef[], asEnum?: boolean): string { ); } -function encodeEnum (typeDef: Pick): string { +function encodeEnum (typeDef: TypeDef): string { assert(typeDef.sub && Array.isArray(typeDef.sub), 'Unable to encode Enum type'); - const sub = typeDef.sub as TypeDef[]; + const sub = typeDef.sub; // c-like enums have all Null entries // TODO We need to take the disciminant into account and auto-add empty entries @@ -69,29 +66,28 @@ function encodeEnum (typeDef: Pick): string { : encodeSubTypes(sub, true); } -function encodeStruct (typeDef: Pick): string { +function encodeStruct (typeDef: TypeDef): string { assert(typeDef.sub && Array.isArray(typeDef.sub), 'Unable to encode Struct type'); - return encodeSubTypes(typeDef.sub as TypeDef[]); + return encodeSubTypes(typeDef.sub); } -function encodeTuple (typeDef: Pick): string { +function encodeTuple (typeDef: TypeDef): string { assert(typeDef.sub && Array.isArray(typeDef.sub), 'Unable to encode Tuple type'); // eslint-disable-next-line @typescript-eslint/no-use-before-define - return `(${(typeDef.sub as TypeDef[]).map((type) => encodeTypeDef(type)).join(', ')})`; + return `(${typeDef.sub.map((type) => encodeTypeDef(type)).join(', ')})`; } -function encodeUInt ({ length }: Pick, type: 'Int' | 'UInt'): string { +function encodeUInt ({ length }: TypeDef, type: 'Int' | 'UInt'): string { assert(isNumber(length), 'Unable to encode VecFixed type'); return `${type}<${length}>`; } -function encodeVecFixed ({ length, sub }: Pick): string { - assert(isNumber(length) && !isUndefined(sub), 'Unable to encode VecFixed type'); +function encodeVecFixed ({ length, sub }: TypeDef): string { + assert(isNumber(length) && !isUndefined(sub) && !Array.isArray(sub), 'Unable to encode VecFixed type'); - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/restrict-template-expressions return `[${sub.type};${length}]`; } @@ -119,15 +115,15 @@ const encoders: Record string> = { [TypeDefInfo.VecFixed]: (typeDef: TypeDef): string => encodeVecFixed(typeDef) }; -function encodeType (typeDef: Pick): string { - const encoder = encoders[(typeDef as TypeDef).info]; +function encodeType (typeDef:TypeDef): string { + const encoder = encoders[typeDef.info]; assert(encoder, `Cannot encode type: ${JSON.stringify(typeDef)}`); - return encoder(typeDef as TypeDef); + return encoder(typeDef); } -export function encodeTypeDef (typeDef: Pick): string { +export function encodeTypeDef (typeDef: TypeDef): string { assert(!isUndefined(typeDef.info), `Invalid type definition with no instance info, ${JSON.stringify(typeDef)}`); return typeDef.displayName || [TypeDefInfo.Enum, TypeDefInfo.Struct].includes(typeDef.info) @@ -135,11 +131,9 @@ export function encodeTypeDef (typeDef: Pick): string { : encodeType(typeDef); } -export function withTypeString (typeDef: Pick): Pick { +export function withTypeString (typeDef: Omit): TypeDef { return { ...typeDef, - type: SPECIAL_TYPES.includes(typeDef.displayName) - ? typeDef.displayName as string - : encodeType(typeDef) + type: encodeType(typeDef as TypeDef) }; }