aboutsummaryrefslogtreecommitdiffstats
path: root/packages/pipeline/src/data_types/events/event_handlers/base_event_handler.ts
blob: 59331fc4f6e6efbbf6f3bfafe8f6f0756ab772c0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { AbiDefinition, BlockParam, BlockParamLiteral, LogEntry } from 'ethereum-types';
import * as R from 'ramda';
import { BaseEntity } from 'typeorm';

import { Etherscan } from '../../../data_sources/etherscan';
import { convertResponseToLogEntry } from '../event_utils';

export abstract class BaseEventHandler<EntityType extends BaseEntity> {
    protected _abi: AbiDefinition[];
    protected _address: string;
    protected _etherscan: Etherscan;
    constructor(abi: AbiDefinition[], address: string, etherscan: Etherscan) {
        this._abi = abi;
        this._address = address;
        this._etherscan = etherscan;
    }
    public abstract convertLogEntryToEventEntity(logEntry: LogEntry): EntityType;

    public async getEventsAsync(
        fromBlock: BlockParam = BlockParamLiteral.Earliest,
        toBlock: BlockParam = BlockParamLiteral.Latest,
    ): Promise<EntityType[]> {
        const rawEventsResponse = await this._etherscan.getContractEventsAsync(this._address, fromBlock, toBlock);
        const logEntries = R.map(convertResponseToLogEntry, rawEventsResponse.result);
        // Note(albrow): Imperative for loop is required here because we can't
        // bind convertLogEntryToEventEntity without having a specific instance
        // of a sub-class.
        const result = [];
        for (const logEntry of logEntries) {
            result.push(this.convertLogEntryToEventEntity(logEntry));
        }
        return result;
    }
}