forked from spotware/connect-nodejs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
145 lines (121 loc) · 4.43 KB
/
app.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
var logTraffic;
var logData;
process.argv.forEach(function(arg){
logAllTraffic = logTraffic || (arg == 'logAllTraffic');
});
var protobuf = require('protobufjs');
var bytebuffer = require('byte');
var tls = require('tls');
var chalk = require('chalk');
var API_HOST = 'sandbox-tradeapi.spotware.com';
var API_PORT = 5032;
var CLIENT_ID = '7_5az7pj935owsss8kgokcco84wc8osk0g0gksow0ow4s4ocwwgc';
var CLIENT_SECRET = '49p1ynqfy7c4sw84gwoogwwsk8cocg8ow8gc8o80c0ws448cs4';
var ACCOUNT_ID = 62002;
var ACCOUNT_TOKEN = 'test002_access_token';
// Proto Message Builds
var commonBuilder = protobuf.loadProtoFile('proto/CommonMessages.proto');
var openApiBuilder = protobuf.loadProtoFile('proto/OpenApiMessages.proto');
// Common Buffers
var PingBuf = commonBuilder.build('ProtoPingReq');
var ProtoPayloadType = commonBuilder.build('ProtoPayloadType');
var ProtoMessageBuf = commonBuilder.build('ProtoMessage');
var ErrorBuf = commonBuilder.build('ProtoErrorRes');
//API Buffers
var ProtoOAPayloadType = openApiBuilder.build('ProtoOAPayloadType');
var OAuthBuf = openApiBuilder.build('ProtoOAAuthReq');
var SpotsBuf = openApiBuilder.build('ProtoOASubscribeForSpotsReq');
var SpotBuf = openApiBuilder.build('ProtoOASpotEvent');
var pingInterval;
var start = Math.floor(new Date() / 1000);
var prevBidPrice, prevAskPrice;
var socket = tls.connect(API_PORT, API_HOST, function() {
console.log('Connected');
// Sending pings in a loop
pingInterval = setInterval(function() {
var pingBuf = new PingBuf({
payloadType: 'PING_REQ',
timestamp: Math.floor(new Date())
});
//console.log('Sending ping...');
var msg = wrapMessage(pingBuf);
socket.write(getLength(msg));
socket.write(msg);
}, 1000);
// Authenticating
var oAuthBuf = new OAuthBuf({
payloadType: 'OA_AUTH_REQ',
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET
});
console.log('Sending auth Request...');
var msg = wrapMessage(oAuthBuf);
socket.write(getLength(msg));
socket.write(msg);
});
socket.on('readable', function() {
var length = new Buffer(socket.read(4)).readInt32BE(0);
var data = socket.read(length);
data = ProtoMessageBuf.decode(data);
var payloadType = data.payloadType;
switch( payloadType ) {
case ProtoPayloadType.ERROR_RES:
var msg = ErrorBuf.decode(data.payload);
console.log(chalk.red('Received error response'));
console.log(chalk.red(msg.description));
break;
case ProtoOAPayloadType.OA_AUTH_RES:
var spotsBuf = new SpotsBuf({
payloadType: 'OA_SUBSCRIBE_FOR_SPOTS_REQ',
accountId: ACCOUNT_ID,
accessToken: ACCOUNT_TOKEN,
symblolName: 'EURUSD'
});
console.log('Sending subscribe event...');
var msg = wrapMessage(spotsBuf);
socket.write(getLength(msg));
socket.write(msg);
break;
case ProtoOAPayloadType.OA_SPOT_EVENT:
var msg = SpotBuf.decode(data.payload);
console.log('Bid price: ' + formatPrice(msg.bidPrice, prevBidPrice) + ', ask price: ' + formatPrice(msg.askPrice, prevAskPrice));
prevBidPrice = msg.bidPrice;
prevAskPrice = msg.askPrice;
break;
default:
//if passed parameter logAllTraffic, the app will log even PING requests.
logAllTraffic && console.log('Received misc payloadType: ' + payloadType);
break;
}
});
socket.on('end', function() {
var finish = Math.floor(new Date() / 1000);
var secs = finish - start;
console.log('Connection closed in ' + secs.toString() + ' secs');
clearInterval(pingInterval);
});
socket.on('error', function(e) {
console.log(chalk.red(e));
});
/***
* Returns colored price according its previous value. Green if the price was less or equal, otherwise red.
* @param price
* @param prevPrice
* @returns {*}
*/
function formatPrice(price, prevPrice) {
return (prevPrice <= price) ? chalk.green(price) : chalk.red(price);
}
function wrapMessage(data) {
var protoMessageBuf = new ProtoMessageBuf({
payloadType: data.payloadType,
payload: data.toBuffer(),
clientMsgId: null
});
return protoMessageBuf.toBuffer();
}
function getLength(msg) {
var sizeBuf = bytebuffer.allocate(4);
sizeBuf.putInt(msg.length);
return sizeBuf.get(0, 4);
}