Move .isEmpty to $isEmpty (#5424)

* Move .isEmpty to $isEmpty

* Adjust usages
This commit is contained in:
Jaco
2023-01-19 11:07:29 +02:00
committed by GitHub
parent 4688291ecf
commit 6f19084a4d
66 changed files with 249 additions and 146 deletions
+7 -7
View File
@@ -75,19 +75,19 @@ function parseJson (json: Record<string, unknown>, chainProperties?: ChainProper
}
export class Abi {
public readonly events: AbiEvent[];
readonly events: AbiEvent[];
public readonly constructors: AbiConstructor[];
readonly constructors: AbiConstructor[];
public readonly info: ContractProjectInfo;
readonly info: ContractProjectInfo;
public readonly json: Record<string, unknown>;
readonly json: Record<string, unknown>;
public readonly messages: AbiMessage[];
readonly messages: AbiMessage[];
public readonly metadata: ContractMetadataLatest;
readonly metadata: ContractMetadataLatest;
public readonly registry: Registry;
readonly registry: Registry;
constructor (abiJson: Record<string, unknown> | string, chainProperties?: ChainProperties) {
[this.json, this.registry, this.metadata, this.info] = parseJson(
+2 -2
View File
@@ -11,9 +11,9 @@ import { isFunction } from '@polkadot/util';
import { Abi } from '../Abi';
export abstract class Base<ApiType extends ApiTypes> {
public readonly abi: Abi;
readonly abi: Abi;
public readonly api: ApiBase<ApiType>;
readonly api: ApiBase<ApiType>;
protected readonly _decorateMethod: DecorateMethod<ApiType>;
+2 -2
View File
@@ -23,7 +23,7 @@ export interface BlueprintConstructor<ApiType extends ApiTypes> {
}
export class BlueprintSubmittableResult<ApiType extends ApiTypes> extends SubmittableResult {
public readonly contract?: Contract<ApiType>;
readonly contract?: Contract<ApiType>;
constructor (result: ISubmittableResult, contract?: Contract<ApiType>) {
super(result);
@@ -36,7 +36,7 @@ export class Blueprint<ApiType extends ApiTypes> extends Base<ApiType> {
/**
* @description The on-chain code hash for this blueprint
*/
public readonly codeHash: Hash;
readonly codeHash: Hash;
readonly #tx: MapConstructorExec<ApiType> = {};
+3 -3
View File
@@ -25,8 +25,8 @@ export interface CodeConstructor<ApiType extends ApiTypes> {
}
export class CodeSubmittableResult<ApiType extends ApiTypes> extends SubmittableResult {
public readonly blueprint?: Blueprint<ApiType>;
public readonly contract?: Contract<ApiType>;
readonly blueprint?: Blueprint<ApiType>;
readonly contract?: Contract<ApiType>;
constructor (result: ISubmittableResult, blueprint?: Blueprint<ApiType>, contract?: Contract<ApiType>) {
super(result);
@@ -37,7 +37,7 @@ export class CodeSubmittableResult<ApiType extends ApiTypes> extends Submittable
}
export class Code<ApiType extends ApiTypes> extends Base<ApiType> {
public readonly code: Uint8Array;
readonly code: Uint8Array;
readonly #tx: MapConstructorExec<ApiType> = {};
+2 -2
View File
@@ -42,7 +42,7 @@ function createTx <ApiType extends ApiTypes> (meta: AbiMessage, fn: (options: Co
}
export class ContractSubmittableResult extends SubmittableResult {
public readonly contractEvents?: DecodedEvent[];
readonly contractEvents?: DecodedEvent[];
constructor (result: ISubmittableResult, contractEvents?: DecodedEvent[]) {
super(result);
@@ -55,7 +55,7 @@ export class Contract<ApiType extends ApiTypes> extends Base<ApiType> {
/**
* @description The on-chain address for this contract
*/
public readonly address: AccountId;
readonly address: AccountId;
readonly #query: MapMessageQuery<ApiType> = {};
readonly #tx: MapMessageTx<ApiType> = {};
+1 -1
View File
@@ -116,7 +116,7 @@ function querySystemAccount (api: DeriveApi, accountId: AccountId): Observable<R
const nonce = (infoOrTuple as AccountInfo).nonce || (infoOrTuple as [Index, AccountData])[0];
if (!data || data.isEmpty) {
if (!data || data.$isEmpty) {
return [
nonce,
[[zeroBalance(api), zeroBalance(api), zeroBalance(api), zeroBalance(api)]]
@@ -30,7 +30,7 @@ function isNewDepositors (depositors: ITuple<[Vec<AccountId>, Balance]> | ITuple
function parse ([proposals, images, optDepositors]: Result): DeriveProposal[] {
return proposals
.filter(([, , proposer], index): boolean =>
!!(optDepositors[index]?.isSome) && !proposer.isEmpty
!!(optDepositors[index]?.isSome) && !proposer.$isEmpty
)
.map(([index, hash, proposer], proposalIndex): DeriveProposal => {
const depositors = optDepositors[proposalIndex].unwrap();
@@ -19,7 +19,7 @@ function mapResult ([result, validators, heartbeats, numBlocks]: [DeriveHeartbea
validators.forEach((validator, index): void => {
const validatorId = validator.toString();
const blockCount = numBlocks[index];
const hasMessage = !heartbeats[index].isEmpty;
const hasMessage = !heartbeats[index].$isEmpty;
const prev = result[validatorId];
if (!prev || prev.hasMessage !== hasMessage || !prev.blockCount.eq(blockCount)) {
+1 -1
View File
@@ -37,7 +37,7 @@ function signingHeader (api: DeriveApi): Observable<Header> {
switchMap((header) =>
// check for chains at genesis (until block 1 is produced, e.g. 6s), since
// we do need to allow transactions at chain start (also dev/seal chains)
header.parentHash.isEmpty
header.parentHash.$isEmpty
? of(header)
// in the case of the current block, we use the parent to minimize the
// impact of forks on the system, but not completely remove it
+1 -1
View File
@@ -196,7 +196,7 @@ export abstract class Init<ApiType extends ApiTypes> extends Decorate<ApiType> {
: await firstValueFrom(this._rpcCore.chain.getHeader.raw(blockHash))
);
if (header.parentHash.isEmpty) {
if (header.parentHash.$isEmpty) {
throw new Error('Unable to retrieve header and parent from supplied hash');
}
+8 -8
View File
@@ -35,21 +35,21 @@ function extractInfo (events: EventRecord[] = []): DispatchInfo | undefined {
}
export class SubmittableResult implements ISubmittableResult {
public readonly dispatchError?: DispatchError;
readonly dispatchError?: DispatchError;
public readonly dispatchInfo?: DispatchInfo;
readonly dispatchInfo?: DispatchInfo;
public readonly internalError?: Error;
readonly internalError?: Error;
public readonly events: EventRecord[];
readonly events: EventRecord[];
public readonly status: ExtrinsicStatus;
readonly status: ExtrinsicStatus;
public readonly txHash: Hash;
readonly txHash: Hash;
public readonly txIndex?: number;
readonly txIndex?: number;
public readonly blockNumber?: BlockNumber;
readonly blockNumber?: BlockNumber;
constructor ({ blockNumber, dispatchError, dispatchInfo, events, internalError, status, txHash, txIndex }: SubmittableResultValue) {
this.dispatchError = dispatchError || extractError(events);
+3 -3
View File
@@ -92,9 +92,9 @@ export class RpcCore {
readonly #storageCache = new Map<string, Codec>();
public readonly mapping = new Map<string, DefinitionRpcExt>();
public readonly provider: ProviderInterface;
public readonly sections: string[] = [];
readonly mapping = new Map<string, DefinitionRpcExt>();
readonly provider: ProviderInterface;
readonly sections: string[] = [];
/**
* @constructor
+2 -2
View File
@@ -7,7 +7,7 @@
const DEFAULT_CAPACITY = 128;
class LRUNode {
public readonly key: string;
readonly key: string;
public next: LRUNode;
public prev: LRUNode;
@@ -20,7 +20,7 @@ class LRUNode {
// https://en.wikipedia.org/wiki/Cache_replacement_policies#LRU
export class LRUCache {
public readonly capacity: number;
readonly capacity: number;
readonly #data: Map<string, unknown> = new Map();
readonly #refs: Map<string, LRUNode> = new Map();
+1 -1
View File
@@ -232,7 +232,7 @@ function addConstants ({ lookup, pallets }: MetadataLatest): string {
description: DESC_CONSTANTS,
sections: pallets
.sort(sortByName)
.filter(({ constants }) => !constants.isEmpty)
.filter(({ constants }) => !constants.$isEmpty)
.map(({ constants, name }) => {
const sectionName = stringLowerFirst(name);
+2 -2
View File
@@ -67,10 +67,10 @@ export function getSimilarTypes (registry: Registry, definitions: Record<string,
}
}
} else if (isChildClass(Enum, Clazz)) {
const { defKeys, isBasic } = new (Clazz as Constructor)(registry) as Enum;
const { $isBasic, defKeys } = new (Clazz as Constructor)(registry) as Enum;
const keys = defKeys.filter((v) => !v.startsWith('__Unused'));
if (isBasic) {
if ($isBasic) {
possibleTypes.push(arrayToStrType(keys), 'number');
} else {
// TODO We don't really want any here, these should be expanded
+7 -2
View File
@@ -16,7 +16,7 @@ import { compareArray, warnGet } from '../utils';
* @noInheritDoc
*/
export abstract class AbstractArray<T extends Codec> extends Array<T> implements IVec<T> {
public readonly registry: Registry;
readonly registry: Registry;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
@@ -46,6 +46,11 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> implements
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
@@ -71,7 +76,7 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> implements
/**
* @description Checks if the value is an empty value
*/
public get isEmpty (): boolean {
public get $isEmpty (): boolean {
return this.length === 0;
}
+8 -3
View File
@@ -11,7 +11,7 @@ import { warnGet } from '../utils';
* @description A type extends the Base class, when it holds a value
*/
export abstract class AbstractBase<T extends Codec> implements Codec {
public readonly registry: Registry;
readonly registry: Registry;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
@@ -35,6 +35,11 @@ export abstract class AbstractBase<T extends Codec> implements Codec {
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
@@ -56,8 +61,8 @@ export abstract class AbstractBase<T extends Codec> implements Codec {
/**
* @description Checks if the value is an empty value
*/
public get isEmpty (): boolean {
return this.#raw.isEmpty;
public get $isEmpty (): boolean {
return this.#raw.$isEmpty;
}
/**
+20 -11
View File
@@ -74,13 +74,13 @@ function decodeAbstractInt (value: Exclude<AnyNumber, Uint8Array> | Record<strin
* @noInheritDoc
*/
export abstract class AbstractInt extends BN implements INumber {
public readonly registry: Registry;
public readonly encodedLength: number;
public readonly isUnsigned: boolean;
readonly registry: Registry;
readonly encodedLength: number;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
public $isStorageFallback?: boolean;
readonly $isUnsigned: boolean;
readonly #bitLength: UIntBitLength;
@@ -99,9 +99,8 @@ export abstract class AbstractInt extends BN implements INumber {
this.registry = registry;
this.#bitLength = bitLength;
this.encodedLength = this.#bitLength / 8;
this.$initialU8aLength = this.#bitLength / 8;
this.isUnsigned = !isSigned;
this.encodedLength = this.$initialU8aLength = this.#bitLength / 8;
this.$isUnsigned = !isSigned;
const isNegative = this.isNeg();
const maxBits = bitLength - (isSigned && !isNegative ? 1 : 0);
@@ -123,6 +122,16 @@ export abstract class AbstractInt extends BN implements INumber {
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
/** @deprecated Use $isUnsigned instead. This getter will be removed in a future version */
public get isUnsigned (): boolean {
return warnGet(this, 'isUnsigned');
}
/**
* @description returns a hash of the contents
*/
@@ -133,7 +142,7 @@ export abstract class AbstractInt extends BN implements INumber {
/**
* @description Checks if the value is a zero value (align elsewhere)
*/
public get isEmpty (): boolean {
public get $isEmpty (): boolean {
return this.isZero();
}
@@ -153,7 +162,7 @@ export abstract class AbstractInt extends BN implements INumber {
// number and BN inputs (no `.eqn` needed) - numbers will be converted
return super.eq(
isHex(other)
? hexToBn(other.toString(), { isLe: false, isNegative: !this.isUnsigned })
? hexToBn(other.toString(), { isLe: false, isNegative: !this.$isUnsigned })
: bnToBn(other as string)
);
}
@@ -198,7 +207,7 @@ export abstract class AbstractInt extends BN implements INumber {
return bnToHex(this, {
bitLength: this.bitLength(),
isLe,
isNegative: !this.isUnsigned
isNegative: !this.$isUnsigned
});
}
@@ -255,7 +264,7 @@ export abstract class AbstractInt extends BN implements INumber {
// underlying it always matches (no matter which length it actually is)
return this instanceof this.registry.createClassUnsafe('Balance')
? 'Balance'
: `${this.isUnsigned ? 'u' : 'i'}${this.bitLength()}`;
: `${this.$isUnsigned ? 'u' : 'i'}${this.bitLength()}`;
}
/**
@@ -276,7 +285,7 @@ export abstract class AbstractInt extends BN implements INumber {
return bnToU8a(this, {
bitLength: this.bitLength(),
isLe: true,
isNegative: !this.isUnsigned
isNegative: !this.$isUnsigned
});
}
}
+8 -3
View File
@@ -47,7 +47,7 @@ function decodeCompact<T extends INumber> (registry: Registry, Type: CodecClass<
* a number and making the compact representation thereof
*/
export class Compact<T extends INumber> implements ICompact<T> {
public readonly registry: Registry;
readonly registry: Registry;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
@@ -76,6 +76,11 @@ export class Compact<T extends INumber> implements ICompact<T> {
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
public static with<O extends INumber> (Type: CodecClass<O> | string): CodecClass<Compact<O>> {
let definition: CodecClass<O> | undefined;
@@ -107,8 +112,8 @@ export class Compact<T extends INumber> implements ICompact<T> {
/**
* @description Checks if the value is an empty value
*/
public get isEmpty (): boolean {
return this.#raw.isEmpty;
public get $isEmpty (): boolean {
return this.#raw.$isEmpty;
}
/**
@@ -12,7 +12,7 @@ import { warnGet } from '../utils';
* An unknown type that fails on construction with the type info
*/
export class DoNotConstruct implements Codec {
public readonly registry: Registry;
readonly registry: Registry;
public $createdAtHash?: IU8a;
public $isStorageFallback?: boolean;
@@ -36,6 +36,11 @@ export class DoNotConstruct implements Codec {
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
public static with (typeName?: string): CodecClass {
return class extends DoNotConstruct {
constructor (registry: Registry) {
@@ -61,7 +66,7 @@ export class DoNotConstruct implements Codec {
/**
* @description Checks if the value is an empty value (always true)
*/
public get isEmpty (): boolean {
public get $isEmpty (): boolean {
throw this.#neverError;
}
+1 -1
View File
@@ -293,7 +293,7 @@ describe('Enum', (): void => {
it('has isNone set, with correct index (i.e. no values are used)', (): void => {
const test = new Enum(registry, ['foo', 'bar'], 1);
expect(test.isNone).toBe(true);
expect(test.$isNone).toBe(true);
expect(test.index).toEqual(1);
});
+29 -20
View File
@@ -183,16 +183,16 @@ function decodeEnum (registry: Registry, def: TypesDef, value?: unknown, index?:
* an extension to enum where the value type is determined by the actual index.
*/
export class Enum implements IEnum {
public readonly registry: Registry;
readonly registry: Registry;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
readonly $isBasic: boolean;
public $isStorageFallback?: boolean;
readonly #def: TypesDef;
readonly #entryIndex: number;
readonly #indexes: number[];
readonly #isBasic: boolean;
readonly #isIndexed: boolean;
readonly #raw: Codec;
@@ -204,9 +204,10 @@ export class Enum implements IEnum {
? createFromU8a(registry, def, value[0], value.subarray(1))
: decodeEnum(registry, def, value, index);
this.$isBasic = isBasic;
this.registry = registry;
this.#def = def;
this.#isBasic = isBasic;
this.#isIndexed = isIndexed;
this.#indexes = Object.values(def).map(({ index }) => index);
this.#entryIndex = this.#indexes.indexOf(decoded.index);
@@ -227,6 +228,21 @@ export class Enum implements IEnum {
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isBasic instead. This getter will be removed in a future version */
public get isBasic (): boolean {
return warnGet(this, 'isBasic');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
/** @deprecated Use $isNone instead. This getter will be removed in a future version */
public get isNone (): boolean {
return warnGet(this, 'isNone');
}
public static with (Types: Record<string, string | CodecClass> | Record<string, number> | string[]): EnumCodecClass<Enum> {
let definition: Definition | undefined;
@@ -296,24 +312,17 @@ export class Enum implements IEnum {
return this.#raw;
}
/**
* @description true if this is a basic enum (no values)
*/
public get isBasic (): boolean {
return this.#isBasic;
}
/**
* @description Checks if the value is an empty value
*/
public get isEmpty (): boolean {
return this.#raw.isEmpty;
public get $isEmpty (): boolean {
return this.#raw.$isEmpty;
}
/**
* @description Checks if the Enum points to a [[Null]] type
*/
public get isNone (): boolean {
public get $isNone (): boolean {
return this.#raw instanceof Null;
}
@@ -354,7 +363,7 @@ export class Enum implements IEnum {
return !this.toU8a().some((entry, index) => entry !== other[index]);
} else if (isNumber(other)) {
return this.toNumber() === other;
} else if (this.#isBasic && isString(other)) {
} else if (this.$isBasic && isString(other)) {
return this.type === other;
} else if (isHex(other)) {
return this.toHex() === other;
@@ -372,7 +381,7 @@ export class Enum implements IEnum {
* @description Returns a breakdown of the hex encoding for this Codec
*/
public inspect (): Inspect {
if (this.#isBasic) {
if (this.$isBasic) {
return { outer: [new Uint8Array([this.index])] };
}
@@ -395,7 +404,7 @@ export class Enum implements IEnum {
* @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.isNone
return this.$isBasic || this.$isNone
? this.type
: { [this.type]: this.#raw.toHuman(isExtended) };
}
@@ -404,7 +413,7 @@ export class Enum implements IEnum {
* @description Converts the Object to JSON, typically used for RPC transfers
*/
public toJSON (): AnyJson {
return this.#isBasic
return this.$isBasic
? this.type
: { [stringCamelCase(this.type)]: this.#raw.toJSON() };
}
@@ -420,7 +429,7 @@ export class Enum implements IEnum {
* @description Converts the value in a best-fit primitive form
*/
public toPrimitive (): AnyJson {
return this.#isBasic
return this.$isBasic
? this.type
: { [stringCamelCase(this.type)]: this.#raw.toPrimitive() };
}
@@ -429,7 +438,7 @@ export class Enum implements IEnum {
* @description Returns a raw struct representation of the enum types
*/
protected _toRawStruct (): string[] | Record<string, string | number> {
if (this.#isBasic) {
if (this.$isBasic) {
return this.#isIndexed
? this.defKeys.reduce((out: Record<string, number>, key, index): Record<string, number> => {
out[key] = this.#indexes[index];
@@ -460,7 +469,7 @@ export class Enum implements IEnum {
* @description Returns the string representation of the value
*/
public toString (): string {
return this.isNone
return this.$isNone
? this.type
: stringify(this.toJSON());
}
+1 -1
View File
@@ -26,7 +26,7 @@ describe('Null', (): void => {
});
it('isEmpty', (): void => {
expect(new Null(registry).isEmpty).toBe(true);
expect(new Null(registry).$isEmpty).toBe(true);
});
it('has an empty hex', (): void => {
+8 -3
View File
@@ -14,13 +14,13 @@ import { warnGet } from '../utils';
* Implements a type that does not contain anything (apart from `null`)
*/
export class Null implements Codec {
public readonly encodedLength = 0;
public readonly isEmpty = true;
public readonly registry: Registry;
readonly encodedLength = 0;
readonly registry: Registry;
public $createdAtHash?: IU8a;
public $initialU8aLength = 0;
public $isStorageFallback?: boolean;
readonly $isEmpty = true;
constructor (registry: Registry) {
this.registry = registry;
@@ -36,6 +36,11 @@ export class Null implements Codec {
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
/**
* @description returns a hash of the contents
*/
+1 -1
View File
@@ -37,7 +37,7 @@ describe('Option', (): void => {
].forEach((test): void => {
expect(test.isSome).toBe(true);
expect(test.isNone).toBe(false);
expect(test.isEmpty).toBe(false);
expect(test.$isEmpty).toBe(false);
expect(test.toU8a()).toEqual(new Uint8Array([1]));
expect(test.unwrap().toHex()).toEqual('0x');
});
+7 -2
View File
@@ -66,7 +66,7 @@ function decodeOption (registry: Registry, Type: CodecClass, value?: unknown): C
* with a value if/as required/found.
*/
export class Option<T extends Codec> implements IOption<T> {
public readonly registry: Registry;
readonly registry: Registry;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
@@ -102,6 +102,11 @@ export class Option<T extends Codec> implements IOption<T> {
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
public static with<O extends Codec> (Type: CodecClass<O> | string): CodecClass<Option<O>> {
let definition: CodecClass<O> | undefined;
@@ -136,7 +141,7 @@ export class Option<T extends Codec> implements IOption<T> {
/**
* @description Checks if the Option has no value
*/
public get isEmpty (): boolean {
public get $isEmpty (): boolean {
return this.isNone;
}
+1 -1
View File
@@ -51,7 +51,7 @@ export class Result<O extends Codec, E extends Codec> extends Enum implements IR
* @description Checks if the Result has no value
*/
public override get isEmpty (): boolean {
return this.isOk && this.value.isEmpty;
return this.isOk && this.value.$isEmpty;
}
/**
@@ -70,7 +70,7 @@ function decodeSet<V extends Codec> (registry: Registry, valType: CodecClass<V>
}
export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V> {
public readonly registry: Registry;
readonly registry: Registry;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
@@ -98,6 +98,11 @@ export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V>
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
public static with<V extends Codec> (valType: CodecClass<V> | string): CodecClass<BTreeSet<V>> {
return class extends BTreeSet<V> {
constructor (registry: Registry, value?: Uint8Array | string | Set<any>) {
@@ -129,7 +134,7 @@ export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V>
/**
* @description Checks if the value is an empty value
*/
public get isEmpty (): boolean {
public get $isEmpty (): boolean {
return this.size === 0;
}
+1 -1
View File
@@ -55,7 +55,7 @@ export class Linkage<T extends Codec> extends Struct {
public override toU8a (): Uint8Array {
// As part of a storage query (where these appear), in the case of empty, the values
// are NOT populated by the node - follow the same logic, leaving it empty
return this.isEmpty
return this.$isEmpty
? EMPTY
: super.toU8a();
}
+7 -2
View File
@@ -94,7 +94,7 @@ function decodeMap<K extends Codec, V extends Codec> (registry: Registry, keyTyp
}
export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends Map<K, V> implements IMap<K, V> {
public readonly registry: Registry;
readonly registry: Registry;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
@@ -126,6 +126,11 @@ export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
@@ -149,7 +154,7 @@ export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends
/**
* @description Checks if the value is an empty value
*/
public get isEmpty (): boolean {
public get $isEmpty (): boolean {
return this.size === 0;
}
+3 -3
View File
@@ -58,9 +58,9 @@ describe('Bool', (): void => {
});
it('has sane isEmpty aligning with the rest', (): void => {
expect(new Bool(registry).isEmpty).toBe(true);
expect(new Bool(registry, false).isEmpty).toBe(true);
expect(new Bool(registry, true).isEmpty).toBe(false);
expect(new Bool(registry).$isEmpty).toBe(true);
expect(new Bool(registry, false).$isEmpty).toBe(true);
expect(new Bool(registry, true).$isEmpty).toBe(false);
});
it('has a sane inspect', (): void => {
+7 -2
View File
@@ -15,7 +15,7 @@ import { warnGet } from '../utils';
* @noInheritDoc
*/
export class bool extends Boolean implements Codec {
public readonly registry: Registry;
readonly registry: Registry;
public $createdAtHash?: IU8a;
public $initialU8aLength = 1;
@@ -43,6 +43,11 @@ export class bool extends Boolean implements Codec {
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
@@ -60,7 +65,7 @@ export class bool extends Boolean implements Codec {
/**
* @description Checks if the value is an empty value (true when it wraps false/default)
*/
public get isEmpty (): boolean {
public get $isEmpty (): boolean {
return this.isFalse;
}
+7 -2
View File
@@ -36,7 +36,7 @@ function decodeDate (value: CodecDate | Date | AnyNumber): Date {
* @noInheritDoc
*/
export class CodecDate extends Date implements INumber {
public readonly registry: Registry;
readonly registry: Registry;
public $createdAtHash?: IU8a;
public $initialU8aLength = BITLENGTH / 8;
@@ -58,6 +58,11 @@ export class CodecDate extends Date implements INumber {
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
@@ -75,7 +80,7 @@ export class CodecDate extends Date implements INumber {
/**
* @description Checks if the value is an empty value
*/
public get isEmpty (): boolean {
public get $isEmpty (): boolean {
return this.getTime() === 0;
}
@@ -22,7 +22,7 @@ describe('Float', (): void => {
it('triggers isEmpty on 0', (): void => {
expect(
new F32(registry, 0).isEmpty
new F32(registry, 0).$isEmpty
).toEqual(true);
});
+8 -3
View File
@@ -21,8 +21,8 @@ interface Options {
* in some eth_* RPCs
*/
export class Float extends Number implements IFloat {
public readonly encodedLength: number;
public readonly registry: Registry;
readonly encodedLength: number;
readonly registry: Registry;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
@@ -55,6 +55,11 @@ export class Float extends Number implements IFloat {
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
public static with (bitLength: 32 | 64): CodecClass<Float> {
return class extends Float {
constructor (registry: Registry, value?: AnyFloat) {
@@ -73,7 +78,7 @@ export class Float extends Number implements IFloat {
/**
* @description Returns true if the type wraps an empty/default all-0 value
*/
get isEmpty (): boolean {
public get $isEmpty (): boolean {
return this.valueOf() === 0;
}
+7 -2
View File
@@ -22,7 +22,7 @@ function decodeJson (value?: Record<string, unknown> | null): [string, any][] {
* @noInheritDoc
*/
export class Json extends Map<string, any> implements Codec {
public readonly registry: Registry;
readonly registry: Registry;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
@@ -48,6 +48,11 @@ export class Json extends Map<string, any> implements Codec {
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
/**
* @description Always 0, never encodes as a Uint8Array
*/
@@ -65,7 +70,7 @@ export class Json extends Map<string, any> implements Codec {
/**
* @description Checks if the value is an empty value
*/
public get isEmpty (): boolean {
public get $isEmpty (): boolean {
return [...this.keys()].length === 0;
}
+7 -2
View File
@@ -18,7 +18,7 @@ import { warnGet } from '../utils';
* @noInheritDoc
*/
export class Raw extends Uint8Array implements IU8a {
public readonly registry: Registry;
readonly registry: Registry;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
@@ -49,6 +49,11 @@ export class Raw extends Uint8Array implements IU8a {
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
@@ -73,7 +78,7 @@ export class Raw extends Uint8Array implements IU8a {
/**
* @description Returns true if the type wraps an empty/default all-0 value
*/
public get isEmpty (): boolean {
public get $isEmpty (): boolean {
return !this.length || isUndefined(this.find((b) => !!b));
}
+1 -1
View File
@@ -33,7 +33,7 @@ describe('Set', (): void => {
it('constructs via an string[]', (): void => {
const set = new CodecSet(registry, SET_ROLES, ['full', 'authority']);
expect(set.isEmpty).toEqual(false);
expect(set.$isEmpty).toEqual(false);
expect(set.toString()).toEqual(
'[full, authority]'
);
+7 -2
View File
@@ -92,7 +92,7 @@ function decodeSet (setValues: SetValues, value: string[] | Set<string> | Uint8A
* a bitwise representation of the values.
*/
export class CodecSet extends Set<string> implements ISet<string> {
public readonly registry: Registry;
readonly registry: Registry;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
@@ -119,6 +119,11 @@ export class CodecSet extends Set<string> implements ISet<string> {
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
public static with (values: SetValues, bitLength?: number): CodecClass<CodecSet> {
return class extends CodecSet {
static {
@@ -157,7 +162,7 @@ export class CodecSet extends Set<string> implements ISet<string> {
/**
* @description true is the Set contains no values
*/
public get isEmpty (): boolean {
public get $isEmpty (): boolean {
return this.size === 0;
}
+8 -3
View File
@@ -103,7 +103,7 @@ export class Struct<
V extends { [K in keyof S]: any } = { [K in keyof S]: any },
// type names, mapped by key, name of Class in S
E extends { [K in keyof S]: string } = { [K in keyof S]: string }> extends Map<keyof S, Codec> implements IStruct<keyof S> {
public readonly registry: Registry;
readonly registry: Registry;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
@@ -138,6 +138,11 @@ export class Struct<
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
public static with<S extends TypesDef> (Types: S, jsonMap?: Map<string, string>): CodecClass<Struct<S>> {
let definition: Definition | undefined;
@@ -170,9 +175,9 @@ export class Struct<
/**
* @description Checks if the value is an empty value
*/
public get isEmpty (): boolean {
public get $isEmpty (): boolean {
for (const v of this.values()) {
if (!v.isEmpty) {
if (!v.$isEmpty) {
return false;
}
}
+7 -2
View File
@@ -50,7 +50,7 @@ function decodeText (value?: null | AnyString | AnyU8a | { toString: () => strin
* @noInheritDoc
*/
export class Text extends String implements IText {
public readonly registry: Registry;
readonly registry: Registry;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
@@ -77,6 +77,11 @@ export class Text extends String implements IText {
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
@@ -94,7 +99,7 @@ export class Text extends String implements IText {
/**
* @description Checks if the value is an empty value
*/
public get isEmpty (): boolean {
public get $isEmpty (): boolean {
return this.length === 0;
}
+1 -1
View File
@@ -10,5 +10,5 @@ import { Float } from '../native/Float';
*/
export class f32 extends Float.with(32) {
// NOTE without this, we cannot properly determine extensions
public readonly __FloatType = 'f32';
readonly __FloatType = 'f32';
}
+1 -1
View File
@@ -10,5 +10,5 @@ import { Float } from '../native/Float';
*/
export class f64 extends Float.with(64) {
// NOTE without this, we cannot properly determine extensions
public readonly __FloatType = 'f64';
readonly __FloatType = 'f64';
}
+1 -1
View File
@@ -10,5 +10,5 @@ import { Int } from '../base/Int';
*/
export class i128 extends Int.with(128) {
// NOTE without this, we cannot properly determine extensions
public readonly __IntType = 'i128';
readonly __IntType = 'i128';
}
+1 -1
View File
@@ -10,5 +10,5 @@ import { Int } from '../base/Int';
*/
export class i16 extends Int.with(16) {
// NOTE without this, we cannot properly determine extensions
public readonly __IntType = 'i16';
readonly __IntType = 'i16';
}
+1 -1
View File
@@ -10,5 +10,5 @@ import { Int } from '../base/Int';
*/
export class i256 extends Int.with(256) {
// NOTE without this, we cannot properly determine extensions
public readonly __IntType = 'i256';
readonly __IntType = 'i256';
}
+1 -1
View File
@@ -10,5 +10,5 @@ import { Int } from '../base/Int';
*/
export class i32 extends Int.with(32) {
// NOTE without this, we cannot properly determine extensions
public readonly __IntType = 'i32';
readonly __IntType = 'i32';
}
+1 -1
View File
@@ -10,5 +10,5 @@ import { Int } from '../base/Int';
*/
export class i64 extends Int.with(64) {
// NOTE without this, we cannot properly determine extensions
public readonly __IntType = 'i64';
readonly __IntType = 'i64';
}
+1 -1
View File
@@ -10,5 +10,5 @@ import { Int } from '../base/Int';
*/
export class i8 extends Int.with(8) {
// NOTE without this, we cannot properly determine extensions
public readonly __IntType = 'i8';
readonly __IntType = 'i8';
}
+1 -1
View File
@@ -10,5 +10,5 @@ import { UInt } from '../base/UInt';
*/
export class u128 extends UInt.with(128) {
// NOTE without this, we cannot properly determine extensions
public readonly __UIntType = 'u128';
readonly __UIntType = 'u128';
}
+1 -1
View File
@@ -10,5 +10,5 @@ import { UInt } from '../base/UInt';
*/
export class u16 extends UInt.with(16) {
// NOTE without this, we cannot properly determine extensions
public readonly __UIntType = 'u16';
readonly __UIntType = 'u16';
}
+1 -1
View File
@@ -10,5 +10,5 @@ import { UInt } from '../base/UInt';
*/
export class u256 extends UInt.with(256) {
// NOTE without this, we cannot properly determine extensions
public readonly __UIntType = 'u256';
readonly __UIntType = 'u256';
}
+1 -1
View File
@@ -10,5 +10,5 @@ import { UInt } from '../base/UInt';
*/
export class u32 extends UInt.with(32) {
// NOTE without this, we cannot properly determine extensions
public readonly __UIntType = 'u32';
readonly __UIntType = 'u32';
}
+1 -1
View File
@@ -10,5 +10,5 @@ import { UInt } from '../base/UInt';
*/
export class u64 extends UInt.with(64) {
// NOTE without this, we cannot properly determine extensions
public readonly __UIntType = 'u64';
readonly __UIntType = 'u64';
}
+1 -1
View File
@@ -10,5 +10,5 @@ import { UInt } from '../base/UInt';
*/
export class u8 extends UInt.with(8) {
// NOTE without this, we cannot properly determine extensions
public readonly __UIntType = 'u8';
readonly __UIntType = 'u8';
}
+9 -5
View File
@@ -20,6 +20,9 @@ interface CodecDeprecated {
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
readonly initialU8aLength: number | undefined;
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version. */
readonly isEmpty: boolean;
}
/**
@@ -45,6 +48,12 @@ export interface Codec extends CodecDeprecated {
*/
$initialU8aLength?: number;
/**
* @description
* Checks if the value is an empty value, with the internal bytes all-zero
*/
$isEmpty: boolean;
/**
* @description
* (internal usage) Indicates that the value was created via a fallback. This
@@ -67,11 +76,6 @@ export interface Codec extends CodecDeprecated {
*/
readonly hash: IU8a;
/**
* @description Checks if the value is an empty value
*/
readonly isEmpty: boolean;
/**
* @description The registry associated with this object
*/
+2 -1
View File
@@ -14,10 +14,11 @@ export interface ICompact<T extends INumber = INumber> extends Codec {
}
export interface IEnum extends Codec {
readonly $isBasic: boolean;
readonly defIndexes: number[];
readonly defKeys: string[];
readonly index: number;
readonly isBasic: boolean;
readonly type: string;
readonly value: Codec;
+1 -1
View File
@@ -4,7 +4,7 @@
const hasWarned: Record<string, boolean> = {};
/** @description Warns (once, per class) on the use of a deprecated interface */
export function warnGet <T> (obj: Record<string, any>, name: 'createdAtHash' | 'initialU8aLength'): T {
export function warnGet <T> (obj: Record<string, any>, name: 'createdAtHash' | 'initialU8aLength' | 'isBasic' | 'isEmpty' | 'isNone' | 'isUnsigned'): T {
const clazz = `${obj.constructor?.name || 'Codec'}`;
const check = `${clazz}.${name}`;
const pass = `$${name}`;
+1 -1
View File
@@ -24,7 +24,7 @@ function checkInstance (created: Codec, matcher: Uint8Array): void {
) ||
(
// when the created is empty and matcher is also empty, let it slide...
created.isEmpty &&
created.$isEmpty &&
matcher.every((v) => !v)
)
);
+1 -1
View File
@@ -98,4 +98,4 @@ const tt9 = registry.createType('(u32, (u32, u64), Vec<u8>, Vec<(u32, u64)>, [u8
// tuple with nested fixed
const tta = registry.createType('([u8;32], [u16;5])');
assert(tt1[2].bitLength() && tt2.bitLength() && tt4[3].bitLength() && tt5.isEmpty && tt6[1].toHuman() && tt7.toHuman() && tt8.toHuman() && tt9.toHuman() && tta[1][1].bitLength(), 'All ok');
assert(tt1[2].bitLength() && tt2.bitLength() && tt4[3].bitLength() && tt5.$isEmpty && tt6[1].toHuman() && tt7.toHuman() && tt8.toHuman() && tt9.toHuman() && tta[1][1].bitLength(), 'All ok');
+1 -1
View File
@@ -22,7 +22,7 @@ describe('createType', (): void => {
expect(f64.toRawType()).toEqual('Fixed64');
expect(f64.bitLength()).toEqual(64);
expect(f64.isUnsigned).toBe(false);
expect(f64.$isUnsigned).toBe(false);
expect(f64 instanceof Int).toBe(true);
});
@@ -19,7 +19,7 @@ describe('EthereumAccountId', (): void => {
});
it('is empty by default', (): void => {
expect(id.isEmpty).toBe(true);
expect(id.$isEmpty).toBe(true);
});
it('equals the empty address', (): void => {
@@ -77,7 +77,7 @@ describe('EthereumAccountId', (): void => {
]));
it('decodes to a non-empty value', (): void => {
expect(registry.createType('EthereumAccountId', '0x4119b2e6c3Cb618F4f0B93ac77f9BeeC7FF02887').isEmpty).toBe(false);
expect(registry.createType('EthereumAccountId', '0x4119b2e6c3Cb618F4f0B93ac77f9BeeC7FF02887').$isEmpty).toBe(false);
});
});
});
@@ -72,7 +72,7 @@ export class GenericExtrinsicSignatureV4 extends Struct implements IExtrinsicSig
* @description `true` if the signature is valid
*/
public get isSigned (): boolean {
return !this.signature.isEmpty;
return !this.signature.$isEmpty;
}
/**
+2 -2
View File
@@ -19,7 +19,7 @@ describe('AccountId', (): void => {
});
it('is empty by default', (): void => {
expect(id.isEmpty).toBe(true);
expect(id.$isEmpty).toBe(true);
});
it('equals the empty address', (): void => {
@@ -125,7 +125,7 @@ describe('AccountId', (): void => {
expect(
registry
.createType('AccountId', 'FVU8T5yaAssHAUwo3PnKso7fqNthEqJZGCZgJZSR9cKjWAf')
.isEmpty
.$isEmpty
).toBe(false);
});
+1 -1
View File
@@ -11,7 +11,7 @@ export class MagicNumber extends U32 {
constructor (registry: Registry, value?: AnyNumber) {
super(registry, value);
if (!this.isEmpty && !this.eq(MAGIC_NUMBER)) {
if (!this.$isEmpty && !this.eq(MAGIC_NUMBER)) {
throw new Error(`MagicNumber mismatch: expected ${registry.createTypeUnsafe('u32', [MAGIC_NUMBER]).toHex()}, found ${this.toHex()}`);
}
}
@@ -17,7 +17,7 @@ export function decorateConstants (registry: Registry, { pallets }: MetadataLate
for (let i = 0; i < pallets.length; i++) {
const { constants, name } = pallets[i];
if (!constants.isEmpty) {
if (!constants.$isEmpty) {
lazyMethod(result, stringCamelCase(name), () =>
lazyMethods(
{},