aboutsummaryrefslogtreecommitdiffstats
path: root/packages/connect/src/utils
diff options
context:
space:
mode:
Diffstat (limited to 'packages/connect/src/utils')
-rw-r--r--packages/connect/src/utils/assert.ts15
-rw-r--r--packages/connect/src/utils/orderbook_channel_message_parser.ts43
-rw-r--r--packages/connect/src/utils/orders_channel_message_parser.ts37
-rw-r--r--packages/connect/src/utils/relayer_response_json_parsers.ts57
-rw-r--r--packages/connect/src/utils/type_converters.ts34
5 files changed, 94 insertions, 92 deletions
diff --git a/packages/connect/src/utils/assert.ts b/packages/connect/src/utils/assert.ts
index a0fd12fbd..de7536ffe 100644
--- a/packages/connect/src/utils/assert.ts
+++ b/packages/connect/src/utils/assert.ts
@@ -1,24 +1,23 @@
-import { assert as sharedAssert } from '@0xproject/assert';
+import { assert as sharedAssert } from '@0x/assert';
// HACK: We need those two unused imports because they're actually used by sharedAssert which gets injected here
// tslint:disable-next-line:no-unused-variable
-import { Schema, schemas } from '@0xproject/json-schemas';
+import { Schema, schemas } from '@0x/json-schemas';
// tslint:disable-next-line:no-unused-variable
-import { ECSignature } from '@0xproject/types';
+import { ECSignature } from '@0x/types';
// tslint:disable-next-line:no-unused-variable
-import { BigNumber } from '@0xproject/utils';
+import { BigNumber } from '@0x/utils';
import * as _ from 'lodash';
export const assert = {
...sharedAssert,
- isOrderbookChannelSubscriptionOpts(variableName: string, subscriptionOpts: any): void {
+ isOrdersChannelSubscriptionOpts(variableName: string, subscriptionOpts: any): void {
sharedAssert.doesConformToSchema(
variableName,
subscriptionOpts,
- schemas.relayerApiOrderbookChannelSubscribePayload,
+ schemas.relayerApiOrdersChannelSubscribePayloadSchema,
);
},
- isOrderbookChannelHandler(variableName: string, handler: any): void {
- sharedAssert.isFunction(`${variableName}.onSnapshot`, _.get(handler, 'onSnapshot'));
+ isOrdersChannelHandler(variableName: string, handler: any): void {
sharedAssert.isFunction(`${variableName}.onUpdate`, _.get(handler, 'onUpdate'));
sharedAssert.isFunction(`${variableName}.onError`, _.get(handler, 'onError'));
sharedAssert.isFunction(`${variableName}.onClose`, _.get(handler, 'onClose'));
diff --git a/packages/connect/src/utils/orderbook_channel_message_parser.ts b/packages/connect/src/utils/orderbook_channel_message_parser.ts
deleted file mode 100644
index 593288078..000000000
--- a/packages/connect/src/utils/orderbook_channel_message_parser.ts
+++ /dev/null
@@ -1,43 +0,0 @@
-import { assert } from '@0xproject/assert';
-import { schemas } from '@0xproject/json-schemas';
-import * as _ from 'lodash';
-
-import { OrderbookChannelMessage, OrderbookChannelMessageTypes } from '../types';
-
-import { relayerResponseJsonParsers } from './relayer_response_json_parsers';
-
-export const orderbookChannelMessageParser = {
- parse(utf8Data: string): OrderbookChannelMessage {
- // parse the message
- const messageObj = JSON.parse(utf8Data);
- // ensure we have a type parameter to switch on
- const type: string = _.get(messageObj, 'type');
- assert.assert(!_.isUndefined(type), `Message is missing a type parameter: ${utf8Data}`);
- assert.isString('type', type);
- // ensure we have a request id for the resulting message
- const requestId: number = _.get(messageObj, 'requestId');
- assert.assert(!_.isUndefined(requestId), `Message is missing a requestId parameter: ${utf8Data}`);
- assert.isNumber('requestId', requestId);
- switch (type) {
- case OrderbookChannelMessageTypes.Snapshot: {
- assert.doesConformToSchema('message', messageObj, schemas.relayerApiOrderbookChannelSnapshotSchema);
- const orderbookJson = messageObj.payload;
- const orderbook = relayerResponseJsonParsers.parseOrderbookResponseJson(orderbookJson);
- return _.assign(messageObj, { payload: orderbook });
- }
- case OrderbookChannelMessageTypes.Update: {
- assert.doesConformToSchema('message', messageObj, schemas.relayerApiOrderbookChannelUpdateSchema);
- const orderJson = messageObj.payload;
- const order = relayerResponseJsonParsers.parseOrderJson(orderJson);
- return _.assign(messageObj, { payload: order });
- }
- default: {
- return {
- type: OrderbookChannelMessageTypes.Unknown,
- requestId,
- payload: undefined,
- };
- }
- }
- },
-};
diff --git a/packages/connect/src/utils/orders_channel_message_parser.ts b/packages/connect/src/utils/orders_channel_message_parser.ts
new file mode 100644
index 000000000..1306a74b1
--- /dev/null
+++ b/packages/connect/src/utils/orders_channel_message_parser.ts
@@ -0,0 +1,37 @@
+import { assert } from '@0x/assert';
+import { schemas } from '@0x/json-schemas';
+import * as _ from 'lodash';
+
+import { OrdersChannelMessage, OrdersChannelMessageTypes } from '@0x/types';
+
+import { relayerResponseJsonParsers } from './relayer_response_json_parsers';
+
+export const ordersChannelMessageParser = {
+ parse(utf8Data: string): OrdersChannelMessage {
+ // parse the message
+ const messageObj = JSON.parse(utf8Data);
+ // ensure we have a type parameter to switch on
+ const type: string = _.get(messageObj, 'type');
+ assert.assert(!_.isUndefined(type), `Message is missing a type parameter: ${utf8Data}`);
+ assert.isString('type', type);
+ // ensure we have a request id for the resulting message
+ const requestId: string = _.get(messageObj, 'requestId');
+ assert.assert(!_.isUndefined(requestId), `Message is missing a requestId parameter: ${utf8Data}`);
+ assert.isString('requestId', requestId);
+ switch (type) {
+ case OrdersChannelMessageTypes.Update: {
+ assert.doesConformToSchema('message', messageObj, schemas.relayerApiOrdersChannelUpdateSchema);
+ const ordersJson = messageObj.payload;
+ const orders = relayerResponseJsonParsers.parseAPIOrdersJson(ordersJson);
+ return _.assign(messageObj, { payload: orders });
+ }
+ default: {
+ return {
+ type: OrdersChannelMessageTypes.Unknown,
+ requestId,
+ payload: undefined,
+ };
+ }
+ }
+ },
+};
diff --git a/packages/connect/src/utils/relayer_response_json_parsers.ts b/packages/connect/src/utils/relayer_response_json_parsers.ts
index ccae8b115..2b1a658d1 100644
--- a/packages/connect/src/utils/relayer_response_json_parsers.ts
+++ b/packages/connect/src/utils/relayer_response_json_parsers.ts
@@ -1,37 +1,50 @@
-import { assert } from '@0xproject/assert';
-import { schemas } from '@0xproject/json-schemas';
-import { SignedOrder } from '@0xproject/types';
+import { assert } from '@0x/assert';
+import { schemas } from '@0x/json-schemas';
+import { orderParsingUtils } from '@0x/order-utils';
-import { FeesResponse, OrderbookResponse, TokenPairsItem } from '../types';
+import {
+ APIOrder,
+ AssetPairsItem,
+ AssetPairsResponse,
+ OrderbookResponse,
+ OrderConfigResponse,
+ OrdersResponse,
+} from '@0x/types';
import { typeConverters } from './type_converters';
export const relayerResponseJsonParsers = {
- parseTokenPairsJson(json: any): TokenPairsItem[] {
- assert.doesConformToSchema('tokenPairs', json, schemas.relayerApiTokenPairsResponseSchema);
- return json.map((tokenPair: any) => {
- return typeConverters.convertStringsFieldsToBigNumbers(tokenPair, [
- 'tokenA.minAmount',
- 'tokenA.maxAmount',
- 'tokenB.minAmount',
- 'tokenB.maxAmount',
+ parseAssetDataPairsJson(json: any): AssetPairsResponse {
+ assert.doesConformToSchema('assetDataPairsResponse', json, schemas.relayerApiAssetDataPairsResponseSchema);
+ return { ...json, records: relayerResponseJsonParsers.parseAssetPairsItemsJson(json.records) };
+ },
+ parseAssetPairsItemsJson(json: any): AssetPairsItem[] {
+ return json.map((assetDataPair: any) => {
+ return orderParsingUtils.convertStringsFieldsToBigNumbers(assetDataPair, [
+ 'assetDataA.minAmount',
+ 'assetDataA.maxAmount',
+ 'assetDataB.minAmount',
+ 'assetDataB.maxAmount',
]);
});
},
- parseOrdersJson(json: any): SignedOrder[] {
- assert.doesConformToSchema('orders', json, schemas.signedOrdersSchema);
- return json.map((order: object) => typeConverters.convertOrderStringFieldsToBigNumber(order));
+ parseOrdersJson(json: any): OrdersResponse {
+ assert.doesConformToSchema('relayerApiOrdersResponse', json, schemas.relayerApiOrdersResponseSchema);
+ return { ...json, records: relayerResponseJsonParsers.parseAPIOrdersJson(json.records) };
+ },
+ parseAPIOrdersJson(json: any): APIOrder[] {
+ return json.map(relayerResponseJsonParsers.parseAPIOrderJson.bind(relayerResponseJsonParsers));
},
- parseOrderJson(json: any): SignedOrder {
- assert.doesConformToSchema('order', json, schemas.signedOrderSchema);
- return typeConverters.convertOrderStringFieldsToBigNumber(json);
+ parseAPIOrderJson(json: any): APIOrder {
+ assert.doesConformToSchema('relayerApiOrder', json, schemas.relayerApiOrderSchema);
+ return typeConverters.convertAPIOrderStringFieldsToBigNumber(json);
},
parseOrderbookResponseJson(json: any): OrderbookResponse {
- assert.doesConformToSchema('orderBook', json, schemas.relayerApiOrderBookResponseSchema);
+ assert.doesConformToSchema('orderBookResponse', json, schemas.relayerApiOrderbookResponseSchema);
return typeConverters.convertOrderbookStringFieldsToBigNumber(json);
},
- parseFeesResponseJson(json: any): FeesResponse {
- assert.doesConformToSchema('fees', json, schemas.relayerApiFeesResponseSchema);
- return typeConverters.convertStringsFieldsToBigNumbers(json, ['makerFee', 'takerFee']);
+ parseOrderConfigResponseJson(json: any): OrderConfigResponse {
+ assert.doesConformToSchema('orderConfigResponse', json, schemas.relayerApiOrderConfigResponseSchema);
+ return orderParsingUtils.convertStringsFieldsToBigNumbers(json, ['makerFee', 'takerFee']);
},
};
diff --git a/packages/connect/src/utils/type_converters.ts b/packages/connect/src/utils/type_converters.ts
index 210d452b9..b262a516b 100644
--- a/packages/connect/src/utils/type_converters.ts
+++ b/packages/connect/src/utils/type_converters.ts
@@ -1,30 +1,26 @@
-import { BigNumber } from '@0xproject/utils';
+import { orderParsingUtils } from '@0x/order-utils';
import * as _ from 'lodash';
+import { APIOrder } from '@0x/types';
+
export const typeConverters = {
convertOrderbookStringFieldsToBigNumber(orderbook: any): any {
const bids = _.get(orderbook, 'bids', []);
const asks = _.get(orderbook, 'asks', []);
+ const convertedBids = {
+ ...bids,
+ records: bids.records.map((order: any) => typeConverters.convertAPIOrderStringFieldsToBigNumber(order)),
+ };
+ const convertedAsks = {
+ ...asks,
+ records: asks.records.map((order: any) => typeConverters.convertAPIOrderStringFieldsToBigNumber(order)),
+ };
return {
- bids: bids.map((order: any) => typeConverters.convertOrderStringFieldsToBigNumber(order)),
- asks: asks.map((order: any) => typeConverters.convertOrderStringFieldsToBigNumber(order)),
+ bids: convertedBids,
+ asks: convertedAsks,
};
},
- convertOrderStringFieldsToBigNumber(order: any): any {
- return typeConverters.convertStringsFieldsToBigNumbers(order, [
- 'makerTokenAmount',
- 'takerTokenAmount',
- 'makerFee',
- 'takerFee',
- 'expirationUnixTimestampSec',
- 'salt',
- ]);
- },
- convertStringsFieldsToBigNumbers(obj: any, fields: string[]): any {
- const result = _.assign({}, obj);
- _.each(fields, field => {
- _.update(result, field, (value: string) => new BigNumber(value));
- });
- return result;
+ convertAPIOrderStringFieldsToBigNumber(apiOrder: any): APIOrder {
+ return { ...apiOrder, order: orderParsingUtils.convertOrderStringFieldsToBigNumber(apiOrder.order) };
},
};