aboutsummaryrefslogtreecommitdiffstats
path: root/packages/order-watcher
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2018-12-16 13:34:35 +0800
committerFabio Berger <me@fabioberger.com>2018-12-16 13:34:35 +0800
commit7cafe396de676cec3859c76d6407a0948a8e398e (patch)
tree746594ed8d3a83819b8c06f173f716c029068e3a /packages/order-watcher
parent6bb2ef923894a3572c3fa824c3bf1a69759eb43a (diff)
downloaddexon-sol-tools-7cafe396de676cec3859c76d6407a0948a8e398e.tar
dexon-sol-tools-7cafe396de676cec3859c76d6407a0948a8e398e.tar.gz
dexon-sol-tools-7cafe396de676cec3859c76d6407a0948a8e398e.tar.bz2
dexon-sol-tools-7cafe396de676cec3859c76d6407a0948a8e398e.tar.lz
dexon-sol-tools-7cafe396de676cec3859c76d6407a0948a8e398e.tar.xz
dexon-sol-tools-7cafe396de676cec3859c76d6407a0948a8e398e.tar.zst
dexon-sol-tools-7cafe396de676cec3859c76d6407a0948a8e398e.zip
Ensure fileName matches class name, fix broadcast
Diffstat (limited to 'packages/order-watcher')
-rw-r--r--packages/order-watcher/src/index.ts1
-rw-r--r--packages/order-watcher/src/order_watcher/order_watcher_websocket_server.ts (renamed from packages/order-watcher/src/order_watcher/order_watcher_websocket.ts)40
-rw-r--r--packages/order-watcher/src/types.ts8
-rw-r--r--packages/order-watcher/test/order_watcher_websocket_server_test.ts (renamed from packages/order-watcher/test/order_watcher_websocket_test.ts)4
4 files changed, 32 insertions, 21 deletions
diff --git a/packages/order-watcher/src/index.ts b/packages/order-watcher/src/index.ts
index 5eeba3e87..5bdef4504 100644
--- a/packages/order-watcher/src/index.ts
+++ b/packages/order-watcher/src/index.ts
@@ -1,4 +1,5 @@
export { OrderWatcher } from './order_watcher/order_watcher';
+export { OrderWatcherWebSocketServer } from './order_watcher/order_watcher_websocket_server';
export { ExpirationWatcher } from './order_watcher/expiration_watcher';
export {
diff --git a/packages/order-watcher/src/order_watcher/order_watcher_websocket.ts b/packages/order-watcher/src/order_watcher/order_watcher_websocket_server.ts
index 7a88597ef..2d2d9e82e 100644
--- a/packages/order-watcher/src/order_watcher/order_watcher_websocket.ts
+++ b/packages/order-watcher/src/order_watcher/order_watcher_websocket_server.ts
@@ -1,19 +1,12 @@
import { ContractAddresses } from '@0x/contract-addresses';
-import { OrderState, SignedOrder } from '@0x/types';
+import { OrderStateInvalid, OrderStateValid, SignedOrder } from '@0x/types';
import { BigNumber, logUtils } from '@0x/utils';
import { Provider } from 'ethereum-types';
import * as http from 'http';
import * as WebSocket from 'websocket';
import { webSocketRequestSchema, webSocketUtf8MessageSchema } from '../schemas/websocket_schemas';
-import {
- GetStatsResult,
- OnOrderStateChangeCallback,
- OrderWatcherConfig,
- OrderWatcherMethod,
- WebSocketRequest,
- WebSocketResponse,
-} from '../types';
+import { GetStatsResult, OrderWatcherConfig, OrderWatcherMethod, WebSocketRequest, WebSocketResponse } from '../types';
import { assert } from '../utils/assert';
import { OrderWatcher } from './order_watcher';
@@ -29,6 +22,7 @@ export class OrderWatcherWebSocketServer {
private readonly _httpServer: http.Server;
private readonly _connectionStore: Set<WebSocket.connection>;
private readonly _wsServer: WebSocket.server;
+ private _jsonRpcRequestId: number;
/**
* Recover types lost when the payload is stringified.
*/
@@ -61,6 +55,7 @@ export class OrderWatcherWebSocketServer {
contractAddresses?: ContractAddresses,
partialConfig?: Partial<OrderWatcherConfig>,
) {
+ this._jsonRpcRequestId = 1;
this._orderWatcher = new OrderWatcher(provider, networkId, contractAddresses, partialConfig);
this._connectionStore = new Set();
this._httpServer = http.createServer();
@@ -87,8 +82,7 @@ export class OrderWatcherWebSocketServer {
// Have the WebSocket server subscribe to the OrderWatcher to receive updates.
// These updates are then broadcast to clients in the _connectionStore.
- const broadcastCallback: OnOrderStateChangeCallback = this._broadcastCallback.bind(this);
- this._orderWatcher.subscribe(broadcastCallback);
+ this._orderWatcher.subscribe(this._broadcastCallback.bind(this));
}
/**
@@ -168,14 +162,24 @@ export class OrderWatcherWebSocketServer {
* we do not support clients subscribing to only a subset of orders. As such,
* Client B will be notified of changes to an order that Client A added.
*/
- private _broadcastCallback(err: Error | null, orderState?: OrderState): void {
+ private _broadcastCallback(err: Error | null, orderState?: OrderStateValid | OrderStateInvalid | undefined): void {
+ const method = OrderWatcherMethod.Update;
+ const response =
+ err === null
+ ? {
+ jsonrpc: JSONRPC_VERSION,
+ method,
+ result: orderState,
+ }
+ : {
+ jsonrpc: JSONRPC_VERSION,
+ method,
+ error: {
+ code: -32000,
+ message: err.message,
+ },
+ };
this._connectionStore.forEach((connection: WebSocket.connection) => {
- const response: WebSocketResponse = {
- id: null,
- jsonrpc: JSONRPC_VERSION,
- method: OrderWatcherMethod.Update,
- result: orderState,
- };
connection.sendUTF(JSON.stringify(response));
});
}
diff --git a/packages/order-watcher/src/types.ts b/packages/order-watcher/src/types.ts
index 90d383660..ecbebe305 100644
--- a/packages/order-watcher/src/types.ts
+++ b/packages/order-watcher/src/types.ts
@@ -83,7 +83,13 @@ interface ErrorWebSocketResponse {
id: null;
jsonrpc: string;
method: null;
- error: string;
+ error: JSONRPCError;
+}
+
+interface JSONRPCError {
+ code: number;
+ message: string;
+ data?: string | object;
}
export interface GetStatsResult {
diff --git a/packages/order-watcher/test/order_watcher_websocket_test.ts b/packages/order-watcher/test/order_watcher_websocket_server_test.ts
index c4d1ede45..9f9db7b1f 100644
--- a/packages/order-watcher/test/order_watcher_websocket_test.ts
+++ b/packages/order-watcher/test/order_watcher_websocket_server_test.ts
@@ -10,7 +10,7 @@ import * as chai from 'chai';
import 'mocha';
import * as WebSocket from 'websocket';
-import { OrderWatcherWebSocketServer } from '../src/order_watcher/order_watcher_websocket';
+import { OrderWatcherWebSocketServer } from '../src/order_watcher/order_watcher_websocket_server';
import { chaiSetup } from './utils/chai_setup';
import { constants } from './utils/constants';
@@ -25,7 +25,7 @@ interface WsMessage {
data: string;
}
-describe.only('OrderWatcherWebSocket', async () => {
+describe.only('OrderWatcherWebSocketServer', async () => {
let contractWrappers: ContractWrappers;
let wsServer: OrderWatcherWebSocketServer;
let wsClient: WebSocket.w3cwebsocket;