aboutsummaryrefslogtreecommitdiffstats
path: root/packages/connect/src/utils
diff options
context:
space:
mode:
authorBrandon Millman <brandon.millman@gmail.com>2017-11-15 07:26:36 +0800
committerBrandon Millman <brandon.millman@gmail.com>2017-11-15 10:55:29 +0800
commit655b0636facc110e9192cc7c3190f4b16f212be9 (patch)
tree562109951a69321e6d8857798d4d5310248b71d7 /packages/connect/src/utils
parent5bd8e172c9415e9d8eca2d3893fe767684018351 (diff)
downloaddexon-sol-tools-655b0636facc110e9192cc7c3190f4b16f212be9.tar
dexon-sol-tools-655b0636facc110e9192cc7c3190f4b16f212be9.tar.gz
dexon-sol-tools-655b0636facc110e9192cc7c3190f4b16f212be9.tar.bz2
dexon-sol-tools-655b0636facc110e9192cc7c3190f4b16f212be9.tar.lz
dexon-sol-tools-655b0636facc110e9192cc7c3190f4b16f212be9.tar.xz
dexon-sol-tools-655b0636facc110e9192cc7c3190f4b16f212be9.tar.zst
dexon-sol-tools-655b0636facc110e9192cc7c3190f4b16f212be9.zip
Add connect to monorepo
Diffstat (limited to 'packages/connect/src/utils')
-rw-r--r--packages/connect/src/utils/orderbook_channel_message_parsers.ts43
-rw-r--r--packages/connect/src/utils/type_converters.ts31
2 files changed, 74 insertions, 0 deletions
diff --git a/packages/connect/src/utils/orderbook_channel_message_parsers.ts b/packages/connect/src/utils/orderbook_channel_message_parsers.ts
new file mode 100644
index 000000000..b590b189b
--- /dev/null
+++ b/packages/connect/src/utils/orderbook_channel_message_parsers.ts
@@ -0,0 +1,43 @@
+import * as _ from 'lodash';
+import {SignedOrder} from '0x.js';
+import {assert} from '@0xproject/assert';
+import {schemas} from '@0xproject/json-schemas';
+import {
+ OrderbookChannelMessage,
+ OrderbookChannelMessageTypes,
+} from '../types';
+import {typeConverters} from './type_converters';
+
+export const orderbookChannelMessageParsers = {
+ parser(utf8Data: string): OrderbookChannelMessage {
+ const messageObj = JSON.parse(utf8Data);
+ const type: string = _.get(messageObj, 'type');
+ assert.assert(!_.isUndefined(type), `Message is missing a type parameter: ${utf8Data}`);
+ switch (type) {
+ case (OrderbookChannelMessageTypes.Snapshot): {
+ assert.doesConformToSchema('message', messageObj, schemas.relayerApiOrderbookChannelSnapshotSchema);
+ const orderbook = messageObj.payload;
+ typeConverters.convertOrderbookStringFieldsToBigNumber(orderbook);
+ return {
+ type,
+ payload: orderbook,
+ };
+ }
+ case (OrderbookChannelMessageTypes.Update): {
+ assert.doesConformToSchema('message', messageObj, schemas.relayerApiOrderbookChannelUpdateSchema);
+ const order = messageObj.payload;
+ typeConverters.convertOrderStringFieldsToBigNumber(order);
+ return {
+ type,
+ payload: order,
+ };
+ }
+ default: {
+ return {
+ type: OrderbookChannelMessageTypes.Unknown,
+ payload: undefined,
+ };
+ }
+ }
+ },
+};
diff --git a/packages/connect/src/utils/type_converters.ts b/packages/connect/src/utils/type_converters.ts
new file mode 100644
index 000000000..bf17a5629
--- /dev/null
+++ b/packages/connect/src/utils/type_converters.ts
@@ -0,0 +1,31 @@
+import * as _ from 'lodash';
+import {BigNumber} from 'bignumber.js';
+
+// TODO: convert all of these to non-mutating, pure functions
+export const typeConverters = {
+ convertOrderbookStringFieldsToBigNumber(orderbook: object): void {
+ _.each(orderbook, (orders: object[]) => {
+ _.each(orders, (order: object) => this.convertOrderStringFieldsToBigNumber(order));
+ });
+ },
+ convertOrderStringFieldsToBigNumber(order: object): void {
+ this.convertStringsFieldsToBigNumbers(order, [
+ 'makerTokenAmount',
+ 'takerTokenAmount',
+ 'makerFee',
+ 'takerFee',
+ 'expirationUnixTimestampSec',
+ 'salt',
+ ]);
+ },
+ convertBigNumberFieldsToStrings(obj: object, fields: string[]): void {
+ _.each(fields, field => {
+ _.update(obj, field, (value: BigNumber) => value.toString());
+ });
+ },
+ convertStringsFieldsToBigNumbers(obj: object, fields: string[]): void {
+ _.each(fields, field => {
+ _.update(obj, field, (value: string) => new BigNumber(value));
+ });
+ },
+};