Adjust named enum fields augmentation (#4919)

* Adjust named enum fields augmentation

* Named tuples

* Adjust sanitize for symbol

* Explicit check

* Remove unused code

* Adjust expansion

* Adjust (only keep to named tuples this round)

* Pass struct
This commit is contained in:
Jaco
2022-06-10 08:57:16 +03:00
committed by GitHub
parent daeb86a7af
commit 1e785b50da
11 changed files with 788 additions and 760 deletions
+2 -2
View File
@@ -131,7 +131,7 @@ export class GenericEventData extends Tuple implements IEventData {
* 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 implements IEvent<Codec[], Record<string, Codec>> {
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) {
@@ -147,7 +147,7 @@ export class GenericEvent extends Struct implements IEvent<Codec[], Record<strin
/**
* @description The wrapped [[EventData]]
*/
public get data (): GenericEventData & Record<string, Codec> {
public get data (): IEvent<Codec[]>['data'] {
return this.getT('data');
}
@@ -1,9 +1,9 @@
// Copyright 2017-2022 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyTuple, Codec, Registry } from '@polkadot/types-codec/types';
import type { AnyTuple, Registry } from '@polkadot/types-codec/types';
import type { MetadataLatest, PalletMetadataLatest, SiVariant } from '../../../interfaces';
import type { IEvent } from '../../../types';
import type { IEvent, IEventLike } from '../../../types';
import type { Events, IsEvent } from '../types';
import { isCodec, isU8a, lazyMethod, stringCamelCase } from '@polkadot/util';
@@ -26,9 +26,9 @@ export function decorateEvents (registry: Registry, { lookup, pallets }: Metadat
const sectionIndex = version >= 12 ? index.toNumber() : i;
lazyMethod(result, stringCamelCase(name), () =>
lazyVariants(lookup, events.unwrap(), objectNameToString, (variant: SiVariant): IsEvent<AnyTuple, Record<string, Codec>> => ({
lazyVariants(lookup, events.unwrap(), objectNameToString, (variant: SiVariant): IsEvent<AnyTuple> => ({
// We sprinkle in isCodec & isU8a to ensure we are dealing with the correct objects
is: <T extends AnyTuple, N extends Record<string, Codec>> (eventRecord: IEvent<AnyTuple, Record<string, Codec>>): eventRecord is IEvent<T, N> =>
is: <T extends AnyTuple> (eventRecord: IEventLike): eventRecord is IEvent<T> =>
isCodec(eventRecord) &&
isU8a(eventRecord.index) &&
sectionIndex === eventRecord.index[0] &&
@@ -4,7 +4,7 @@
import type { AnyTuple, Codec, Registry } from '@polkadot/types-codec/types';
import type { DispatchErrorModule, DispatchErrorModuleU8, DispatchErrorModuleU8a, ErrorMetadataLatest, EventMetadataLatest, PalletConstantMetadataLatest } from '../../interfaces';
import type { StorageEntry } from '../../primitive/types';
import type { CallFunction, IEvent } from '../../types';
import type { CallFunction, IEvent, IEventLike } from '../../types';
export interface ConstantCodec extends Codec {
readonly meta: PalletConstantMetadataLatest;
@@ -16,17 +16,17 @@ export interface IsError {
is: (moduleError: DispatchErrorModule | DispatchErrorModuleU8 | DispatchErrorModuleU8a) => boolean;
}
export interface IsEvent <T extends AnyTuple, N extends Record<string, Codec>> {
export interface IsEvent <T extends AnyTuple, N = unknown> {
readonly meta: EventMetadataLatest;
is: (event: IEvent<AnyTuple, Record<string, Codec>>) => event is IEvent<T, N>;
is: (event: IEventLike) => event is IEvent<T, N>;
}
export type ModuleConstants = Record<string, ConstantCodec>;
export type ModuleErrors = Record<string, IsError>;
export type ModuleEvents = Record<string, IsEvent<AnyTuple, Record<string, Codec>>>;
export type ModuleEvents = Record<string, IsEvent<AnyTuple>>;
export type ModuleExtrinsics = Record<string, CallFunction>;
+16 -6
View File
@@ -1,25 +1,35 @@
// Copyright 2017-2022 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Codec, ITuple } from '@polkadot/types-codec/types';
import type { Codec } from '@polkadot/types-codec/types';
import type { TypeDef } from '@polkadot/types-create/types';
import type { EventMetadataLatest } from '../interfaces/metadata';
import type { Hash } from '../interfaces/runtime';
import type { EventId, Phase } from '../interfaces/system';
export interface IEventRecord<T extends Codec[], N extends Record<string, Codec> = Record<string, Codec>> {
export interface IEventRecord<T extends Codec[]> extends Codec {
readonly phase: Phase;
readonly event: IEvent<T, N>;
readonly event: IEvent<T>;
readonly topics: Hash[];
}
export interface IEventData {
export interface IEventData extends Codec {
readonly meta: EventMetadataLatest;
readonly method: string;
readonly section: string;
readonly typeDef: TypeDef[];
}
export interface IEvent<T extends Codec[], N extends Record<string, Codec> = Record<string, Codec>> {
readonly data: ITuple<T> & IEventData & N;
export interface IEventLike {
readonly index: unknown;
readonly method: unknown;
readonly section: unknown;
}
export interface IEvent<T extends Codec[], N = unknown> extends IEventLike, Codec {
readonly data: N extends Record<string, Codec>
? N & T & IEventData
: T & IEventData;
readonly index: EventId;
readonly method: string;
readonly section: string;