-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathticker.js
executable file
·116 lines (91 loc) · 2.89 KB
/
ticker.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
#!/usr/bin/env node
"use strict";
require('./core/polyfill');
const throttle = require('lodash.throttle');
const api = require('./quoinex/api');
const pub = new api.PublicAPI();
const term = require('./core/terminal');
const model = require('./core/model');
const products = require('./core/product');
const render_wait = 200;
let product_map = new Map();
let tickers = null;
const _render = () => {
let out = process.stdout;
out.cork();
out.write(term.clear);
out.write(term.nl);
out.write(" Exchange:".padEnd(20));
out.write("Liquid Exchange".padStart(26));
out.write(term.nl);
out.write(" Last Update:".padEnd(20));
out.write(new Date().toLocaleTimeString().padStart(26));
out.write(term.nl);
out.write(term.nl);
out.write(" " + "Code".padEnd(8));
out.write(" " + "Price".padStart(10));
out.write(" " + "24H".padStart(8));
out.write(" " + "Volume".padStart(15));
out.write(term.nl);
out.write(term.separator + term.nl);
product_map.forEach((p, id) => {
const data = tickers.get(id);
out.write(" " + p.pair.toUpperCase().padEnd(8));
out.write(" " + term.colorful(
term.updown_color(data.price, data.price_old),
p.format_price(data.price).padStart(10)));
out.write(" " + term.colorful(
term.updown_color(data.change, 0.0),
p.format_change_p(data.change).padStart(8)));
out.write(" " + p.format_volume(data.volume).padStart(15));
out.write(term.nl);
});
out.write(term.separator + term.nl);
out.write(term.nl);
process.nextTick(() => out.uncork());
};
const render = throttle(_render, render_wait);
const main = (program) => {
program.product
.split(",").filter(s => s.trim())
.forEach(s => {
const p = products.get_product(s);
product_map.set(p.id, p);
});
tickers = new model.TickerBoard(product_map);
pub.call("GET", "/products")
.then(models => {
models.forEach(m => tickers.update(m.id, m));
render();
})
.then(() => {
const socket = new api.RealtimeAPI();
const callback = data => {
const data_ = JSON.parse(data);
tickers.update(data_.id, data_);
render();
}
product_map.forEach(v => socket.subscribe(v.get_ticker_channel()).bind('updated', callback));
});
};
process.on("uncaughtException", (err) => {
console.error("Error:", err.message || err);
process.exit(1);
});
const program = require('commander');
program
.version(require("./package.json").version)
.description("Display LIQUID's ticker")
.option("-p, --product <code>",
"Currency pair codes, comma separated (default: BTCJPY,ETHJPY,BCHJPY,ETHBTC,BTCUSD)",
s => s.toUpperCase(),
"BTCJPY,ETHJPY,BCHJPY,ETHBTC,BTCUSD")
.on("--help", () => {
console.log("");
console.log(" Examples:");
console.log("");
console.log(" $ node ticker.js -p BTCUSD,ETHBTC");
console.log("");
})
.parse(process.argv);
main(program);