1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
import { ContractWrappers } from '@0x/contract-wrappers';
import { tokenUtils } from '@0x/contract-wrappers/lib/test/utils/token_utils';
import { BlockchainLifecycle } from '@0x/dev-utils';
import { FillScenarios } from '@0x/fill-scenarios';
import { assetDataUtils, orderHashUtils } from '@0x/order-utils';
import { ExchangeContractErrs, OrderStateInvalid, OrderStateValid, SignedOrder } from '@0x/types';
import { BigNumber, logUtils } from '@0x/utils';
import { Web3Wrapper } from '@0x/web3-wrapper';
import * as chai from 'chai';
import 'mocha';
import * as WebSocket from 'websocket';
import { OrderWatcherWebSocketServer } from '../src/order_watcher/order_watcher_web_socket_server';
import { AddOrderRequest, OrderWatcherMethod, RemoveOrderRequest } from '../src/types';
import { chaiSetup } from './utils/chai_setup';
import { constants } from './utils/constants';
import { migrateOnceAsync } from './utils/migrate';
import { provider, web3Wrapper } from './utils/web3_wrapper';
chaiSetup.configure();
const expect = chai.expect;
const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper);
interface WsMessage {
data: string;
}
describe.only('OrderWatcherWebSocketServer', async () => {
let contractWrappers: ContractWrappers;
let wsServer: OrderWatcherWebSocketServer;
let wsClient: WebSocket.w3cwebsocket;
let wsClientTwo: WebSocket.w3cwebsocket;
let fillScenarios: FillScenarios;
let userAddresses: string[];
let makerAssetData: string;
let takerAssetData: string;
let makerTokenAddress: string;
let takerTokenAddress: string;
let makerAddress: string;
let takerAddress: string;
let zrxTokenAddress: string;
let signedOrder: SignedOrder;
let orderHash: string;
let addOrderPayload: AddOrderRequest;
let removeOrderPayload: RemoveOrderRequest;
const decimals = constants.ZRX_DECIMALS;
const fillableAmount = Web3Wrapper.toBaseUnitAmount(new BigNumber(5), decimals);
// HACK: createFillableSignedOrderAsync is Promise-based, which forces us
// to use Promises instead of the done() callbacks for tests.
// onmessage callback must thus be wrapped as a Promise.
const _getOnMessagePromise = async (client: WebSocket.w3cwebsocket, method: string | null) =>
new Promise<WsMessage>(resolve => {
client.onmessage = (msg: WsMessage) => {
const data = JSON.parse(msg.data);
if (data.method === method) {
resolve(msg);
}
};
});
before(async () => {
// Set up constants
const contractAddresses = await migrateOnceAsync();
await blockchainLifecycle.startAsync();
const networkId = constants.TESTRPC_NETWORK_ID;
const config = {
networkId,
contractAddresses,
};
contractWrappers = new ContractWrappers(provider, config);
userAddresses = await web3Wrapper.getAvailableAddressesAsync();
zrxTokenAddress = contractAddresses.zrxToken;
[makerAddress, takerAddress] = userAddresses;
[makerTokenAddress, takerTokenAddress] = tokenUtils.getDummyERC20TokenAddresses();
[makerAssetData, takerAssetData] = [
assetDataUtils.encodeERC20AssetData(makerTokenAddress),
assetDataUtils.encodeERC20AssetData(takerTokenAddress),
];
fillScenarios = new FillScenarios(
provider,
userAddresses,
zrxTokenAddress,
contractAddresses.exchange,
contractAddresses.erc20Proxy,
contractAddresses.erc721Proxy,
);
signedOrder = await fillScenarios.createFillableSignedOrderAsync(
makerAssetData,
takerAssetData,
makerAddress,
takerAddress,
fillableAmount,
);
orderHash = orderHashUtils.getOrderHashHex(signedOrder);
addOrderPayload = {
id: 1,
jsonrpc: '2.0',
method: OrderWatcherMethod.AddOrder,
params: { signedOrder },
};
removeOrderPayload = {
id: 1,
jsonrpc: '2.0',
method: OrderWatcherMethod.RemoveOrder,
params: { orderHash },
};
// Prepare OrderWatcher WebSocket server
const orderWatcherConfig = {
isVerbose: true,
};
wsServer = new OrderWatcherWebSocketServer(provider, networkId, contractAddresses, orderWatcherConfig);
});
after(async () => {
await blockchainLifecycle.revertAsync();
});
beforeEach(async () => {
wsServer.start();
await blockchainLifecycle.startAsync();
wsClient = new WebSocket.w3cwebsocket('ws://127.0.0.1:8080/');
logUtils.log(`${new Date()} [Client] Connected.`);
});
afterEach(async () => {
wsClient.close();
await blockchainLifecycle.revertAsync();
wsServer.stop();
logUtils.log(`${new Date()} [Client] Closed.`);
});
it('responds to getStats requests correctly', (done: any) => {
const payload = {
id: 1,
jsonrpc: '2.0',
method: 'GET_STATS',
};
wsClient.onopen = () => wsClient.send(JSON.stringify(payload));
wsClient.onmessage = (msg: any) => {
const responseData = JSON.parse(msg.data);
expect(responseData.id).to.be.eq(1);
expect(responseData.jsonrpc).to.be.eq('2.0');
expect(responseData.method).to.be.eq('GET_STATS');
expect(responseData.result.orderCount).to.be.eq(0);
done();
};
});
it('throws an error when an invalid method is attempted', async () => {
const invalidMethodPayload = {
id: 1,
jsonrpc: '2.0',
method: 'BAD_METHOD',
};
wsClient.onopen = () => wsClient.send(JSON.stringify(invalidMethodPayload));
const errorMsg = await _getOnMessagePromise(wsClient, null);
const errorData = JSON.parse(errorMsg.data);
// tslint:disable-next-line:no-unused-expression
expect(errorData.id).to.be.null;
// tslint:disable-next-line:no-unused-expression
expect(errorData.method).to.be.null;
expect(errorData.jsonrpc).to.be.eq('2.0');
expect(errorData.error).to.match(/^Error: Expected request to conform to schema/);
});
it('throws an error when jsonrpc field missing from request', async () => {
const noJsonRpcPayload = {
id: 1,
method: 'GET_STATS',
};
wsClient.onopen = () => wsClient.send(JSON.stringify(noJsonRpcPayload));
const errorMsg = await _getOnMessagePromise(wsClient, null);
const errorData = JSON.parse(errorMsg.data);
// tslint:disable-next-line:no-unused-expression
expect(errorData.method).to.be.null;
expect(errorData.jsonrpc).to.be.eq('2.0');
expect(errorData.error).to.match(/^Error: Expected request to conform to schema/);
});
it('throws an error when we try to add an order without a signedOrder', async () => {
const noSignedOrderAddOrderPayload = {
id: 1,
jsonrpc: '2.0',
method: 'ADD_ORDER',
orderHash: '0x7337e2f2a9aa2ed6afe26edc2df7ad79c3ffa9cf9b81a964f707ea63f5272355',
};
wsClient.onopen = () => wsClient.send(JSON.stringify(noSignedOrderAddOrderPayload));
const errorMsg = await _getOnMessagePromise(wsClient, null);
const errorData = JSON.parse(errorMsg.data);
// tslint:disable-next-line:no-unused-expression
expect(errorData.id).to.be.null;
// tslint:disable-next-line:no-unused-expression
expect(errorData.method).to.be.null;
expect(errorData.jsonrpc).to.be.eq('2.0');
expect(errorData.error).to.match(/^Error: Expected request to conform to schema/);
});
it('throws an error when we try to add a bad signedOrder', async () => {
const invalidAddOrderPayload = {
id: 1,
jsonrpc: '2.0',
method: 'ADD_ORDER',
signedOrder: {
makerAddress: '0x0',
},
};
wsClient.onopen = () => wsClient.send(JSON.stringify(invalidAddOrderPayload));
const errorMsg = await _getOnMessagePromise(wsClient, null);
const errorData = JSON.parse(errorMsg.data);
// tslint:disable-next-line:no-unused-expression
expect(errorData.id).to.be.null;
// tslint:disable-next-line:no-unused-expression
expect(errorData.method).to.be.null;
expect(errorData.error).to.match(/^Error: Expected request to conform to schema/);
});
it('executes addOrder and removeOrder requests correctly', async () => {
wsClient.onopen = () => wsClient.send(JSON.stringify(addOrderPayload));
const addOrderMsg = await _getOnMessagePromise(wsClient, OrderWatcherMethod.AddOrder);
const addOrderData = JSON.parse(addOrderMsg.data);
expect(addOrderData.method).to.be.eq('ADD_ORDER');
expect((wsServer as any)._orderWatcher._orderByOrderHash).to.deep.include({
[orderHash]: signedOrder,
});
const clientOnMessagePromise = _getOnMessagePromise(wsClient, OrderWatcherMethod.RemoveOrder);
wsClient.send(JSON.stringify(removeOrderPayload));
const removeOrderMsg = await clientOnMessagePromise;
const removeOrderData = JSON.parse(removeOrderMsg.data);
expect(removeOrderData.method).to.be.eq('REMOVE_ORDER');
expect((wsServer as any)._orderWatcher._orderByOrderHash).to.not.deep.include({
[orderHash]: signedOrder,
});
});
it('broadcasts orderStateInvalid message when makerAddress allowance set to 0 for watched order', async () => {
// Add the regular order
wsClient.onopen = () => wsClient.send(JSON.stringify(addOrderPayload));
const clientOnMessagePromise = _getOnMessagePromise(wsClient, OrderWatcherMethod.Update);
// Set the allowance to 0
await contractWrappers.erc20Token.setProxyAllowanceAsync(makerTokenAddress, makerAddress, new BigNumber(0));
// Ensure that orderStateInvalid message is received.
const orderWatcherUpdateMsg = await clientOnMessagePromise;
const orderWatcherUpdateData = JSON.parse(orderWatcherUpdateMsg.data);
expect(orderWatcherUpdateData.method).to.be.eq('UPDATE');
const invalidOrderState = orderWatcherUpdateData.result as OrderStateInvalid;
expect(invalidOrderState.isValid).to.be.false();
expect(invalidOrderState.orderHash).to.be.eq(orderHash);
expect(invalidOrderState.error).to.be.eq(ExchangeContractErrs.InsufficientMakerAllowance);
});
it('broadcasts to multiple clients when an order backing ZRX allowance changes', async () => {
// Prepare order
const makerFee = Web3Wrapper.toBaseUnitAmount(new BigNumber(2), decimals);
const takerFee = Web3Wrapper.toBaseUnitAmount(new BigNumber(0), decimals);
const nonZeroMakerFeeSignedOrder = await fillScenarios.createFillableSignedOrderWithFeesAsync(
makerAssetData,
takerAssetData,
makerFee,
takerFee,
makerAddress,
takerAddress,
fillableAmount,
takerAddress,
);
const nonZeroMakerFeeOrderPayload = {
id: 1,
jsonrpc: '2.0',
method: 'ADD_ORDER',
signedOrder: nonZeroMakerFeeSignedOrder,
};
// Set up a second client and have it add the order
wsClientTwo = new WebSocket.w3cwebsocket('ws://127.0.0.1:8080/');
logUtils.log(`${new Date()} [Client] Connected.`);
wsClientTwo.onopen = () => wsClientTwo.send(JSON.stringify(nonZeroMakerFeeOrderPayload));
const clientOneOnMessagePromise = _getOnMessagePromise(wsClient, OrderWatcherMethod.Update);
const clientTwoOnMessagePromise = _getOnMessagePromise(wsClientTwo, OrderWatcherMethod.Update);
// Change the allowance
await contractWrappers.erc20Token.setProxyAllowanceAsync(zrxTokenAddress, makerAddress, new BigNumber(0));
// Check that both clients receive the emitted event
let updateMsg = await clientOneOnMessagePromise;
let updateData = JSON.parse(updateMsg.data);
let orderState = updateData.result as OrderStateValid;
expect(orderState.isValid).to.be.true();
expect(orderState.orderRelevantState.makerFeeProxyAllowance).to.be.eq('0');
updateMsg = await clientTwoOnMessagePromise;
updateData = JSON.parse(updateMsg.data);
orderState = updateData.result as OrderStateValid;
expect(orderState.isValid).to.be.true();
expect(orderState.orderRelevantState.makerFeeProxyAllowance).to.be.eq('0');
wsClientTwo.close();
logUtils.log(`${new Date()} [Client] Closed.`);
});
});
|