crypto-nodes
Version:
136 lines (99 loc) • 3.4 kB
JavaScript
var moment = require('moment');
var ccxt = require('ccxt');
var redis = require("redis")
, publisher = redis.createClient('6379', process.env.REDIS_SERVER || 'node1.builder-apps.org', { password: process.env.REDIS_PASSWORD });
var ccxt_balances = {
exchanges: {},
set_exchange(exchange_id, config) {
var opts = {
enableRateLimit: false,
urls: {}
};
// console.log(config);
// Set USER / PASS CONFIG
if(config.key) {
opts.apiKey = config.key;
}
if(config.secret) {
opts.secret = config.secret;
}
if(config.password) {
opts.password = config.password;
}
var tmp = new ccxt[exchange_id] (opts);
// Set TESTNET CONFIG
// console.log(exchange_id);
if(config.testnet && tmp.urls.test) {
opts.urls.api = tmp.urls.test;
}
if(exchange_id == 'gemini' && config.testnet) {
opts.urls.api = 'https://api.sandbox.gemini.com';
}
//console.log(opts);
this.exchanges[exchange_id] = new ccxt[exchange_id] (opts);
},
async get_balances(exchanges, callback) {
var out = {};
if(exchanges == 'all') {
exchanges = Object.keys(this.exchanges);
}
for(x in exchanges) {
if(!this.exchanges[exchanges[x]]) {
callback(true, 'Misconfigured Exchange :: ' + exchanges[x]);
}
}
for(x in exchanges) {
var ex = exchanges[x];
var cctx_obj = this.exchanges[ex];
let balances = await cctx_obj.fetchBalance().catch(function (e) {
out[ex] = { ERR: { balance: 0, err: e }};
console.log(e);
});
if(!out[ex] && balances && balances.free) {
out[ex] = {};
for(x in balances.free) {
out[ex][x] = { balance: balances.free[x] };
}
}
}
//console.log(out);
callback(false, out);
publisher.publish("crypto_exchanges_balances", JSON.stringify(out));
//node.send({ payload: { exchange: exchange_id, op: 'get_balance', balances: out } });
/*
for(x in exchanges) {
out[x] = {};
}
callback(out);
*/
},
}
module.exports = function(RED) {
function symbolConfig(config) {
RED.nodes.createNode(this,config);
var node = this;
// Try parsing conf
var symbols = [];
var conf = { flipped: [], exchanges: {} };
if(config.symbols && config.symbols != '') {
symbols = config.symbols.split("\n");
}
if(config.extra && config.extra != '') {
try {
conf = JSON.parse(config.extra);
} catch(e) {
//
}
}
// Timer to fire automatically on INIT
setTimeout(function() {
// Passthru config to all connected nodes on the next tick
node.send([{ payload: { op: 'subscribe', symbols: symbols, config: conf } }, null]);
node.status({ fill: 'green',shape:'ring', text: 'Send subscribe operation for ' + symbols.length + ' symbols.'});
//console.log('Psss..');
}, 1000);
node.on('input', function(msg) {
});
}
RED.nodes.registerType("symbolConfig", symbolConfig);
}