Adjust Compact toBigInt() (#3525)
This commit is contained in:
@@ -62,8 +62,6 @@ function decodeAbstractInt (value: AnyNumber, bitLength: UIntBitLength, isNegati
|
||||
* @ignore
|
||||
* @noInheritDoc
|
||||
*/
|
||||
// TODO:
|
||||
// - Apart from encoding/decoding we don't actually keep check on the sizes, is this good enough?
|
||||
export abstract class AbstractInt extends BN implements Codec {
|
||||
public readonly registry: Registry;
|
||||
|
||||
@@ -140,7 +138,7 @@ export abstract class AbstractInt extends BN implements Codec {
|
||||
* @description True if this value is the max of the type
|
||||
*/
|
||||
public isMax (): boolean {
|
||||
const u8a = this.toU8a().filter((byte): boolean => byte === 0xff);
|
||||
const u8a = this.toU8a().filter((b) => b === 0xff);
|
||||
|
||||
return u8a.length === (this.#bitLength / 8);
|
||||
}
|
||||
|
||||
@@ -96,7 +96,7 @@ export class Compact<T extends CompactEncodable> implements ICompact<T> {
|
||||
* @description Returns a BigInt representation of the number
|
||||
*/
|
||||
public toBigInt (): bigint {
|
||||
return BigInt(this.toString());
|
||||
return this.#raw.toBigInt();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -11,6 +11,20 @@ import { bnToBn, bnToHex, bnToU8a, isString, isU8a, u8aToBn } from '@polkadot/ut
|
||||
|
||||
const BITLENGTH: UIntBitLength = 64;
|
||||
|
||||
function decodeDate (value: CodecDate | Date | AnyNumber): Date {
|
||||
if (value instanceof Date) {
|
||||
return value;
|
||||
} else if (isU8a(value)) {
|
||||
value = u8aToBn(value.subarray(0, BITLENGTH / 8), true);
|
||||
} else if (isString(value)) {
|
||||
value = new BN(value.toString(), 10, 'le');
|
||||
}
|
||||
|
||||
return new Date(
|
||||
bnToBn(value as BN).toNumber() * 1000
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @name Date
|
||||
* @description
|
||||
@@ -27,26 +41,11 @@ export class CodecDate extends Date implements Codec {
|
||||
public createdAtHash?: Hash;
|
||||
|
||||
constructor (registry: Registry, value: CodecDate | Date | AnyNumber = 0) {
|
||||
super(CodecDate.decodeDate(value));
|
||||
super(decodeDate(value));
|
||||
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
public static decodeDate (value: CodecDate | Date | AnyNumber | unknown): Date {
|
||||
if (value instanceof Date) {
|
||||
return value;
|
||||
} else if (isU8a(value)) {
|
||||
value = u8aToBn(value.subarray(0, BITLENGTH / 8), true);
|
||||
} else if (isString(value)) {
|
||||
value = new BN(value.toString(), 10, 'le');
|
||||
}
|
||||
|
||||
return new Date(
|
||||
bnToBn(value as BN).toNumber() * 1000
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
@@ -72,7 +71,7 @@ export class CodecDate extends Date implements Codec {
|
||||
* @description Compares the value of the input to see if there is a match
|
||||
*/
|
||||
public eq (other?: unknown): boolean {
|
||||
return CodecDate.decodeDate(other).getTime() === this.getTime();
|
||||
return decodeDate(other as AnyNumber).getTime() === this.getTime();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -81,7 +81,7 @@ export class Json extends Map<string, any> implements Codec {
|
||||
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
||||
*/
|
||||
public toHuman (): Record<string, AnyJson> {
|
||||
return [...this.entries()].reduce((json: Record<string, AnyJson>, [key, value]): Record<string, AnyJson> => {
|
||||
return [...this.entries()].reduce<Record<string, AnyJson>>((json, [key, value]): Record<string, AnyJson> => {
|
||||
json[key] = isFunction((value as Codec).toHuman)
|
||||
? (value as Codec).toHuman()
|
||||
: value as AnyJson;
|
||||
@@ -94,7 +94,7 @@ export class Json extends Map<string, any> implements Codec {
|
||||
* @description Converts the Object to JSON, typically used for RPC transfers
|
||||
*/
|
||||
public toJSON (): Record<string, AnyJson> {
|
||||
return [...this.entries()].reduce((json: Record<string, AnyJson>, [key, value]): Record<string, AnyJson> => {
|
||||
return [...this.entries()].reduce<Record<string, AnyJson>>((json, [key, value]): Record<string, AnyJson> => {
|
||||
json[key] = value as AnyJson;
|
||||
|
||||
return json;
|
||||
|
||||
@@ -6,6 +6,7 @@ import type { Codec } from '../types';
|
||||
|
||||
export interface CompactEncodable extends Codec {
|
||||
bitLength (): number;
|
||||
toBigInt (): bigint;
|
||||
toBn (): BN;
|
||||
toNumber (): number;
|
||||
}
|
||||
|
||||
@@ -54,10 +54,8 @@ function initType<T extends Codec = Codec, K extends string = string> (registry:
|
||||
// An unsafe version of the `createType` below. It's unsafe because the `type`
|
||||
// argument here can be any string, which, when it cannot parse, will yield a
|
||||
// runtime error.
|
||||
export function createTypeUnsafe<T extends Codec = Codec, K extends string = string> (registry: Registry, type: K, params: any[] = [], options: CreateOptions = {}): T {
|
||||
export function createTypeUnsafe<T extends Codec = Codec, K extends string = string> (registry: Registry, type: K, params: unknown[] = [], options: CreateOptions = {}): T {
|
||||
try {
|
||||
// Circle back to isPedantic when it handles all cases 100% - as of now,
|
||||
// it provides false warning which is more hinderance than help
|
||||
return initType(registry, createClass<T, K>(registry, type), params, options);
|
||||
} catch (error) {
|
||||
throw new Error(`createType(${type}):: ${(error as Error).message}`);
|
||||
@@ -70,6 +68,6 @@ export function createTypeUnsafe<T extends Codec = Codec, K extends string = str
|
||||
* instance from
|
||||
* @param params - The value to instantiate the type with
|
||||
*/
|
||||
export function createType<K extends keyof InterfaceTypes> (registry: Registry, type: K, ...params: any[]): InterfaceTypes[K] {
|
||||
export function createType<K extends keyof InterfaceTypes> (registry: Registry, type: K, ...params: unknown[]): InterfaceTypes[K] {
|
||||
return createTypeUnsafe<InterfaceTypes[K], K>(registry, type, params);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user