From 59f48d6d5702210acea868c3ac2ede6315f0c8ec Mon Sep 17 00:00:00 2001 From: David Sun Date: Mon, 4 Feb 2019 11:19:18 -0500 Subject: working on big number coercion function --- packages/instant/src/index.umd.ts | 7 ++++++ packages/instant/src/util/signed_order_coercion.ts | 25 ++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 packages/instant/src/util/signed_order_coercion.ts (limited to 'packages/instant/src') diff --git a/packages/instant/src/index.umd.ts b/packages/instant/src/index.umd.ts index 0acf3f2ad..0c2ce5ec1 100644 --- a/packages/instant/src/index.umd.ts +++ b/packages/instant/src/index.umd.ts @@ -19,6 +19,7 @@ import { analytics } from './util/analytics'; import { assert } from './util/assert'; import { providerFactory } from './util/provider_factory'; import { util } from './util/util'; +import { coerceSignedOrderBigNumberOfString } from './util/signed_order_coercion' const isInstantRendered = (): boolean => !!document.getElementById(INJECTED_DIV_ID); @@ -94,6 +95,12 @@ export interface ZeroExInstantConfig extends ZeroExInstantOverlayProps { export const render = (config: ZeroExInstantConfig, selector: string = DEFAULT_ZERO_EX_CONTAINER_SELECTOR) => { validateInstantRenderConfig(config, selector); + + // TODO(David Sun) test functionality of order bignumber version coercion + if (!_.isString(config.orderSource)) { + config.orderSource = config.orderSource.map(coerceSignedOrderBigNumberOfString); + } + if (config.shouldDisablePushToHistory) { if (!isInstantRendered()) { renderInstant(config, selector); diff --git a/packages/instant/src/util/signed_order_coercion.ts b/packages/instant/src/util/signed_order_coercion.ts new file mode 100644 index 000000000..649596a3d --- /dev/null +++ b/packages/instant/src/util/signed_order_coercion.ts @@ -0,0 +1,25 @@ +import { BigNumber } from '@0x/asset-buyer'; +import { SignedOrder } from '@0x/types'; + +export const coerceBigNumberOrString = (value: any): BigNumber => { + if (typeof value === 'string') { + return new BigNumber(value); + } + if (BigNumber.isBigNumber(value)) { + return new BigNumber(value.toString()); + } + return value; +}; + +// function implies that the signed order already has been invalidated +export const coerceSignedOrderBigNumberOfString = (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), + }; +}; -- cgit