From 91e7485ecc4e780ef454b642a52c3d93faa95af1 Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Wed, 17 Oct 2018 16:44:07 -0700 Subject: Update to use ContractWrappers + Infura instead of Etherscan --- packages/pipeline/src/parsers/events/index.ts | 111 ++++++++++++++++++++++ packages/pipeline/src/parsers/sra_orders/index.ts | 54 +++++++++++ 2 files changed, 165 insertions(+) create mode 100644 packages/pipeline/src/parsers/events/index.ts create mode 100644 packages/pipeline/src/parsers/sra_orders/index.ts (limited to 'packages/pipeline/src/parsers') diff --git a/packages/pipeline/src/parsers/events/index.ts b/packages/pipeline/src/parsers/events/index.ts new file mode 100644 index 000000000..66f382dda --- /dev/null +++ b/packages/pipeline/src/parsers/events/index.ts @@ -0,0 +1,111 @@ +import { + ExchangeCancelEventArgs, + ExchangeCancelUpToEventArgs, + ExchangeEventArgs, + ExchangeFillEventArgs, +} from '@0xproject/contract-wrappers'; +import { assetDataUtils } from '@0xproject/order-utils'; +import { AssetProxyId, ERC721AssetData } from '@0xproject/types'; +import { LogWithDecodedArgs } from 'ethereum-types'; +import * as R from 'ramda'; + +import { ExchangeCancelEvent } from '../../entities/ExchangeCancelEvent'; +import { ExchangeCancelUpToEvent } from '../../entities/ExchangeCancelUpToEvent'; +import { ExchangeFillEvent } from '../../entities/ExchangeFillEvent'; +import { bigNumbertoStringOrNull } from '../../utils'; + +export type ExchangeEventEntity = ExchangeFillEvent | ExchangeCancelEvent | ExchangeCancelUpToEvent; + +export const parseExchangeEvents: ( + eventLogs: Array>, +) => ExchangeEventEntity[] = R.map(_convertToEntity); + +export function _convertToEntity(eventLog: LogWithDecodedArgs): ExchangeEventEntity { + switch (eventLog.event) { + case 'Fill': + return _convertToExchangeFillEvent(eventLog as LogWithDecodedArgs); + case 'Cancel': + return _convertToExchangeCancelEvent(eventLog as LogWithDecodedArgs); + case 'CancelUpTo': + return _convertToExchangeCancelUpToEvent(eventLog as LogWithDecodedArgs); + default: + throw new Error('unexpected eventLog.event type: ' + eventLog.event); + } +} + +export function _convertToExchangeFillEvent(eventLog: LogWithDecodedArgs): 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.contractAddress = eventLog.address as string; + exchangeFillEvent.blockNumber = eventLog.blockNumber as number; + exchangeFillEvent.logIndex = eventLog.logIndex as number; + exchangeFillEvent.rawData = eventLog.data as string; + 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; +} + +export function _convertToExchangeCancelEvent( + eventLog: LogWithDecodedArgs, +): ExchangeCancelEvent { + 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 exchangeCancelEvent = new ExchangeCancelEvent(); + exchangeCancelEvent.contractAddress = eventLog.address as string; + exchangeCancelEvent.blockNumber = eventLog.blockNumber as number; + exchangeCancelEvent.logIndex = eventLog.logIndex as number; + exchangeCancelEvent.rawData = eventLog.data as string; + exchangeCancelEvent.makerAddress = eventLog.args.makerAddress.toString(); + exchangeCancelEvent.takerAddress = + eventLog.args.takerAddress == null ? null : eventLog.args.takerAddress.toString(); + exchangeCancelEvent.feeRecepientAddress = eventLog.args.feeRecipientAddress; + exchangeCancelEvent.senderAddress = eventLog.args.senderAddress; + exchangeCancelEvent.orderHash = eventLog.args.orderHash; + exchangeCancelEvent.rawMakerAssetData = eventLog.args.makerAssetData; + exchangeCancelEvent.makerAssetType = makerAssetType; + exchangeCancelEvent.makerAssetProxyId = makerAssetData.assetProxyId; + exchangeCancelEvent.makerTokenAddress = makerAssetData.tokenAddress; + exchangeCancelEvent.makerTokenId = bigNumbertoStringOrNull((makerAssetData as ERC721AssetData).tokenId); + exchangeCancelEvent.rawTakerAssetData = eventLog.args.takerAssetData; + exchangeCancelEvent.takerAssetType = takerAssetType; + exchangeCancelEvent.takerAssetProxyId = takerAssetData.assetProxyId; + exchangeCancelEvent.takerTokenAddress = takerAssetData.tokenAddress; + exchangeCancelEvent.takerTokenId = bigNumbertoStringOrNull((takerAssetData as ERC721AssetData).tokenId); + return exchangeCancelEvent; +} + +export function _convertToExchangeCancelUpToEvent( + eventLog: LogWithDecodedArgs, +): ExchangeCancelUpToEvent { + const exchangeCancelUpToEvent = new ExchangeCancelUpToEvent(); + exchangeCancelUpToEvent.contractAddress = eventLog.address as string; + exchangeCancelUpToEvent.blockNumber = eventLog.blockNumber as number; + exchangeCancelUpToEvent.logIndex = eventLog.logIndex as number; + exchangeCancelUpToEvent.rawData = eventLog.data as string; + exchangeCancelUpToEvent.makerAddress = eventLog.args.makerAddress.toString(); + exchangeCancelUpToEvent.senderAddress = eventLog.args.senderAddress.toString(); + exchangeCancelUpToEvent.orderEpoch = eventLog.args.orderEpoch.toString(); + return exchangeCancelUpToEvent; +} diff --git a/packages/pipeline/src/parsers/sra_orders/index.ts b/packages/pipeline/src/parsers/sra_orders/index.ts new file mode 100644 index 000000000..fb2b74dfe --- /dev/null +++ b/packages/pipeline/src/parsers/sra_orders/index.ts @@ -0,0 +1,54 @@ +import { APIOrder, OrdersResponse } from '@0xproject/connect'; +import { assetDataUtils, orderHashUtils } from '@0xproject/order-utils'; +import { AssetProxyId, ERC721AssetData } from '@0xproject/types'; +import * as R from 'ramda'; + +import { SraOrder } from '../../entities/SraOrder'; +import { bigNumbertoStringOrNull } from '../../utils'; + +export function parseSraOrders(rawOrdersResponse: OrdersResponse): SraOrder[] { + return R.map(_convertToEntity, rawOrdersResponse.records); +} + +export function _convertToEntity(apiOrder: APIOrder): SraOrder { + // TODO(albrow): refactor out common asset data decoding code. + const makerAssetData = assetDataUtils.decodeAssetDataOrThrow(apiOrder.order.makerAssetData); + const makerAssetType = makerAssetData.assetProxyId === AssetProxyId.ERC20 ? 'erc20' : 'erc721'; + const takerAssetData = assetDataUtils.decodeAssetDataOrThrow(apiOrder.order.takerAssetData); + const takerAssetType = takerAssetData.assetProxyId === AssetProxyId.ERC20 ? 'erc20' : 'erc721'; + + const sraOrder = new SraOrder(); + sraOrder.exchangeAddress = apiOrder.order.exchangeAddress; + sraOrder.orderHashHex = orderHashUtils.getOrderHashHex(apiOrder.order); + + // TODO(albrow): Set these fields to the correct values upstack. + sraOrder.lastUpdatedTimestamp = 0; + sraOrder.firstSeenTimestamp = 0; + + sraOrder.makerAddress = apiOrder.order.makerAddress; + sraOrder.takerAddress = apiOrder.order.takerAddress; + sraOrder.feeRecipientAddress = apiOrder.order.feeRecipientAddress; + sraOrder.senderAddress = apiOrder.order.senderAddress; + sraOrder.makerAssetAmount = apiOrder.order.makerAssetAmount.toString(); + sraOrder.takerAssetAmount = apiOrder.order.takerAssetAmount.toString(); + sraOrder.makerFee = apiOrder.order.makerFee.toString(); + sraOrder.takerFee = apiOrder.order.takerFee.toString(); + sraOrder.expirationTimeSeconds = apiOrder.order.expirationTimeSeconds.toString(); + sraOrder.salt = apiOrder.order.salt.toString(); + sraOrder.signature = apiOrder.order.signature; + + sraOrder.rawMakerAssetData = apiOrder.order.makerAssetData; + sraOrder.makerAssetType = makerAssetType; + sraOrder.makerAssetProxyId = makerAssetData.assetProxyId; + sraOrder.makerTokenAddress = makerAssetData.tokenAddress; + sraOrder.makerTokenId = bigNumbertoStringOrNull((makerAssetData as ERC721AssetData).tokenId); + sraOrder.rawTakerAssetData = apiOrder.order.takerAssetData; + sraOrder.takerAssetType = takerAssetType; + sraOrder.takerAssetProxyId = takerAssetData.assetProxyId; + sraOrder.takerTokenAddress = takerAssetData.tokenAddress; + sraOrder.takerTokenId = bigNumbertoStringOrNull((takerAssetData as ERC721AssetData).tokenId); + + sraOrder.metaDataJson = JSON.stringify(apiOrder.metaData); + + return sraOrder; +} -- cgit