diff options
author | Alex Browne <stephenalexbrowne@gmail.com> | 2018-09-21 08:25:48 +0800 |
---|---|---|
committer | Fred Carlsen <fred@sjelfull.no> | 2018-12-13 01:12:06 +0800 |
commit | 0dc000183aabab129d6b642eae468ed2996a75c4 (patch) | |
tree | d37a93d8b51a371181bee20c8ad8f9c5bbe42174 /packages/pipeline/src/data-sources/etherscan | |
parent | 90e1028d2fd154b8c637139145a2dc1c6999d585 (diff) | |
download | dexon-0x-contracts-0dc000183aabab129d6b642eae468ed2996a75c4.tar.gz dexon-0x-contracts-0dc000183aabab129d6b642eae468ed2996a75c4.tar.zst dexon-0x-contracts-0dc000183aabab129d6b642eae468ed2996a75c4.zip |
Add support for decoding asset data
Diffstat (limited to 'packages/pipeline/src/data-sources/etherscan')
-rw-r--r-- | packages/pipeline/src/data-sources/etherscan/events.ts | 78 | ||||
-rw-r--r-- | packages/pipeline/src/data-sources/etherscan/index.ts | 6 |
2 files changed, 74 insertions, 10 deletions
diff --git a/packages/pipeline/src/data-sources/etherscan/events.ts b/packages/pipeline/src/data-sources/etherscan/events.ts index edc8cde7b..a828af527 100644 --- a/packages/pipeline/src/data-sources/etherscan/events.ts +++ b/packages/pipeline/src/data-sources/etherscan/events.ts @@ -1,8 +1,15 @@ -import { ExchangeEventArgs } from '@0xproject/contract-wrappers'; -import { AbiDecoder } from '@0xproject/utils'; +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'; + +// TODO(albrow): Union with other exchange event entity types +export type ExchangeEventEntity = ExchangeFillEvent; + // Raw events response from etherescan.io export interface EventsResponse { status: string; @@ -56,17 +63,74 @@ export const _decodeLogEntry = R.curry((contractAbi: AbiDefinition[], log: LogEn return logWithDecodedArgs as LogWithDecodedArgs<ExchangeEventArgs>; }); +export function _convertToEntity(eventLog: LogWithDecodedArgs<ExchangeEventArgs>): ExchangeEventEntity { + switch (eventLog.event) { + case 'Fill': + return _convertToExchangeFillEvent(eventLog as LogWithDecodedArgs<ExchangeFillEventArgs>); + default: + 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); +} + /** * Parses and abi-decodes the raw events response from etherscan.io. * @param contractAbi The ABI for the contract that the events where emited from. * @param rawEventsResponse The raw events response from etherescan.io. - * @returns Parsed and decoded events. + * @returns Parsed and decoded event entities, ready to be saved to database. */ export function parseRawEventsResponse( contractAbi: AbiDefinition[], rawEventsResponse: EventsResponse, -): Array<LogWithDecodedArgs<ExchangeEventArgs>> { - return R.pipe(R.map(_convertResponseToLogEntry), R.map(_decodeLogEntry(contractAbi)))(rawEventsResponse.result); +): ExchangeEventEntity[] { + return R.pipe( + R.map(_convertResponseToLogEntry), + R.map(_decodeLogEntry(contractAbi)), + filterEventLogs, + R.map(_convertToEntity), + )(rawEventsResponse.result); } - -// export const parseRawEventsResponse = R.pipe(R.map(_convertResponseToLogEntry), R.map(_decodeLogEntry)); diff --git a/packages/pipeline/src/data-sources/etherscan/index.ts b/packages/pipeline/src/data-sources/etherscan/index.ts index 66b6b1a8d..c5eb2b9c6 100644 --- a/packages/pipeline/src/data-sources/etherscan/index.ts +++ b/packages/pipeline/src/data-sources/etherscan/index.ts @@ -1,7 +1,7 @@ import { default as axios } from 'axios'; -import { AbiDefinition, BlockParam, BlockParamLiteral, DecodedLogArgs, LogWithDecodedArgs } from 'ethereum-types'; +import { AbiDefinition, BlockParam, BlockParamLiteral } from 'ethereum-types'; -import { EventsResponse, parseRawEventsResponse } from './events'; +import { EventsResponse, ExchangeEventEntity, parseRawEventsResponse } from './events'; const ETHERSCAN_URL = 'https://api.etherscan.io/api'; @@ -24,7 +24,7 @@ export class Etherscan { contractAbi: AbiDefinition[], fromBlock: BlockParam = BlockParamLiteral.Earliest, toBlock: BlockParam = BlockParamLiteral.Latest, - ): Promise<Array<LogWithDecodedArgs<DecodedLogArgs>>> { + ): Promise<ExchangeEventEntity[]> { const fullURL = `${ETHERSCAN_URL}?module=logs&action=getLogs&address=${contractAddress}&fromBlock=${fromBlock}&toBlock=${toBlock}&apikey=${ this._apiKey }`; |