aboutsummaryrefslogtreecommitdiffstats
path: root/packages/pipeline/src/data-sources/etherscan/index.ts
blob: c5eb2b9c6f9d5cee8d36f24ae22d6f915b7320a7 (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
import { default as axios } from 'axios';
import { AbiDefinition, BlockParam, BlockParamLiteral } from 'ethereum-types';

import { EventsResponse, ExchangeEventEntity, parseRawEventsResponse } from './events';

const ETHERSCAN_URL = 'https://api.etherscan.io/api';

export class Etherscan {
    private readonly _apiKey: string;
    constructor(apiKey: string) {
        this._apiKey = apiKey;
    }

    /**
     * Gets the decoded events for a specific contract and block range.
     * @param contractAddress  The address of the contract to get the events for.
     * @param constractAbi The ABI of the contract.
     * @param fromBlock The start of the block range to get events for (inclusive).
     * @param toBlock The end of the block range to get events for (inclusive).
     * @returns A list of decoded events.
     */
    public async getContractEventsAsync(
        contractAddress: string,
        contractAbi: AbiDefinition[],
        fromBlock: BlockParam = BlockParamLiteral.Earliest,
        toBlock: BlockParam = BlockParamLiteral.Latest,
    ): Promise<ExchangeEventEntity[]> {
        const fullURL = `${ETHERSCAN_URL}?module=logs&action=getLogs&address=${contractAddress}&fromBlock=${fromBlock}&toBlock=${toBlock}&apikey=${
            this._apiKey
        }`;
        const resp = await axios.get<EventsResponse>(fullURL);
        // TODO(albrow): Check response code.
        const decodedEvents = parseRawEventsResponse(contractAbi, resp.data);
        return decodedEvents;
    }
}