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:
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { IsEvent } from '@polkadot/types/metadata/decorate/types';
|
||||
import type { AnyTuple, Codec } from '@polkadot/types/types';
|
||||
import type { AnyTuple } from '@polkadot/types/types';
|
||||
import type { ApiTypes } from './base';
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars,@typescript-eslint/no-empty-interface
|
||||
@@ -16,7 +16,7 @@ export interface DecoratedEvents<ApiType extends ApiTypes> extends AugmentedEven
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
export type AugmentedEvent<ApiType extends ApiTypes, T extends AnyTuple = AnyTuple, N extends Record<string, Codec> = Record<string, Codec>> = IsEvent<T, N>;
|
||||
export type AugmentedEvent<ApiType extends ApiTypes, T extends AnyTuple = AnyTuple, N = unknown> = IsEvent<T, N>;
|
||||
|
||||
export interface ModuleEvents<ApiType extends ApiTypes> {
|
||||
[key: string]: AugmentedEvent<ApiType>;
|
||||
|
||||
@@ -59,15 +59,31 @@ function events (api: ApiPromise): void {
|
||||
// the types are correctly expanded
|
||||
const [afrom, ato, aamount] = event.data;
|
||||
|
||||
console.log(
|
||||
afrom.toHuman(),
|
||||
ato.toHuman(),
|
||||
aamount.toBn()
|
||||
);
|
||||
|
||||
// the types have getters
|
||||
const { amount, from, to } = event.data;
|
||||
|
||||
console.log(afrom.toHuman(), ato.toHuman(), aamount.toBn(), from.toHuman(), to.toHuman(), amount.toBn());
|
||||
console.log(
|
||||
from.toHuman(),
|
||||
to.toHuman(),
|
||||
amount.toBn()
|
||||
);
|
||||
}
|
||||
|
||||
// something random
|
||||
// something with only tuple data
|
||||
if (api.events.staking.Bonded.is(event)) {
|
||||
const [account, amount] = event.data;
|
||||
|
||||
console.log(account.toHuman(), amount.toBn());
|
||||
}
|
||||
|
||||
// something random, just codec[]
|
||||
if (api.events.something.Random.is(event)) {
|
||||
// the types are just codec
|
||||
const [a, b] = event.data;
|
||||
|
||||
console.log(a.toHuman(), b.toHuman());
|
||||
|
||||
@@ -16,6 +16,9 @@ import { compareName, createImports, formatType, initMeta, readTemplate, setImpo
|
||||
|
||||
const generateForMetaTemplate = Handlebars.compile(readTemplate('events'));
|
||||
|
||||
// For babel itself we need some extra aliasing
|
||||
const ALIAS = ['symbol'];
|
||||
|
||||
/** @internal */
|
||||
function generateForMeta (meta: Metadata, dest: string, extraTypes: ExtraTypes, isStrict: boolean, customLookupDefinitions?: Definitions): void {
|
||||
writeFile(dest, (): string => {
|
||||
@@ -50,11 +53,10 @@ function generateForMeta (meta: Metadata, dest: string, extraTypes: ExtraTypes,
|
||||
|
||||
return {
|
||||
docs,
|
||||
fields: names.length === args.length
|
||||
? `{ ${names.map((n, i) => `${n}: ${args[i]}`).join(', ')} }`
|
||||
: '{}',
|
||||
name: name.toString(),
|
||||
type: `[${args.join(', ')}]`
|
||||
type: names.length !== 0 && names.length === args.length
|
||||
? `[${names.map((n, i) => `${ALIAS.includes(n) ? `${n}_` : n}: ${args[i]}`).join(', ')}], { ${names.map((n, i) => `${n}: ${args[i]}`).join(', ')} }`
|
||||
: `[${args.join(', ')}]`
|
||||
};
|
||||
})
|
||||
.sort(compareName),
|
||||
|
||||
@@ -8,7 +8,7 @@ declare module '@polkadot/api-base/types/events' {
|
||||
{{{name}}}: {
|
||||
{{#each items}}
|
||||
{{> docs}}
|
||||
{{{name}}}: AugmentedEvent<ApiType, {{{type}}}, {{{fields}}}>;
|
||||
{{{name}}}: AugmentedEvent<ApiType, {{{type}}}>;
|
||||
{{/each}}
|
||||
{{#unless @root.isStrict}}
|
||||
/**
|
||||
|
||||
@@ -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>;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user