From edff4231ffc4c60a2548d21e9f1127936afab822 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 27 Feb 2020 18:08:41 +0100 Subject: [PATCH] Dec-clutter UInt & Int (#1969) --- packages/types/src/codec/AbstractInt.ts | 36 +++++++++++++++--- packages/types/src/codec/Int.ts | 33 ----------------- packages/types/src/codec/UInt.ts | 49 ------------------------- 3 files changed, 30 insertions(+), 88 deletions(-) diff --git a/packages/types/src/codec/AbstractInt.ts b/packages/types/src/codec/AbstractInt.ts index f3c8620bb9..5b3bab4ce5 100644 --- a/packages/types/src/codec/AbstractInt.ts +++ b/packages/types/src/codec/AbstractInt.ts @@ -6,7 +6,7 @@ import { H256 } from '../interfaces/runtime'; import { AnyNumber, Codec, Registry } from '../types'; import BN from 'bn.js'; -import { assert, bnToBn, formatNumber, hexToBn, isHex, isString, isU8a, u8aToBn } from '@polkadot/util'; +import { assert, bnToBn, bnToHex, bnToU8a, formatBalance, formatNumber, hexToBn, isHex, isString, isU8a, u8aToBn } from '@polkadot/util'; import { blake2AsU8a } from '@polkadot/util-crypto'; import Raw from './Raw'; @@ -140,14 +140,24 @@ export default abstract class AbstractInt extends BN implements Codec { /** * @description Returns a hex string representation of the value */ - abstract toHex (): string; + public toHex (isLe = false): string { + // For display/JSON, this is BE, for compare, use isLe + return bnToHex(this, { + bitLength: this.bitLength(), + isLe, + isNegative: !this.isUnsigned + }); + } /** * @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); + public toHuman (isExpanded?: boolean): any { + // FIXME we need proper expansion here + return this instanceof this.registry.createClass('Balance') + ? formatBalance(this, { decimals: this.registry.chainDecimals, withSi: true, withUnit: this.registry.chainToken }) + : formatNumber(this); } /** @@ -165,7 +175,14 @@ export default abstract class AbstractInt extends BN implements Codec { /** * @description Returns the base runtime type name for this instance */ - abstract toRawType (): string; + public toRawType (): string { + // NOTE In the case of balances, which have a special meaning on the UI + // and can be interpreted differently, return a specific value for it so + // underlying it always matches (no matter which length it actually is) + return this instanceof this.registry.createClass('Balance') + ? 'Balance' + : `${this.isUnsigned ? 'u' : 'i'}${this.bitLength()}`; + } /** * @description Returns the string representation of the value @@ -180,5 +197,12 @@ export default abstract class AbstractInt extends BN implements Codec { * @description Encodes the value as a Uint8Array as per the SCALE specifications * @param isBare true when the value has none of the type-specific prefixes (internal) */ - abstract toU8a (isBare?: boolean): Uint8Array; + // eslint-disable-next-line @typescript-eslint/no-unused-vars + public toU8a (isBare?: boolean): Uint8Array { + return bnToU8a(this, { + bitLength: this.bitLength(), + isLe: true, + isNegative: !this.isUnsigned + }); + } } diff --git a/packages/types/src/codec/Int.ts b/packages/types/src/codec/Int.ts index b54cad411c..4ae326538d 100644 --- a/packages/types/src/codec/Int.ts +++ b/packages/types/src/codec/Int.ts @@ -4,8 +4,6 @@ import { AnyNumber, Constructor, Registry } from '../types'; -import { bnToHex, bnToU8a } from '@polkadot/util'; - import AbstractInt, { DEFAULT_UINT_BITS, UIntBitLength } from './AbstractInt'; /** @@ -34,35 +32,4 @@ export default class Int extends AbstractInt { } }; } - - /** - * @description Returns a hex string representation of the value - */ - public toHex (isLe = false): string { - return bnToHex(this, { - bitLength: this.bitLength(), - isLe, - isNegative: true - }); - } - - /** - * @description Returns the base runtime type name for this instance - */ - public toRawType (): string { - return `i${this.bitLength()}`; - } - - /** - * @description Encodes the value as a Uint8Array as per the SCALE specifications - * @param isBare true when the value has none of the type-specific prefixes (internal) - */ - // eslint-disable-next-line @typescript-eslint/no-unused-vars - public toU8a (isBare?: boolean): Uint8Array { - return bnToU8a(this, { - bitLength: this.bitLength(), - isLe: true, - isNegative: true - }); - } } diff --git a/packages/types/src/codec/UInt.ts b/packages/types/src/codec/UInt.ts index 45039662a2..75ba9c3cb3 100644 --- a/packages/types/src/codec/UInt.ts +++ b/packages/types/src/codec/UInt.ts @@ -4,8 +4,6 @@ import { AnyNumber, Constructor, Registry } from '../types'; -import { bnToHex, bnToU8a, formatBalance } from '@polkadot/util'; - import AbstractInt, { DEFAULT_UINT_BITS, UIntBitLength } from './AbstractInt'; /** @@ -34,51 +32,4 @@ export default class UInt extends AbstractInt { } }; } - - /** - * @description Returns a hex string representation of the value - */ - public toHex (isLe = false): string { - // For display/JSON, this is BE, for compare, use isLe - return bnToHex(this, { - bitLength: this.bitLength(), - isLe, - isNegative: false - }); - } - - /** - * @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 this.registry.createClass('Balance') - ? formatBalance(this, { decimals: this.registry.chainDecimals, withSi: true, withUnit: this.registry.chainToken }) - : super.toHuman(isExpanded); - } - - /** - * @description Returns the base runtime type name for this instance - */ - public toRawType (): string { - // NOTE In the case of balances, which have a special meaning on the UI - // and can be interpreted differently, return a specific value for it so - // underlying it always matches (no matter which length it actually is) - return this instanceof this.registry.createClass('Balance') - ? 'Balance' - : `u${this.bitLength()}`; - } - - /** - * @description Encodes the value as a Uint8Array as per the SCALE specifications - * @param isBare true when the value has none of the type-specific prefixes (internal) - */ - // eslint-disable-next-line @typescript-eslint/no-unused-vars - public toU8a (isBare?: boolean): Uint8Array { - return bnToU8a(this, { - bitLength: this.bitLength(), - isLe: true, - isNegative: false - }); - } }