@0x/order-watcher 中文文档教程
OrderWatcher [Deprecated]
:noentry: 警告:该项目已弃用。 请使用 0x Mesh 来满足您所有的订单簿修剪需求。 它可以在启用或不启用订单共享的情况下使用。 如果您对如何使用 Mesh 有任何疑问,请通过 Discord 上的#mesh 频道 联系我们:否条目:
监视订单有效性的订单观察程序守护进程。
Read the wiki article.
OrderWatcher 还带有一个 WebSocket 服务器以提供与语言无关的访问 订购观看功能。 我们使用了 Node 的 WebSocket 客户端和服务器实现。 服务器发送和接收符合 JSON RPC 规范 的消息。
Installation
安装
npm install @0x/order-watcher --save
导入
import { OrderWatcher } from '@0x/order-watcher';
如果您的项目使用 TypeScript,请将以下内容添加到您的 < code>tsconfig.json:
"compilerOptions": {
"typeRoots": ["node_modules/@0x/typescript-typings/types", "node_modules/@types"],
}
Using the WebSocket Server
设置
环境变量 可以设置几个环境变量来配置服务器:
ORDER_WATCHER_HTTP_PORT
specifies the port that the http server will listen on and accept connections from. When this is not set, we default to 8080.
Requests 服务器接受三种类型的请求:ADD_ORDER
、REMOVE_ORDER
和GET_STATS
。 这些反映了底层 OrderWatcher 所做的事情。 您可以在 wiki 中阅读更多内容。 与 OrderWatcher 不同,它不公开任何 subscribe
或 unsubscribe
功能,因为 WebSocket 服务器为所有客户端保持单个订阅打开。
发出请求的第一步是与服务器建立连接。 在 Javascript 中:
var W3CWebSocket = require('websocket').w3cwebsocket;
wsClient = new W3CWebSocket('ws://127.0.0.1:8080');
在 Python 中,您可以使用 websocket-client library 并运行:
from websocket import create_connection
wsClient = create_connection("ws://127.0.0.1:8080")
建立连接后,您为您的请求准备有效载荷。 有效载荷是一个 json 对象,其格式由 JSON RPC 规范建立:
id
: All requests require you to specify a numericalid
. When the server responds to the request, the response will have the sameid
as the one supplied with your request.jsonrpc
: This is always the string'2.0'
.method
: This specifies the OrderWatcher method you want to call. I.e.,'ADD_ORDER'
,'REMOVE_ORDER'
or'GET_STATS'
.params
: These contain the parameters needed by OrderWatcher to execute the method you called. ForADD_ORDER
, provide{ signedOrder: <your signedOrder> }
. ForREMOVE_ORDER
, provide{ orderHash: <your orderHash> }
. ForGET_STATS
, no parameters are needed, so you may leave this empty.
接下来,将有效载荷转换为字符串并通过连接。 在 Javascript 中:
const addOrderPayload = {
id: 1,
jsonrpc: '2.0',
method: 'ADD_ORDER',
params: { signedOrder: <your signedOrder> },
};
wsClient.send(JSON.stringify(addOrderPayload));
在 Python 中:
import json
remove_order_payload = {
'id': 1,
'jsonrpc': '2.0',
'method': 'REMOVE_ORDER',
'params': {'orderHash': '0x6edc16bf37fde79f5012088c33784c730e2f103d9ab1caf73060c386ad107b7e'},
}
wsClient.send(json.dumps(remove_order_payload));
响应 服务器以类似的格式响应所有请求。 在数据字段中,您会发现另一个包含以下字段的对象:
id
: The id corresponding to the request that the server is responding to.UPDATE
responses are not based on any requests so theid
field is omitted`.jsonrpc
: Always'2.0'
.method
: The method the server is responding to. Eg.ADD_ORDER
. When order states change the server may also initiate a response. In this case, method will be listed asUPDATE
.result
: This field varies based on the method.UPDATE
responses contain the new order state.GET_STATS
responses contain the current order count. When there are errors, this field is omitted.error
: When there is an error executing a request, the JSON RPC error object is listed here. When the server responds successfully, this field is omitted.
在 Javascript 中,可以使用 onmessage
回调来解析响应:
wsClient.onmessage = (msg) => {
const responseData = JSON.parse(msg.data);
const method = responseData.method
};
在 Python 中,recv
收到回复的方式:
result = wsClient.recv()
method = result.method
Contributing
我们强烈建议社区帮助我们进行改进并确定协议的未来方向。 要报告此包中的错误,请在此存储库中创建一个问题。
请在开始之前阅读我们的贡献指南。
Install dependencies
如果您没有启用 yarn workspaces (Yarn < v1.0) - 启用它们:
yarn config set workspaces-experimental true
然后安装依赖
yarn install
Build
项 要构建此包和它所依赖的所有其他 monorepo 包,请从 monorepo 根目录运行以下命令:
PKG=@0x/order-watcher yarn build
或持续重建关于改变:
PKG=@0x/order-watcher yarn watch
Clean
yarn clean
Lint
yarn lint
Run Tests
yarn test
OrderWatcher [Deprecated]
:noentry: WARNING: This project is deprecated. Please use 0x Mesh for all your orderbook pruning needs. It can be used with or without order sharing enabled. If you have any questions about how to use Mesh, reach out to us in the #mesh channel on Discord :noentry:
An order watcher daemon that watches for order validity.
Read the wiki article.
OrderWatcher also comes with a WebSocket server to provide language-agnostic access to order watching functionality. We used the WebSocket Client and Server Implementation for Node. The server sends and receives messages that conform to the JSON RPC specifications.
Installation
Install
npm install @0x/order-watcher --save
Import
import { OrderWatcher } from '@0x/order-watcher';
If your project is in TypeScript, add the following to your tsconfig.json
:
"compilerOptions": {
"typeRoots": ["node_modules/@0x/typescript-typings/types", "node_modules/@types"],
}
Using the WebSocket Server
Setup
Environmental Variables Several environmental variables can be set to configure the server:
ORDER_WATCHER_HTTP_PORT
specifies the port that the http server will listen on and accept connections from. When this is not set, we default to 8080.
Requests The server accepts three types of requests: ADD_ORDER
, REMOVE_ORDER
and GET_STATS
. These mirror what the underlying OrderWatcher does. You can read more in the wiki. Unlike the OrderWatcher, it does not expose any subscribe
or unsubscribe
functionality because the WebSocket server keeps a single subscription open for all clients.
The first step for making a request is establishing a connection with the server. In Javascript:
var W3CWebSocket = require('websocket').w3cwebsocket;
wsClient = new W3CWebSocket('ws://127.0.0.1:8080');
In Python, you could use the websocket-client library and run:
from websocket import create_connection
wsClient = create_connection("ws://127.0.0.1:8080")
With the connection established, you prepare the payload for your request. The payload is a json object with a format established by the JSON RPC specification:
id
: All requests require you to specify a numericalid
. When the server responds to the request, the response will have the sameid
as the one supplied with your request.jsonrpc
: This is always the string'2.0'
.method
: This specifies the OrderWatcher method you want to call. I.e.,'ADD_ORDER'
,'REMOVE_ORDER'
or'GET_STATS'
.params
: These contain the parameters needed by OrderWatcher to execute the method you called. ForADD_ORDER
, provide{ signedOrder: <your signedOrder> }
. ForREMOVE_ORDER
, provide{ orderHash: <your orderHash> }
. ForGET_STATS
, no parameters are needed, so you may leave this empty.
Next, convert the payload to a string and send it through the connection. In Javascript:
const addOrderPayload = {
id: 1,
jsonrpc: '2.0',
method: 'ADD_ORDER',
params: { signedOrder: <your signedOrder> },
};
wsClient.send(JSON.stringify(addOrderPayload));
In Python:
import json
remove_order_payload = {
'id': 1,
'jsonrpc': '2.0',
'method': 'REMOVE_ORDER',
'params': {'orderHash': '0x6edc16bf37fde79f5012088c33784c730e2f103d9ab1caf73060c386ad107b7e'},
}
wsClient.send(json.dumps(remove_order_payload));
Response The server responds to all requests in a similar format. In the data field, you'll find another object containing the following fields:
id
: The id corresponding to the request that the server is responding to.UPDATE
responses are not based on any requests so theid
field is omitted`.jsonrpc
: Always'2.0'
.method
: The method the server is responding to. Eg.ADD_ORDER
. When order states change the server may also initiate a response. In this case, method will be listed asUPDATE
.result
: This field varies based on the method.UPDATE
responses contain the new order state.GET_STATS
responses contain the current order count. When there are errors, this field is omitted.error
: When there is an error executing a request, the JSON RPC error object is listed here. When the server responds successfully, this field is omitted.
In Javascript, the responses can be parsed using the onmessage
callback:
wsClient.onmessage = (msg) => {
const responseData = JSON.parse(msg.data);
const method = responseData.method
};
In Python, recv
is a lightweight way to receive a response:
result = wsClient.recv()
method = result.method
Contributing
We strongly recommend that the community help us make improvements and determine the future direction of the protocol. To report bugs within this package, please create an issue in this repository.
Please read our contribution guidelines before getting started.
Install dependencies
If you don't have yarn workspaces enabled (Yarn < v1.0) - enable them:
yarn config set workspaces-experimental true
Then install dependencies
yarn install
Build
To build this package and all other monorepo packages that it depends on, run the following from the monorepo root directory:
PKG=@0x/order-watcher yarn build
Or continuously rebuild on change:
PKG=@0x/order-watcher yarn watch
Clean
yarn clean
Lint
yarn lint
Run Tests
yarn test