Add $isStorageFallback on Codec interface (#5421)
* Add $isStorageFallback on Codec interface * Rename in CHANGELOG * Update specs with new naming * Small formatting adjustments * Abstract values * Adjust initialU8a expectation * Mark old interface as deprecated (& type to never) * Adjust @deprecated comments * Add deprecated interfaces to all
This commit is contained in:
@@ -1,5 +1,13 @@
|
|||||||
# CHANGELOG
|
# CHANGELOG
|
||||||
|
|
||||||
|
## master
|
||||||
|
|
||||||
|
Changes:
|
||||||
|
|
||||||
|
- Add `$isStorageFallback` on the `Codec` interface. (`true` if entry was created from a metadata fallback value from an empty storage item)
|
||||||
|
- Rename `initialU8aLength/createdAtHash` to `$initialU8aLength/$createdAtHash` on the `Codec` interface. (these are marked for internal usage)
|
||||||
|
|
||||||
|
|
||||||
## 9.11.3 Jan 15, 2023
|
## 9.11.3 Jan 15, 2023
|
||||||
|
|
||||||
Changes:
|
Changes:
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ export function subscribeFinalizedBlocks (instanceId: string, api: DeriveApi): (
|
|||||||
return memo(instanceId, (): Observable<SignedBlockExtended> =>
|
return memo(instanceId, (): Observable<SignedBlockExtended> =>
|
||||||
api.derive.chain.subscribeFinalizedHeads().pipe(
|
api.derive.chain.subscribeFinalizedHeads().pipe(
|
||||||
switchMap((header) =>
|
switchMap((header) =>
|
||||||
api.derive.chain.getBlock(header.createdAtHash || header.hash)
|
api.derive.chain.getBlock(header.$createdAtHash || header.hash)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ export function subscribeFinalizedHeads (instanceId: string, api: DeriveApi): ()
|
|||||||
const endHash = prevHash;
|
const endHash = prevHash;
|
||||||
const startHash = header.parentHash;
|
const startHash = header.parentHash;
|
||||||
|
|
||||||
prevHash = header.createdAtHash = header.hash;
|
prevHash = header.$createdAtHash = header.hash;
|
||||||
|
|
||||||
return endHash === null || startHash.eq(endHash)
|
return endHash === null || startHash.eq(endHash)
|
||||||
? of(header)
|
? of(header)
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ export function subscribeNewBlocks (instanceId: string, api: DeriveApi): () => O
|
|||||||
return memo(instanceId, (): Observable<SignedBlockExtended> =>
|
return memo(instanceId, (): Observable<SignedBlockExtended> =>
|
||||||
api.derive.chain.subscribeNewHeads().pipe(
|
api.derive.chain.subscribeNewHeads().pipe(
|
||||||
switchMap((header) =>
|
switchMap((header) =>
|
||||||
api.derive.chain.getBlock(header.createdAtHash || header.hash)
|
api.derive.chain.getBlock(header.$createdAtHash || header.hash)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ export function subscribeNewHeads (instanceId: string, api: DeriveApi): () => Ob
|
|||||||
getAuthorDetails(header, queryAt)
|
getAuthorDetails(header, queryAt)
|
||||||
),
|
),
|
||||||
map(([header, validators, author]): HeaderExtended => {
|
map(([header, validators, author]): HeaderExtended => {
|
||||||
header.createdAtHash = header.hash;
|
header.$createdAtHash = header.hash;
|
||||||
|
|
||||||
return createHeaderExtended(header.registry, header, validators, author);
|
return createHeaderExtended(header.registry, header, validators, author);
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ function _getUpdates (api: DeriveApi, paraId: string | number | BN): Observable<
|
|||||||
added = added.concat(...changes.added);
|
added = added.concat(...changes.added);
|
||||||
removed = removed.concat(...changes.removed);
|
removed = removed.concat(...changes.removed);
|
||||||
|
|
||||||
return of({ added, addedDelta: changes.added, blockHash: events.createdAtHash?.toHex() || '-', removed, removedDelta: changes.removed });
|
return of({ added, addedDelta: changes.added, blockHash: events.$createdAtHash?.toHex() || '-', removed, removedDelta: changes.removed });
|
||||||
}
|
}
|
||||||
|
|
||||||
return EMPTY;
|
return EMPTY;
|
||||||
@@ -52,7 +52,7 @@ function _eventTriggerAll (api: DeriveApi, paraId: string | number | BN): Observ
|
|||||||
);
|
);
|
||||||
|
|
||||||
return items.length
|
return items.length
|
||||||
? of(events.createdAtHash?.toHex() || '-')
|
? of(events.$createdAtHash?.toHex() || '-')
|
||||||
: EMPTY;
|
: EMPTY;
|
||||||
}),
|
}),
|
||||||
startWith('-')
|
startWith('-')
|
||||||
|
|||||||
@@ -29,5 +29,5 @@ export function extractContributed (paraId: string | number | BN, events: Vec<Fr
|
|||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}, { added, blockHash: events.createdAtHash?.toHex() || '-', removed });
|
}, { added, blockHash: events.$createdAtHash?.toHex() || '-', removed });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ export function createHeaderExtended (registry: Registry, header?: Header, valid
|
|||||||
super(registry, header);
|
super(registry, header);
|
||||||
|
|
||||||
this.#author = author || extractAuthor(this.digest, validators || []);
|
this.#author = author || extractAuthor(this.digest, validators || []);
|
||||||
this.createdAtHash = header?.createdAtHash;
|
this.$createdAtHash = header?.$createdAtHash;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ export function createSignedBlockExtended (registry: Registry, block?: SignedBlo
|
|||||||
this.#author = author || extractAuthor(this.block.header.digest, validators || []);
|
this.#author = author || extractAuthor(this.block.header.digest, validators || []);
|
||||||
this.#events = events || ([] as EventRecord[]);
|
this.#events = events || ([] as EventRecord[]);
|
||||||
this.#extrinsics = mapExtrinsics(this.block.extrinsics, this.#events);
|
this.#extrinsics = mapExtrinsics(this.block.extrinsics, this.#events);
|
||||||
this.createdAtHash = block?.createdAtHash;
|
this.$createdAtHash = block?.$createdAtHash;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -483,7 +483,7 @@ export class RpcCore {
|
|||||||
: meta.modifier.isOptional
|
: meta.modifier.isOptional
|
||||||
? registry.createTypeUnsafe(type, [input], { blockHash, isPedantic: true })
|
? registry.createTypeUnsafe(type, [input], { blockHash, isPedantic: true })
|
||||||
: input
|
: input
|
||||||
], { blockHash, isOptional: meta.modifier.isOptional, isPedantic: !meta.modifier.isOptional });
|
], { blockHash, isFallback: isEmpty && !!meta.fallback, isOptional: meta.modifier.isOptional, isPedantic: !meta.modifier.isOptional });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new Error(`Unable to decode storage ${key.section || 'unknown'}.${key.method || 'unknown'}:${entryNum}: ${(error as Error).message}`);
|
throw new Error(`Unable to decode storage ${key.section || 'unknown'}.${key.method || 'unknown'}:${entryNum}: ${(error as Error).message}`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,8 @@ class LRUNode {
|
|||||||
|
|
||||||
// https://en.wikipedia.org/wiki/Cache_replacement_policies#LRU
|
// https://en.wikipedia.org/wiki/Cache_replacement_policies#LRU
|
||||||
export class LRUCache {
|
export class LRUCache {
|
||||||
readonly capacity: number;
|
public readonly capacity: number;
|
||||||
|
|
||||||
readonly #data: Map<string, unknown> = new Map();
|
readonly #data: Map<string, unknown> = new Map();
|
||||||
readonly #refs: Map<string, LRUNode> = new Map();
|
readonly #refs: Map<string, LRUNode> = new Map();
|
||||||
|
|
||||||
|
|||||||
@@ -16,9 +16,16 @@ import { compareArray } from '../utils/compareArray';
|
|||||||
* @noInheritDoc
|
* @noInheritDoc
|
||||||
*/
|
*/
|
||||||
export abstract class AbstractArray<T extends Codec> extends Array<T> implements IVec<T> {
|
export abstract class AbstractArray<T extends Codec> extends Array<T> implements IVec<T> {
|
||||||
|
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
|
||||||
|
public createdAtHash?: never;
|
||||||
|
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
|
||||||
|
public initialU8aLength?: never;
|
||||||
|
|
||||||
public readonly registry: Registry;
|
public readonly registry: Registry;
|
||||||
|
|
||||||
public createdAtHash?: IU8a;
|
public $createdAtHash?: IU8a;
|
||||||
|
public $initialU8aLength?: number;
|
||||||
|
public $isStorageFallback?: boolean;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description This ensures that operators such as clice, filter, map, etc. return
|
* @description This ensures that operators such as clice, filter, map, etc. return
|
||||||
|
|||||||
@@ -9,17 +9,22 @@ import type { AnyJson, BareOpts, Codec, Inspect, IU8a, Registry } from '../types
|
|||||||
* @description A type extends the Base class, when it holds a value
|
* @description A type extends the Base class, when it holds a value
|
||||||
*/
|
*/
|
||||||
export abstract class AbstractBase<T extends Codec> implements Codec {
|
export abstract class AbstractBase<T extends Codec> implements Codec {
|
||||||
public createdAtHash?: IU8a;
|
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
|
||||||
|
public createdAtHash?: never;
|
||||||
public readonly initialU8aLength?: number;
|
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
|
||||||
|
public initialU8aLength?: never;
|
||||||
|
|
||||||
public readonly registry: Registry;
|
public readonly registry: Registry;
|
||||||
|
|
||||||
|
public $createdAtHash?: IU8a;
|
||||||
|
public $initialU8aLength?: number;
|
||||||
|
public $isStorageFallback?: boolean;
|
||||||
|
|
||||||
readonly #raw: T;
|
readonly #raw: T;
|
||||||
|
|
||||||
protected constructor (registry: Registry, value: T, initialU8aLength?: number) {
|
protected constructor (registry: Registry, value: T, initialU8aLength?: number) {
|
||||||
|
this.$initialU8aLength = initialU8aLength;
|
||||||
this.#raw = value;
|
this.#raw = value;
|
||||||
this.initialU8aLength = initialU8aLength;
|
|
||||||
this.registry = registry;
|
this.registry = registry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -72,14 +72,19 @@ function decodeAbstractInt (value: Exclude<AnyNumber, Uint8Array> | Record<strin
|
|||||||
* @noInheritDoc
|
* @noInheritDoc
|
||||||
*/
|
*/
|
||||||
export abstract class AbstractInt extends BN implements INumber {
|
export abstract class AbstractInt extends BN implements INumber {
|
||||||
|
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
|
||||||
|
public createdAtHash?: never;
|
||||||
|
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
|
||||||
|
public initialU8aLength?: never;
|
||||||
|
|
||||||
public readonly registry: Registry;
|
public readonly registry: Registry;
|
||||||
|
|
||||||
public createdAtHash?: IU8a;
|
|
||||||
|
|
||||||
public readonly encodedLength: number;
|
public readonly encodedLength: number;
|
||||||
|
|
||||||
public readonly isUnsigned: boolean;
|
public readonly isUnsigned: boolean;
|
||||||
|
|
||||||
|
public $createdAtHash?: IU8a;
|
||||||
|
public $initialU8aLength?: number;
|
||||||
|
public $isStorageFallback?: boolean;
|
||||||
|
|
||||||
readonly #bitLength: UIntBitLength;
|
readonly #bitLength: UIntBitLength;
|
||||||
|
|
||||||
constructor (registry: Registry, value: AnyNumber = 0, bitLength: UIntBitLength = DEFAULT_UINT_BITS, isSigned = false) {
|
constructor (registry: Registry, value: AnyNumber = 0, bitLength: UIntBitLength = DEFAULT_UINT_BITS, isSigned = false) {
|
||||||
@@ -98,6 +103,7 @@ export abstract class AbstractInt extends BN implements INumber {
|
|||||||
this.registry = registry;
|
this.registry = registry;
|
||||||
this.#bitLength = bitLength;
|
this.#bitLength = bitLength;
|
||||||
this.encodedLength = this.#bitLength / 8;
|
this.encodedLength = this.#bitLength / 8;
|
||||||
|
this.$initialU8aLength = this.#bitLength / 8;
|
||||||
this.isUnsigned = !isSigned;
|
this.isUnsigned = !isSigned;
|
||||||
|
|
||||||
const isNegative = this.isNeg();
|
const isNegative = this.isNeg();
|
||||||
|
|||||||
@@ -47,14 +47,18 @@ function decodeCompact<T extends INumber> (registry: Registry, Type: CodecClass<
|
|||||||
* a number and making the compact representation thereof
|
* a number and making the compact representation thereof
|
||||||
*/
|
*/
|
||||||
export class Compact<T extends INumber> implements ICompact<T> {
|
export class Compact<T extends INumber> implements ICompact<T> {
|
||||||
|
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
|
||||||
|
public createdAtHash?: never;
|
||||||
|
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
|
||||||
|
public initialU8aLength?: never;
|
||||||
|
|
||||||
public readonly registry: Registry;
|
public readonly registry: Registry;
|
||||||
|
|
||||||
public createdAtHash?: IU8a;
|
public $createdAtHash?: IU8a;
|
||||||
|
public $initialU8aLength?: number;
|
||||||
readonly initialU8aLength?: number;
|
public $isStorageFallback?: boolean;
|
||||||
|
|
||||||
readonly #Type: CodecClass<T>;
|
readonly #Type: CodecClass<T>;
|
||||||
|
|
||||||
readonly #raw: T;
|
readonly #raw: T;
|
||||||
|
|
||||||
constructor (registry: Registry, Type: CodecClass<T> | string, value: Compact<T> | AnyNumber = 0, { definition, setDefinition = noopSetDefinition }: Options<T> = {}) {
|
constructor (registry: Registry, Type: CodecClass<T> | string, value: Compact<T> | AnyNumber = 0, { definition, setDefinition = noopSetDefinition }: Options<T> = {}) {
|
||||||
@@ -63,7 +67,7 @@ export class Compact<T extends INumber> implements ICompact<T> {
|
|||||||
|
|
||||||
const [raw, decodedLength] = decodeCompact<T>(registry, this.#Type, value);
|
const [raw, decodedLength] = decodeCompact<T>(registry, this.#Type, value);
|
||||||
|
|
||||||
this.initialU8aLength = decodedLength;
|
this.$initialU8aLength = decodedLength;
|
||||||
this.#raw = raw;
|
this.#raw = raw;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,9 +10,15 @@ import type { AnyJson, Codec, CodecClass, Inspect, IU8a, Registry } from '../typ
|
|||||||
* An unknown type that fails on construction with the type info
|
* An unknown type that fails on construction with the type info
|
||||||
*/
|
*/
|
||||||
export class DoNotConstruct implements Codec {
|
export class DoNotConstruct implements Codec {
|
||||||
|
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
|
||||||
|
public createdAtHash?: never;
|
||||||
|
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
|
||||||
|
public initialU8aLength?: never;
|
||||||
|
|
||||||
public readonly registry: Registry;
|
public readonly registry: Registry;
|
||||||
|
|
||||||
public createdAtHash?: IU8a;
|
public $createdAtHash?: IU8a;
|
||||||
|
public $isStorageFallback?: boolean;
|
||||||
|
|
||||||
#neverError: Error;
|
#neverError: Error;
|
||||||
|
|
||||||
|
|||||||
@@ -183,22 +183,22 @@ function decodeEnum (registry: Registry, def: TypesDef, value?: unknown, index?:
|
|||||||
* an extension to enum where the value type is determined by the actual index.
|
* an extension to enum where the value type is determined by the actual index.
|
||||||
*/
|
*/
|
||||||
export class Enum implements IEnum {
|
export class Enum implements IEnum {
|
||||||
|
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
|
||||||
|
public createdAtHash?: never;
|
||||||
|
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
|
||||||
|
public initialU8aLength?: never;
|
||||||
|
|
||||||
public readonly registry: Registry;
|
public readonly registry: Registry;
|
||||||
|
|
||||||
public createdAtHash?: IU8a;
|
public $createdAtHash?: IU8a;
|
||||||
|
public $initialU8aLength?: number;
|
||||||
|
public $isStorageFallback?: boolean;
|
||||||
|
|
||||||
readonly #def: TypesDef;
|
readonly #def: TypesDef;
|
||||||
|
|
||||||
readonly #entryIndex: number;
|
readonly #entryIndex: number;
|
||||||
|
|
||||||
readonly initialU8aLength?: number;
|
|
||||||
|
|
||||||
readonly #indexes: number[];
|
readonly #indexes: number[];
|
||||||
|
|
||||||
readonly #isBasic: boolean;
|
readonly #isBasic: boolean;
|
||||||
|
|
||||||
readonly #isIndexed: boolean;
|
readonly #isIndexed: boolean;
|
||||||
|
|
||||||
readonly #raw: Codec;
|
readonly #raw: Codec;
|
||||||
|
|
||||||
constructor (registry: Registry, Types: Record<string, string | CodecClass> | Record<string, number> | string[], value?: unknown, index?: number, { definition, setDefinition = noopSetDefinition }: Options = {}) {
|
constructor (registry: Registry, Types: Record<string, string | CodecClass> | Record<string, number> | string[], value?: unknown, index?: number, { definition, setDefinition = noopSetDefinition }: Options = {}) {
|
||||||
@@ -217,8 +217,8 @@ export class Enum implements IEnum {
|
|||||||
this.#entryIndex = this.#indexes.indexOf(decoded.index);
|
this.#entryIndex = this.#indexes.indexOf(decoded.index);
|
||||||
this.#raw = decoded.value;
|
this.#raw = decoded.value;
|
||||||
|
|
||||||
if (this.#raw.initialU8aLength) {
|
if (this.#raw.$initialU8aLength) {
|
||||||
this.initialU8aLength = 1 + this.#raw.initialU8aLength;
|
this.$initialU8aLength = 1 + this.#raw.$initialU8aLength;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,16 +12,18 @@ import { isNull } from '@polkadot/util';
|
|||||||
* Implements a type that does not contain anything (apart from `null`)
|
* Implements a type that does not contain anything (apart from `null`)
|
||||||
*/
|
*/
|
||||||
export class Null implements Codec {
|
export class Null implements Codec {
|
||||||
|
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
|
||||||
|
public createdAtHash?: never;
|
||||||
|
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
|
||||||
|
public initialU8aLength?: never;
|
||||||
|
|
||||||
public readonly encodedLength = 0;
|
public readonly encodedLength = 0;
|
||||||
|
|
||||||
public readonly isEmpty = true;
|
public readonly isEmpty = true;
|
||||||
|
|
||||||
public readonly registry: Registry;
|
public readonly registry: Registry;
|
||||||
|
|
||||||
public createdAtHash?: IU8a;
|
public $createdAtHash?: IU8a;
|
||||||
|
public $initialU8aLength = 0;
|
||||||
// Added for compatibility reasons, e.g. see Option
|
public $isStorageFallback?: boolean;
|
||||||
public readonly initialU8aLength = 0;
|
|
||||||
|
|
||||||
constructor (registry: Registry) {
|
constructor (registry: Registry) {
|
||||||
this.registry = registry;
|
this.registry = registry;
|
||||||
|
|||||||
@@ -66,14 +66,18 @@ function decodeOption (registry: Registry, Type: CodecClass, value?: unknown): C
|
|||||||
* with a value if/as required/found.
|
* with a value if/as required/found.
|
||||||
*/
|
*/
|
||||||
export class Option<T extends Codec> implements IOption<T> {
|
export class Option<T extends Codec> implements IOption<T> {
|
||||||
|
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
|
||||||
|
public createdAtHash?: never;
|
||||||
|
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
|
||||||
|
public initialU8aLength?: never;
|
||||||
|
|
||||||
public readonly registry: Registry;
|
public readonly registry: Registry;
|
||||||
|
|
||||||
public createdAtHash?: IU8a;
|
public $createdAtHash?: IU8a;
|
||||||
|
public $initialU8aLength?: number;
|
||||||
public initialU8aLength?: number;
|
public $isStorageFallback?: boolean;
|
||||||
|
|
||||||
readonly #Type: CodecClass<T>;
|
readonly #Type: CodecClass<T>;
|
||||||
|
|
||||||
readonly #raw: T;
|
readonly #raw: T;
|
||||||
|
|
||||||
constructor (registry: Registry, typeName: CodecClass<T> | string, value?: unknown, { definition, setDefinition = noopSetDefinition }: Options<T> = {}) {
|
constructor (registry: Registry, typeName: CodecClass<T> | string, value?: unknown, { definition, setDefinition = noopSetDefinition }: Options<T> = {}) {
|
||||||
@@ -88,8 +92,8 @@ export class Option<T extends Codec> implements IOption<T> {
|
|||||||
this.#Type = Type;
|
this.#Type = Type;
|
||||||
this.#raw = decoded as T;
|
this.#raw = decoded as T;
|
||||||
|
|
||||||
if (decoded && decoded.initialU8aLength) {
|
if (decoded?.$initialU8aLength) {
|
||||||
this.initialU8aLength = 1 + decoded.initialU8aLength;
|
this.$initialU8aLength = 1 + decoded.$initialU8aLength;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -55,8 +55,6 @@ function decodeTuple (registry: Registry, result: Codec[], value: Exclude<AnyTup
|
|||||||
* own type. It extends the base JS `Array` object.
|
* own type. It extends the base JS `Array` object.
|
||||||
*/
|
*/
|
||||||
export class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
|
export class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
|
||||||
readonly initialU8aLength?: number;
|
|
||||||
|
|
||||||
#Types: Definition;
|
#Types: Definition;
|
||||||
|
|
||||||
constructor (registry: Registry, Types: TupleTypes | TupleType, value?: AnyTupleValue, { definition, setDefinition = noopSetDefinition }: Options = {}) {
|
constructor (registry: Registry, Types: TupleTypes | TupleType, value?: AnyTupleValue, { definition, setDefinition = noopSetDefinition }: Options = {}) {
|
||||||
@@ -70,7 +68,7 @@ export class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
|
|||||||
|
|
||||||
super(registry, Classes[0].length);
|
super(registry, Classes[0].length);
|
||||||
|
|
||||||
this.initialU8aLength = (
|
this.$initialU8aLength = (
|
||||||
isU8a(value)
|
isU8a(value)
|
||||||
? decodeU8a(registry, this, value, Classes)
|
? decodeU8a(registry, this, value, Classes)
|
||||||
: decodeTuple(registry, this, value, Classes)
|
: decodeTuple(registry, this, value, Classes)
|
||||||
|
|||||||
@@ -71,8 +71,6 @@ export function decodeVec<T extends Codec> (registry: Registry, result: T[], val
|
|||||||
* specific encoding/decoding on top of the base type.
|
* specific encoding/decoding on top of the base type.
|
||||||
*/
|
*/
|
||||||
export class Vec<T extends Codec> extends AbstractArray<T> {
|
export class Vec<T extends Codec> extends AbstractArray<T> {
|
||||||
readonly initialU8aLength?: number;
|
|
||||||
|
|
||||||
#Type: CodecClass<T>;
|
#Type: CodecClass<T>;
|
||||||
|
|
||||||
constructor (registry: Registry, Type: CodecClass<T> | string, value: Uint8Array | HexString | unknown[] = [], { definition, setDefinition = noopSetDefinition }: Options<T> = {}) {
|
constructor (registry: Registry, Type: CodecClass<T> | string, value: Uint8Array | HexString | unknown[] = [], { definition, setDefinition = noopSetDefinition }: Options<T> = {}) {
|
||||||
@@ -82,7 +80,7 @@ export class Vec<T extends Codec> extends AbstractArray<T> {
|
|||||||
|
|
||||||
this.#Type = definition || setDefinition(typeToConstructor<T>(registry, Type));
|
this.#Type = definition || setDefinition(typeToConstructor<T>(registry, Type));
|
||||||
|
|
||||||
this.initialU8aLength = (
|
this.$initialU8aLength = (
|
||||||
isU8a(decodeFrom)
|
isU8a(decodeFrom)
|
||||||
? decodeU8aVec(registry, this, decodeFrom, startAt, this.#Type)
|
? decodeU8aVec(registry, this, decodeFrom, startAt, this.#Type)
|
||||||
: decodeVec(registry, this, decodeFrom, startAt, this.#Type)
|
: decodeVec(registry, this, decodeFrom, startAt, this.#Type)
|
||||||
|
|||||||
@@ -25,8 +25,6 @@ function noopSetDefinition <T extends Codec> (d: CodecClass<T>): CodecClass<T> {
|
|||||||
* This manages codec arrays of a fixed length
|
* This manages codec arrays of a fixed length
|
||||||
*/
|
*/
|
||||||
export class VecFixed<T extends Codec> extends AbstractArray<T> {
|
export class VecFixed<T extends Codec> extends AbstractArray<T> {
|
||||||
readonly initialU8aLength?: number;
|
|
||||||
|
|
||||||
#Type: CodecClass<T>;
|
#Type: CodecClass<T>;
|
||||||
|
|
||||||
constructor (registry: Registry, Type: CodecClass<T> | string, length: number, value: Uint8Array | HexString | unknown[] = [] as unknown[], { definition, setDefinition = noopSetDefinition }: Options<T> = {}) {
|
constructor (registry: Registry, Type: CodecClass<T> | string, length: number, value: Uint8Array | HexString | unknown[] = [] as unknown[], { definition, setDefinition = noopSetDefinition }: Options<T> = {}) {
|
||||||
@@ -34,7 +32,7 @@ export class VecFixed<T extends Codec> extends AbstractArray<T> {
|
|||||||
|
|
||||||
this.#Type = definition || setDefinition(typeToConstructor<T>(registry, Type));
|
this.#Type = definition || setDefinition(typeToConstructor<T>(registry, Type));
|
||||||
|
|
||||||
this.initialU8aLength = (
|
this.$initialU8aLength = (
|
||||||
isU8a(value)
|
isU8a(value)
|
||||||
? decodeU8aVec(registry, this, value, 0, this.#Type)
|
? decodeU8aVec(registry, this, value, 0, this.#Type)
|
||||||
: decodeVec(registry, this, value, 0, this.#Type)
|
: decodeVec(registry, this, value, 0, this.#Type)
|
||||||
|
|||||||
@@ -141,7 +141,7 @@ describe('BTreeSet', (): void => {
|
|||||||
expect(none.toHex()).toEqual('0x00');
|
expect(none.toHex()).toEqual('0x00');
|
||||||
expect(none.encodedLength).toEqual(1);
|
expect(none.encodedLength).toEqual(1);
|
||||||
expect(
|
expect(
|
||||||
(new (BTreeSet.with(U32))(registry, none.toHex())).initialU8aLength
|
(new (BTreeSet.with(U32))(registry, none.toHex())).$initialU8aLength
|
||||||
).toEqual(none.encodedLength);
|
).toEqual(none.encodedLength);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -152,7 +152,7 @@ describe('BTreeSet', (): void => {
|
|||||||
expect(some.toHex()).toEqual('0x080100000002000000');
|
expect(some.toHex()).toEqual('0x080100000002000000');
|
||||||
expect(some.encodedLength).toEqual(1 + (4 * 2));
|
expect(some.encodedLength).toEqual(1 + (4 * 2));
|
||||||
expect(
|
expect(
|
||||||
(new (BTreeSet.with(U32))(registry, some.toHex())).initialU8aLength
|
(new (BTreeSet.with(U32))(registry, some.toHex())).$initialU8aLength
|
||||||
).toEqual(some.encodedLength);
|
).toEqual(some.encodedLength);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -70,11 +70,16 @@ function decodeSet<V extends Codec> (registry: Registry, valType: CodecClass<V>
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V> {
|
export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V> {
|
||||||
|
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
|
||||||
|
public createdAtHash?: never;
|
||||||
|
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
|
||||||
|
public initialU8aLength?: never;
|
||||||
|
|
||||||
public readonly registry: Registry;
|
public readonly registry: Registry;
|
||||||
|
|
||||||
public createdAtHash?: IU8a;
|
public $createdAtHash?: IU8a;
|
||||||
|
public $initialU8aLength?: number;
|
||||||
readonly initialU8aLength?: number;
|
public $isStorageFallback?: boolean;
|
||||||
|
|
||||||
readonly #ValClass: CodecClass<V>;
|
readonly #ValClass: CodecClass<V>;
|
||||||
|
|
||||||
@@ -84,7 +89,7 @@ export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V>
|
|||||||
super(sortSet(values));
|
super(sortSet(values));
|
||||||
|
|
||||||
this.registry = registry;
|
this.registry = registry;
|
||||||
this.initialU8aLength = decodedLength;
|
this.$initialU8aLength = decodedLength;
|
||||||
this.#ValClass = ValClass;
|
this.#ValClass = ValClass;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ describe('CodecMap', (): void => {
|
|||||||
expect(none.toHex()).toEqual('0x00');
|
expect(none.toHex()).toEqual('0x00');
|
||||||
expect(none.encodedLength).toEqual(1);
|
expect(none.encodedLength).toEqual(1);
|
||||||
expect(
|
expect(
|
||||||
new CodecMap(registry, Text, Text, none.toHex()).initialU8aLength
|
new CodecMap(registry, Text, Text, none.toHex()).$initialU8aLength
|
||||||
).toEqual(none.encodedLength);
|
).toEqual(none.encodedLength);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -97,7 +97,7 @@ describe('CodecMap', (): void => {
|
|||||||
expect(some.toHex()).toEqual('0x0804310c666f6f04320c626172');
|
expect(some.toHex()).toEqual('0x0804310c666f6f04320c626172');
|
||||||
expect(some.encodedLength).toEqual(1 + ((1 + 1) * 2) + ((1 + 3) * 2));
|
expect(some.encodedLength).toEqual(1 + ((1 + 1) * 2) + ((1 + 3) * 2));
|
||||||
expect(
|
expect(
|
||||||
new CodecMap(registry, Text, Text, some.toHex()).initialU8aLength
|
new CodecMap(registry, Text, Text, some.toHex()).$initialU8aLength
|
||||||
).toEqual(some.encodedLength);
|
).toEqual(some.encodedLength);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -94,16 +94,19 @@ 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> {
|
export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends Map<K, V> implements IMap<K, V> {
|
||||||
|
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
|
||||||
|
public createdAtHash?: never;
|
||||||
|
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
|
||||||
|
public initialU8aLength?: never;
|
||||||
|
|
||||||
public readonly registry: Registry;
|
public readonly registry: Registry;
|
||||||
|
|
||||||
public createdAtHash?: IU8a;
|
public $createdAtHash?: IU8a;
|
||||||
|
public $initialU8aLength?: number;
|
||||||
|
public $isStorageFallback?: boolean;
|
||||||
|
|
||||||
readonly #KeyClass: CodecClass<K>;
|
readonly #KeyClass: CodecClass<K>;
|
||||||
|
|
||||||
readonly #ValClass: CodecClass<V>;
|
readonly #ValClass: CodecClass<V>;
|
||||||
|
|
||||||
readonly initialU8aLength?: number;
|
|
||||||
|
|
||||||
readonly #type: string;
|
readonly #type: string;
|
||||||
|
|
||||||
constructor (registry: Registry, keyType: CodecClass<K> | string, valType: CodecClass<V> | string, rawValue: Uint8Array | string | Map<any, any> | undefined, type: 'BTreeMap' | 'HashMap' = 'HashMap') {
|
constructor (registry: Registry, keyType: CodecClass<K> | string, valType: CodecClass<V> | string, rawValue: Uint8Array | string | Map<any, any> | undefined, type: 'BTreeMap' | 'HashMap' = 'HashMap') {
|
||||||
@@ -112,7 +115,7 @@ export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends
|
|||||||
super(type === 'BTreeMap' ? sortMap(decoded) : decoded);
|
super(type === 'BTreeMap' ? sortMap(decoded) : decoded);
|
||||||
|
|
||||||
this.registry = registry;
|
this.registry = registry;
|
||||||
this.initialU8aLength = decodedLength;
|
this.$initialU8aLength = decodedLength;
|
||||||
this.#KeyClass = KeyClass;
|
this.#KeyClass = KeyClass;
|
||||||
this.#ValClass = ValClass;
|
this.#ValClass = ValClass;
|
||||||
this.#type = type;
|
this.#type = type;
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ export class OptionBool extends Option<Bool> {
|
|||||||
: value
|
: value
|
||||||
);
|
);
|
||||||
|
|
||||||
this.initialU8aLength = 1;
|
this.$initialU8aLength = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -39,9 +39,7 @@ function decodeRaw<T extends Codec> (registry: Registry, typeName: CodecClass<T>
|
|||||||
|
|
||||||
export class WrapperKeepOpaque<T extends Codec> extends Bytes {
|
export class WrapperKeepOpaque<T extends Codec> extends Bytes {
|
||||||
readonly #Type: CodecClass<T>;
|
readonly #Type: CodecClass<T>;
|
||||||
|
|
||||||
readonly #decoded: T | null;
|
readonly #decoded: T | null;
|
||||||
|
|
||||||
readonly #opaqueName: OpaqueName;
|
readonly #opaqueName: OpaqueName;
|
||||||
|
|
||||||
constructor (registry: Registry, typeName: CodecClass<T> | string, value?: unknown, { opaqueName = 'WrapperKeepOpaque' }: Options = {}) {
|
constructor (registry: Registry, typeName: CodecClass<T> | string, value?: unknown, { opaqueName = 'WrapperKeepOpaque' }: Options = {}) {
|
||||||
|
|||||||
@@ -13,9 +13,16 @@ import { isU8a, u8aToHex } from '@polkadot/util';
|
|||||||
* @noInheritDoc
|
* @noInheritDoc
|
||||||
*/
|
*/
|
||||||
export class bool extends Boolean implements Codec {
|
export class bool extends Boolean implements Codec {
|
||||||
|
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
|
||||||
|
public createdAtHash?: never;
|
||||||
|
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
|
||||||
|
public initialU8aLength?: never;
|
||||||
|
|
||||||
public readonly registry: Registry;
|
public readonly registry: Registry;
|
||||||
|
|
||||||
public createdAtHash?: IU8a;
|
public $createdAtHash?: IU8a;
|
||||||
|
public $initialU8aLength = 1;
|
||||||
|
public $isStorageFallback?: boolean;
|
||||||
|
|
||||||
constructor (registry: Registry, value: bool | AnyBool | Uint8Array | number = false) {
|
constructor (registry: Registry, value: bool | AnyBool | Uint8Array | number = false) {
|
||||||
super(
|
super(
|
||||||
|
|||||||
@@ -34,9 +34,16 @@ function decodeDate (value: CodecDate | Date | AnyNumber): Date {
|
|||||||
* @noInheritDoc
|
* @noInheritDoc
|
||||||
*/
|
*/
|
||||||
export class CodecDate extends Date implements INumber {
|
export class CodecDate extends Date implements INumber {
|
||||||
|
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
|
||||||
|
public createdAtHash?: never;
|
||||||
|
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
|
||||||
|
public initialU8aLength?: never;
|
||||||
|
|
||||||
public readonly registry: Registry;
|
public readonly registry: Registry;
|
||||||
|
|
||||||
public createdAtHash?: IU8a;
|
public $createdAtHash?: IU8a;
|
||||||
|
public $initialU8aLength = BITLENGTH / 8;
|
||||||
|
public $isStorageFallback?: boolean;
|
||||||
|
|
||||||
constructor (registry: Registry, value: CodecDate | Date | AnyNumber = 0) {
|
constructor (registry: Registry, value: CodecDate | Date | AnyNumber = 0) {
|
||||||
super(decodeDate(value));
|
super(decodeDate(value));
|
||||||
|
|||||||
@@ -19,16 +19,20 @@ interface Options {
|
|||||||
* in some eth_* RPCs
|
* in some eth_* RPCs
|
||||||
*/
|
*/
|
||||||
export class Float extends Number implements IFloat {
|
export class Float extends Number implements IFloat {
|
||||||
|
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
|
||||||
|
public createdAtHash?: never;
|
||||||
|
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
|
||||||
|
public initialU8aLength?: never;
|
||||||
|
|
||||||
|
public readonly encodedLength: number;
|
||||||
|
public readonly registry: Registry;
|
||||||
|
|
||||||
|
public $createdAtHash?: IU8a;
|
||||||
|
public $initialU8aLength?: number;
|
||||||
|
public $isStorageFallback?: boolean;
|
||||||
|
|
||||||
readonly #bitLength: 32 | 64;
|
readonly #bitLength: 32 | 64;
|
||||||
|
|
||||||
public createdAtHash?: IU8a;
|
|
||||||
|
|
||||||
readonly encodedLength: number;
|
|
||||||
|
|
||||||
readonly initialU8aLength?: number;
|
|
||||||
|
|
||||||
readonly registry: Registry;
|
|
||||||
|
|
||||||
constructor (registry: Registry, value?: AnyFloat, { bitLength = 32 }: Options = {}) {
|
constructor (registry: Registry, value?: AnyFloat, { bitLength = 32 }: Options = {}) {
|
||||||
super(
|
super(
|
||||||
isU8a(value) || isHex(value)
|
isU8a(value) || isHex(value)
|
||||||
@@ -40,7 +44,7 @@ export class Float extends Number implements IFloat {
|
|||||||
|
|
||||||
this.#bitLength = bitLength;
|
this.#bitLength = bitLength;
|
||||||
this.encodedLength = bitLength / 8;
|
this.encodedLength = bitLength / 8;
|
||||||
this.initialU8aLength = this.encodedLength;
|
this.$initialU8aLength = this.encodedLength;
|
||||||
this.registry = registry;
|
this.registry = registry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,9 +22,16 @@ function decodeJson (value?: Record<string, unknown> | null): [string, any][] {
|
|||||||
* @noInheritDoc
|
* @noInheritDoc
|
||||||
*/
|
*/
|
||||||
export class Json extends Map<string, any> implements Codec {
|
export class Json extends Map<string, any> implements Codec {
|
||||||
|
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
|
||||||
|
public createdAtHash?: never;
|
||||||
|
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
|
||||||
|
public initialU8aLength?: never;
|
||||||
|
|
||||||
public readonly registry: Registry;
|
public readonly registry: Registry;
|
||||||
|
|
||||||
public createdAtHash?: IU8a;
|
public $createdAtHash?: IU8a;
|
||||||
|
public $initialU8aLength?: number;
|
||||||
|
public $isStorageFallback?: boolean;
|
||||||
|
|
||||||
constructor (registry: Registry, value?: Record<string, unknown> | null) {
|
constructor (registry: Registry, value?: Record<string, unknown> | null) {
|
||||||
const decoded = decodeJson(value);
|
const decoded = decodeJson(value);
|
||||||
|
|||||||
@@ -16,12 +16,17 @@ import { isAscii, isUndefined, isUtf8, u8aToHex, u8aToString, u8aToU8a } from '@
|
|||||||
* @noInheritDoc
|
* @noInheritDoc
|
||||||
*/
|
*/
|
||||||
export class Raw extends Uint8Array implements IU8a {
|
export class Raw extends Uint8Array implements IU8a {
|
||||||
public createdAtHash?: IU8a;
|
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
|
||||||
|
public createdAtHash?: never;
|
||||||
public readonly initialU8aLength?: number;
|
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
|
||||||
|
public initialU8aLength?: never;
|
||||||
|
|
||||||
public readonly registry: Registry;
|
public readonly registry: Registry;
|
||||||
|
|
||||||
|
public $createdAtHash?: IU8a;
|
||||||
|
public $initialU8aLength?: number;
|
||||||
|
public $isStorageFallback?: boolean;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description This ensures that operators such as clice, filter, map, etc. return
|
* @description This ensures that operators such as clice, filter, map, etc. return
|
||||||
* new Array instances (without this we need to apply overrides)
|
* new Array instances (without this we need to apply overrides)
|
||||||
@@ -34,7 +39,7 @@ export class Raw extends Uint8Array implements IU8a {
|
|||||||
super(u8aToU8a(value));
|
super(u8aToU8a(value));
|
||||||
|
|
||||||
this.registry = registry;
|
this.registry = registry;
|
||||||
this.initialU8aLength = initialU8aLength;
|
this.$initialU8aLength = initialU8aLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -92,9 +92,16 @@ function decodeSet (setValues: SetValues, value: string[] | Set<string> | Uint8A
|
|||||||
* a bitwise representation of the values.
|
* a bitwise representation of the values.
|
||||||
*/
|
*/
|
||||||
export class CodecSet extends Set<string> implements ISet<string> {
|
export class CodecSet extends Set<string> implements ISet<string> {
|
||||||
|
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
|
||||||
|
public createdAtHash?: never;
|
||||||
|
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
|
||||||
|
public initialU8aLength?: never;
|
||||||
|
|
||||||
public readonly registry: Registry;
|
public readonly registry: Registry;
|
||||||
|
|
||||||
public createdAtHash?: IU8a;
|
public $createdAtHash?: IU8a;
|
||||||
|
public $initialU8aLength?: number;
|
||||||
|
public $isStorageFallback?: boolean;
|
||||||
|
|
||||||
readonly #allowed: SetValues;
|
readonly #allowed: SetValues;
|
||||||
|
|
||||||
|
|||||||
@@ -103,14 +103,18 @@ export class Struct<
|
|||||||
V extends { [K in keyof S]: any } = { [K in keyof S]: any },
|
V extends { [K in keyof S]: any } = { [K in keyof S]: any },
|
||||||
// type names, mapped by key, name of Class in S
|
// 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> {
|
E extends { [K in keyof S]: string } = { [K in keyof S]: string }> extends Map<keyof S, Codec> implements IStruct<keyof S> {
|
||||||
public createdAtHash?: IU8a;
|
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
|
||||||
|
public createdAtHash?: never;
|
||||||
public readonly initialU8aLength?: number;
|
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
|
||||||
|
public initialU8aLength?: never;
|
||||||
|
|
||||||
public readonly registry: Registry;
|
public readonly registry: Registry;
|
||||||
|
|
||||||
readonly #jsonMap: Map<keyof S, string>;
|
public $createdAtHash?: IU8a;
|
||||||
|
public $initialU8aLength?: number;
|
||||||
|
public $isStorageFallback?: boolean;
|
||||||
|
|
||||||
|
readonly #jsonMap: Map<keyof S, string>;
|
||||||
readonly #Types: Definition;
|
readonly #Types: Definition;
|
||||||
|
|
||||||
constructor (registry: Registry, Types: S, value?: V | Map<unknown, unknown> | unknown[] | HexString | null, jsonMap = new Map<string, string>(), { definition, setDefinition = noopSetDefinition }: Options = {}) {
|
constructor (registry: Registry, Types: S, value?: V | Map<unknown, unknown> | unknown[] | HexString | null, jsonMap = new Map<string, string>(), { definition, setDefinition = noopSetDefinition }: Options = {}) {
|
||||||
@@ -123,7 +127,7 @@ export class Struct<
|
|||||||
|
|
||||||
super(decoded);
|
super(decoded);
|
||||||
|
|
||||||
this.initialU8aLength = decodedLength;
|
this.$initialU8aLength = decodedLength;
|
||||||
this.registry = registry;
|
this.registry = registry;
|
||||||
this.#jsonMap = jsonMap;
|
this.#jsonMap = jsonMap;
|
||||||
this.#Types = typeMap;
|
this.#Types = typeMap;
|
||||||
|
|||||||
@@ -49,12 +49,17 @@ function decodeText (value?: null | AnyString | AnyU8a | { toString: () => strin
|
|||||||
* @noInheritDoc
|
* @noInheritDoc
|
||||||
*/
|
*/
|
||||||
export class Text extends String implements IText {
|
export class Text extends String implements IText {
|
||||||
public createdAtHash?: IU8a;
|
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
|
||||||
|
public createdAtHash?: never;
|
||||||
public readonly initialU8aLength?: number;
|
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
|
||||||
|
public initialU8aLength?: never;
|
||||||
|
|
||||||
public readonly registry: Registry;
|
public readonly registry: Registry;
|
||||||
|
|
||||||
|
public $createdAtHash?: IU8a;
|
||||||
|
public $initialU8aLength?: number;
|
||||||
|
public $isStorageFallback?: boolean;
|
||||||
|
|
||||||
#override: string | null = null;
|
#override: string | null = null;
|
||||||
|
|
||||||
constructor (registry: Registry, value?: null | AnyString | AnyU8a | { toString: () => string }) {
|
constructor (registry: Registry, value?: null | AnyString | AnyU8a | { toString: () => string }) {
|
||||||
@@ -63,7 +68,7 @@ export class Text extends String implements IText {
|
|||||||
super(str);
|
super(str);
|
||||||
|
|
||||||
this.registry = registry;
|
this.registry = registry;
|
||||||
this.initialU8aLength = decodedLength;
|
this.$initialU8aLength = decodedLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -17,11 +17,44 @@ export interface Inspect {
|
|||||||
/**
|
/**
|
||||||
* @name Codec
|
* @name Codec
|
||||||
* @description
|
* @description
|
||||||
* The base Codec interface. All types implement the interface provided here. Additionally
|
* The base Codec interface. All types implement the interface provided here.
|
||||||
* implementors can add their own specific interfaces and helpers with getters and functions.
|
* Additionally implementors can add their own specific interfaces and helpers
|
||||||
* The Codec Base is however required for operating as an encoding/decoding layer
|
* with getters and functions. The Codec Base is however required for operating
|
||||||
|
* as an encoding/decoding layer
|
||||||
*/
|
*/
|
||||||
export interface Codec {
|
export interface Codec {
|
||||||
|
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
|
||||||
|
createdAtHash?: never;
|
||||||
|
|
||||||
|
/** @deprecated This is not populated anymore. Use $initialU8aLength instead. */
|
||||||
|
initialU8aLength?: never;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description
|
||||||
|
* The block at which this value was retrieved/created (set to non-empty when
|
||||||
|
* retrieved from storage)
|
||||||
|
*/
|
||||||
|
$createdAtHash?: IU8a;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description
|
||||||
|
* The length of the initial encoded value (Only available when the value was
|
||||||
|
* constructed from a Uint8Array input)
|
||||||
|
*/
|
||||||
|
$initialU8aLength?: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description
|
||||||
|
* (internal usage) Indicates that the value was created via a fallback. This
|
||||||
|
* is used when with data specified in the metadata when the storage entry is
|
||||||
|
* empty.
|
||||||
|
*
|
||||||
|
* With metadata fallback values (available as defaults on most storage entries)
|
||||||
|
* any empty storage item should erturn the default. (This is the same as the
|
||||||
|
* implementation on the Substrate runtime)
|
||||||
|
*/
|
||||||
|
$isStorageFallback?: boolean;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description The length of the value when encoded as a Uint8Array
|
* @description The length of the value when encoded as a Uint8Array
|
||||||
*/
|
*/
|
||||||
@@ -32,11 +65,6 @@ export interface Codec {
|
|||||||
*/
|
*/
|
||||||
readonly hash: IU8a;
|
readonly hash: IU8a;
|
||||||
|
|
||||||
/**
|
|
||||||
* @description The length of the initial encoded value (Only available when constructed from a Uint8Array)
|
|
||||||
*/
|
|
||||||
readonly initialU8aLength?: number;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description Checks if the value is an empty value
|
* @description Checks if the value is an empty value
|
||||||
*/
|
*/
|
||||||
@@ -47,11 +75,6 @@ export interface Codec {
|
|||||||
*/
|
*/
|
||||||
readonly registry: Registry;
|
readonly registry: Registry;
|
||||||
|
|
||||||
/**
|
|
||||||
* @description The block at which this value was retrieved/created (set to non-empty when retrieved from storage)
|
|
||||||
*/
|
|
||||||
createdAtHash?: IU8a;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description Compares the value of the input to see if there is a match
|
* @description Compares the value of the input to see if there is a match
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ export type RegistryTypes =
|
|||||||
|
|
||||||
export interface CodecCreateOptions {
|
export interface CodecCreateOptions {
|
||||||
blockHash?: Uint8Array | string | null;
|
blockHash?: Uint8Array | string | null;
|
||||||
|
isFallback?: boolean;
|
||||||
isOptional?: boolean;
|
isOptional?: boolean;
|
||||||
isPedantic?: boolean;
|
isPedantic?: boolean;
|
||||||
}
|
}
|
||||||
@@ -51,13 +52,8 @@ export interface Registry {
|
|||||||
readonly chainDecimals: number[];
|
readonly chainDecimals: number[];
|
||||||
readonly chainSS58: number | undefined;
|
readonly chainSS58: number | undefined;
|
||||||
readonly chainTokens: string[];
|
readonly chainTokens: string[];
|
||||||
// readonly knownTypes: RegisteredTypes;
|
|
||||||
// readonly lookup: PortableRegistry;
|
|
||||||
// readonly metadata: MetadataLatest;
|
|
||||||
// readonly unknownTypes: string[];
|
|
||||||
// readonly signedExtensions: string[];
|
|
||||||
|
|
||||||
createdAtHash?: IU8a;
|
$createdAtHash?: IU8a;
|
||||||
|
|
||||||
findMetaCall (callIndex: Uint8Array): CallFunction;
|
findMetaCall (callIndex: Uint8Array): CallFunction;
|
||||||
findMetaError (errorIndex: Uint8Array | { error: BN, index: BN } | { error: BN | Uint8Array, index: BN }): RegistryError;
|
findMetaError (errorIndex: Uint8Array | { error: BN, index: BN } | { error: BN | Uint8Array, index: BN }): RegistryError;
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ export function decodeU8a <T extends Codec = Codec> (registry: Registry, result:
|
|||||||
while (i < count) {
|
while (i < count) {
|
||||||
const value = new Types[i](registry, u8a.subarray(offset));
|
const value = new Types[i](registry, u8a.subarray(offset));
|
||||||
|
|
||||||
offset += value.initialU8aLength || value.encodedLength;
|
offset += value.$initialU8aLength || value.encodedLength;
|
||||||
result[i] = value;
|
result[i] = value;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
@@ -67,7 +67,7 @@ export function decodeU8aStruct (registry: Registry, result: [string, Codec][],
|
|||||||
while (i < count) {
|
while (i < count) {
|
||||||
const value = new Types[i](registry, u8a.subarray(offset));
|
const value = new Types[i](registry, u8a.subarray(offset));
|
||||||
|
|
||||||
offset += value.initialU8aLength || value.encodedLength;
|
offset += value.$initialU8aLength || value.encodedLength;
|
||||||
result[i] = [keys[i], value];
|
result[i] = [keys[i], value];
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
@@ -93,7 +93,7 @@ export function decodeU8aVec <T extends Codec = Codec> (registry: Registry, resu
|
|||||||
while (i < count) {
|
while (i < count) {
|
||||||
const value = new Type(registry, u8a.subarray(offset));
|
const value = new Type(registry, u8a.subarray(offset));
|
||||||
|
|
||||||
offset += value.initialU8aLength || value.encodedLength;
|
offset += value.$initialU8aLength || value.encodedLength;
|
||||||
result[i] = value;
|
result[i] = value;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,13 +44,21 @@ function checkPedantic (created: Codec, [value]: unknown[]): void {
|
|||||||
|
|
||||||
// Initializes a type with a value. This also checks for fallbacks and in the cases
|
// Initializes a type with a value. This also checks for fallbacks and in the cases
|
||||||
// where isPedantic is specified (storage decoding), also check the format/structure
|
// where isPedantic is specified (storage decoding), also check the format/structure
|
||||||
function initType<T extends Codec> (registry: Registry, Type: CodecClass, params: unknown[] = [], { blockHash, isOptional, isPedantic }: CreateOptions = {}): T {
|
function initType<T extends Codec> (registry: Registry, Type: CodecClass, params: unknown[] = [], { blockHash, isFallback, isOptional, isPedantic }: CreateOptions = {}): T {
|
||||||
const created = new (isOptional ? Option.with(Type) : Type)(registry, ...params);
|
const created = new (
|
||||||
|
isOptional
|
||||||
|
? Option.with(Type)
|
||||||
|
: Type
|
||||||
|
)(registry, ...params);
|
||||||
|
|
||||||
isPedantic && checkPedantic(created, params);
|
isPedantic && checkPedantic(created, params);
|
||||||
|
|
||||||
if (blockHash) {
|
if (blockHash) {
|
||||||
created.createdAtHash = createTypeUnsafe<IU8a>(registry, 'Hash', [blockHash]);
|
created.$createdAtHash = createTypeUnsafe<IU8a>(registry, 'BlockHash', [blockHash]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isFallback) {
|
||||||
|
created.$isStorageFallback = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return created as T;
|
return created as T;
|
||||||
|
|||||||
@@ -166,43 +166,30 @@ function extractProperties (registry: TypeRegistry, metadata: Metadata): ChainPr
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class TypeRegistry implements Registry {
|
export class TypeRegistry implements Registry {
|
||||||
#classes = new Map<string, CodecClass>();
|
/** @deprecated This is not populated anymore. Use $createdAtHash instead. */
|
||||||
|
public createdAtHash?: never;
|
||||||
#definitions = new Map<string, string>();
|
|
||||||
|
|
||||||
#firstCallIndex: Uint8Array | null = null;
|
|
||||||
|
|
||||||
#lookup?: PortableRegistry;
|
|
||||||
|
|
||||||
#metadata?: MetadataLatest;
|
|
||||||
|
|
||||||
#metadataVersion = 0;
|
|
||||||
|
|
||||||
readonly #metadataCalls: Record<string, Record<string, CallFunction>> = {};
|
|
||||||
|
|
||||||
readonly #metadataErrors: Record<string, Record<string, RegistryError>> = {};
|
|
||||||
|
|
||||||
readonly #metadataEvents: Record<string, Record<string, CodecClass<GenericEventData>>> = {};
|
|
||||||
|
|
||||||
readonly #moduleMap: Record<string, string[]> = {};
|
|
||||||
|
|
||||||
#unknownTypes = new Map<string, boolean>();
|
|
||||||
|
|
||||||
#chainProperties?: ChainProperties;
|
#chainProperties?: ChainProperties;
|
||||||
|
#classes = new Map<string, CodecClass>();
|
||||||
|
#definitions = new Map<string, string>();
|
||||||
|
#firstCallIndex: Uint8Array | null = null;
|
||||||
#hasher: (data: Uint8Array) => Uint8Array = blake2AsU8a;
|
#hasher: (data: Uint8Array) => Uint8Array = blake2AsU8a;
|
||||||
|
|
||||||
readonly #knownDefaults: Record<string, CodecClass>;
|
|
||||||
|
|
||||||
readonly #knownDefinitions: Record<string, Definitions>;
|
|
||||||
|
|
||||||
#knownTypes: RegisteredTypes = {};
|
#knownTypes: RegisteredTypes = {};
|
||||||
|
#lookup?: PortableRegistry;
|
||||||
|
#metadata?: MetadataLatest;
|
||||||
|
#metadataVersion = 0;
|
||||||
#signedExtensions: string[] = fallbackExtensions;
|
#signedExtensions: string[] = fallbackExtensions;
|
||||||
|
#unknownTypes = new Map<string, boolean>();
|
||||||
#userExtensions?: ExtDef;
|
#userExtensions?: ExtDef;
|
||||||
|
|
||||||
public createdAtHash?: Hash;
|
readonly #knownDefaults: Record<string, CodecClass>;
|
||||||
|
readonly #knownDefinitions: Record<string, Definitions>;
|
||||||
|
readonly #metadataCalls: Record<string, Record<string, CallFunction>> = {};
|
||||||
|
readonly #metadataErrors: Record<string, Record<string, RegistryError>> = {};
|
||||||
|
readonly #metadataEvents: Record<string, Record<string, CodecClass<GenericEventData>>> = {};
|
||||||
|
readonly #moduleMap: Record<string, string[]> = {};
|
||||||
|
|
||||||
|
public $createdAtHash?: Hash;
|
||||||
|
|
||||||
constructor (createdAtHash?: Hash | Uint8Array | string) {
|
constructor (createdAtHash?: Hash | Uint8Array | string) {
|
||||||
this.#knownDefaults = objectSpread({ Json, Metadata, PortableRegistry, Raw }, baseTypes);
|
this.#knownDefaults = objectSpread({ Json, Metadata, PortableRegistry, Raw }, baseTypes);
|
||||||
@@ -215,7 +202,7 @@ export class TypeRegistry implements Registry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (createdAtHash) {
|
if (createdAtHash) {
|
||||||
this.createdAtHash = this.createType('Hash', createdAtHash);
|
this.$createdAtHash = this.createType('BlockHash', createdAtHash);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ describe('ExtrinsicPayload', (): void => {
|
|||||||
const a = new ExtrinsicPayload(registry, TEST, { version: 4 });
|
const a = new ExtrinsicPayload(registry, TEST, { version: 4 });
|
||||||
const b = new ExtrinsicPayload(registry, a.toU8a(), { version: 4 });
|
const b = new ExtrinsicPayload(registry, a.toU8a(), { version: 4 });
|
||||||
|
|
||||||
|
expect(a.inspect()).toEqual(b.inspect());
|
||||||
expect(a.toJSON()).toEqual(b.toJSON());
|
expect(a.toJSON()).toEqual(b.toJSON());
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -30,6 +31,7 @@ describe('ExtrinsicPayload', (): void => {
|
|||||||
const a = new ExtrinsicPayload(registry, TEST, { version: 4 });
|
const a = new ExtrinsicPayload(registry, TEST, { version: 4 });
|
||||||
const b = new ExtrinsicPayload(registry, a.toHex(), { version: 4 });
|
const b = new ExtrinsicPayload(registry, a.toHex(), { version: 4 });
|
||||||
|
|
||||||
|
expect(a.inspect()).toEqual(b.inspect());
|
||||||
expect(a.toJSON()).toEqual(b.toJSON());
|
expect(a.toJSON()).toEqual(b.toJSON());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user