Expose registry: Registry on Call/Extrinstic/Metadata (#4339)

* Expose registry: Registry on Call/Extrinstic/Metadata

* Adjust

* fix Call

* Adjust

* Fix it?
This commit is contained in:
Jaco
2021-12-16 15:22:51 +02:00
committed by GitHub
parent 30db589eee
commit 1e472eebc9
13 changed files with 85 additions and 29 deletions
+6 -2
View File
@@ -9,7 +9,7 @@ import type { AnyJson, BareOpts, Codec, CodecRegistry, IU8a } from '../types';
* @description A type extends the Base class, when it holds a value
*/
export abstract class Base<T extends Codec> implements Codec {
public readonly registry: CodecRegistry;
readonly #registry: CodecRegistry;
public createdAtHash?: IU8a;
@@ -18,7 +18,7 @@ export abstract class Base<T extends Codec> implements Codec {
readonly #raw: T;
protected constructor (registry: CodecRegistry, value: T, initialU8aLength?: number) {
this.registry = registry;
this.#registry = registry;
this.#raw = value;
this.initialU8aLength = initialU8aLength;
}
@@ -48,6 +48,10 @@ export abstract class Base<T extends Codec> implements Codec {
return this.#raw.isEmpty;
}
public get registry (): CodecRegistry {
return this.#registry;
}
/**
* @description Compares the value of the input to see if there is a match
*/
+6 -2
View File
@@ -90,7 +90,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> {
readonly registry: CodecRegistry;
#registry: CodecRegistry;
public createdAtHash?: IU8a;
@@ -112,7 +112,7 @@ export class Struct<
super(decoded);
this.registry = registry;
this.#registry = registry;
this.initialU8aLength = decodedLength;
this.#jsonMap = jsonMap;
this.#Types = typeMap;
@@ -190,6 +190,10 @@ export class Struct<
return this.registry.hash(this.toU8a());
}
public get registry (): CodecRegistry {
return this.#registry;
}
/**
* @description Compares the value of the input to see if there is a match
*/
+2 -1
View File
@@ -77,11 +77,12 @@ export interface IVec<T extends Codec = Codec> extends Array<T>, Codec {
toArray (): T[];
}
export interface IMethod<A extends AnyTuple = AnyTuple> extends Codec {
export interface IMethod<A extends AnyTuple = AnyTuple, M = any> extends Codec {
readonly args: A;
readonly argsDef: ArgsDef;
readonly callIndex: Uint8Array;
readonly data: Uint8Array;
readonly meta: M;
is: (tx: IMethod<AnyTuple>) => tx is IMethod<A>;
}
+24 -2
View File
@@ -3,6 +3,7 @@
import type { BN } from '@polkadot/util';
import type { Codec, CodecClass } from './codec';
import type { AnyTuple } from './helpers';
import type { ICompact, IEnum, IMap, IMethod, INumber, IOption, IResult, ISet, IStruct, ITuple, IU8a, IVec } from './interfaces';
export type OnlyCodec<K, T, Types> =
@@ -14,6 +15,16 @@ export type OnlyCodec<K, T, Types> =
? T
: never;
export interface RegistryError {
args: string[];
docs: string[];
index: number;
// compat
method: string;
name: string;
section: string;
}
export type RegistryTypes =
Record<string, CodecClass | string | Record<string, string> |
{ _enum: string[] | Record<string, number> | Record<string, string | null> } |
@@ -25,6 +36,17 @@ export interface CodecCreateOptions {
isPedantic?: boolean;
}
export interface CallBase<A extends AnyTuple, M = any> extends IMethod<A, M> {
readonly method: string;
readonly section: string;
toJSON: () => any;
}
export interface CallFunction<A extends AnyTuple = AnyTuple, M = any> extends CallBase<A, M> {
(...args: any[]): IMethod<A, M>;
}
export interface CodecRegistry {
readonly chainDecimals: number[];
readonly chainSS58: number | undefined;
@@ -37,8 +59,8 @@ export interface CodecRegistry {
createdAtHash?: IU8a;
findMetaCall (callIndex: Uint8Array): unknown;
findMetaError (errorIndex: Uint8Array | { error: BN, index: BN }): unknown;
findMetaCall (callIndex: Uint8Array): CallFunction;
findMetaError (errorIndex: Uint8Array | { error: BN, index: BN }): RegistryError;
findMetaEvent (eventIndex: Uint8Array): CodecClass<any>;
isLookupType (value: string): boolean;