Add composite base object (#5448)
* Add composite base object * Missing toRawType
This commit is contained in:
@@ -37,6 +37,9 @@ export abstract class AbstractBase<T extends Codec> 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<T extends Codec> 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<AnyNumber, Uint8Array> | Record<string, string>, isNegative: boolean): string | number {
|
||||
function decodeAbstractInt (value: Exclude<AnyNumber, Uint8Array> | Record<string, string> | 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<AnyNumber, Uint8Array> | Record<strin
|
||||
}
|
||||
|
||||
return value;
|
||||
} else if (isBn(value)) {
|
||||
} else if (isBn(value) || isBigInt(value)) {
|
||||
return value.toString();
|
||||
} else if (isObject(value) && !isFunction(value.toBn)) {
|
||||
} else if (isObject(value)) {
|
||||
if (isToBn(value)) {
|
||||
return value.toBn().toString();
|
||||
}
|
||||
|
||||
// Allow the construction from an object with a single top-level key. This means that
|
||||
// single key objects can be treated equivalently to numbers, assuming they meet the
|
||||
// specific requirements. (This is useful in Weights 1.5 where Objects are compact)
|
||||
@@ -54,16 +62,10 @@ function decodeAbstractInt (value: Exclude<AnyNumber, Uint8Array> | Record<strin
|
||||
throw new Error('Unable to construct number from multi-key object');
|
||||
}
|
||||
|
||||
const inner = value[keys[0]];
|
||||
|
||||
if (!isString(inner) && !isNumber(inner)) {
|
||||
throw new Error('Unable to construct from object with non-string/non-number value');
|
||||
}
|
||||
|
||||
return decodeAbstractInt(inner, isNegative);
|
||||
return decodeAbstractInt(value[keys[0]], isNegative);
|
||||
}
|
||||
|
||||
return bnToBn(value as bigint).toString();
|
||||
throw new Error(`Unable to create BN from unknown type ${typeof value}`);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
// Copyright 2017-2023 @polkadot/types-codec authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { AnyJson, BareOpts, CodecObject, Inspect, IU8a, Registry, ToString } from '../types';
|
||||
|
||||
/**
|
||||
* @name Object
|
||||
* @description A type extends the Base class, when it holds a value
|
||||
*/
|
||||
export abstract class AbstractObject<T extends ToString> implements CodecObject<T> {
|
||||
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.$;
|
||||
}
|
||||
}
|
||||
@@ -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<T = Codec> {
|
||||
new(registry: Registry, ...args: any[]): T;
|
||||
}
|
||||
|
||||
export interface CodecObject<T extends ToString> extends Codec {
|
||||
readonly $: T;
|
||||
|
||||
valueOf (): T;
|
||||
}
|
||||
|
||||
export type CodecTo = 'toHex' | 'toJSON' | 'toPrimitive' | 'toString' | 'toU8a';
|
||||
|
||||
export type ArgsDef = Record<string, CodecClass | string>;
|
||||
|
||||
@@ -34,4 +34,12 @@ export type U8aBitLength = 8 | 16 | 32 | 64 | 128 | 160 | 256 | 264 | 512 | 520
|
||||
|
||||
export type AnyTupleValue = Exclude<AnyU8a, string> | HexString | (Codec | AnyU8a | AnyNumber | AnyString | undefined | null)[];
|
||||
|
||||
export interface ToString {
|
||||
toString: () => string;
|
||||
}
|
||||
|
||||
export interface ToBn {
|
||||
toBn: () => BN;
|
||||
}
|
||||
|
||||
export type LookupString = `Lookup${number}`;
|
||||
|
||||
@@ -144,6 +144,13 @@ export class GenericExtrinsicPayload extends AbstractBase<ExtrinsicPayloadVx> {
|
||||
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
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user