Add toHuman with base formatting (#1878)
* Add toHuman with base formatting * Balance formatting * Externalize Enum decode
This commit is contained in:
@@ -79,6 +79,15 @@ export default abstract class AbstractArray<T extends Codec> extends Array<T> 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
|
||||
*/
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
@@ -146,6 +146,19 @@ export default class BTreeMap<K extends Codec = Codec, V extends Codec = 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 (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
|
||||
*/
|
||||
|
||||
@@ -140,14 +140,29 @@ export default class BTreeSet<V extends Codec = Codec> extends Set<V> 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;
|
||||
}
|
||||
|
||||
|
||||
@@ -57,6 +57,13 @@ export default abstract class Base<T extends Codec> 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
|
||||
*/
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
@@ -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<Codec> {
|
||||
|
||||
constructor (registry: Registry, def: Record<string, InterfaceTypes | Constructor> | 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<Codec> {
|
||||
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, InterfaceTypes | Constructor> | string[]): EnumConstructor<Enum> {
|
||||
return class extends Enum {
|
||||
constructor (registry: Registry, value?: any, index?: number) {
|
||||
@@ -261,6 +261,15 @@ export default class Enum extends Base<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 (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
|
||||
*/
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
@@ -176,6 +176,13 @@ export default class CodecSet extends Set<string> 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
|
||||
*/
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
@@ -83,6 +83,13 @@ export default class StructAny extends Map<string, any> 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
|
||||
*/
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
@@ -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<ExtrinsicVx | ExtrinsicUnknown> 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
|
||||
*/
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
@@ -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<ExtrinsicPayloadVx> {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
@@ -55,5 +55,6 @@ export type ITuple<Sub extends Codec[]> = Sub & Codec
|
||||
|
||||
export interface IU8a extends Uint8Array, Codec {
|
||||
bitLength (): number;
|
||||
toHuman (isExtended?: boolean): any;
|
||||
toJSON (): any;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user