aboutsummaryrefslogtreecommitdiffstats
path: root/packages/pipeline/src/parsers/bloxy/index.ts
blob: af07c7507d31eb58d532b91f35f3a4d5191831a2 (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
import { BigNumber } from '@0x/utils';
import * as R from 'ramda';

import { BLOXY_DEX_TRADES_URL, BloxyTrade } from '../../data_sources/bloxy';
import { DexTrade } from '../../entities';

/**
 * Parses a raw trades response from the Bloxy Dex API and returns an array of
 * DexTrade entities.
 * @param rawTrades A raw order response from an SRA endpoint.
 */
export function parseBloxyTrades(rawTrades: BloxyTrade[]): DexTrade[] {
    return R.map(_parseBloxyTrade, rawTrades);
}

/**
 * Converts a single Bloxy trade into a DexTrade entity.
 * @param rawTrade A single trade from the response from the Bloxy API.
 */
export function _parseBloxyTrade(rawTrade: BloxyTrade): DexTrade {
    const dexTrade = new DexTrade();
    dexTrade.sourceUrl = BLOXY_DEX_TRADES_URL;
    dexTrade.txHash = rawTrade.tx_hash;
    dexTrade.txTimestamp = new Date(rawTrade.tx_time).getTime();
    dexTrade.txDate = rawTrade.tx_date;
    dexTrade.txSender = rawTrade.tx_sender;
    dexTrade.smartContractId = rawTrade.smart_contract_id;
    dexTrade.smartContractAddress = rawTrade.smart_contract_address;
    dexTrade.contractType = rawTrade.contract_type;
    dexTrade.maker = rawTrade.maker;
    dexTrade.taker = rawTrade.taker;
    // TODO(albrow): The Bloxy API returns amounts and fees as a `number` type
    // but some of their values have too many significant digits to be
    // represented that way. Ideally they will switch to using strings and then
    // we can update this code.
    dexTrade.amountBuy = new BigNumber(rawTrade.amountBuy.toString());
    dexTrade.makerFeeAmount = new BigNumber(rawTrade.makerFee.toString());
    dexTrade.buyCurrencyId = rawTrade.buyCurrencyId;
    dexTrade.buySymbol = filterNullCharacters(rawTrade.buySymbol);
    dexTrade.amountSell = new BigNumber(rawTrade.amountSell.toString());
    dexTrade.takerFeeAmount = new BigNumber(rawTrade.takerFee.toString());
    dexTrade.sellCurrencyId = rawTrade.sellCurrencyId;
    dexTrade.sellSymbol = filterNullCharacters(rawTrade.sellSymbol);
    dexTrade.makerAnnotation = rawTrade.maker_annotation;
    dexTrade.takerAnnotation = rawTrade.taker_annotation;
    dexTrade.protocol = rawTrade.protocol;
    dexTrade.buyAddress = rawTrade.buyAddress;
    dexTrade.sellAddress = rawTrade.sellAddress;
    return dexTrade;
}

// Works with any form of escaoed null character (e.g., '\0' and '\u0000').
const filterNullCharacters = R.replace(/\0/g, '');