aboutsummaryrefslogtreecommitdiffstats
path: root/packages/connect/src/utils/type_converters.ts
blob: beea557c2c6e186fb956b03c36dcf7b2c58db8f4 (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
import { BigNumber } from '@0xproject/utils';
import * as _ from 'lodash';

export const typeConverters = {
    convertOrderbookStringFieldsToBigNumber(orderbook: any): any {
        const bids = _.get(orderbook, 'bids', []);
        const asks = _.get(orderbook, 'asks', []);
        return {
            bids: bids.map((order: any) => this.convertOrderStringFieldsToBigNumber(order)),
            asks: asks.map((order: any) => this.convertOrderStringFieldsToBigNumber(order)),
        };
    },
    convertOrderStringFieldsToBigNumber(order: any): any {
        return this.convertStringsFieldsToBigNumbers(order, [
            'makerTokenAmount',
            'takerTokenAmount',
            'makerFee',
            'takerFee',
            'expirationUnixTimestampSec',
            'salt',
        ]);
    },
    convertStringsFieldsToBigNumbers(obj: any, fields: string[]): any {
        const result = _.assign({}, obj);
        _.each(fields, field => {
            _.update(result, field, (value: string) => new BigNumber(value));
        });
        return result;
    },
};