crypto-nodes
Version:
209 lines (136 loc) • 4.81 kB
JavaScript
var debug_symbols = [ 'BLK', 'BTC', 'BTCD', 'DASH', 'DOGE', 'ETH', 'EXP', 'LTC', 'XPR' ];
var request = require('request');
const BFX = require('bitfinex-api-node')
var per_second = 0;
var bfx_obj = {
fees: {},
interval: false,
connected: false,
exchange_symbols: [],
configured_symbols: [],
enabled_symbols: [],
get_exchange_symbols: function(callback) {
if (bfx_obj.cache_exchange_symbols) {
return bfx_obj.cache_exchange_symbols;
}
request('https://api.bitfinex.com/v1/symbols', function (error, response, body) {
if(response && response.statusCode && response.statusCode == 200) {
all_symbols = JSON.parse(body);
bfx_obj.cache_exchange_symbols = all_symbols;
//console.log('BITFINEX ::');
//console.log(JSON.stringify(all_symbols));
callback(all_symbols);
} else {
callback([]);
}
});
},
get_enabled_symbols: function() {
var out = [];
for(x in this.exchange_symbols) {
var pass = false;
var exc_tmp = [ this.exchange_symbols[x].substr(0,3).toUpperCase() , this.exchange_symbols[x].substr(3).toUpperCase() ];
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;
},
msg: function(node, sym, ob) {
var sym_from = sym.substr(0,3);
var sym_to = sym.substr(3);
per_second++;
node.send({ payload: { 'exchange' : 'bitfinex', fees: this.fees, sym_from: sym_from, sym_to: sym_to, 'data' : ob } });
},
init: function(config, node, symbols, callback) {
var that = this;
try { this.fees = JSON.parse(config.fees); } catch (e) {}
this.configured_symbols = symbols;
var bfx_conf = {
ws: {
autoReconnect: true,
seqAudit: true,
packetWDDelay: 10 * 1000
}
};
if(config.api_key && config.api_secret) {
bfx_conf.apiKey = config.api_key;
bfx_conf.apiSecret = config.api_secret;
}
this.bfx = new BFX(bfx_conf);
this.ws = this.bfx.ws(2, {
// manageOrderBooks: true, // tell the ws client to maintain full sorted OBs
transform: true // auto-transform array OBs to OrderBook objects
});
if(!this.interval) {
this.interval = setInterval(function () {
var color = per_second > 0 ? 'green' : 'red';
// console.log('BITFINEX :: ' + per_second + ' requests per second');
//console.log('BITFINEX :: LATEST ' + JSON.stringify(latest));
node.status({ fill: color,shape:'ring', text: per_second + ' requests per second' });
per_second = 0;
}, 1000);
}
this.ws.on('error', (err) => { console.log('BITFINEX :: ' + err); });
this.ws.on('close', () => { that.ws.close(); console.log(`BITFINEX closed:`) });
this.ws.on('open', () => {
console.log('BITFINEX :: Connection Open');
that.get_exchange_symbols(function (symbols) {
that.exchange_symbols = symbols;
var enabled_symbols = that.get_enabled_symbols();
for(x in enabled_symbols) {
function sub(sym) {
//console.log('BITFINEX :: Subscribing to :: ' + 't' + sym);
that.ws.subscribeOrderBook('t' + sym, 'P0', 100);
setInterval(function () {
that.ws.unsubscribeOrderBook('t' + sym, 'P0', 100);
that.ws.subscribeOrderBook('t' + sym, 'P0', 100);
}, 60000);
that.ws.onOrderBook({ symbol: 't' + sym }, (ob) => { that.msg(node, sym, ob); });
}
var sym = enabled_symbols[x].toUpperCase();
sub(sym);
}
});
});
this.ws.open();
},
}
var interval;
module.exports = function(RED) {
function bitfinexConnector(config) {
RED.nodes.createNode(this,config);
var node = this;
node.on('input', function(msg) {
if(config.disabled) {
// Disable UI Updates
if(bfx_obj.interval) {
clearInterval(bfx_obj.interval);
}
// Set node status
node.status({ fill: 'red',shape:'ring', text: 'Disabled' });
// Emit flush orderbook
node.send({payload: { exchange: 'bitfinex', op: 'flush' }});
return;
}
if(msg.payload && msg.payload.op == 'subscribe') {
bfx_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: 'bitfinex', op: 'flush' }});
});
}
RED.nodes.registerType("bitfinexConnector", bitfinexConnector);
}