UNPKG

crypto-nodes

Version:

346 lines (226 loc) 8.17 kB
const Poloniex = require('poloniex-api-node'); //var debug_symbols = ['BLK', 'BTC', 'BTCD', 'DASH', 'DOGE', 'ETH', 'EXP', 'LTC', 'XPR']; var per_second = 0; var poloniex_obj = { fees: {}, interval: false, poloniex: false, connected: false, exchange_symbols: [], configured_symbols: [], enabled_symbols: [], get_exchange_symbols: function(callback) { this.poloniex.returnTicker((err, ticker) => { if (err) { callback([]); } else { const currencies = Object.keys(ticker); callback(currencies); } }); }, 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; }, msg: function(node, channelName, data) { var that = this; var sym = channelName.split('_'); // I don't like this idea.. // FLIPPED!! var sym_from = sym[1]; var sym_to = sym[0]; node.send({ payload: { exchange: 'poloniex', fees: that.fees, sym_from: sym_from, sym_to: sym_to, data: data } }); }, init: function(config, node, symbols, callback) { var that = this; try { this.fees = JSON.parse(config.fees); } catch (e) {} this.configured_symbols = symbols; if(this.poloniex && this.connected) { if(callback) { callback(this.poloniex); } return; } this.poloniex = new Poloniex(config.api_key, config.api_secret, { socketTimeout: 5000 }); 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.poloniex.on('message', (channelName, data, seq) => { that.msg(node, channelName, data); per_second++; }); this.poloniex.on('open', () => { that.get_exchange_symbols(function (exchange_symbols) { that.exchange_symbols = exchange_symbols; var enabled_symbols = that.get_enabled_symbols(); function resub(key) { setInterval(function () { //console.log('Poloniex resubscribe ' + key); that.poloniex.unsubscribe(key); that.poloniex.subscribe(key); }, 60000); } for (var i = 0; i < enabled_symbols.length; i++) { //console.log('POLONIEX SUBSCRIBE :: ' + enabled_symbols[i]); that.poloniex.subscribe(enabled_symbols[i]); resub(enabled_symbols[i]); } node.status({ fill: 'red', shape: 'ring', text: 'Subscribed to ' + that.enabled_symbols.length + ' symbols.' }); }); }); this.poloniex.on('close', (reason, details) => { poloniex.close(); console.log('Poloniex WebSocket connection disconnected' + reason + ' details : ' + details); }); this.poloniex.on('error', (error) => { console.log('POLONIEX ERR:'); console.log(error); }); this.poloniex.openWebSocket({ version: 2 }); }, } module.exports = function(RED) { function PoloniexConnector(config) { var node = this; RED.nodes.createNode(this, config); node.on('input', function(msg) { if(config.disabled) { // Disable UI Updates if(poloniex_obj.interval) { clearInterval(poloniex_obj.interval); } // Set node status node.status({ fill: 'red',shape:'ring', text: 'Disabled' }); // Emit flush orderbook node.send({payload: { exchange: 'poloniex', 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 { poloniex_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: 'poloniex', op: 'flush' }}); }); } RED.nodes.registerType("poloniexConnector", PoloniexConnector); } /* var autobahn = require('autobahn'); var request = require('request'); var wsuri = "wss://api.poloniex.com"; var debug_symbols = [ 'BLK', 'BTC', 'BTCD', 'DASH', 'DOGE', 'ETH', 'EXP', 'LTC', 'XPR' ]; module.exports = function(RED) { function poloniexConnector(config) { /* request('https://poloniex.com/public?command=returnCurrencies',function (error, response, body) { //console.log('error:', error); // Print the error if one occurred //console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received //console.log('body:', body); // Print the HTML for the Google homepage. if(response && response.statusCode && response.statusCode == 200) { var all_poloniex_currencies = JSON.parse(body); var active_poloniex_count = 0; var disabled_poloniex_count = 0; var active_poloniex_currencies = []; var active_poloniex_pairs = []; for(symbol in all_poloniex_currencies) { var tmp = all_poloniex_currencies[symbol]; if(!tmp.disabled && !tmp.delisted && !tmp.frozen) { active_poloniex_count++; active_poloniex_currencies.push(symbol); } else { disabled_poloniex_count++; } } active_poloniex_currencies = debug_symbols; //console.log('Symbols '); console.log(active_poloniex_currencies); console.log('Active ' + active_poloniex_count); console.log('Disabled ' + disabled_poloniex_count); for(x in active_poloniex_currencies) { var sym_from = active_poloniex_currencies[x]; for(y in active_poloniex_currencies) { var sym_to = active_poloniex_currencies[y]; if(sym_from != sym_to) { active_poloniex_pairs.push(sym_from + '_' + sym_to); } } } console.log(JSON.stringify(active_poloniex_pairs)); var connection = new autobahn.Connection({ url: wsuri,realm: "realm1" }); connection.onopen = function (session) { console.log("Websocket connection opened"); function marketEvent (args,kwargs) { console.log(args); console.log(kwargs); } function tickerEvent (args,kwargs) { console.log(args); console.log(kwargs); } function trollboxEvent (args,kwargs) { console.log(args); console.log(kwargs); } for(x in active_poloniex_pairs) { var pair = active_poloniex_pairs[x]; console.log('Subscribing to pair ' + pair); session.subscribe(pair, marketEvent); } // //session.subscribe('BTC_XPR', marketEvent); //session.subscribe('BTC_ETH', marketEvent); session.subscribe('ticker', tickerEvent); session.subscribe('trollbox', trollboxEvent); } connection.onclose = function () { console.log("Websocket connection closed"); } connection.open(); } else { console.log('Error receiving symbol data from Polienex'); } }); RED.nodes.createNode(this,config); var node = this; // node.on('input', function(msg) { console.log(msg); }); } RED.nodes.registerType("poloniexConnector", poloniexConnector); } */