Skip to content

Commit

Permalink
Make handler types generic (#1194)
Browse files Browse the repository at this point in the history
* Make handler types generic

* Fix typeo, update types for ds processors

* Update changelogs

* Improve changelog message
  • Loading branch information
stwiname committed Jul 21, 2022
1 parent 6e2234d commit fccf423
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 31 deletions.
2 changes: 2 additions & 0 deletions packages/node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- Make handler data types generic (#1194)

## [1.5.1] - 2022-07-15
### Fixed
Expand Down
22 changes: 13 additions & 9 deletions packages/node/src/indexer/ds-processor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import fs from 'fs';
import path from 'path';
import { Injectable } from '@nestjs/common';
import { AnyTuple } from '@polkadot/types-codec/types';
import {
isCustomDs,
SubstrateCustomDataSource,
Expand Down Expand Up @@ -35,12 +36,13 @@ export function isSecondLayerHandlerProcessor_0_0_0<
K extends SubstrateHandlerKind,
F,
E,
IT extends AnyTuple = AnyTuple,
DS extends SubstrateCustomDatasource = SubstrateCustomDatasource,
>(
processor:
| SecondLayerHandlerProcessor_0_0_0<K, F, E, DS>
| SecondLayerHandlerProcessor_1_0_0<K, F, E, DS>,
): processor is SecondLayerHandlerProcessor_0_0_0<K, F, E, DS> {
| SecondLayerHandlerProcessor_0_0_0<K, F, E, IT, DS>
| SecondLayerHandlerProcessor_1_0_0<K, F, E, IT, DS>,
): processor is SecondLayerHandlerProcessor_0_0_0<K, F, E, IT, DS> {
// Exisiting datasource processors had no concept of specVersion, therefore undefined is equivalent to 0.0.0
return processor.specVersion === undefined;
}
Expand All @@ -49,25 +51,27 @@ export function isSecondLayerHandlerProcessor_1_0_0<
K extends SubstrateHandlerKind,
F,
E,
IT extends AnyTuple = AnyTuple,
DS extends SubstrateCustomDatasource = SubstrateCustomDatasource,
>(
processor:
| SecondLayerHandlerProcessor_0_0_0<K, F, E, DS>
| SecondLayerHandlerProcessor_1_0_0<K, F, E, DS>,
): processor is SecondLayerHandlerProcessor_1_0_0<K, F, E, DS> {
| SecondLayerHandlerProcessor_0_0_0<K, F, E, IT, DS>
| SecondLayerHandlerProcessor_1_0_0<K, F, E, IT, DS>,
): processor is SecondLayerHandlerProcessor_1_0_0<K, F, E, IT, DS> {
return processor.specVersion === '1.0.0';
}

export function asSecondLayerHandlerProcessor_1_0_0<
K extends SubstrateHandlerKind,
F,
E,
IT extends AnyTuple = AnyTuple,
DS extends SubstrateCustomDatasource = SubstrateCustomDatasource,
>(
processor:
| SecondLayerHandlerProcessor_0_0_0<K, F, E, DS>
| SecondLayerHandlerProcessor_1_0_0<K, F, E, DS>,
): SecondLayerHandlerProcessor_1_0_0<K, F, E, DS> {
| SecondLayerHandlerProcessor_0_0_0<K, F, E, IT, DS>
| SecondLayerHandlerProcessor_1_0_0<K, F, E, IT, DS>,
): SecondLayerHandlerProcessor_1_0_0<K, F, E, IT, DS> {
if (isSecondLayerHandlerProcessor_1_0_0(processor)) {
return processor;
}
Expand Down
1 change: 0 additions & 1 deletion packages/node/src/indexer/indexer.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import { Inject, Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { ApiPromise } from '@polkadot/api';
import { RuntimeVersion } from '@polkadot/types/interfaces';
import { hexToU8a, u8aEq } from '@polkadot/util';
import {
isBlockHandlerProcessor,
Expand Down
3 changes: 3 additions & 0 deletions packages/types/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ All logs must start with the format: [x.y.z] - yyyy-mm-dd

## [Unreleased]

### Changed
- Make `SubstrateExtrinsic` and `SubstrateEvent` types generic. This allows specifying the data/args type rather than being provided with `Codec[]` or `AnyTuple` (#1194)

## [1.1.0] - 2022-05-31
### Changed
- Update name for substrate types (#1012)
Expand Down
17 changes: 12 additions & 5 deletions packages/types/src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Copyright 2020-2022 OnFinality Limited authors & contributors
// SPDX-License-Identifier: Apache-2.0

import {Extrinsic, EventRecord, SignedBlock} from '@polkadot/types/interfaces';
import {AnyTuple, Codec} from '@polkadot/types-codec/types';
import {GenericExtrinsic} from '@polkadot/types/extrinsic';
import {EventRecord, SignedBlock, Extrinsic} from '@polkadot/types/interfaces';
import {IEvent, IExtrinsic} from '@polkadot/types/types';

export interface Entity {
id: string;
Expand All @@ -27,20 +30,24 @@ export interface SubstrateBlock extends SignedBlock {
events: EventRecord[];
}

export interface SubstrateExtrinsic {
export interface SubstrateExtrinsic<A extends AnyTuple = AnyTuple> {
// index in the block
idx: number;
extrinsic: Extrinsic;
extrinsic: GenericExtrinsic<A>;
block: SubstrateBlock;
events: EventRecord[];
events: TypedEventRecord<Codec[]>[];
success: boolean;
}

export interface SubstrateEvent extends EventRecord {
export interface SubstrateEvent<T extends AnyTuple = AnyTuple> extends TypedEventRecord<T> {
// index in the block
idx: number;
extrinsic?: SubstrateExtrinsic;
block: SubstrateBlock;
}

export type DynamicDatasourceCreator = (name: string, args: Record<string, unknown>) => Promise<void>;

type TypedEventRecord<T extends AnyTuple> = Omit<EventRecord, 'event'> & {
event: IEvent<T>;
};
38 changes: 22 additions & 16 deletions packages/types/src/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import {ApiPromise} from '@polkadot/api';
import {RegistryTypes} from '@polkadot/types/types';
import {AnyTuple, RegistryTypes} from '@polkadot/types/types';
import {SubstrateBlock, SubstrateEvent, SubstrateExtrinsic} from './interfaces';

export enum SubstrateDatasourceKind {
Expand All @@ -15,10 +15,10 @@ export enum SubstrateHandlerKind {
Event = 'substrate/EventHandler',
}

export type RuntimeHandlerInputMap = {
export type RuntimeHandlerInputMap<T extends AnyTuple = AnyTuple> = {
[SubstrateHandlerKind.Block]: SubstrateBlock;
[SubstrateHandlerKind.Event]: SubstrateEvent;
[SubstrateHandlerKind.Call]: SubstrateExtrinsic;
[SubstrateHandlerKind.Event]: SubstrateEvent<T>;
[SubstrateHandlerKind.Call]: SubstrateExtrinsic<T>;
};

type RuntimeFilterMap = {
Expand Down Expand Up @@ -124,19 +124,21 @@ export interface SubstrateCustomDatasource<
export interface HandlerInputTransformer_0_0_0<
T extends SubstrateHandlerKind,
E,
IT extends AnyTuple,
DS extends SubstrateCustomDatasource = SubstrateCustomDatasource
> {
(input: RuntimeHandlerInputMap[T], ds: DS, api: ApiPromise, assets?: Record<string, string>): Promise<E>; // | SubstrateBuiltinDataSource
(input: RuntimeHandlerInputMap<IT>[T], ds: DS, api: ApiPromise, assets?: Record<string, string>): Promise<E>; // | SubstrateBuiltinDataSource
}

export interface HandlerInputTransformer_1_0_0<
T extends SubstrateHandlerKind,
F,
E,
IT extends AnyTuple,
DS extends SubstrateCustomDatasource = SubstrateCustomDatasource
> {
(params: {
input: RuntimeHandlerInputMap[T];
input: RuntimeHandlerInputMap<IT>[T];
ds: DS;
filter?: F;
api: ApiPromise;
Expand All @@ -148,19 +150,20 @@ type SecondLayerHandlerProcessorArray<
K extends string,
F extends SubstrateNetworkFilter,
T,
IT extends AnyTuple = AnyTuple,
DS extends SubstrateCustomDatasource<K, F> = SubstrateCustomDatasource<K, F>
> =
| SecondLayerHandlerProcessor<SubstrateHandlerKind.Block, F, T, DS>
| SecondLayerHandlerProcessor<SubstrateHandlerKind.Call, F, T, DS>
| SecondLayerHandlerProcessor<SubstrateHandlerKind.Event, F, T, DS>;
| SecondLayerHandlerProcessor<SubstrateHandlerKind.Block, F, T, IT, DS>
| SecondLayerHandlerProcessor<SubstrateHandlerKind.Call, F, T, IT, DS>
| SecondLayerHandlerProcessor<SubstrateHandlerKind.Event, F, T, IT, DS>;

export interface SubstrateDatasourceProcessor<
K extends string,
F extends SubstrateNetworkFilter,
DS extends SubstrateCustomDatasource<K, F> = SubstrateCustomDatasource<K, F>,
P extends Record<string, SecondLayerHandlerProcessorArray<K, F, any, DS>> = Record<
P extends Record<string, SecondLayerHandlerProcessorArray<K, F, any, any, DS>> = Record<
string,
SecondLayerHandlerProcessorArray<K, F, any, DS>
SecondLayerHandlerProcessorArray<K, F, any, any, DS>
>
> {
kind: K;
Expand Down Expand Up @@ -195,27 +198,30 @@ export interface SecondLayerHandlerProcessor_0_0_0<
K extends SubstrateHandlerKind,
F,
E,
IT extends AnyTuple = AnyTuple,
DS extends SubstrateCustomDatasource = SubstrateCustomDatasource
> extends SecondLayerHandlerProcessorBase<K, F, DS> {
specVersion: undefined;
transformer: HandlerInputTransformer_0_0_0<K, E, DS>;
filterProcessor: (filter: F | undefined, input: RuntimeHandlerInputMap[K], ds: DS) => boolean;
transformer: HandlerInputTransformer_0_0_0<K, E, IT, DS>;
filterProcessor: (filter: F | undefined, input: RuntimeHandlerInputMap<IT>[K], ds: DS) => boolean;
}

export interface SecondLayerHandlerProcessor_1_0_0<
K extends SubstrateHandlerKind,
F,
E,
IT extends AnyTuple = AnyTuple,
DS extends SubstrateCustomDatasource = SubstrateCustomDatasource
> extends SecondLayerHandlerProcessorBase<K, F, DS> {
specVersion: '1.0.0';
transformer: HandlerInputTransformer_1_0_0<K, F, E, DS>;
filterProcessor: (params: {filter: F | undefined; input: RuntimeHandlerInputMap[K]; ds: DS}) => boolean;
transformer: HandlerInputTransformer_1_0_0<K, F, E, IT, DS>;
filterProcessor: (params: {filter: F | undefined; input: RuntimeHandlerInputMap<IT>[K]; ds: DS}) => boolean;
}

export type SecondLayerHandlerProcessor<
K extends SubstrateHandlerKind,
F,
E,
IT extends AnyTuple = AnyTuple,
DS extends SubstrateCustomDatasource = SubstrateCustomDatasource
> = SecondLayerHandlerProcessor_0_0_0<K, F, E, DS> | SecondLayerHandlerProcessor_1_0_0<K, F, E, DS>;
> = SecondLayerHandlerProcessor_0_0_0<K, F, E, IT, DS> | SecondLayerHandlerProcessor_1_0_0<K, F, E, IT, DS>;

0 comments on commit fccf423

Please sign in to comment.