diff options
Diffstat (limited to 'packages/instant/src/util')
-rw-r--r-- | packages/instant/src/util/maybe_big_number.ts | 11 | ||||
-rw-r--r-- | packages/instant/src/util/signed_order_coercion.ts | 41 |
2 files changed, 32 insertions, 20 deletions
diff --git a/packages/instant/src/util/maybe_big_number.ts b/packages/instant/src/util/maybe_big_number.ts index f48473389..7e206a125 100644 --- a/packages/instant/src/util/maybe_big_number.ts +++ b/packages/instant/src/util/maybe_big_number.ts @@ -16,6 +16,17 @@ export const maybeBigNumberUtil = { return validBigNumber.isNaN() ? undefined : validBigNumber; }, + // converts a BigNumber or String to the BigNumber used by 0x libraries + bigNumberOrStringToMaybeBigNumber: (value: any): Maybe<BigNumber> => { + if (_.isString(value)) { + return maybeBigNumberUtil.stringToMaybeBigNumber(value); + } + // checks for pre v8 bignumber with member variable + if (BigNumber.isBigNumber(value) || value.isBigNumber) { + return new BigNumber(value.toString()); + } + return undefined; + }, areMaybeBigNumbersEqual: (val1: Maybe<BigNumber>, val2: Maybe<BigNumber>): boolean => { if (!_.isUndefined(val1) && !_.isUndefined(val2)) { return val1.isEqualTo(val2); diff --git a/packages/instant/src/util/signed_order_coercion.ts b/packages/instant/src/util/signed_order_coercion.ts index 649596a3d..4209e05e1 100644 --- a/packages/instant/src/util/signed_order_coercion.ts +++ b/packages/instant/src/util/signed_order_coercion.ts @@ -1,25 +1,26 @@ -import { BigNumber } from '@0x/asset-buyer'; import { SignedOrder } from '@0x/types'; +import { BigNumber } from '@0x/utils'; +import * as _ from 'lodash'; -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; +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 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), - }; +// 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), + }; + }, }; |