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

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 typeConverters.convertStringsFieldsToBigNumbers(assetDataPair, [
                'assetDataA.minAmount',
                'assetDataA.maxAmount',
                'assetDataB.minAmount',
                'assetDataB.maxAmount',
            ]);
        });
    },
    parseOrdersJson(json: any): OrdersResponse {
        assert.doesConformToSchema('relayerApiOrdersResponse', json, schemas.relayerApiOrdersResponseSchema);
        return { ...json, records: json.records.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 typeConverters.convertStringsFieldsToBigNumbers(json, ['makerFee', 'takerFee']);
    },
};