diff options
author | Alex Browne <stephenalexbrowne@gmail.com> | 2018-09-26 05:41:45 +0800 |
---|---|---|
committer | Alex Browne <stephenalexbrowne@gmail.com> | 2018-12-05 06:24:06 +0800 |
commit | 2207f09ce2c8b595f75fce6387256a64c64b23c3 (patch) | |
tree | 2bd5eb25bf57576ec9ba731b666ae3f2d8a5ca0c /packages/pipeline/src/data_types/events/event_handlers | |
parent | fe523e1f3f765077bdaf4dfc345c9dca67693668 (diff) | |
download | dexon-sol-tools-2207f09ce2c8b595f75fce6387256a64c64b23c3.tar dexon-sol-tools-2207f09ce2c8b595f75fce6387256a64c64b23c3.tar.gz dexon-sol-tools-2207f09ce2c8b595f75fce6387256a64c64b23c3.tar.bz2 dexon-sol-tools-2207f09ce2c8b595f75fce6387256a64c64b23c3.tar.lz dexon-sol-tools-2207f09ce2c8b595f75fce6387256a64c64b23c3.tar.xz dexon-sol-tools-2207f09ce2c8b595f75fce6387256a64c64b23c3.tar.zst dexon-sol-tools-2207f09ce2c8b595f75fce6387256a64c64b23c3.zip |
Make event parsing more functional and less class-based
Diffstat (limited to 'packages/pipeline/src/data_types/events/event_handlers')
-rw-r--r-- | packages/pipeline/src/data_types/events/event_handlers/base_event_handler.ts | 34 | ||||
-rw-r--r-- | packages/pipeline/src/data_types/events/event_handlers/exchange_event_handler.ts | 76 |
2 files changed, 0 insertions, 110 deletions
diff --git a/packages/pipeline/src/data_types/events/event_handlers/base_event_handler.ts b/packages/pipeline/src/data_types/events/event_handlers/base_event_handler.ts deleted file mode 100644 index 59331fc4f..000000000 --- a/packages/pipeline/src/data_types/events/event_handlers/base_event_handler.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { AbiDefinition, BlockParam, BlockParamLiteral, LogEntry } from 'ethereum-types'; -import * as R from 'ramda'; -import { BaseEntity } from 'typeorm'; - -import { Etherscan } from '../../../data_sources/etherscan'; -import { convertResponseToLogEntry } from '../event_utils'; - -export abstract class BaseEventHandler<EntityType extends BaseEntity> { - protected _abi: AbiDefinition[]; - protected _address: string; - protected _etherscan: Etherscan; - constructor(abi: AbiDefinition[], address: string, etherscan: Etherscan) { - this._abi = abi; - this._address = address; - this._etherscan = etherscan; - } - public abstract convertLogEntryToEventEntity(logEntry: LogEntry): EntityType; - - public async getEventsAsync( - fromBlock: BlockParam = BlockParamLiteral.Earliest, - toBlock: BlockParam = BlockParamLiteral.Latest, - ): Promise<EntityType[]> { - const rawEventsResponse = await this._etherscan.getContractEventsAsync(this._address, fromBlock, toBlock); - const logEntries = R.map(convertResponseToLogEntry, rawEventsResponse.result); - // Note(albrow): Imperative for loop is required here because we can't - // bind convertLogEntryToEventEntity without having a specific instance - // of a sub-class. - const result = []; - for (const logEntry of logEntries) { - result.push(this.convertLogEntryToEventEntity(logEntry)); - } - return result; - } -} diff --git a/packages/pipeline/src/data_types/events/event_handlers/exchange_event_handler.ts b/packages/pipeline/src/data_types/events/event_handlers/exchange_event_handler.ts deleted file mode 100644 index 38ff20595..000000000 --- a/packages/pipeline/src/data_types/events/event_handlers/exchange_event_handler.ts +++ /dev/null @@ -1,76 +0,0 @@ -import { ExchangeEventArgs, ExchangeFillEventArgs } from '@0xproject/contract-wrappers'; -import { assetDataUtils } from '@0xproject/order-utils'; -import { AssetProxyId, ERC721AssetData } from '@0xproject/types'; -import { AbiDecoder, BigNumber } from '@0xproject/utils'; -import { AbiDefinition, LogEntry, LogWithDecodedArgs } from 'ethereum-types'; -import * as R from 'ramda'; - -import { ExchangeFillEvent } from '../../../entities/ExchangeFillEvent'; -import { decodeLogEntry } from '../event_utils'; - -import { BaseEventHandler } from './base_event_handler'; - -// TODO(albrow): Union with other exchange event entity types -export type ExchangeEventEntity = ExchangeFillEvent; - -export class ExchangeEventHandler extends BaseEventHandler<ExchangeEventEntity> { - public convertLogEntryToEventEntity(logEntry: LogEntry): ExchangeEventEntity { - const decodedLogEntry = decodeLogEntry<ExchangeEventArgs>(this._abi, logEntry); - return _convertToEntity(decodedLogEntry); - } -} - -export function _convertToEntity(eventLog: LogWithDecodedArgs<ExchangeEventArgs>): ExchangeEventEntity { - switch (eventLog.event) { - case 'Fill': - return _convertToExchangeFillEvent(eventLog as LogWithDecodedArgs<ExchangeFillEventArgs>); - default: - return new ExchangeFillEvent(); - // throw new Error('unexpected eventLog.event type: ' + eventLog.event); - } -} - -export function _convertToExchangeFillEvent(eventLog: LogWithDecodedArgs<ExchangeFillEventArgs>): ExchangeFillEvent { - const makerAssetData = assetDataUtils.decodeAssetDataOrThrow(eventLog.args.makerAssetData); - const makerAssetType = makerAssetData.assetProxyId === AssetProxyId.ERC20 ? 'erc20' : 'erc721'; - const takerAssetData = assetDataUtils.decodeAssetDataOrThrow(eventLog.args.takerAssetData); - const takerAssetType = takerAssetData.assetProxyId === AssetProxyId.ERC20 ? 'erc20' : 'erc721'; - const exchangeFillEvent = new ExchangeFillEvent(); - exchangeFillEvent.logIndex = eventLog.logIndex as number; - exchangeFillEvent.address = eventLog.address as string; - exchangeFillEvent.rawData = eventLog.data as string; - exchangeFillEvent.blockNumber = eventLog.blockNumber as number; - exchangeFillEvent.makerAddress = eventLog.args.makerAddress.toString(); - exchangeFillEvent.takerAddress = eventLog.args.takerAddress.toString(); - exchangeFillEvent.feeRecepientAddress = eventLog.args.feeRecipientAddress; - exchangeFillEvent.senderAddress = eventLog.args.senderAddress; - exchangeFillEvent.makerAssetFilledAmount = eventLog.args.makerAssetFilledAmount.toString(); - exchangeFillEvent.takerAssetFilledAmount = eventLog.args.takerAssetFilledAmount.toString(); - exchangeFillEvent.makerFeePaid = eventLog.args.makerFeePaid.toString(); - exchangeFillEvent.takerFeePaid = eventLog.args.takerFeePaid.toString(); - exchangeFillEvent.orderHash = eventLog.args.orderHash; - exchangeFillEvent.rawMakerAssetData = eventLog.args.makerAssetData; - exchangeFillEvent.makerAssetType = makerAssetType; - exchangeFillEvent.makerAssetProxyId = makerAssetData.assetProxyId; - exchangeFillEvent.makerTokenAddress = makerAssetData.tokenAddress; - exchangeFillEvent.makerTokenId = bigNumbertoStringOrNull((makerAssetData as ERC721AssetData).tokenId); - exchangeFillEvent.rawTakerAssetData = eventLog.args.takerAssetData; - exchangeFillEvent.takerAssetType = takerAssetType; - exchangeFillEvent.takerAssetProxyId = takerAssetData.assetProxyId; - exchangeFillEvent.takerTokenAddress = takerAssetData.tokenAddress; - exchangeFillEvent.takerTokenId = bigNumbertoStringOrNull((takerAssetData as ERC721AssetData).tokenId); - return exchangeFillEvent; -} - -function bigNumbertoStringOrNull(n: BigNumber): string | null { - if (n == null) { - return null; - } - return n.toString(); -} - -function filterEventLogs( - eventLogs: Array<LogWithDecodedArgs<ExchangeEventArgs>>, -): Array<LogWithDecodedArgs<ExchangeEventArgs>> { - return R.filter(eventLog => eventLog.event === 'Fill', eventLogs); -} |