aboutsummaryrefslogtreecommitdiffstats
path: root/packages/website/ts/utils/order_parser.ts
blob: 2e1d4d2f22f9b62edabf342167b84b3eadbc619f (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
import { BigNumber, logUtils } from '@0xproject/utils';
import * as _ from 'lodash';

import { portalOrderSchema } from 'ts/schemas/portal_order_schema';
import { validator } from 'ts/schemas/validator';
import { PortalOrder } from 'ts/types';

export const orderParser = {
    parseQueryString(queryString: string): PortalOrder | undefined {
        if (queryString.length === 0) {
            return undefined;
        }
        const queryParams = queryString.substring(1).split('&');
        const orderQueryParam = _.find(queryParams, queryParam => {
            const queryPair = queryParam.split('=');
            return queryPair[0] === 'order';
        });
        if (_.isUndefined(orderQueryParam)) {
            return undefined;
        }
        const orderPair = orderQueryParam.split('=');
        if (orderPair.length !== 2) {
            return undefined;
        }
        const order = JSON.parse(decodeURIComponent(orderPair[1]));
        const validationResult = validator.validate(order, portalOrderSchema);
        if (validationResult.errors.length > 0) {
            logUtils.log(`Invalid shared order: ${validationResult.errors}`);
            return undefined;
        }
        const signedOrder = _.get(order, 'signedOrder');
        const convertedSignedOrder = convertOrderStringFieldsToBigNumber(signedOrder);
        const result = {
            ...order,
            signedOrder: convertedSignedOrder,
        };
        return result;
    },
    parseJsonString(orderJson: string): PortalOrder {
        const order = JSON.parse(orderJson);
        const signedOrder = _.get(order, 'signedOrder');
        const convertedSignedOrder = convertOrderStringFieldsToBigNumber(signedOrder);
        const result = {
            ...order,
            signedOrder: convertedSignedOrder,
        };
        return result;
    },
};

// TODO: consolidate this function with that in typeConverters in @0xproject/connect
function convertOrderStringFieldsToBigNumber(order: any): any {
    return convertStringsFieldsToBigNumbers(order, [
        'makerAssetAmount',
        'takerAssetAmount',
        'makerFee',
        'takerFee',
        'expirationTimeSeconds',
        'salt',
    ]);
}

// TODO: consolidate this function with that in typeConverters in @0xproject/connect
function convertStringsFieldsToBigNumbers(obj: any, fields: string[]): any {
    const result = _.assign({}, obj);
    _.each(fields, field => {
        _.update(result, field, (value: string) => {
            if (_.isUndefined(value)) {
                throw new Error(`Could not find field '${field}' while converting string fields to BigNumber.`);
            }
            return new BigNumber(value);
        });
    });
    return result;
}