diff --git a/packages/types/src/codec/utils/encodeTypes.ts b/packages/types/src/codec/utils/encodeTypes.ts index 27660ded99..591f2bd945 100644 --- a/packages/types/src/codec/utils/encodeTypes.ts +++ b/packages/types/src/codec/utils/encodeTypes.ts @@ -8,7 +8,7 @@ import { assert } from '@polkadot/util'; import { getTypeDef } from '../create'; -const SPECIAL_TYPES = ['AccountId', 'AccountIndex', 'Address', 'Balance']; +export const SPECIAL_TYPES = ['AccountId', 'AccountIndex', 'Address', 'Balance']; const identity = (value: string): string => value; @@ -23,22 +23,32 @@ export function paramsNotation (outer: string, inner?: string | any[], transform } function encodeWithParams (typeDef: Pick, outer = typeDef.displayName || typeDef.type): string { - const { params } = typeDef; + const { sub, params } = typeDef; return paramsNotation( outer, - params, + params || sub, // eslint-disable-next-line @typescript-eslint/no-use-before-define (param: TypeDef) => displayType(param) ); } function encodeSubTypes (sub: TypeDef[], asEnum?: boolean): string { - return `{ ${asEnum ? '"_enum": { ' : ''}${ - sub - .map((type: TypeDef): string => `"${type.name}": "${encodeWithParams(type)}"`) - .join(', ') - }${asEnum ? '" }"' : ''} }`; + const inner = sub.reduce( + (result: Record, type: TypeDef): Record => { + return { + ...result, + [type.name as string]: encodeWithParams(type) + }; + }, + {} + ); + + return JSON.stringify( + asEnum + ? { _enum: inner } + : inner + ); } function encodeEnum (typeDef: Pick): string { @@ -138,10 +148,12 @@ export function displayType (typeDef: Pick): string { } export function withTypeString (typeDef: Pick): Pick { + const type = SPECIAL_TYPES.includes(typeDef.displayName) + ? typeDef.displayName + : encodeType(typeDef); + return { ...typeDef, - type: SPECIAL_TYPES.includes(typeDef.name) - ? typeDef.name - : encodeType(typeDef) + type }; }