diff --git a/packages/types-codec/src/abstract/Base.ts b/packages/types-codec/src/abstract/Base.ts index ea904c48fe..dbd673dee1 100644 --- a/packages/types-codec/src/abstract/Base.ts +++ b/packages/types-codec/src/abstract/Base.ts @@ -37,6 +37,9 @@ export abstract class AbstractBase implements Codec { return this.registry.hash(this.toU8a()); } + /** + * @description returns the inner (wrapped value) + */ public get inner (): T { return this.#raw; } @@ -108,11 +111,19 @@ export abstract class AbstractBase implements Codec { /** * @description Returns the base runtime type name for this instance */ - public toRawType (): string { - return 'Base'; - } + public abstract toRawType (): string; + /** + * @description Returns the inner wrapped value (equivalent to valueOf) + */ public unwrap (): T { return this.#raw; } + + /** + * @description Returns the inner wrapped value + */ + public valueOf (): T { + return this.#raw; + } } diff --git a/packages/types-codec/src/abstract/Int.ts b/packages/types-codec/src/abstract/Int.ts index e00217e0f5..1947cfd52e 100644 --- a/packages/types-codec/src/abstract/Int.ts +++ b/packages/types-codec/src/abstract/Int.ts @@ -2,9 +2,9 @@ // SPDX-License-Identifier: Apache-2.0 import type { HexString } from '@polkadot/util/types'; -import type { AnyNumber, Inspect, INumber, IU8a, Registry, UIntBitLength } from '../types'; +import type { AnyNumber, Inspect, INumber, IU8a, Registry, ToBn, UIntBitLength } from '../types'; -import { BN, BN_BILLION, BN_HUNDRED, BN_MILLION, BN_QUINTILL, bnToBn, bnToHex, bnToU8a, formatBalance, formatNumber, hexToBn, isBn, isFunction, isHex, isNumber, isObject, isString, isU8a, u8aToBn, u8aToNumber } from '@polkadot/util'; +import { BN, BN_BILLION, BN_HUNDRED, BN_MILLION, BN_QUINTILL, bnToBn, bnToHex, bnToU8a, formatBalance, formatNumber, hexToBn, isBigInt, isBn, isFunction, isHex, isNumber, isObject, isString, isU8a, u8aToBn, u8aToNumber } from '@polkadot/util'; export const DEFAULT_UINT_BITS = 64; @@ -20,12 +20,16 @@ const FORMATTERS: [string, BN][] = [ ['Percent', BN_HUNDRED] ]; +function isToBn (value: unknown): value is ToBn { + return isFunction((value as ToBn).toBn); +} + function toPercentage (value: BN, divisor: BN): string { return `${(value.mul(MUL_P).div(divisor).toNumber() / 100).toFixed(2)}%`; } /** @internal */ -function decodeAbstractInt (value: Exclude | Record, isNegative: boolean): string | number { +function decodeAbstractInt (value: Exclude | Record | ToBn, isNegative: boolean): string | number { if (isNumber(value)) { if (!Number.isInteger(value) || value > Number.MAX_SAFE_INTEGER || value < Number.MIN_SAFE_INTEGER) { throw new Error('Number needs to be an integer <= Number.MAX_SAFE_INTEGER, i.e. 2 ^ 53 - 1'); @@ -42,9 +46,13 @@ function decodeAbstractInt (value: Exclude | Record | Record implements CodecObject { + readonly registry: Registry; + + public createdAtHash?: IU8a; + public initialU8aLength?: number; + public isStorageFallback?: boolean; + + readonly $: T; + + protected constructor (registry: Registry, value: T, initialU8aLength?: number) { + this.$ = value; + this.initialU8aLength = initialU8aLength; + this.registry = registry; + } + + /** + * @description The length of the value when encoded as a Uint8Array + */ + public get encodedLength (): number { + return this.toU8a().length; + } + + /** + * @description returns a hash of the contents + */ + public get hash (): IU8a { + return this.registry.hash(this.toU8a()); + } + + /** + * @description Checks if the value is an empty value + */ + public abstract get isEmpty (): boolean; + + /** + * @description Compares the value of the input to see if there is a match + */ + public abstract eq (other?: unknown): boolean; + + /** + * @description Returns a breakdown of the hex encoding for this Codec + */ + public abstract inspect (): Inspect; + + /** + * @description Returns a hex string representation of the value. isLe returns a LE (number-only) representation + */ + public abstract toHex (isLe?: boolean): HexString; + + /** + * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information + */ + public abstract toHuman (isExtended?: boolean): AnyJson; + + /** + * @description Converts the Object to JSON, typically used for RPC transfers + */ + public abstract toJSON (): AnyJson; + + /** + * @description Converts the value in a best-fit primitive form + */ + public abstract toPrimitive (): AnyJson; + + /** + * @description Returns the string representation of the value + */ + public toString (): string { + return this.$.toString(); + } + + /** + * @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) + */ + public abstract toU8a (isBare?: BareOpts): Uint8Array; + + /** + * @description Returns the base runtime type name for this instance + */ + public abstract toRawType (): string; + + /** + * @description Return the internal value (JS-aligned, same result as $) + */ + public valueOf (): T { + return this.$; + } +} diff --git a/packages/types-codec/src/types/codec.ts b/packages/types-codec/src/types/codec.ts index 5b3fa73810..c37dc0d36e 100644 --- a/packages/types-codec/src/types/codec.ts +++ b/packages/types-codec/src/types/codec.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { HexString } from '@polkadot/util/types'; -import type { AnyJson } from './helpers'; +import type { AnyJson, ToString } from './helpers'; import type { IU8a } from './interfaces'; import type { Registry } from './registry'; @@ -127,6 +127,12 @@ export interface CodecClass { new(registry: Registry, ...args: any[]): T; } +export interface CodecObject extends Codec { + readonly $: T; + + valueOf (): T; +} + export type CodecTo = 'toHex' | 'toJSON' | 'toPrimitive' | 'toString' | 'toU8a'; export type ArgsDef = Record; diff --git a/packages/types-codec/src/types/helpers.ts b/packages/types-codec/src/types/helpers.ts index 6723004f10..b809207f80 100644 --- a/packages/types-codec/src/types/helpers.ts +++ b/packages/types-codec/src/types/helpers.ts @@ -34,4 +34,12 @@ export type U8aBitLength = 8 | 16 | 32 | 64 | 128 | 160 | 256 | 264 | 512 | 520 export type AnyTupleValue = Exclude | HexString | (Codec | AnyU8a | AnyNumber | AnyString | undefined | null)[]; +export interface ToString { + toString: () => string; +} + +export interface ToBn { + toBn: () => BN; +} + export type LookupString = `Lookup${number}`; diff --git a/packages/types/src/extrinsic/ExtrinsicPayload.ts b/packages/types/src/extrinsic/ExtrinsicPayload.ts index 2d6fc1a068..4ed3446fc0 100644 --- a/packages/types/src/extrinsic/ExtrinsicPayload.ts +++ b/packages/types/src/extrinsic/ExtrinsicPayload.ts @@ -144,6 +144,13 @@ export class GenericExtrinsicPayload extends AbstractBase { return this.toHex(); } + /** + * @description Returns the base runtime type name for this instance + */ + public toRawType (): string { + return 'ExtrinsicPayload'; + } + /** * @description Returns the string representation of the value */