UNPKG

crypto-nodes

Version:

181 lines (121 loc) 4.52 kB
var events = require('events'); var ev = { events: new events.EventEmitter(), } const ccxt = require('ccxt'); module.exports = function(RED) { function genericExchange(config) { let subscribeOrderBook = async (id, symbols, node) => { console.log('SUBSCRIBING ' + id + " " + symbols.join(',')); // check if the exchange is supported by ccxt let exchangeFound = ccxt.exchanges.indexOf (id) > -1 if (exchangeFound) { // instantiate the exchange by id let exchange = new ccxt[id] ({ enableRateLimit: false }) // load all markets from the exchange let markets = await exchange.loadMarkets().catch(function (err) { //subscribeOrderBook(id, symbol, node); //return; }); // // output a list of all market symbols // log (id.green, 'has', exchange.symbols.length, 'symbols:', exchange.symbols.join (', ').yellow) for(x in symbols) { var symbol = symbols[x]; if (exchange.markets && symbol in exchange.markets) { async function fnk(symbol, e) { setTimeout(async function () { var e = false; const orderbook = await exchange.fetchOrderBook (symbol, 50).catch(function (err) { node.status({ fill: 'red' ,shape:'ring', text: err.toString() }); e = err; }); //console.log('.'); if(!e) { per_second++; node.send({ payload: { exchange: 'generic_' + id, fees: {},//exchange.fees, sym_from: symbol.split('/')[0], sym_to: symbol.split('/')[1], data: orderbook } }); } fnk(symbol , e); }, e ? 10000 : 1000 + Math.floor(Math.random() * 500)); } fnk(symbol, false); } else { //console.log('Symbol ' + symbol + ' not found') } } } else { console.log('Exchange ' + id + ' not found') } } //console.log(Object.keys(ccxt.exchanges)); var per_second = 0; RED.nodes.createNode(this,config); var node = this; if(config.exchange_id) { /* this.exchange = new ccxt[config.exchange_id]({}); //console.log(this.exchange); this.exchange.loadMarkets(function (err, res) { //console.log(err); //console.log(res); }); //console.log(markets); */ 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: 'generic_' + config.exchange_id, op: 'flush' }}); }); node.on('input', function(msg) { if(msg.payload && msg.payload.op == 'subscribe') { subscribeOrderBook(config.exchange_id, msg.payload.symbols, node); /* for(x in msg.payload.symbols) { var sym = msg.payload.symbols[x]; function fnk(sym,x) { setTimeout(function () { },x); } fnk(sym, x * 10); } */ } }); if(!this.interval) { this.interval = setInterval(function () { var color = per_second > 0 ? 'green' : 'red'; // console.log('BINANCE :: ' + per_second + ' requests per second'); //console.log('BINANCE :: LATEST ' + JSON.stringify(latest)); if(per_second) { node.status({ fill: color,shape:'ring', text: per_second + ' requests per second' }); } per_second = 0; }, 1000); } } } RED.nodes.registerType("genericExchange", genericExchange); RED.httpAdmin.get("/all_crypto_exchanges", function(req,res) { var exchanges = {}; for(x in ccxt.exchanges) { exchanges[x] = new ccxt[ccxt.exchanges[x]]({}); } res.json({ config: exchanges }); }); RED.httpAdmin.get("/crypto_exchange_markets", function(req,res) { if(req.query.exchange) { var exchange = new ccxt[req.query.exchange]({}); res.json({ config: exchange }); } }); }