aboutsummaryrefslogtreecommitdiffstats
path: root/packages/pipeline/src/parsers/idex_orders/index.ts
blob: dfe27455c2fd2875609ca3b1dd0c63f7fc0f6174 (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
67
68
69
70
71
72
73
74
75
76
77
import { BigNumber } from '@0x/utils';

import { aggregateOrders } from '../utils';

import { IdexOrder, IdexOrderbook, IdexOrderParam } from '../../data_sources/idex';
import { TokenOrderbookSnapshot as TokenOrder } from '../../entities';
import { OrderType } from '../../types';

/**
 * Marque function of this file.
 * 1) Takes in orders from an orderbook,
 * 2) Aggregates them by price point,
 * 3) Parses them into entities which are then saved into the database.
 * @param idexOrderbook raw orderbook that we pull from the Idex API.
 * @param observedTimestamp Time at which the orders for the market were pulled.
 * @param source The exchange where these orders are placed. In this case 'idex'.
 */
export function parseIdexOrders(idexOrderbook: IdexOrderbook, observedTimestamp: number, source: string): TokenOrder[] {
    const aggregatedBids = aggregateOrders(idexOrderbook.bids);
    // Any of the bid orders' params will work
    const idexBidOrder = idexOrderbook.bids[0];
    const parsedBids =
        aggregatedBids.length > 0
            ? aggregatedBids.map(order => parseIdexOrder(idexBidOrder.params, observedTimestamp, 'bid', source, order))
            : [];

    const aggregatedAsks = aggregateOrders(idexOrderbook.asks);
    // Any of the ask orders' params will work
    const idexAskOrder = idexOrderbook.asks[0];
    const parsedAsks =
        aggregatedAsks.length > 0
            ? aggregatedAsks.map(order => parseIdexOrder(idexAskOrder.params, observedTimestamp, 'ask', source, order))
            : [];
    return parsedBids.concat(parsedAsks);
}

/**
 * Parse a single aggregated Idex order in order to form a tokenOrder entity
 * which can be saved into the database.
 * @param idexOrderParam An object containing information about the market where these
 * trades have been placed.
 * @param observedTimestamp The time when the API response returned back to us.
 * @param orderType 'bid' or 'ask' enum.
 * @param source Exchange where these orders were placed.
 * @param idexOrder A <price, amount> tuple which we will convert to volume-basis.
 */
export function parseIdexOrder(
    idexOrderParam: IdexOrderParam,
    observedTimestamp: number,
    orderType: OrderType,
    source: string,
    idexOrder: [string, BigNumber],
): TokenOrder {
    const tokenOrder = new TokenOrder();
    const price = new BigNumber(idexOrder[0]);
    const amount = idexOrder[1];

    tokenOrder.source = source;
    tokenOrder.observedTimestamp = observedTimestamp;
    tokenOrder.orderType = orderType;
    tokenOrder.price = price;
    tokenOrder.baseVolume = amount;
    tokenOrder.quoteVolume = price.times(amount);

    if (orderType === 'bid') {
        tokenOrder.baseAssetSymbol = idexOrderParam.buySymbol;
        tokenOrder.baseAssetAddress = idexOrderParam.tokenBuy;
        tokenOrder.quoteAssetSymbol = idexOrderParam.sellSymbol;
        tokenOrder.quoteAssetAddress = idexOrderParam.tokenSell;
    } else {
        tokenOrder.baseAssetSymbol = idexOrderParam.sellSymbol;
        tokenOrder.baseAssetAddress = idexOrderParam.tokenSell;
        tokenOrder.quoteAssetSymbol = idexOrderParam.buySymbol;
        tokenOrder.quoteAssetAddress = idexOrderParam.tokenBuy;
    }
    return tokenOrder;
}