aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant/src/util/order_coercion.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/instant/src/util/order_coercion.ts')
-rw-r--r--packages/instant/src/util/order_coercion.ts42
1 files changed, 42 insertions, 0 deletions
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);
+ });
+ },
+};