From 83d780220c9bf870ed612193776ce8ed415a0247 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Mon, 17 Feb 2020 11:43:08 +0100 Subject: [PATCH] Add toHuman with base formatting (#1878) * Add toHuman with base formatting * Balance formatting * Externalize Enum decode --- packages/types/src/codec/AbstractArray.ts | 9 +++++ packages/types/src/codec/AbstractInt.ts | 10 ++++- packages/types/src/codec/BTreeMap.ts | 13 +++++++ packages/types/src/codec/BTreeSet.ts | 15 +++++++ packages/types/src/codec/Base.ts | 7 ++++ packages/types/src/codec/Date.ts | 7 ++++ packages/types/src/codec/Enum.ts | 39 ++++++++++++------- packages/types/src/codec/Raw.ts | 7 ++++ packages/types/src/codec/Set.ts | 7 ++++ packages/types/src/codec/Struct.ts | 14 +++++++ packages/types/src/codec/StructAny.ts | 7 ++++ packages/types/src/codec/UInt.ts | 12 +++++- packages/types/src/primitive/Bool.ts | 7 ++++ .../src/primitive/Extrinsic/Extrinsic.ts | 22 ++++++++++- .../src/primitive/Extrinsic/ExtrinsicEra.ts | 8 ++++ .../primitive/Extrinsic/ExtrinsicPayload.ts | 9 ++++- .../types/src/primitive/Generic/AccountId.ts | 7 ++++ .../src/primitive/Generic/AccountIndex.ts | 7 ++++ packages/types/src/primitive/Generic/Call.ts | 24 +++++++++++- packages/types/src/primitive/Null.ts | 7 ++++ packages/types/src/primitive/Text.ts | 7 ++++ packages/types/src/types/codec.ts | 5 +++ packages/types/src/types/interfaces.ts | 1 + 23 files changed, 230 insertions(+), 21 deletions(-) diff --git a/packages/types/src/codec/AbstractArray.ts b/packages/types/src/codec/AbstractArray.ts index fa9dc55109..bc32fe6ed8 100644 --- a/packages/types/src/codec/AbstractArray.ts +++ b/packages/types/src/codec/AbstractArray.ts @@ -79,6 +79,15 @@ export default abstract class AbstractArray extends Array im return u8aToHex(this.toU8a()); } + /** + * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information + */ + public toHuman (isExtended?: boolean): AnyJsonArray { + return this.map((entry): AnyJson => + entry.toHuman(isExtended) + ); + } + /** * @description Converts the Object to JSON, typically used for RPC transfers */ diff --git a/packages/types/src/codec/AbstractInt.ts b/packages/types/src/codec/AbstractInt.ts index ffd420df65..cc895613c2 100644 --- a/packages/types/src/codec/AbstractInt.ts +++ b/packages/types/src/codec/AbstractInt.ts @@ -5,7 +5,7 @@ import { AnyNumber, Codec, IHash, Registry } from '../types'; import BN from 'bn.js'; -import { bnToBn, hexToBn, isHex, isString, isU8a, u8aToBn } from '@polkadot/util'; +import { bnToBn, formatNumber, hexToBn, isHex, isString, isU8a, u8aToBn } from '@polkadot/util'; import { blake2AsU8a } from '@polkadot/util-crypto'; import Raw from './Raw'; @@ -132,6 +132,14 @@ export default abstract class AbstractInt extends BN implements Codec { */ abstract toHex (): string; + /** + * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information + */ + // eslint-disable-next-line @typescript-eslint/no-unused-vars + public toHuman (isExpanded?: boolean): string { + return formatNumber(this); + } + /** * @description Converts the Object to JSON, typically used for RPC transfers */ diff --git a/packages/types/src/codec/BTreeMap.ts b/packages/types/src/codec/BTreeMap.ts index 54f660757e..128cd6eccb 100644 --- a/packages/types/src/codec/BTreeMap.ts +++ b/packages/types/src/codec/BTreeMap.ts @@ -146,6 +146,19 @@ export default class BTreeMap return u8aToHex(this.toU8a()); } + /** + * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information + */ + public toHuman (isExtended?: boolean): AnyJson { + const json: any = {}; + + this.forEach((v: V, k: K) => { + json[k.toString()] = v.toHuman(isExtended); + }); + + return json; + } + /** * @description Converts the Object to JSON, typically used for RPC transfers */ diff --git a/packages/types/src/codec/BTreeSet.ts b/packages/types/src/codec/BTreeSet.ts index f578e7c139..fbabf2911b 100644 --- a/packages/types/src/codec/BTreeSet.ts +++ b/packages/types/src/codec/BTreeSet.ts @@ -140,14 +140,29 @@ export default class BTreeSet extends Set implements return u8aToHex(this.toU8a()); } + /** + * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information + */ + public toHuman (isExtended?: boolean): AnyJsonArray { + const json: any = []; + + this.forEach((v: V) => { + json.push(v.toHuman(isExtended)); + }); + + return json; + } + /** * @description Converts the Object to JSON, typically used for RPC transfers */ public toJSON (): AnyJsonArray { const json: any = []; + this.forEach((v: V) => { json.push(v.toJSON()); }); + return json; } diff --git a/packages/types/src/codec/Base.ts b/packages/types/src/codec/Base.ts index de7e89606a..078f1ec2a5 100644 --- a/packages/types/src/codec/Base.ts +++ b/packages/types/src/codec/Base.ts @@ -57,6 +57,13 @@ export default abstract class Base implements Codec { return this.raw.toHex(isLe); } + /** + * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information + */ + public toHuman (isExtended?: boolean): AnyJson { + return this.raw.toHuman(isExtended); + } + /** * @description Converts the Object to JSON, typically used for RPC transfers */ diff --git a/packages/types/src/codec/Date.ts b/packages/types/src/codec/Date.ts index 01ef206eb4..7102b35bb5 100644 --- a/packages/types/src/codec/Date.ts +++ b/packages/types/src/codec/Date.ts @@ -103,6 +103,13 @@ export default class CodecDate extends Date implements Codec { }); } + /** + * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information + */ + public toHuman (): string { + return this.toISOString(); + } + /** * @description Converts the Object to JSON, typically used for RPC transfers */ diff --git a/packages/types/src/codec/Enum.ts b/packages/types/src/codec/Enum.ts index 78160b6c3f..1090125fa7 100644 --- a/packages/types/src/codec/Enum.ts +++ b/packages/types/src/codec/Enum.ts @@ -91,6 +91,20 @@ function decodeFromValue (registry: Registry, def: TypesDef, value?: any): Decod return createFromValue(registry, def, 0); } +function decodeEnum (registry: Registry, def: TypesDef, value?: any, index?: number): Decoded { + // NOTE We check the index path first, before looking at values - this allows treating + // the optional indexes before anything else, more-specific > less-specific + if (isNumber(index)) { + return createFromValue(registry, def, index, value); + // eslint-disable-next-line @typescript-eslint/no-use-before-define + } else if (value instanceof Enum) { + return createFromValue(registry, def, value.index, value.value); + } + + // Or else, we just look at `value` + return decodeFromValue(registry, def, value); +} + /** * @name Enum * @description @@ -111,7 +125,7 @@ export default class Enum extends Base { constructor (registry: Registry, def: Record | string[], value?: any, index?: number) { const defInfo = extractDef(registry, def); - const decoded = Enum.decodeEnum(registry, defInfo.def, value, index); + const decoded = decodeEnum(registry, defInfo.def, value, index); super(registry, decoded.value); @@ -121,20 +135,6 @@ export default class Enum extends Base { this._index = this._indexes.indexOf(decoded.index) || 0; } - /** @internal */ - private static decodeEnum (registry: Registry, def: TypesDef, value?: any, index?: number): Decoded { - // NOTE We check the index path first, before looking at values - this allows treating - // the optional indexes before anything else, more-specific > less-specific - if (isNumber(index)) { - return createFromValue(registry, def, index, value); - } else if (value instanceof Enum) { - return createFromValue(registry, def, value._index, value.raw); - } - - // Or else, we just look at `value` - return decodeFromValue(registry, def, value); - } - public static with (Types: Record | string[]): EnumConstructor { return class extends Enum { constructor (registry: Registry, value?: any, index?: number) { @@ -261,6 +261,15 @@ export default class Enum extends Base { return u8aToHex(this.toU8a()); } + /** + * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information + */ + public toHuman (isExtended?: boolean): AnyJson { + return this._isBasic + ? this.type + : { [this.type]: this.raw.toHuman(isExtended) }; + } + /** * @description Converts the Object to JSON, typically used for RPC transfers */ diff --git a/packages/types/src/codec/Raw.ts b/packages/types/src/codec/Raw.ts index 09790b07db..4dba116f30 100644 --- a/packages/types/src/codec/Raw.ts +++ b/packages/types/src/codec/Raw.ts @@ -98,6 +98,13 @@ export default class Raw extends Uint8Array implements IU8a { return u8aToHex(this); } + /** + * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information + */ + public toHuman (): string { + return this.toJSON(); + } + /** * @description Converts the Object to JSON, typically used for RPC transfers */ diff --git a/packages/types/src/codec/Set.ts b/packages/types/src/codec/Set.ts index dee4925dbd..36aa594648 100644 --- a/packages/types/src/codec/Set.ts +++ b/packages/types/src/codec/Set.ts @@ -176,6 +176,13 @@ export default class CodecSet extends Set implements Codec { return u8aToHex(this.toU8a()); } + /** + * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information + */ + public toHuman (): string[] { + return this.toJSON(); + } + /** * @description Converts the Object to JSON, typically used for RPC transfers */ diff --git a/packages/types/src/codec/Struct.ts b/packages/types/src/codec/Struct.ts index d439f14677..b41f6de4b5 100644 --- a/packages/types/src/codec/Struct.ts +++ b/packages/types/src/codec/Struct.ts @@ -228,6 +228,20 @@ export default class Struct< return u8aToHex(this.toU8a()); } + /** + * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information + */ + public toHuman (isExtended?: boolean): AnyJsonObject { + return [...this.keys()].reduce((json, key): any => { + const jsonKey = this._jsonMap.get(key) || key; + const value = this.get(key); + + json[jsonKey] = value && value.toHuman(isExtended); + + return json; + }, {} as any); + } + /** * @description Converts the Object to JSON, typically used for RPC transfers */ diff --git a/packages/types/src/codec/StructAny.ts b/packages/types/src/codec/StructAny.ts index 584947e736..ead9cb5aa7 100644 --- a/packages/types/src/codec/StructAny.ts +++ b/packages/types/src/codec/StructAny.ts @@ -83,6 +83,13 @@ export default class StructAny extends Map implements Codec { throw new Error('Unimplemented'); } + /** + * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information + */ + public toHuman (): AnyJsonObject { + return this.toJSON(); + } + /** * @description Converts the Object to JSON, typically used for RPC transfers */ diff --git a/packages/types/src/codec/UInt.ts b/packages/types/src/codec/UInt.ts index 414b8a4974..c2c301e262 100644 --- a/packages/types/src/codec/UInt.ts +++ b/packages/types/src/codec/UInt.ts @@ -4,7 +4,7 @@ import { AnyNumber, Constructor, Registry } from '../types'; -import { bnToHex, bnToU8a } from '@polkadot/util'; +import { bnToHex, bnToU8a, formatBalance } from '@polkadot/util'; import { ClassOf } from '../create/createClass'; import AbstractInt, { DEFAULT_UINT_BITS, UIntBitLength } from './AbstractInt'; @@ -44,6 +44,16 @@ export default class UInt extends AbstractInt { }); } + /** + * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information + */ + public toHuman (isExpanded?: boolean): any { + // FIXME we need proper expansion here + return this instanceof ClassOf(this.registry, 'Balance') + ? formatBalance(this, { withSi: true, withUnit: true }) + : super.toHuman(isExpanded); + } + /** * @description Returns the base runtime type name for this instance */ diff --git a/packages/types/src/primitive/Bool.ts b/packages/types/src/primitive/Bool.ts index dd48f19a2c..f66a85177f 100644 --- a/packages/types/src/primitive/Bool.ts +++ b/packages/types/src/primitive/Bool.ts @@ -89,6 +89,13 @@ export default class Bool extends Boolean implements Codec { return u8aToHex(this.toU8a()); } + /** + * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information + */ + public toHuman (): boolean { + return this.toJSON(); + } + /** * @description Converts the Object to JSON, typically used for RPC transfers */ diff --git a/packages/types/src/primitive/Extrinsic/Extrinsic.ts b/packages/types/src/primitive/Extrinsic/Extrinsic.ts index df50591834..5827f33bf6 100644 --- a/packages/types/src/primitive/Extrinsic/Extrinsic.ts +++ b/packages/types/src/primitive/Extrinsic/Extrinsic.ts @@ -4,7 +4,7 @@ import { FunctionMetadataLatest } from '../../interfaces/metadata/types'; import { Address, Balance, Call, EcdsaSignature, Ed25519Signature, ExtrinsicUnknown, ExtrinsicV1, ExtrinsicV2, ExtrinsicV3, ExtrinsicV4, Index, Sr25519Signature } from '../../interfaces/runtime'; -import { AnyU8a, ArgsDef, Codec, ExtrinsicPayloadValue, IExtrinsic, IKeyringPair, InterfaceTypes, Registry, SignatureOptions } from '../../types'; +import { ArgsDef, AnyJsonObject, AnyU8a, Codec, ExtrinsicPayloadValue, IExtrinsic, IKeyringPair, InterfaceTypes, Registry, SignatureOptions } from '../../types'; import { assert, isHex, isU8a, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util'; @@ -272,6 +272,26 @@ export default class Extrinsic extends Base impl return u8aToHex(this.toU8a(isBare)); } + /** + * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information + */ + public toHuman (isExpanded?: boolean): AnyJsonObject { + return { + call: this.method.toHuman(isExpanded), + isSigned: this.isSigned, + ...(this.isSigned + ? { + signer: this.signer.toHuman(isExpanded), + nonce: this.nonce.toHuman(isExpanded), + era: this.era.toHuman(isExpanded), + tip: this.tip.toHuman(isExpanded), + signature: this.signature.toHex() + } + : {} + ) + }; + } + /** * @description Converts the Object to JSON, typically used for RPC transfers */ diff --git a/packages/types/src/primitive/Extrinsic/ExtrinsicEra.ts b/packages/types/src/primitive/Extrinsic/ExtrinsicEra.ts index d5839a15e1..604c168f7a 100644 --- a/packages/types/src/primitive/Extrinsic/ExtrinsicEra.ts +++ b/packages/types/src/primitive/Extrinsic/ExtrinsicEra.ts @@ -123,6 +123,14 @@ export class MortalEra extends Tuple { return this[1] as U64; } + /** + * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information + */ + public toHuman (): any { + // FIXME we need proper expansion here + return this.toJSON(); + } + /** * @description Returns a JSON representation of the actual value */ diff --git a/packages/types/src/primitive/Extrinsic/ExtrinsicPayload.ts b/packages/types/src/primitive/Extrinsic/ExtrinsicPayload.ts index 7c18db6eb5..ad151ebba4 100644 --- a/packages/types/src/primitive/Extrinsic/ExtrinsicPayload.ts +++ b/packages/types/src/primitive/Extrinsic/ExtrinsicPayload.ts @@ -3,7 +3,7 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { Balance, ExtrinsicPayloadV1, ExtrinsicPayloadV2, ExtrinsicPayloadV3, ExtrinsicPayloadV4, Hash, Index } from '../../interfaces/runtime'; -import { BareOpts, ExtrinsicPayloadValue, IKeyringPair, InterfaceTypes, Registry } from '../../types'; +import { AnyJsonObject, BareOpts, ExtrinsicPayloadValue, IKeyringPair, InterfaceTypes, Registry } from '../../types'; import { u8aToHex } from '@polkadot/util'; @@ -124,6 +124,13 @@ export default class ExtrinsicPayload extends Base { }; } + /** + * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information + */ + public toHuman (isExtended?: boolean): AnyJsonObject { + return this.raw.toHuman(isExtended); + } + /** * @description Converts the Object to JSON, typically used for RPC transfers */ diff --git a/packages/types/src/primitive/Generic/AccountId.ts b/packages/types/src/primitive/Generic/AccountId.ts index 448bea8ed6..980bf59972 100644 --- a/packages/types/src/primitive/Generic/AccountId.ts +++ b/packages/types/src/primitive/Generic/AccountId.ts @@ -45,6 +45,13 @@ export default class AccountId extends U8aFixed { return super.eq(decodeAccountId(other)); } + /** + * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information + */ + public toHuman (): string { + return this.toJSON(); + } + /** * @description Converts the Object to JSON, typically used for RPC transfers */ diff --git a/packages/types/src/primitive/Generic/AccountIndex.ts b/packages/types/src/primitive/Generic/AccountIndex.ts index 728bd5d49a..0b04f572d3 100644 --- a/packages/types/src/primitive/Generic/AccountIndex.ts +++ b/packages/types/src/primitive/Generic/AccountIndex.ts @@ -95,6 +95,13 @@ export default class AccountIndex extends U32 { return super.eq(createType(this.registry, 'AccountIndex', other)); } + /** + * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information + */ + public toHuman (): string { + return this.toJSON(); + } + /** * @description Converts the Object to JSON, typically used for RPC transfers */ diff --git a/packages/types/src/primitive/Generic/Call.ts b/packages/types/src/primitive/Generic/Call.ts index f36e982d7a..7ee51c333c 100644 --- a/packages/types/src/primitive/Generic/Call.ts +++ b/packages/types/src/primitive/Generic/Call.ts @@ -3,9 +3,9 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { FunctionArgumentMetadataLatest, FunctionMetadataLatest } from '../../interfaces/metadata'; -import { AnyU8a, ArgsDef, Codec, IMethod, Registry } from '../../types'; +import { AnyJsonObject, AnyU8a, ArgsDef, CallFunction, Codec, IMethod, Registry } from '../../types'; -import { isHex, isObject, isU8a, u8aToU8a } from '@polkadot/util'; +import { isHex, isObject, isU8a, u8aToHex, u8aToU8a } from '@polkadot/util'; import { getTypeDef, getTypeClass } from '../../create'; import Struct from '../../codec/Struct'; @@ -199,6 +199,26 @@ export default class Call extends Struct implements IMethod { return this.registry.findMetaCall(this.callIndex).section; } + /** + * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information + */ + public toHuman (isExpanded?: boolean): AnyJsonObject { + let call: CallFunction | undefined; + + try { + call = this.registry.findMetaCall(this.callIndex); + } catch (error) { + // swallow + } + + return { + callIndex: u8aToHex(this.callIndex), + section: call?.section, + method: call?.method, + args: this.args.map((arg) => arg.toHuman(isExpanded)) + }; + } + /** * @description Returns the base runtime type name for this instance */ diff --git a/packages/types/src/primitive/Null.ts b/packages/types/src/primitive/Null.ts index bc11380db3..50d35f6db6 100644 --- a/packages/types/src/primitive/Null.ts +++ b/packages/types/src/primitive/Null.ts @@ -53,6 +53,13 @@ export default class Null implements Codec { return '0x'; } + /** + * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information + */ + public toHuman (): null { + return this.toJSON(); + } + /** * @description Converts the Object to JSON, typically used for RPC transfers */ diff --git a/packages/types/src/primitive/Text.ts b/packages/types/src/primitive/Text.ts index 5cd8d61ffc..30720aa7e9 100644 --- a/packages/types/src/primitive/Text.ts +++ b/packages/types/src/primitive/Text.ts @@ -111,6 +111,13 @@ export default class Text extends String implements Codec { return u8aToHex(this.toU8a(true)); } + /** + * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information + */ + public toHuman (): string { + return this.toJSON(); + } + /** * @description Converts the Object to JSON, typically used for RPC transfers */ diff --git a/packages/types/src/types/codec.ts b/packages/types/src/types/codec.ts index 734b88f1e5..c6b06e5fd9 100644 --- a/packages/types/src/types/codec.ts +++ b/packages/types/src/types/codec.ts @@ -67,6 +67,11 @@ export interface Codec { */ toHex (isLe?: boolean): string; + /** + * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information + */ + toHuman (isExtended?: boolean): AnyJson; + /** * @description Converts the Object to JSON, typically used for RPC transfers */ diff --git a/packages/types/src/types/interfaces.ts b/packages/types/src/types/interfaces.ts index 3040764e58..0e74d8e741 100644 --- a/packages/types/src/types/interfaces.ts +++ b/packages/types/src/types/interfaces.ts @@ -55,5 +55,6 @@ export type ITuple = Sub & Codec export interface IU8a extends Uint8Array, Codec { bitLength (): number; + toHuman (isExtended?: boolean): any; toJSON (): any; }