Add api.{errors, events, query, tx}.<method>.<section>.is(...) (#2984)
* api.{event, error}.<section>.is<Type>
* Full generation, with checkTypes
* .is
* Extrinsic with arg type definition
* decorate is
* Call is
* Remove hasOrigin
* Renames
* Query is
* Fix extrinsic is
* Backwards compatibility for generated types
* event with optional AnyTuple
* Cleanups
* Cleanups
* Adjust
* Cleanup
This commit is contained in:
@@ -88,7 +88,7 @@ function injectEvents (registry: Registry, metadata: Metadata, metadataEvents: R
|
||||
|
||||
// create extrinsic mapping from metadata
|
||||
function injectExtrinsics (registry: Registry, metadata: Metadata, metadataCalls: Record<string, CallFunction>): void {
|
||||
const extrinsics = decorateExtrinsics(registry, metadata.asLatest);
|
||||
const extrinsics = decorateExtrinsics(registry, metadata.asLatest, metadata.version);
|
||||
|
||||
// decorate the extrinsics
|
||||
Object.values(extrinsics).forEach((methods): void =>
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import type { EcdsaSignature, Ed25519Signature, ExtrinsicUnknown, ExtrinsicV4, Sr25519Signature } from '../interfaces/extrinsics';
|
||||
import type { FunctionMetadataLatest } from '../interfaces/metadata/types';
|
||||
import type { Address, Balance, Call, Index } from '../interfaces/runtime';
|
||||
import type { AnyJson, AnyU8a, ArgsDef, Codec, ExtrinsicPayloadValue, IExtrinsic, IKeyringPair, InterfaceTypes, Registry, SignatureOptions } from '../types';
|
||||
import type { AnyJson, AnyTuple, AnyU8a, ArgsDef, ExtrinsicPayloadValue, IExtrinsic, IKeyringPair, IMethod, InterfaceTypes, Registry, SignatureOptions } from '../types';
|
||||
import type { GenericExtrinsicEra } from './ExtrinsicEra';
|
||||
import type { ExtrinsicValueV4 } from './v4/Extrinsic';
|
||||
|
||||
@@ -34,11 +34,11 @@ const VERSIONS: (keyof InterfaceTypes)[] = [
|
||||
|
||||
export { EXTRINSIC_VERSION as LATEST_EXTRINSIC_VERSION } from './v4/Extrinsic';
|
||||
|
||||
abstract class ExtrinsicBase extends Base<ExtrinsicVx | ExtrinsicUnknown> {
|
||||
abstract class ExtrinsicBase<A extends AnyTuple> extends Base<ExtrinsicVx | ExtrinsicUnknown> {
|
||||
/**
|
||||
* @description The arguments passed to for the call, exposes args so it is compatible with [[Call]]
|
||||
*/
|
||||
public get args (): Codec[] {
|
||||
public get args (): A {
|
||||
return this.method.args;
|
||||
}
|
||||
|
||||
@@ -77,13 +77,6 @@ abstract class ExtrinsicBase extends Base<ExtrinsicVx | ExtrinsicUnknown> {
|
||||
return this.toU8a().length;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description `true` is method has `Origin` argument (compatibility with [Call])
|
||||
*/
|
||||
public get hasOrigin (): boolean {
|
||||
return this.method.hasOrigin;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description `true` id the extrinsic is signed
|
||||
*/
|
||||
@@ -108,8 +101,8 @@ abstract class ExtrinsicBase extends Base<ExtrinsicVx | ExtrinsicUnknown> {
|
||||
/**
|
||||
* @description The [[Call]] this extrinsic wraps
|
||||
*/
|
||||
public get method (): Call {
|
||||
return (this._raw as ExtrinsicVx).method;
|
||||
public get method (): IMethod<A> {
|
||||
return (this._raw as ExtrinsicVx).method as unknown as IMethod<A>;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -153,6 +146,13 @@ abstract class ExtrinsicBase extends Base<ExtrinsicVx | ExtrinsicUnknown> {
|
||||
public get version (): number {
|
||||
return this.type | (this.isSigned ? BIT_SIGNED : BIT_UNSIGNED);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Checks if the source matches this in type
|
||||
*/
|
||||
public is (other: IMethod<AnyTuple>): other is IMethod<A> {
|
||||
return this.method.is(other);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -167,7 +167,7 @@ abstract class ExtrinsicBase extends Base<ExtrinsicVx | ExtrinsicUnknown> {
|
||||
* - signed, to create a transaction
|
||||
* - left as is, to create an inherent
|
||||
*/
|
||||
export class GenericExtrinsic extends ExtrinsicBase implements IExtrinsic {
|
||||
export class GenericExtrinsic<A extends AnyTuple = AnyTuple> extends ExtrinsicBase<A> implements IExtrinsic<A> {
|
||||
constructor (registry: Registry, value: GenericExtrinsic | ExtrinsicValue | AnyU8a | Call | undefined, { version }: CreateOptions = {}) {
|
||||
super(registry, GenericExtrinsic._decodeExtrinsic(registry, value, version));
|
||||
}
|
||||
@@ -216,7 +216,7 @@ export class GenericExtrinsic extends ExtrinsicBase implements IExtrinsic {
|
||||
/**
|
||||
* @description Injects an already-generated signature into the extrinsic
|
||||
*/
|
||||
public addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | string, payload: ExtrinsicPayloadValue | Uint8Array | string): GenericExtrinsic {
|
||||
public addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | string, payload: ExtrinsicPayloadValue | Uint8Array | string): GenericExtrinsic<A> {
|
||||
(this._raw as ExtrinsicVx).addSignature(signer, signature, payload);
|
||||
|
||||
return this;
|
||||
@@ -225,7 +225,7 @@ export class GenericExtrinsic extends ExtrinsicBase implements IExtrinsic {
|
||||
/**
|
||||
* @description Sign the extrinsic with a specific keypair
|
||||
*/
|
||||
public sign (account: IKeyringPair, options: SignatureOptions): GenericExtrinsic {
|
||||
public sign (account: IKeyringPair, options: SignatureOptions): GenericExtrinsic<A> {
|
||||
(this._raw as ExtrinsicVx).sign(account, options);
|
||||
|
||||
return this;
|
||||
@@ -234,7 +234,7 @@ export class GenericExtrinsic extends ExtrinsicBase implements IExtrinsic {
|
||||
/**
|
||||
* @describe Adds a fake signature to the extrinsic
|
||||
*/
|
||||
public signFake (signer: Address | Uint8Array | string, options: SignatureOptions): GenericExtrinsic {
|
||||
public signFake (signer: Address | Uint8Array | string, options: SignatureOptions): GenericExtrinsic<A> {
|
||||
(this._raw as ExtrinsicVx).signFake(signer, options);
|
||||
|
||||
return this;
|
||||
|
||||
@@ -16,7 +16,7 @@ const keyring = createTestPairs({ type: 'ed25519' }, false);
|
||||
|
||||
registry.setMetadata(metadata);
|
||||
|
||||
const tx = decorateExtrinsics(registry, metadata.asLatest);
|
||||
const tx = decorateExtrinsics(registry, metadata.asLatest, metadata.version);
|
||||
|
||||
describe('ExtrinsicV4', (): void => {
|
||||
it.only('constructs a sane Uint8Array (default)', (): void => {
|
||||
|
||||
@@ -27,29 +27,4 @@ describe('Call', (): void => {
|
||||
new Call(registry, '0x0601').toU8a()
|
||||
).toEqual(new Uint8Array([6, 1, 0, 0, 0])); // balances.setBalance
|
||||
});
|
||||
|
||||
describe('hasOrigin', (): void => {
|
||||
const test = {
|
||||
args: [],
|
||||
callIndex: [2, 2] // timestamp
|
||||
};
|
||||
|
||||
it('is false with no arguments', (): void => {
|
||||
expect(
|
||||
new Call(registry, test, { args: [] } as any).hasOrigin
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('is false with first argument as non-Origin', (): void => {
|
||||
expect(
|
||||
new Call(registry, test, { args: [{ name: 'a', type: 'u32' }] } as any).hasOrigin
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('is false with first argument as non-Origin', (): void => {
|
||||
expect(
|
||||
new Call(registry, test, { args: [{ name: 'a', type: 'Origin' }] } as any).hasOrigin
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { FunctionArgumentMetadataLatest, FunctionMetadataLatest } from '../interfaces/metadata';
|
||||
import type { AnyJson, AnyU8a, ArgsDef, CallFunction, Codec, IMethod, Registry } from '../types';
|
||||
import type { AnyJson, AnyTuple, AnyU8a, ArgsDef, CallFunction, IMethod, Registry } from '../types';
|
||||
|
||||
import { isHex, isObject, isU8a, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
@@ -117,7 +117,7 @@ export class GenericCallIndex extends U8aFixed {
|
||||
* @description
|
||||
* Extrinsic function descriptor
|
||||
*/
|
||||
export class GenericCall extends Struct implements IMethod {
|
||||
export class GenericCall<A extends AnyTuple = AnyTuple> extends Struct implements IMethod<A> {
|
||||
protected _meta: FunctionMetadataLatest;
|
||||
|
||||
constructor (registry: Registry, value: unknown, meta?: FunctionMetadataLatest) {
|
||||
@@ -159,9 +159,9 @@ export class GenericCall extends Struct implements IMethod {
|
||||
/**
|
||||
* @description The arguments for the function call
|
||||
*/
|
||||
public get args (): Codec[] {
|
||||
public get args (): A {
|
||||
// FIXME This should return a Struct instead of an Array
|
||||
return [...(this.get('args') as Struct).values()];
|
||||
return [...(this.get('args') as Struct).values()] as A;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -185,15 +185,6 @@ export class GenericCall extends Struct implements IMethod {
|
||||
return (this.get('args') as Struct).toU8a();
|
||||
}
|
||||
|
||||
/**
|
||||
* @description `true` if the `Origin` type is on the method (extrinsic method)
|
||||
*/
|
||||
public get hasOrigin (): boolean {
|
||||
const firstArg = this.meta.args[0];
|
||||
|
||||
return !!firstArg && firstArg.type.toString() === 'Origin';
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The [[FunctionMetadata]]
|
||||
*/
|
||||
@@ -229,6 +220,13 @@ export class GenericCall extends Struct implements IMethod {
|
||||
return this.sectionName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Checks if the source matches this in type
|
||||
*/
|
||||
public is (other: IMethod<AnyTuple>): other is IMethod<A> {
|
||||
return other.callIndex[0] === this.callIndex[0] && other.callIndex[1] === this.callIndex[1];
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
||||
*/
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import type { TypeDef } from '../create/types';
|
||||
import type { EventMetadataLatest } from '../interfaces/metadata';
|
||||
import type { EventId } from '../interfaces/system';
|
||||
import type { AnyJson, Constructor, Registry } from '../types';
|
||||
import type { AnyJson, Codec, Constructor, IEvent, IEventData, Registry } from '../types';
|
||||
|
||||
import { Struct } from '../codec/Struct';
|
||||
import { Tuple } from '../codec/Tuple';
|
||||
@@ -15,7 +15,7 @@ import { Null } from '../primitive/Null';
|
||||
* @description
|
||||
* Wrapper for the actual data that forms part of an [[Event]]
|
||||
*/
|
||||
export class GenericEventData extends Tuple {
|
||||
export class GenericEventData extends Tuple implements IEventData {
|
||||
readonly #meta: EventMetadataLatest;
|
||||
|
||||
readonly #method: string;
|
||||
@@ -68,7 +68,7 @@ export class GenericEventData extends Tuple {
|
||||
* A representation of a system event. These are generated via the [[Metadata]] interfaces and
|
||||
* specific to a specific Substrate runtime
|
||||
*/
|
||||
export class GenericEvent extends Struct {
|
||||
export class GenericEvent extends Struct implements IEvent<Codec[]> {
|
||||
// Currently we _only_ decode from Uint8Array, since we expect it to
|
||||
// be used via EventRecord
|
||||
constructor (registry: Registry, _value?: Uint8Array) {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { StorageEntryMetadataLatest, StorageEntryTypeLatest, StorageHasher } from '../interfaces/metadata';
|
||||
import type { AnyJson, AnyU8a, Codec, InterfaceTypes, Registry } from '../types';
|
||||
import type { AnyJson, AnyTuple, AnyU8a, Codec, InterfaceTypes, IStorageKey, Registry } from '../types';
|
||||
import type { StorageEntry } from './types';
|
||||
|
||||
import { assert, isFunction, isString, isU8a } from '@polkadot/util';
|
||||
@@ -85,7 +85,7 @@ function decodeStorageKey (value?: AnyU8a | StorageKey | StorageEntry | [Storage
|
||||
throw new Error(`Unable to convert input ${value as string} to StorageKey`);
|
||||
}
|
||||
|
||||
function decodeHashers (registry: Registry, value: Uint8Array, hashers: [StorageHasher, string][]): Codec[] {
|
||||
function decodeHashers <A extends AnyTuple> (registry: Registry, value: Uint8Array, hashers: [StorageHasher, string][]): A {
|
||||
// the storage entry is xxhashAsU8a(prefix, 128) + xxhashAsU8a(method, 128), 256 bits total
|
||||
let offset = 32;
|
||||
|
||||
@@ -99,13 +99,13 @@ function decodeHashers (registry: Registry, value: Uint8Array, hashers: [Storage
|
||||
result.push(decoded);
|
||||
|
||||
return result;
|
||||
}, []);
|
||||
}, []) as A;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
function decodeArgsFromMeta (registry: Registry, value: Uint8Array, meta?: StorageEntryMetadataLatest): Codec[] {
|
||||
function decodeArgsFromMeta <A extends AnyTuple> (registry: Registry, value: Uint8Array, meta?: StorageEntryMetadataLatest): A {
|
||||
if (!meta || !(meta.type.isDoubleMap || meta.type.isMap)) {
|
||||
return [];
|
||||
return [] as unknown as A;
|
||||
}
|
||||
|
||||
if (meta.type.isMap) {
|
||||
@@ -130,10 +130,10 @@ function decodeArgsFromMeta (registry: Registry, value: Uint8Array, meta?: Stora
|
||||
* A representation of a storage key (typically hashed) in the system. It can be
|
||||
* constructed by passing in a raw key or a StorageEntry with (optional) arguments.
|
||||
*/
|
||||
export class StorageKey extends Bytes {
|
||||
export class StorageKey<A extends AnyTuple = AnyTuple> extends Bytes implements IStorageKey<A> {
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore This is assigned via this.decodeArgsFromMeta()
|
||||
private _args: Codec[];
|
||||
private _args: A;
|
||||
|
||||
private _meta?: StorageEntryMetadataLatest;
|
||||
|
||||
@@ -188,7 +188,7 @@ export class StorageKey extends Bytes {
|
||||
/**
|
||||
* @description Return the decoded arguments (applicable to map/doublemap with decodable values)
|
||||
*/
|
||||
public get args (): Codec[] {
|
||||
public get args (): A {
|
||||
return this._args;
|
||||
}
|
||||
|
||||
@@ -220,6 +220,10 @@ export class StorageKey extends Bytes {
|
||||
return this._section;
|
||||
}
|
||||
|
||||
public is (key: IStorageKey<AnyTuple>): key is IStorageKey<A> {
|
||||
return key.section === this.section && key.method === this.method;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Sets the meta for this key
|
||||
*/
|
||||
|
||||
@@ -3,15 +3,19 @@
|
||||
|
||||
import type { FunctionMetadataLatest } from '../interfaces/metadata';
|
||||
import type { Call } from '../interfaces/runtime';
|
||||
import type { AnyTuple } from './codec';
|
||||
import type { IMethod } from './interfaces';
|
||||
|
||||
export interface CallBase {
|
||||
callIndex: Uint8Array;
|
||||
meta: FunctionMetadataLatest;
|
||||
method: string;
|
||||
section: string;
|
||||
export interface CallBase<A extends AnyTuple> {
|
||||
readonly callIndex: Uint8Array;
|
||||
readonly meta: FunctionMetadataLatest;
|
||||
readonly method: string;
|
||||
readonly section: string;
|
||||
|
||||
is: (tx: IMethod<AnyTuple>) => tx is IMethod<A>;
|
||||
toJSON: () => any;
|
||||
}
|
||||
|
||||
export interface CallFunction extends CallBase {
|
||||
(...args: any[]): Call;
|
||||
export interface CallFunction<A extends AnyTuple = AnyTuple> extends CallBase<A> {
|
||||
(...args: any[]): Call & IMethod<A>;
|
||||
}
|
||||
|
||||
@@ -13,6 +13,8 @@ export type AnyNumber = BN | BigInt | Uint8Array | number | string;
|
||||
|
||||
export type AnyString = string | string;
|
||||
|
||||
export type AnyTuple = Codec[];
|
||||
|
||||
export type AnyU8a = Uint8Array | number[] | string;
|
||||
|
||||
// helper to extract keys from an array
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
// Copyright 2017-2020 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { EventMetadataLatest } from '../interfaces/metadata';
|
||||
import type { Hash } from '../interfaces/runtime';
|
||||
import type { EventId, Phase } from '../interfaces/system';
|
||||
import type { Codec } from './codec';
|
||||
import type { ITuple } from './interfaces';
|
||||
|
||||
export interface IEventRecord<T extends Codec[]> {
|
||||
readonly phase: Phase;
|
||||
readonly event: IEvent<T>;
|
||||
readonly topics: Hash[];
|
||||
}
|
||||
|
||||
export interface IEventData {
|
||||
readonly meta: EventMetadataLatest;
|
||||
readonly method: string;
|
||||
readonly section: string;
|
||||
}
|
||||
|
||||
export interface IEvent<T extends Codec[]> {
|
||||
readonly data: ITuple<T> & IEventData;
|
||||
readonly index: EventId;
|
||||
readonly method: string;
|
||||
readonly section: string;
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import type { ExtrinsicStatus } from '../interfaces/author';
|
||||
import type { EcdsaSignature, Ed25519Signature, Sr25519Signature } from '../interfaces/extrinsics';
|
||||
import type { Address, Balance, Call, H256, Index } from '../interfaces/runtime';
|
||||
import type { DispatchError, DispatchInfo, EventRecord } from '../interfaces/system';
|
||||
import type { AnyJson, AnyNumber, AnyU8a, Codec } from './codec';
|
||||
import type { AnyJson, AnyNumber, AnyTuple, AnyU8a, Codec } from './codec';
|
||||
import type { ICompact, IKeyringPair, IMethod, IRuntimeVersion } from './interfaces';
|
||||
|
||||
export interface ISubmittableResult {
|
||||
@@ -173,7 +173,7 @@ export interface ExtrinsicPayloadValue {
|
||||
blockHash: AnyU8a;
|
||||
era: AnyU8a | IExtrinsicEra;
|
||||
genesisHash: AnyU8a;
|
||||
method: AnyU8a | IMethod;
|
||||
method: AnyU8a | IMethod<AnyTuple>;
|
||||
nonce: AnyNumber;
|
||||
specVersion: AnyNumber;
|
||||
tip: AnyNumber;
|
||||
@@ -198,9 +198,9 @@ export interface IExtrinsicImpl extends IExtrinsicSignable<IExtrinsicImpl>, Code
|
||||
readonly version: number;
|
||||
}
|
||||
|
||||
export interface IExtrinsic extends IExtrinsicSignable<IExtrinsic>, ExtrinsicSignatureBase, IMethod {
|
||||
export interface IExtrinsic<A extends AnyTuple = AnyTuple> extends IExtrinsicSignable<IExtrinsic<A>>, ExtrinsicSignatureBase, IMethod<A> {
|
||||
readonly length: number;
|
||||
readonly method: Call;
|
||||
readonly method: IMethod<A>;
|
||||
readonly type: number;
|
||||
readonly version: number;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ export * from '../create/types';
|
||||
export * from './calls';
|
||||
export * from './codec';
|
||||
export * from './definitions';
|
||||
export * from './events';
|
||||
export * from './extrinsic';
|
||||
export * from './interfaces';
|
||||
export * from './registry';
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
|
||||
import type BN from 'bn.js';
|
||||
import type { SignOptions } from '@polkadot/keyring/types';
|
||||
import type { FunctionMetadataLatest } from '../interfaces/metadata';
|
||||
import type { FunctionMetadataLatest, StorageEntryMetadataLatest } from '../interfaces/metadata';
|
||||
import type { Hash } from '../interfaces/runtime';
|
||||
import type { ArgsDef, Codec } from './codec';
|
||||
import type { AnyTuple, ArgsDef, Codec } from './codec';
|
||||
|
||||
export interface ICompact<T> extends Codec {
|
||||
toBigInt (): BigInt;
|
||||
@@ -18,17 +18,19 @@ export interface IKeyringPair {
|
||||
address: string;
|
||||
addressRaw: Uint8Array;
|
||||
publicKey: Uint8Array;
|
||||
|
||||
sign: (data: Uint8Array, options?: SignOptions) => Uint8Array;
|
||||
}
|
||||
|
||||
export interface IMethod extends Codec {
|
||||
readonly args: Codec[];
|
||||
export interface IMethod<A extends AnyTuple = AnyTuple> extends Codec {
|
||||
readonly args: A;
|
||||
readonly argsDef: ArgsDef;
|
||||
readonly callIndex: Uint8Array;
|
||||
readonly data: Uint8Array;
|
||||
readonly hash: Hash;
|
||||
readonly hasOrigin: boolean;
|
||||
readonly meta: FunctionMetadataLatest;
|
||||
|
||||
is: (tx: IMethod<AnyTuple>) => tx is IMethod<A>;
|
||||
}
|
||||
|
||||
export interface IRuntimeVersion {
|
||||
@@ -44,9 +46,19 @@ export interface IRuntimeVersion {
|
||||
readonly transactionVersion: BN;
|
||||
}
|
||||
|
||||
export interface IStorageKey<A extends AnyTuple> {
|
||||
readonly args: A;
|
||||
readonly meta: StorageEntryMetadataLatest | undefined;
|
||||
readonly method: string | undefined;
|
||||
readonly outputType: string;
|
||||
readonly section: string | undefined;
|
||||
|
||||
is: (key: IStorageKey<AnyTuple>) => key is IStorageKey<A>;
|
||||
}
|
||||
|
||||
// A type alias for [Type1, Type2] & Codec, representing a tuple (Type1, Type2)
|
||||
// FIXME Implement this generic <Sub> on Tuple.ts itself.
|
||||
export type ITuple<Sub extends Codec[]> = Sub & Codec
|
||||
export type ITuple<Sub extends AnyTuple> = Sub & Codec
|
||||
|
||||
export interface IU8a extends Uint8Array, Codec {
|
||||
bitLength (): number;
|
||||
|
||||
Reference in New Issue
Block a user