aboutsummaryrefslogtreecommitdiffstats
path: root/packages/pipeline/src/data-sources/etherscan/events.ts
blob: 3083af267680af6bb941a2ab8d6a585b7c4559c1 (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
55
56
57
58
59
60
61
62
63
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;
}

const hexRadix = 16;

function hexToInt(hex: string): number {
    return parseInt(hex.replace('0x', ''), hexRadix);
}

// Converts a raw event response to a LogEntry
// tslint:disable-next-line:completed-docs
export function _convertResponseToLogEntry(result: EventsResponseResult): LogEntry {
    return {
        logIndex: hexToInt(result.logIndex),
        transactionIndex: hexToInt(result.transactionIndex),
        transactionHash: result.transactionHash,
        blockHash: '',
        blockNumber: hexToInt(result.blockNumber),
        address: result.address,
        data: result.data,
        topics: result.topics,
    };
}

// Decodes a LogEntry into a LogWithDecodedArgs
// tslint:disable-next-line:completed-docs
export function _decodeLogEntry(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(_decodeLogEntry));