aboutsummaryrefslogtreecommitdiffstats
path: root/packages/connect/src/utils/relayer_response_json_parsers.ts
blob: bc31f231de59bba3352ad5365bf3138a97110920 (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
import { assert } from '@0xproject/assert';
import { schemas } from '@0xproject/json-schemas';
import { orderParsingUtils } from '@0xproject/order-utils';

import {
    APIOrder,
    AssetPairsItem,
    AssetPairsResponse,
    OrderbookResponse,
    OrderConfigResponse,
    OrdersResponse,
} from '../types';

import { typeConverters } from './type_converters';

export const relayerResponseJsonParsers = {
    parseAssetDataPairsJson(json: any): AssetPairsResponse {
        assert.doesConformToSchema('assetDataPairsResponse', json, schemas.relayerApiAssetDataPairsResponseSchema);
        return { ...json, records: relayerResponseJsonParsers.parseAssetPairsItemsJson(json.records) };
    },
    parseAssetPairsItemsJson(json: any): AssetPairsItem[] {
        return json.map((assetDataPair: any) => {
            return orderParsingUtils.convertStringsFieldsToBigNumbers(assetDataPair, [
                'assetDataA.minAmount',
                'assetDataA.maxAmount',
                'assetDataB.minAmount',
                'assetDataB.maxAmount',
            ]);
        });
    },
    parseOrdersJson(json: any): OrdersResponse {
        assert.doesConformToSchema('relayerApiOrdersResponse', json, schemas.relayerApiOrdersResponseSchema);
        return { ...json, records: relayerResponseJsonParsers.parseAPIOrdersJson(json.records) };
    },
    parseAPIOrdersJson(json: any): APIOrder[] {
        return json.map(relayerResponseJsonParsers.parseAPIOrderJson.bind(relayerResponseJsonParsers));
    },
    parseAPIOrderJson(json: any): APIOrder {
        assert.doesConformToSchema('relayerApiOrder', json, schemas.relayerApiOrderSchema);
        return typeConverters.convertAPIOrderStringFieldsToBigNumber(json);
    },
    parseOrderbookResponseJson(json: any): OrderbookResponse {
        assert.doesConformToSchema('orderBookResponse', json, schemas.relayerApiOrderbookResponseSchema);
        return typeConverters.convertOrderbookStringFieldsToBigNumber(json);
    },
    parseOrderConfigResponseJson(json: any): OrderConfigResponse {
        assert.doesConformToSchema('orderConfigResponse', json, schemas.relayerApiOrderConfigResponseSchema);
        return orderParsingUtils.convertStringsFieldsToBigNumbers(json, ['makerFee', 'takerFee']);
    },
};