From 0bcb81d3a918fbcf71d68f42fa661d884d5d74cf Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 8 Jan 2019 14:29:45 +0100 Subject: Create server entry point and Dockerfile for OrderWatcher --- packages/order-watcher/Dockerfile | 13 +++++++++++ packages/order-watcher/package.json | 1 + packages/order-watcher/src/server.ts | 44 ++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 packages/order-watcher/Dockerfile create mode 100644 packages/order-watcher/src/server.ts (limited to 'packages/order-watcher') diff --git a/packages/order-watcher/Dockerfile b/packages/order-watcher/Dockerfile new file mode 100644 index 000000000..3ffa1b72f --- /dev/null +++ b/packages/order-watcher/Dockerfile @@ -0,0 +1,13 @@ +FROM node + +WORKDIR /order-watcher + +COPY package.json . +RUN npm i +RUN npm install forever -g + +COPY . . + +EXPOSE 8080 + +CMD ["forever", "./lib/src/server.js"] diff --git a/packages/order-watcher/package.json b/packages/order-watcher/package.json index 16a46294e..c4a56c982 100644 --- a/packages/order-watcher/package.json +++ b/packages/order-watcher/package.json @@ -36,6 +36,7 @@ "@0x/dev-utils": "^1.0.21", "@0x/migrations": "^2.2.2", "@0x/tslint-config": "^2.0.0", + "@0x/subproviders": "^2.1.8", "@types/bintrees": "^1.0.2", "@types/lodash": "4.14.104", "@types/mocha": "^2.2.42", diff --git a/packages/order-watcher/src/server.ts b/packages/order-watcher/src/server.ts new file mode 100644 index 000000000..1d31e87ab --- /dev/null +++ b/packages/order-watcher/src/server.ts @@ -0,0 +1,44 @@ +import { getContractAddressesForNetworkOrThrow } from '@0x/contract-addresses'; +import { RPCSubprovider, Web3ProviderEngine } from '@0x/subproviders'; +import * as _ from 'lodash'; + +import { OrderWatcherWebSocketServer } from './order_watcher/order_watcher_web_socket_server'; + +const GANACHE_NETWORK_ID = 50; +const DEFAULT_RPC_URL = 'http://localhost:8545'; + +const provider = new Web3ProviderEngine(); +const jsonRpcUrl = process.env.JSON_RPC_URL || DEFAULT_RPC_URL; +const rpcSubprovider = new RPCSubprovider(jsonRpcUrl); +provider.addProvider(rpcSubprovider); +provider.start(); + +const networkId = process.env.NETWORK_ID !== undefined ? _.parseInt(process.env.NETWORK_ID) : GANACHE_NETWORK_ID; + +const contractAddressesString = process.env.contractAddresses; +const contractAddressesIfExists = + contractAddressesString === undefined + ? getContractAddressesForNetworkOrThrow(networkId) + : JSON.parse(contractAddressesString); + +const orderWatcherConfig: any = { + isVerbose: process.env.IS_VERBOSE === 'true', +}; +const orderExpirationCheckingIntervalMs = process.env.ORDER_EXPIRATION_CHECKING_INTERVAL_MS; +if (orderExpirationCheckingIntervalMs !== undefined) { + orderWatcherConfig.orderExpirationCheckingIntervalMs = _.parseInt(orderExpirationCheckingIntervalMs); +} +const eventPollingIntervalMs = process.env.EVENT_POLLING_INTERVAL_MS; +if (eventPollingIntervalMs !== undefined) { + orderWatcherConfig.eventPollingIntervalMs = _.parseInt(eventPollingIntervalMs); +} +const expirationMarginMs = process.env.EXPIRATION_MARGIN_MS; +if (expirationMarginMs !== undefined) { + orderWatcherConfig.expirationMarginMs = _.parseInt(expirationMarginMs); +} +const cleanupJobIntervalMs = process.env.CLEANUP_JOB_INTERVAL_MS; +if (cleanupJobIntervalMs !== undefined) { + orderWatcherConfig.cleanupJobIntervalMs = _.parseInt(cleanupJobIntervalMs); +} +const wsServer = new OrderWatcherWebSocketServer(provider, networkId, contractAddressesIfExists, orderWatcherConfig); +wsServer.start(); -- cgit v1.2.3 From 0ea86695524b91167f8824ba820eed4bb0ee6ee1 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 8 Jan 2019 15:37:14 +0100 Subject: Fixx OrderWatcher WS tests so they are completely independent from one another --- .../test/order_watcher_web_socket_server_test.ts | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'packages/order-watcher') diff --git a/packages/order-watcher/test/order_watcher_web_socket_server_test.ts b/packages/order-watcher/test/order_watcher_web_socket_server_test.ts index 6894f42fb..e3f416369 100644 --- a/packages/order-watcher/test/order_watcher_web_socket_server_test.ts +++ b/packages/order-watcher/test/order_watcher_web_socket_server_test.ts @@ -1,9 +1,10 @@ +import { ContractAddresses } from '@0x/contract-addresses'; 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 { ExchangeContractErrs, OrderStateInvalid, SignedOrder } from '@0x/types'; import { BigNumber, logUtils } from '@0x/utils'; import { Web3Wrapper } from '@0x/web3-wrapper'; import * as chai from 'chai'; @@ -44,14 +45,16 @@ describe('OrderWatcherWebSocketServer', async () => { let orderHash: string; let addOrderPayload: AddOrderRequest; let removeOrderPayload: RemoveOrderRequest; + let networkId: number; + let contractAddresses: ContractAddresses; const decimals = constants.ZRX_DECIMALS; const fillableAmount = Web3Wrapper.toBaseUnitAmount(new BigNumber(5), decimals); before(async () => { // Set up constants - const contractAddresses = await migrateOnceAsync(); + contractAddresses = await migrateOnceAsync(); await blockchainLifecycle.startAsync(); - const networkId = constants.TESTRPC_NETWORK_ID; + networkId = constants.TESTRPC_NETWORK_ID; const config = { networkId, contractAddresses, @@ -93,17 +96,16 @@ describe('OrderWatcherWebSocketServer', async () => { 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 () => { + // Prepare OrderWatcher WebSocket server + const orderWatcherConfig = { + isVerbose: true, + }; + wsServer = new OrderWatcherWebSocketServer(provider, networkId, contractAddresses, orderWatcherConfig); wsServer.start(); await blockchainLifecycle.startAsync(); wsClient = new WebSocket.w3cwebsocket('ws://127.0.0.1:8080/'); @@ -260,7 +262,7 @@ describe('OrderWatcherWebSocketServer', async () => { id: 1, jsonrpc: '2.0', method: 'ADD_ORDER', - signedOrder: nonZeroMakerFeeSignedOrder, + signedOrder: nonZeroMakerFeeSignedOrder, }; // Set up a second client and have it add the order -- cgit v1.2.3 From 5e39eae84edc2bb8ad4d40936bf2005363dc1e59 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 8 Jan 2019 15:37:50 +0100 Subject: Fixx request payload and expected responses --- .../test/order_watcher_web_socket_server_test.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'packages/order-watcher') diff --git a/packages/order-watcher/test/order_watcher_web_socket_server_test.ts b/packages/order-watcher/test/order_watcher_web_socket_server_test.ts index e3f416369..36135f65c 100644 --- a/packages/order-watcher/test/order_watcher_web_socket_server_test.ts +++ b/packages/order-watcher/test/order_watcher_web_socket_server_test.ts @@ -262,7 +262,9 @@ describe('OrderWatcherWebSocketServer', async () => { id: 1, jsonrpc: '2.0', method: 'ADD_ORDER', + params: { signedOrder: nonZeroMakerFeeSignedOrder, + }, }; // Set up a second client and have it add the order @@ -280,15 +282,15 @@ describe('OrderWatcherWebSocketServer', async () => { // Check that both clients receive the emitted event by awaiting the onMessageAsync promises 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'); + let orderState = updateData.result as OrderStateInvalid; + expect(orderState.isValid).to.be.false(); + expect(orderState.error).to.be.eq('INSUFFICIENT_MAKER_FEE_ALLOWANCE'); 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'); + orderState = updateData.result as OrderStateInvalid; + expect(orderState.isValid).to.be.false(); + expect(orderState.error).to.be.eq('INSUFFICIENT_MAKER_FEE_ALLOWANCE'); wsClientTwo.close(); logUtils.log(`${new Date()} [Client] Closed.`); -- cgit v1.2.3