From ada78d140b3d60997461e3786564baec291b7220 Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Thu, 27 Sep 2018 17:32:56 -0700 Subject: Add tests for SRA order conversion --- .../pipeline/src/data_types/sra_order/index.ts | 54 ---------------------- .../pipeline/src/data_types/sra_orders/index.ts | 54 ++++++++++++++++++++++ packages/pipeline/src/index.ts | 4 +- 3 files changed, 56 insertions(+), 56 deletions(-) delete mode 100644 packages/pipeline/src/data_types/sra_order/index.ts create mode 100644 packages/pipeline/src/data_types/sra_orders/index.ts (limited to 'packages/pipeline/src') diff --git a/packages/pipeline/src/data_types/sra_order/index.ts b/packages/pipeline/src/data_types/sra_order/index.ts deleted file mode 100644 index b6364415c..000000000 --- a/packages/pipeline/src/data_types/sra_order/index.ts +++ /dev/null @@ -1,54 +0,0 @@ -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 = Date.now(); - sraOrder.firstSeenTimestamp = Date.now(); - - 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; -} diff --git a/packages/pipeline/src/data_types/sra_orders/index.ts b/packages/pipeline/src/data_types/sra_orders/index.ts new file mode 100644 index 000000000..fb2b74dfe --- /dev/null +++ b/packages/pipeline/src/data_types/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; +} diff --git a/packages/pipeline/src/index.ts b/packages/pipeline/src/index.ts index c442c7970..a1dbb35ff 100644 --- a/packages/pipeline/src/index.ts +++ b/packages/pipeline/src/index.ts @@ -4,7 +4,7 @@ import { Connection, createConnection } from 'typeorm'; import { Etherscan } from './data_sources/etherscan'; import { parseExchangeEvents } from './data_types/events/exchange_events'; -import { parseSraOrders } from './data_types/sra_order'; +import { parseSraOrders } from './data_types/sra_orders'; import { ExchangeCancelEvent } from './entities/ExchangeCancelEvent'; import { ExchangeCancelUpToEvent } from './entities/ExchangeCancelUpToEvent'; import { ExchangeFillEvent } from './entities/ExchangeFillEvent'; @@ -53,7 +53,7 @@ async function getSraOrdersAsync(): Promise { const orders = parseSraOrders(rawOrders); for (const order of orders) { order.sourceUrl = sraUrl; - order.save(); + await order.save(); } console.log(`now there are ${await orderRepository.count()} total orders`); } -- cgit v1.2.3