aboutsummaryrefslogtreecommitdiffstats
path: root/packages/pipeline/src/parsers/sra_orders/index.ts
blob: 3d7f73fca88cc09170bf3c4877fa9f57dcacf141 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { APIOrder, OrdersResponse } from '@0x/connect';
import { assetDataUtils, orderHashUtils } from '@0x/order-utils';
import { AssetProxyId, ERC721AssetData } from '@0x/types';
import * as R from 'ramda';

import { SraOrder } from '../../entities';
import { bigNumbertoStringOrNull } from '../../utils';

/**
 * Parses a raw order response from an SRA endpoint and returns an array of
 * SraOrder entities.
 * @param rawOrdersResponse A raw order response from an SRA endpoint.
 */
export function parseSraOrders(rawOrdersResponse: OrdersResponse): SraOrder[] {
    return R.map(_convertToEntity, rawOrdersResponse.records);
}

/**
 * Converts a single APIOrder into an SraOrder entity.
 * @param apiOrder A single order from the response from an SRA endpoint.
 */
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;
    // tslint has a false positive here. Type assertion is required.
    // tslint:disable-next-line:no-unnecessary-type-assertion
    sraOrder.makerTokenId = bigNumbertoStringOrNull((makerAssetData as ERC721AssetData).tokenId);
    sraOrder.rawTakerAssetData = apiOrder.order.takerAssetData;
    sraOrder.takerAssetType = takerAssetType;
    sraOrder.takerAssetProxyId = takerAssetData.assetProxyId;
    sraOrder.takerTokenAddress = takerAssetData.tokenAddress;
    // tslint:disable-next-line:no-unnecessary-type-assertion
    sraOrder.takerTokenId = bigNumbertoStringOrNull((takerAssetData as ERC721AssetData).tokenId);

    sraOrder.metadataJson = JSON.stringify(apiOrder.metaData);

    return sraOrder;
}