diff options
Diffstat (limited to 'packages/instant/src')
-rw-r--r-- | packages/instant/src/index.umd.ts | 6 | ||||
-rw-r--r-- | packages/instant/src/util/maybe_big_number.ts | 14 | ||||
-rw-r--r-- | packages/instant/src/util/order_coercion.ts | 42 | ||||
-rw-r--r-- | packages/instant/src/util/signed_order_coercion.ts | 26 |
4 files changed, 52 insertions, 36 deletions
diff --git a/packages/instant/src/index.umd.ts b/packages/instant/src/index.umd.ts index 45913aa47..7b2b45a71 100644 --- a/packages/instant/src/index.umd.ts +++ b/packages/instant/src/index.umd.ts @@ -17,8 +17,8 @@ import { ZeroExInstantOverlay, ZeroExInstantOverlayProps } from './index'; import { Network, OrderSource } from './types'; import { analytics } from './util/analytics'; import { assert } from './util/assert'; +import { orderCoercionUtil } from './util/order_coercion'; import { providerFactory } from './util/provider_factory'; -import { signedOrderCoercionUtil } from './util/signed_order_coercion'; import { util } from './util/util'; const isInstantRendered = (): boolean => !!document.getElementById(INJECTED_DIV_ID); @@ -94,8 +94,8 @@ export interface ZeroExInstantConfig extends ZeroExInstantOverlayProps { } export const render = (config: ZeroExInstantConfig, selector: string = DEFAULT_ZERO_EX_CONTAINER_SELECTOR) => { - if (!_.isString(config.orderSource)) { - config.orderSource = config.orderSource.map(signedOrderCoercionUtil.bigNumberCoercion); + if (_.isArray(config.orderSource)) { + config.orderSource = orderCoercionUtil.coerceOrderArrayFieldsToBigNumber(config.orderSource); } validateInstantRenderConfig(config, selector); diff --git a/packages/instant/src/util/maybe_big_number.ts b/packages/instant/src/util/maybe_big_number.ts index 7e206a125..95fbd8695 100644 --- a/packages/instant/src/util/maybe_big_number.ts +++ b/packages/instant/src/util/maybe_big_number.ts @@ -16,8 +16,14 @@ export const maybeBigNumberUtil = { return validBigNumber.isNaN() ? undefined : validBigNumber; }, + areMaybeBigNumbersEqual: (val1: Maybe<BigNumber>, val2: Maybe<BigNumber>): boolean => { + if (!_.isUndefined(val1) && !_.isUndefined(val2)) { + return val1.isEqualTo(val2); + } + return _.isUndefined(val1) && _.isUndefined(val2); + }, // converts a BigNumber or String to the BigNumber used by 0x libraries - bigNumberOrStringToMaybeBigNumber: (value: any): Maybe<BigNumber> => { + toMaybeBigNumber: (value: any): Maybe<BigNumber> => { if (_.isString(value)) { return maybeBigNumberUtil.stringToMaybeBigNumber(value); } @@ -27,10 +33,4 @@ export const maybeBigNumberUtil = { } return undefined; }, - areMaybeBigNumbersEqual: (val1: Maybe<BigNumber>, val2: Maybe<BigNumber>): boolean => { - if (!_.isUndefined(val1) && !_.isUndefined(val2)) { - return val1.isEqualTo(val2); - } - return _.isUndefined(val1) && _.isUndefined(val2); - }, }; diff --git a/packages/instant/src/util/order_coercion.ts b/packages/instant/src/util/order_coercion.ts new file mode 100644 index 000000000..a1b468baf --- /dev/null +++ b/packages/instant/src/util/order_coercion.ts @@ -0,0 +1,42 @@ +import { BigNumber } from '@0x/utils'; +import * as _ from 'lodash'; + +import { maybeBigNumberUtil } from './maybe_big_number'; + +const coerceBigNumberOrString = (value: any): BigNumber => { + const bn = maybeBigNumberUtil.toMaybeBigNumber(value); + return !!bn ? bn : value; +}; + +// function implies that the signed order already has been validated +export const orderCoercionUtil = { + // coerces order big number values to the BigNumber version utilized by 0x + coerceFieldsToBigNumbers(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 fields to BigNumber.`); + } + return coerceBigNumberOrString(value); + }); + }); + return result; + }, + + coerceOrderFieldsToBigNumber: (order: any): any => { + return orderCoercionUtil.coerceFieldsToBigNumbers(order, [ + 'makerFee', + 'takerFee', + 'makerAssetAmount', + 'takerAssetAmount', + 'salt', + 'expirationTimeSeconds', + ]); + }, + coerceOrderArrayFieldsToBigNumber: (orders: any[]): any[] => { + return _.map(orders, (value: any) => { + return orderCoercionUtil.coerceOrderFieldsToBigNumber(value); + }); + }, +}; diff --git a/packages/instant/src/util/signed_order_coercion.ts b/packages/instant/src/util/signed_order_coercion.ts deleted file mode 100644 index 4209e05e1..000000000 --- a/packages/instant/src/util/signed_order_coercion.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { SignedOrder } from '@0x/types'; -import { BigNumber } from '@0x/utils'; -import * as _ from 'lodash'; - -import { maybeBigNumberUtil } from './maybe_big_number'; - -const coerceBigNumberOrString = (value: any): BigNumber => { - const bn = maybeBigNumberUtil.bigNumberOrStringToMaybeBigNumber(value); - return !!bn ? bn : value; -}; - -// function implies that the signed order already has been validated -export const signedOrderCoercionUtil = { - // coerces order big number values to the BigNumber version utilized by 0x - bigNumberCoercion: (order: SignedOrder): SignedOrder => { - return { - ...order, - makerFee: coerceBigNumberOrString(order.makerFee), - takerFee: coerceBigNumberOrString(order.takerFee), - makerAssetAmount: coerceBigNumberOrString(order.makerAssetAmount), - takerAssetAmount: coerceBigNumberOrString(order.takerAssetAmount), - salt: coerceBigNumberOrString(order.salt), - expirationTimeSeconds: coerceBigNumberOrString(order.expirationTimeSeconds), - }; - }, -}; |