crypto-nodes
Version:
223 lines (146 loc) • 4.85 kB
JavaScript
const CEXIO = require('cexio-api-node')
var per_second = 0;
var cex_obj = {
init_ws: function (node, symbols) {
var that = this;
this.cexWS.open();
this.cexWS.on('open', function() {
console.log('WebSocket connected')
that.cexWS.auth();
that.cexWS.subscribeTick();
});
this.cexWS.on('close', function() {
console.log('WebSocket disconnected')
setTimeout(function () {
that.cexWS.open();
}, 500);
});
this.cexWS.on('auth', function() {
console.log('WebSocket authenticated');
for(x in symbols) {
that.cexWS.subscribeOrderBook(symbols[x], true, 5)
}
});
this.cexWS.on('message', function(msg) {
if(msg.e == 'md_update' || msg.e == 'order-book-subscribe') {
per_second++;
if(!msg.data || !msg.data.pair) {
console.log('ERR');
console.log(msg);
return;
}
var s = msg.data.pair.split(':');
node.send({
payload: {
exchange: 'cex',
fees: that.fees,
sym_from: s[0],
sym_to: s[1],
data: msg.data
}
});
}
if (msg.e === 'ping') {
that.cexWS.authTicker('DASH-USD')
for(x in symbols) {
that.cexWS.subscribeOrderBook(symbols[x], true, 0)
}
}
});
},
fees: {},
interval: false,
connected: false,
exchange_symbols: [],
configured_symbols: [],
enabled_symbols: [],
get_exchange_symbols: function(callback) {
this.cexPub.currency_limits(function (err,data) {
var out = [];
if(!err) {
out = data.pairs.map(function (o) {
return o.symbol1 + '-' + o.symbol2;
});
}
callback(out);
});
},
get_enabled_symbols: function() {
var out = [];
for(x in this.exchange_symbols) {
var pass = false;
var exc_tmp = this.exchange_symbols[x].split('-');
for(y in this.configured_symbols) {
var cfg_tmp = this.configured_symbols[y].split('/');
// If its matching or inverted variant matches its enabled symbol
if((exc_tmp[0] == cfg_tmp[0] && exc_tmp[1] == cfg_tmp[1]) ||
(exc_tmp[0] == cfg_tmp[1] && exc_tmp[1] == cfg_tmp[0])
) {
pass = true;
}
}
if(pass) {
out.push(this.exchange_symbols[x]);
}
}
return out;
},
init: function(config, node, symbols, callback) {
var that = this;
this.configured_symbols = symbols;
console.log(symbols);
try { this.fees = JSON.parse(config.fees); } catch (e) {}
// Configure public / private clients
this.cexPub = new CEXIO().rest
if(config.api_secret && config.api_key) {
this.cexWS = new CEXIO(config.api_key, config.api_secret).ws
} else {
this.cexWS = new CEXIO().ws
}
if (!this.interval) {
this.interval = setInterval(function() {
var color = per_second > 0 ? 'green' : 'red';
node.status({ fill: color, shape: 'ring', text: per_second + ' requests per second' });
per_second = 0;
}, 1000);
}
this.get_exchange_symbols(function (exchange_symbols) {
that.exchange_symbols = exchange_symbols;
var enabled_symbols = that.get_enabled_symbols();
that.init_ws(node, enabled_symbols);
});
},
}
module.exports = function(RED) {
function cexConnector(config) {
RED.nodes.createNode(this, config);
var node = this;
node.on('input', function(msg) {
if(config.disabled) {
// Disable UI Updates
if(cex_obj.interval) {
clearInterval(cex_obj.interval);
}
// Set node status
node.status({ fill: 'red',shape:'ring', text: 'Disabled' });
// Emit flush orderbook
node.send({payload: { exchange: 'cex', op: 'flush' }});
return;
}
if(msg.payload && msg.payload.op == 'subscribe') {
if(!config.api_key || !config.api_secret) {
node.status({ fill: 'red', shape: 'ring', text: 'Missing API key or SECRET' });
return;
} else {
cex_obj.init(config, node, msg.payload.symbols);
}
}
});
node.on('close', function() {
// tidy up any async code here - shutdown connections and so on.
node.status({ fill: 'red',shape:'ring', text: 'Offline' });
node.send({payload: { exchange: 'cex', op: 'flush' }});
});
}
RED.nodes.registerType("Cex", cexConnector);
}