import { ContractWrappers, ExchangeCancelEventArgs, ExchangeCancelUpToEventArgs, ExchangeEventArgs, ExchangeEvents, ExchangeFillEventArgs, ExchangeWrapper, } from '@0x/contract-wrappers'; import { Web3ProviderEngine } from '@0x/subproviders'; import { LogWithDecodedArgs } from 'ethereum-types'; import { GetEventsFunc, getEventsWithPaginationAsync } from './utils'; export class ExchangeEventsSource { private readonly _exchangeWrapper: ExchangeWrapper; constructor(provider: Web3ProviderEngine, networkId: number) { const contractWrappers = new ContractWrappers(provider, { networkId }); this._exchangeWrapper = contractWrappers.exchange; } public async getFillEventsAsync( startBlock: number, endBlock: number, ): Promise>> { const getFillEventsForRangeAsync = this._makeGetterFuncForEventType(ExchangeEvents.Fill); return getEventsWithPaginationAsync(getFillEventsForRangeAsync, startBlock, endBlock); } public async getCancelEventsAsync( startBlock: number, endBlock: number, ): Promise>> { const getCancelEventsForRangeAsync = this._makeGetterFuncForEventType( ExchangeEvents.Cancel, ); return getEventsWithPaginationAsync(getCancelEventsForRangeAsync, startBlock, endBlock); } public async getCancelUpToEventsAsync( startBlock: number, endBlock: number, ): Promise>> { const getCancelUpToEventsForRangeAsync = this._makeGetterFuncForEventType( ExchangeEvents.CancelUpTo, ); return getEventsWithPaginationAsync(getCancelUpToEventsForRangeAsync, startBlock, endBlock); } // Returns a getter function which gets all events of a specific type for a // specific sub-range. This getter function will be called during each step // of pagination. private _makeGetterFuncForEventType( eventType: ExchangeEvents, ): GetEventsFunc { return async (fromBlock: number, toBlock: number) => this._exchangeWrapper.getLogsAsync(eventType, { fromBlock, toBlock }, {}); } }