forked from nocaremc/nodex-ws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index-old.js
444 lines (388 loc) · 10.9 KB
/
index-old.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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
'use strict'
const dotenv = require('dotenv')
const WebSocket = require('ws')
const request = require('request-json')
let Events = {
login: 0,
db_api: 1,
get_account_by_name: 2,
get_account_balances: 3,
lookup_asset_symbols: 4,
get_limit_orders: 5,
get_ticker: 6,
wallet_api: 7,
currentRequestID: 100
}
let States = {
login: false,
db_api: false,
wallet_api: false,
account_id: false,
asset_list: false,
}
let Data = {
// integer|string
db_api_id: undefined,
wallet_api_id: undefined,
// string
user_id: undefined,
// string
user_name: undefined,
// string
user_pass: undefined,
// array {asset}
asset_list: [],
// array {id, symbol}
asset_pairs: [],
/* array {
id,
asset_pair_id,
orders: [{
id,
quantity,
// NOW: Just run the math for this upon collection
price: {
base: {amount, asset_id},
quote: {amount, asset_id}
}
}]
} */
orders: []
}
/**
* Based on this https://stackoverflow.com/a/41407246
* LATER: create or use a logging library with colors
*/
const colors = {
reset: "\x1b[0m",
fgRed: "\x1b[31m",
fgGreen: "\x1b[32m",
fgYellow: "\x1b[33m",
fgWhite: "\x1b[37m"
}
// Print to console log using a color. Revert color after.
function log(message, color)
{
if(typeof color === 'undefined') {
color = colors.fgWhite
}
console.log(color, message, colors.reset)
}
// Print given error to console in red
function logError(error)
{
log(error, colors.fgRed)
}
function logWarn(warn)
{
log(warn, colors.fgYellow)
}
let Wallet = {
client: undefined,
request: {
jsonrpc: "2.0",
id: 1,
method: "",
params:[]
},
init: () => {
Wallet.client = request.createClient(process.env.WALLET_NODE)
},
is_new: () => {
Wallet.set_request("is_new")
Wallet.client.post("/", Wallet.request, (err, res, body) => {
logWarn(body)
})
},
set_request: (method, params) => {
if(typeof method === "undefined") {
method = ""
}
if(!Array.isArray(params) && typeof params !== "undefined") {
params = []
}
Wallet.request.method = method
Wallet.request.params = params
}
}
/**
* Return all symbols in given list paired with given symbol
* @param {String} symbol
* @param {Array} list
*/
function makePairs(symbol, list)
{
return list
// Do not pair onself
.filter(item => symbol !== item)
// get pairs
.map(item => [symbol, item])
}
/**
* Returns true if a given order's id matches the current get_limit_order event
* @param {Order} item
*/
function getOrderByEventID(item)
{
return item.id === Events.get_limit_orders + Events.currentRequestID
}
/**
* Returns a subset of information in an order from rpc
* @param {Order Object} order
*/
function mapOrderData(order)
{
return {
id: order.id,
quantity: order.for_sale,
price: order.sell_price
}
}
function update(dt)
{
let request = {}
// Log in
if(!States.login) {
Data.user_name = process.env.DEX_USER
request = {
id: Events.login,
method: "call",
params: [
1,
"login",
[
Data.user_name,
process.env.DEX_PASS
]
]
}
ws.jsend(request)
return;
}
// Request access to db api
if(!States.db_api) {
request = {
id: Events.db_api,
method: "call",
params: [
1,
"database",
[]
]
}
ws.jsend(request)
return;
}
if(!States.wallet_api) {
request = {
id: Events.wallet_api,
method: "call",
params: [
1,
"wallet",
[]
]
}
ws.jsend(request)
return
}
// Obtain user_id for a given username
if(!States.account_id) {
request = {
id: Events.get_account_by_name,
method: "call",
params: [
Data.db_api_id,
"get_account_by_name",
[
Data.user_name
]
]
}
ws.jsend(request)
return;
}
// Let's grab a list of assets we care about
if(!States.asset_list) {
request = {
id: Events.lookup_asset_symbols,
method: "call",
params: [
Data.db_api_id,
"lookup_asset_symbols",
[
JSON.parse(process.env.ASSET_SYMBOLS)
]
]
}
ws.jsend(request)
return;
}
/* Get limit orders for an asset *
/* This example will get the first 20 orders for the first asset pair *
// Our id serves as the request incrementer + event id base
// This is how we uniquely identify it in the router
Events.currentRequestID = Events.currentRequestID + 1
let unique_id = Events.get_limit_orders + Events.currentRequestID
// We'll want to referance this specific order collection later
Data.orders.push({id: unique_id, asset_pair_key: 0})
request = {
id: unique_id,
method: "call",
params: [
Data.db_api_id,
"get_limit_orders",
[
Data.asset_pairs[0][0].id,
Data.asset_pairs[0][1].id,
20
]
]
}
ws.jsend(request)
return;*/
/* Send money to another account *
request = {
id: Events.
}*/
/* Get ticker price for a pair
Events.currentRequestID = Events.currentRequestID + 1
let ticker_id = Events.get_ticker + Events.currentRequestID
request = {
id: ticker_id,
method: "call",
params: [
Data.db_api_id,
"get_ticker",
[
Data.asset_pairs[0][0].id,
Data.asset_pairs[0][1].id,
]
]
}
ws.jsend(request)
return;*/
logError("Quitting")
ws.close()
/* Check account balances
request = {
id: Events.get_account_balances,
method: "call",
params: [
Data.db_api_id,
"get_account_balances",
[
Data.user_id,
[] // flat array of asset ids
]
]
}
ws.jsend(request)*/
}
/**
* This is function handles/routes every incoming request
*
* @param {String} data - JSON body of incoming blah
*/
function router(data)
{
data = JSON.parse(data);
switch(data.id)
{
case Events.login:
if(data.result === true) {
log("Logged in", colors.fgGreen)
States.login = true
} else {
logError("Unable to login!")
}
break;
case Events.db_api:
Data.db_api_id = data.result
States.db_api = true
log("Database API ID: " + Data.db_api_id, colors.fgGreen)
break;
case Events.wallet_api:
logError(data)
break;
case Events.get_account_by_name:
Data.user_id = data.result.id
// We can extract a whole lot more data about this account here
// logError(data)
States.account_id = true
log("Got account for: " + Data.user_name + ' | ' + Data.user_id, colors.fgGreen)
break;
case Events.get_account_balances:
// We'll need a lookup list of these assets to know what they are.
// AND these values look incorrect for my account.. 0.o
log("Got balance info for: " + Data.user_id, colors.fgGreen)
// logWarn(data.result)
break;
case Events.lookup_asset_symbols:
Data.asset_list = data.result;
log("Retrieved desired assets list", colors.fgGreen)
// Simplify asset list. We don't all the much data in pairs
let list = Data.asset_list.map(item => {
return {symbol: item.symbol, id: item.id}
})
// We'll iterate to create pairs and remove used (first) item each time
do {
let symbol_pairs = makePairs(list[0], list)
if(symbol_pairs.length > 0) {
Data.asset_pairs.push(symbol_pairs)
}
// Remove current symbol from list
list.splice(0, 1)
}
while (list.length > 0)
// Flatten the array of pairs
Data.asset_pairs = [].concat(...Data.asset_pairs)
States.asset_list = true
log("Created asset pairs", colors.fgGreen)
//logWarn(Data.asset_pairs)
break;
// The most recent get_limit_order request response
case (Events.get_limit_orders + Events.currentRequestID):
let orders = data.result
Data.orders
// Get the order that matches this event
.filter(getOrderByEventID)
.map(item => {
// Collect the desired data we want for each order
item.orders = orders.map(mapOrderData)
})
log("Collected Orders for asset: " + Data.orders.filter(getOrderByEventID)[0].asset_pair_key, colors.fgGreen)
/* likely a bug on my end. Either multiply value by 10 or divide by 10
let price = Data.orders[0].orders[0].price
let key = Data.orders.filter(getOrderByEventID)[0].asset_pair_key
let pair = Data.asset_pairs[key]
logError(pair[0].symbol + pair[1].symbol + ": " +(price.quote.amount/price.base.amount)*10)
*/
break;
// Ticker price data
case (Events.get_ticker + Events.currentRequestID):
logWarn(data)
break;
default:
logError("Unknown event given: " + data.id + " " + (Events.get_limit_orders + Events.currentRequestID))
logError(data)
break;
}
setTimeout(update, 250)
}
/* Startup */
// Load environment variables
dotenv.load()
// Establish a connection with the given node address
let ws = new WebSocket(process.env.RPC_NODE, {perMessageDeflate: false})
Wallet.init()
// Proxy WS' send function to json-encode given data before sending
// Could be something in that library that already can do this.
ws.jsend = request => ws.send(JSON.stringify(request))
// A socket connection is established
//ws.on('open', update)
// A message was recieved on socket connection
//ws.on('message', router)
// WS threw an error back at us
//ws.on('error', logError)
//Wallet.is_new()