aboutsummaryrefslogtreecommitdiffstats
path: root/packages/pipeline/src/data_sources/contract-wrappers/erc20_events.ts
blob: e0098122fd556dac6199095f2525649f53cc5160 (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
import {
    ContractWrappers,
    ERC20TokenApprovalEventArgs,
    ERC20TokenEvents,
    ERC20TokenWrapper,
} from '@0x/contract-wrappers';
import { Web3ProviderEngine } from '@0x/subproviders';
import { LogWithDecodedArgs } from 'ethereum-types';

import { GetEventsFunc, getEventsWithPaginationAsync } from './utils';

export class ERC20EventsSource {
    private readonly _erc20Wrapper: ERC20TokenWrapper;
    private readonly _tokenAddress: string;
    constructor(provider: Web3ProviderEngine, networkId: number, tokenAddress: string) {
        const contractWrappers = new ContractWrappers(provider, { networkId });
        this._erc20Wrapper = contractWrappers.erc20Token;
        this._tokenAddress = tokenAddress;
    }

    public async getApprovalEventsAsync(
        startBlock: number,
        endBlock: number,
    ): Promise<Array<LogWithDecodedArgs<ERC20TokenApprovalEventArgs>>> {
        return getEventsWithPaginationAsync(
            this._getApprovalEventsForRangeAsync.bind(this) as GetEventsFunc<ERC20TokenApprovalEventArgs>,
            startBlock,
            endBlock,
        );
    }

    // Gets all approval events of for a specific sub-range. This getter
    // function will be called during each step of pagination.
    private async _getApprovalEventsForRangeAsync(
        fromBlock: number,
        toBlock: number,
    ): Promise<Array<LogWithDecodedArgs<ERC20TokenApprovalEventArgs>>> {
        return this._erc20Wrapper.getLogsAsync<ERC20TokenApprovalEventArgs>(
            this._tokenAddress,
            ERC20TokenEvents.Approval,
            { fromBlock, toBlock },
            {},
        );
    }
}