diff options
author | fragosti <francesco.agosti93@gmail.com> | 2018-08-21 02:42:29 +0800 |
---|---|---|
committer | fragosti <francesco.agosti93@gmail.com> | 2018-08-21 02:42:29 +0800 |
commit | 075e3a41c876797907e3ad98f20940e32e8d0762 (patch) | |
tree | d1f9db70ee1a56b500a3293a9ee962fd08f7abec /packages/connect/src | |
parent | f2d1d953553adfa59f0a39bf2cf98817fae0a4ff (diff) | |
download | dexon-0x-contracts-075e3a41c876797907e3ad98f20940e32e8d0762.tar.gz dexon-0x-contracts-075e3a41c876797907e3ad98f20940e32e8d0762.tar.zst dexon-0x-contracts-075e3a41c876797907e3ad98f20940e32e8d0762.zip |
Update websocket for SRA v2
Diffstat (limited to 'packages/connect/src')
-rw-r--r-- | packages/connect/src/types.ts | 8 | ||||
-rw-r--r-- | packages/connect/src/utils/orders_channel_message_parser.ts (renamed from packages/connect/src/utils/orderbook_channel_message_parser.ts) | 10 | ||||
-rw-r--r-- | packages/connect/src/utils/relayer_response_json_parsers.ts | 5 | ||||
-rw-r--r-- | packages/connect/src/ws_orders_channel.ts | 15 |
4 files changed, 23 insertions, 15 deletions
diff --git a/packages/connect/src/types.ts b/packages/connect/src/types.ts index f90f0808d..dbed8899f 100644 --- a/packages/connect/src/types.ts +++ b/packages/connect/src/types.ts @@ -31,7 +31,7 @@ export interface OrdersChannelHandler { onUpdate: ( channel: OrdersChannel, subscriptionOpts: OrdersChannelSubscriptionOpts, - order: APIOrder, + orders: APIOrder[], ) => void; onError: (channel: OrdersChannel, err: Error, subscriptionOpts?: OrdersChannelSubscriptionOpts) => void; onClose: (channel: OrdersChannel) => void; @@ -48,13 +48,13 @@ export enum OrdersChannelMessageTypes { export interface UpdateOrdersChannelMessage { type: OrdersChannelMessageTypes.Update; - requestId: number; - payload: APIOrder; + requestId: string; + payload: APIOrder[]; } export interface UnknownOrdersChannelMessage { type: OrdersChannelMessageTypes.Unknown; - requestId: number; + requestId: string; payload: undefined; } diff --git a/packages/connect/src/utils/orderbook_channel_message_parser.ts b/packages/connect/src/utils/orders_channel_message_parser.ts index 97d8f2d6a..1b6cda17b 100644 --- a/packages/connect/src/utils/orderbook_channel_message_parser.ts +++ b/packages/connect/src/utils/orders_channel_message_parser.ts @@ -15,15 +15,15 @@ export const ordersChannelMessageParser = { 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'); + const requestId: string = _.get(messageObj, 'requestId'); assert.assert(!_.isUndefined(requestId), `Message is missing a requestId parameter: ${utf8Data}`); - assert.isNumber('requestId', requestId); + assert.isString('requestId', requestId); switch (type) { case OrdersChannelMessageTypes.Update: { assert.doesConformToSchema('message', messageObj, schemas.relayerApiOrdersChannelUpdateSchema); - const orderJson = messageObj.payload; - const order = relayerResponseJsonParsers.parseAPIOrderJson(orderJson); - return _.assign(messageObj, { payload: order }); + const ordersJson = messageObj.payload; + const orders = relayerResponseJsonParsers.parseAPIOrdersJson(ordersJson); + return _.assign(messageObj, { payload: orders }); } default: { return { diff --git a/packages/connect/src/utils/relayer_response_json_parsers.ts b/packages/connect/src/utils/relayer_response_json_parsers.ts index 2b2e1efe7..dff854dfb 100644 --- a/packages/connect/src/utils/relayer_response_json_parsers.ts +++ b/packages/connect/src/utils/relayer_response_json_parsers.ts @@ -22,7 +22,10 @@ export const relayerResponseJsonParsers = { }, parseOrdersJson(json: any): OrdersResponse { assert.doesConformToSchema('relayerApiOrdersResponse', json, schemas.relayerApiOrdersResponseSchema); - return { ...json, records: json.records.map(relayerResponseJsonParsers.parseAPIOrderJson.bind(relayerResponseJsonParsers)) }; + return { ...json, records: relayerResponseJsonParsers.parseAPIOrdersJson(json.records) }; + }, + parseAPIOrdersJson(json: any): APIOrder[] { + return json.map(relayerResponseJsonParsers.parseAPIOrderJson.bind(relayerResponseJsonParsers)); }, parseAPIOrderJson(json: any): APIOrder { assert.doesConformToSchema('relayerApiOrder', json, schemas.relayerApiOrderSchema); diff --git a/packages/connect/src/ws_orders_channel.ts b/packages/connect/src/ws_orders_channel.ts index 9d45b6570..62960d23a 100644 --- a/packages/connect/src/ws_orders_channel.ts +++ b/packages/connect/src/ws_orders_channel.ts @@ -9,7 +9,11 @@ import { OrdersChannelSubscriptionOpts, } from './types'; import { assert } from './utils/assert'; -import { ordersChannelMessageParser } from './utils/orderbook_channel_message_parser'; +import { ordersChannelMessageParser } from './utils/orders_channel_message_parser'; + +export interface OrdersChannelSubscriptionOptsMap { + [key: string]: OrdersChannelSubscriptionOpts; +} /** * This class includes all the functionality related to interacting with a websocket endpoint @@ -18,7 +22,7 @@ import { ordersChannelMessageParser } from './utils/orderbook_channel_message_pa export class WebSocketOrdersChannel implements OrdersChannel { private readonly _client: WebSocket.w3cwebsocket; private readonly _handler: OrdersChannelHandler; - private readonly _subscriptionOptsList: OrdersChannelSubscriptionOpts[] = []; + private readonly _subscriptionOptsMap: OrdersChannelSubscriptionOptsMap = {}; /** * Instantiates a new WebSocketOrdersChannel instance * @param client A WebSocket client @@ -50,11 +54,12 @@ export class WebSocketOrdersChannel implements OrdersChannel { public subscribe(subscriptionOpts: OrdersChannelSubscriptionOpts): void { assert.isOrdersChannelSubscriptionOpts('subscriptionOpts', subscriptionOpts); assert.assert(this._client.readyState === WebSocket.w3cwebsocket.OPEN, 'WebSocket connection is closed'); - this._subscriptionOptsList.push(subscriptionOpts); + const requestId = uuid(); + this._subscriptionOptsMap[requestId] = subscriptionOpts; const subscribeMessage = { type: 'subscribe', channel: 'orders', - requestId: uuid(), + requestId, payload: subscriptionOpts, }; this._client.send(JSON.stringify(subscribeMessage)); @@ -73,7 +78,7 @@ export class WebSocketOrdersChannel implements OrdersChannel { try { const data = message.data; const parserResult = ordersChannelMessageParser.parse(data); - const subscriptionOpts = this._subscriptionOptsList[parserResult.requestId]; + const subscriptionOpts = this._subscriptionOptsMap[parserResult.requestId]; if (_.isUndefined(subscriptionOpts)) { this._handler.onError( this, |