This repository has been archived by the owner on Feb 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
168 lines (140 loc) · 4.32 KB
/
index.js
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
/*
* Copyright 2015 Parity authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const MessageType = {
VERSION: 'V',
ORDER_ADDED: 'A',
ORDER_EXECUTED: 'E',
ORDER_CANCELED: 'X',
};
exports.MessageType = MessageType;
exports.Side = {
BUY: 'B',
SELL: 'S',
};
exports.format = (message) => {
switch (message.messageType) {
case MessageType.VERSION:
return formatVersion(message);
case MessageType.ORDER_ADDED:
return formatOrderAdded(message);
case MessageType.ORDER_EXECUTED:
return formatOrderExecuted(message);
case MessageType.ORDER_CANCELED:
return formatOrderCanceled(message);
default:
throw new Error('Unknown message type: ' + message.messageType);
}
};
exports.parse = (buffer) => {
const messageType = buffer.readUInt8(0);
switch (messageType) {
case 0x56:
return parseVersion(buffer);
case 0x41:
return parseOrderAdded(buffer);
case 0x45:
return parseOrderExecuted(buffer);
case 0x58:
return parseOrderCanceled(buffer);
default:
throw new Error('Unknown message type: ' + messageType);
}
};
function formatVersion(message) {
const buffer = Buffer.allocUnsafe(5);
buffer.writeUInt8(0x56, 0);
buffer.writeUInt32BE(message.version, 1);
return buffer;
}
function parseVersion(buffer) {
return {
messageType: MessageType.VERSION,
version: buffer.readUInt32BE(1),
};
}
function formatOrderAdded(message) {
const buffer = Buffer.allocUnsafe(42);
buffer.writeUInt8(0x41, 0);
writeUInt64BE(buffer, message.timestamp, 1);
writeUInt64BE(buffer, message.orderNumber, 9);
writeString(buffer, message.side, 17, 1);
writeString(buffer, message.instrument, 18, 8);
writeUInt64BE(buffer, message.quantity, 26);
writeUInt64BE(buffer, message.price, 34);
return buffer;
}
function parseOrderAdded(buffer) {
return {
messageType: MessageType.ORDER_ADDED,
timestamp: readUInt64BE(buffer, 1),
orderNumber: readUInt64BE(buffer, 9),
side: readString(buffer, 17, 1),
instrument: readString(buffer, 18, 8),
quantity: readUInt64BE(buffer, 26),
price: readUInt64BE(buffer, 34),
};
}
function formatOrderExecuted(message) {
const buffer = Buffer.allocUnsafe(29);
buffer.writeUInt8(0x45, 0);
writeUInt64BE(buffer, message.timestamp, 1);
writeUInt64BE(buffer, message.orderNumber, 9);
writeUInt64BE(buffer, message.quantity, 17);
buffer.writeUInt32BE(message.matchNumber, 25);
return buffer;
}
function parseOrderExecuted(buffer) {
return {
messageType: MessageType.ORDER_EXECUTED,
timestamp: readUInt64BE(buffer, 1),
orderNumber: readUInt64BE(buffer, 9),
quantity: readUInt64BE(buffer, 17),
matchNumber: buffer.readUInt32BE(25),
};
}
function formatOrderCanceled(message) {
const buffer = Buffer.allocUnsafe(25);
buffer.writeUInt8(0x58, 0);
writeUInt64BE(buffer, message.timestamp, 1);
writeUInt64BE(buffer, message.orderNumber, 9);
writeUInt64BE(buffer, message.canceledQuantity, 17);
return buffer;
}
function parseOrderCanceled(buffer) {
return {
messageType: MessageType.ORDER_CANCELED,
timestamp: readUInt64BE(buffer, 1),
orderNumber: readUInt64BE(buffer, 9),
canceledQuantity: readUInt64BE(buffer, 17),
};
}
function writeUInt64BE(buffer, value, offset) {
buffer.writeUInt32BE(value / 0x100000000, offset);
buffer.writeUInt32BE(value % 0x100000000, offset + 4);
}
function readUInt64BE(buffer, offset) {
const high = buffer.readUInt32BE(offset);
const low = buffer.readUInt32BE(offset + 4);
return 0x100000000 * high + low;
}
function writeString(buffer, value, offset, length) {
const count = buffer.write(value, offset, length, 'ascii');
buffer.fill(0x20, offset + count, offset + length);
}
function readString(buffer, offset, length) {
return buffer.toString('ascii', offset, offset + length);
}