aboutsummaryrefslogtreecommitdiffstats
path: root/packages/pipeline/src/data-sources/etherscan/events.ts
blob: 50962a266638a668354a54295568f575a151daa8 (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { AbiDecoder } from '@0xproject/utils';
import { DecodedLogArgs, LogEntry, LogWithDecodedArgs } from 'ethereum-types';
import * as R from 'ramda';

import { artifacts } from '../../artifacts';

// Raw events response from etherescan.io
export interface EventsResponse {
    status: string;
    message: string;
    result: EventsResponseResult[];
}

// Events as represented in the response from etherscan.io
export interface EventsResponseResult {
    address: string;
    topics: string[];
    data: string;
    blockNumber: string;
    timeStamp: string;
    gasPrice: string;
    gasUsed: string;
    logIndex: string;
    transactionHash: string;
    transactionIndex: string;
}

function convertResponseToLogEntry(result: EventsResponseResult): LogEntry {
    const radix = 10;
    return {
        logIndex: parseInt(result.logIndex, radix),
        transactionIndex: parseInt(result.logIndex, radix),
        transactionHash: result.transactionHash,
        blockHash: '',
        blockNumber: parseInt(result.blockNumber, radix),
        address: result.address,
        data: result.data,
        topics: result.topics,
    };
}

function tryToDecodeLogOrNoop(log: LogEntry): LogWithDecodedArgs<DecodedLogArgs> {
    const abiDecoder = new AbiDecoder([artifacts.Exchange.compilerOutput.abi]);
    const logWithDecodedArgs = abiDecoder.tryToDecodeLogOrNoop(log);
    // tslint:disable-next-line:no-unnecessary-type-assertion
    return logWithDecodedArgs as LogWithDecodedArgs<DecodedLogArgs>;
}

/**
 * Parses and abi-decodes the raw events response from etherscan.io.
 * @param rawEventsResponse The raw events response from etherescan.io.
 * @returns Parsed and decoded events.
 */
export const parseRawEventsResponse = R.pipe(R.map(convertResponseToLogEntry), R.map(tryToDecodeLogOrNoop));