diff options
author | Alex Browne <stephenalexbrowne@gmail.com> | 2018-10-18 07:44:07 +0800 |
---|---|---|
committer | Fred Carlsen <fred@sjelfull.no> | 2018-12-06 19:04:24 +0800 |
commit | 427c2cd164f841f6b296b059c119fabb113c4e65 (patch) | |
tree | 5a10964c642ac1f313e236429a672fcd6761c7a6 /packages/pipeline/src/data_sources | |
parent | 8c0dfc1936340a08ff627db3c6203bf6836c2992 (diff) | |
download | dexon-0x-contracts-427c2cd164f841f6b296b059c119fabb113c4e65.tar.gz dexon-0x-contracts-427c2cd164f841f6b296b059c119fabb113c4e65.tar.zst dexon-0x-contracts-427c2cd164f841f6b296b059c119fabb113c4e65.zip |
Update to use ContractWrappers + Infura instead of Etherscan
Diffstat (limited to 'packages/pipeline/src/data_sources')
-rw-r--r-- | packages/pipeline/src/data_sources/contract-wrappers/exchange_events.ts | 51 | ||||
-rw-r--r-- | packages/pipeline/src/data_sources/etherscan/index.ts | 52 |
2 files changed, 51 insertions, 52 deletions
diff --git a/packages/pipeline/src/data_sources/contract-wrappers/exchange_events.ts b/packages/pipeline/src/data_sources/contract-wrappers/exchange_events.ts new file mode 100644 index 000000000..77217c601 --- /dev/null +++ b/packages/pipeline/src/data_sources/contract-wrappers/exchange_events.ts @@ -0,0 +1,51 @@ +import { ContractWrappers, ExchangeEvents, ExchangeFillEventArgs, ExchangeWrapper } from '@0xproject/contract-wrappers'; +import { Web3ProviderEngine } from '@0xproject/subproviders'; +import { Web3Wrapper } from '@0xproject/web3-wrapper'; +import { LogWithDecodedArgs } from 'ethereum-types'; + +const BLOCK_FINALITY_THRESHOLD = 10; // When to consider blocks as final. Used to compute default toBlock. +const NUM_BLOCKS_PER_QUERY = 100000; // Number of blocks to query for events at a time. +const EXCHANGE_START_BLOCK = 6271590; // Block number when the Exchange contract was deployed to mainnet. + +export class ExchangeEventsSource { + private _exchangeWrapper: ExchangeWrapper; + private _web3Wrapper: Web3Wrapper; + constructor(provider: Web3ProviderEngine, networkId: number) { + this._web3Wrapper = new Web3Wrapper(provider); + const contractWrappers = new ContractWrappers(provider, { networkId }); + this._exchangeWrapper = contractWrappers.exchange; + } + + // TODO(albrow): Get Cancel and CancelUpTo events. + + public async getFillEventsAsync( + fromBlock: number = EXCHANGE_START_BLOCK, + toBlock?: number, + ): Promise<Array<LogWithDecodedArgs<ExchangeFillEventArgs>>> { + const calculatedToBlock = + toBlock === undefined + ? (await this._web3Wrapper.getBlockNumberAsync()) - BLOCK_FINALITY_THRESHOLD + : toBlock; + let events: Array<LogWithDecodedArgs<ExchangeFillEventArgs>> = []; + for (let currFromBlock = fromBlock; currFromBlock <= calculatedToBlock; currFromBlock += NUM_BLOCKS_PER_QUERY) { + events = events.concat( + await this._getFillEventsForRangeAsync(currFromBlock, currFromBlock + NUM_BLOCKS_PER_QUERY - 1), + ); + } + return events; + } + + private async _getFillEventsForRangeAsync( + fromBlock: number, + toBlock: number, + ): Promise<Array<LogWithDecodedArgs<ExchangeFillEventArgs>>> { + return this._exchangeWrapper.getLogsAsync<ExchangeFillEventArgs>( + ExchangeEvents.Fill, + { + fromBlock, + toBlock, + }, + {}, + ); + } +} diff --git a/packages/pipeline/src/data_sources/etherscan/index.ts b/packages/pipeline/src/data_sources/etherscan/index.ts deleted file mode 100644 index 044fff02e..000000000 --- a/packages/pipeline/src/data_sources/etherscan/index.ts +++ /dev/null @@ -1,52 +0,0 @@ -import { default as axios } from 'axios'; -import { BlockParam, BlockParamLiteral } from 'ethereum-types'; - -const ETHERSCAN_URL = 'https://api.etherscan.io/api'; - -export class Etherscan { - private readonly _apiKey: string; - constructor(apiKey: string) { - this._apiKey = apiKey; - } - - /** - * Gets the raw events for a specific contract and block range. - * @param contractAddress The address of the contract to get the events for. - * @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, - fromBlock: BlockParam = BlockParamLiteral.Earliest, - toBlock: BlockParam = BlockParamLiteral.Latest, - ): Promise<EventsResponse> { - 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. - return resp.data; - } -} - -// 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; -} |