Browse Source

switched foreach with async, parse sessionkey param from url

all-modes
pbca26 8 years ago
parent
commit
a269897610
  1. 241
      routes/shepherd.js

241
routes/shepherd.js

@ -12,7 +12,8 @@ const electron = require('electron'),
md5 = require('md5'), md5 = require('md5'),
pm2 = require('pm2'), pm2 = require('pm2'),
readLastLines = require('read-last-lines'), readLastLines = require('read-last-lines'),
request = require('request'); request = require('request'),
async = require('async');
Promise = require('bluebird'); Promise = require('bluebird');
@ -86,135 +87,137 @@ shepherd.get('/appconf', function(req, res, next) {
res.send(obj); res.send(obj);
}); });
shepherd.post('/allcoins', function(req, res, next) { var allcoinsInProgress = false;
var sessionKey = req.body.sessionKey,
_obj = { shepherd.get('/allcoins', function(req, res, next) {
'msg': 'error', if (!allcoinsInProgress) {
'result': 'error' allcoinsInProgress = true;
},
outObj = { var sessionKey = req.query.userpass,
basilisk: {} _obj = {
}, 'msg': 'error',
pubkey, 'result': 'error'
writeCache = function() { },
fs.writeFile(iguanaDir + '/cache-' + pubkey + '.json', JSON.stringify(outObj), function(err) { outObj = {
if (err) { basilisk: {}
return console.log(err); },
} pubkey,
writeCache = function() {
fs.writeFile(iguanaDir + '/cache-' + pubkey + '.json', JSON.stringify(outObj), function(err) {
if (err) {
return console.log(err);
}
console.log('file ' + iguanaDir + '/cache-' + pubkey + '.json is updated'); console.log('file ' + iguanaDir + '/cache-' + pubkey + '.json is updated');
}); });
}; };
res.end(JSON.stringify({ res.end(JSON.stringify({
'msg': 'success', 'msg': 'success',
'result': 'call is initiated' 'result': 'call is initiated'
})); }));
console.time('allcoins'); /*console.time('allcoins');
request({ request({
url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/SuperNET/activehandle?userpass=' + sessionKey, url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/SuperNET/activehandle?userpass=' + sessionKey,
method: 'GET' method: 'GET'
}, function (error, response, body) { }, function (error, response, body) {
if (response.statusCode && response.statusCode === 200) { if (response.statusCode && response.statusCode === 200) {
pubkey = JSON.parse(body).pubkey; pubkey = JSON.parse(body).pubkey;
request({ request({
url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/InstantDEX/allcoins?userpass=' + sessionKey, url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/InstantDEX/allcoins?userpass=' + sessionKey,
method: 'GET' method: 'GET'
}, function (error, response, body) { }, function (error, response, body) {
if (response.statusCode && response.statusCode === 200) { if (response.statusCode && response.statusCode === 200) {
body = JSON.parse(body); var callStack = [];
// basilisk coins body = JSON.parse(body);
if (body.basilisk.length) { // basilisk coins
// get coin addresses if (body.basilisk.length) {
body.basilisk.forEach(function(coin) { // get coin addresses
outObj.basilisk[coin] = {}; async.each(body.basilisk, function(coin) {
writeCache(); outObj.basilisk[coin] = {};
writeCache();
request({
url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/bitcoinrpc/getaddressesbyaccount?userpass=' + sessionKey + '&coin=' + coin + '&account=*', request({
method: 'GET' url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/bitcoinrpc/getaddressesbyaccount?userpass=' + sessionKey + '&coin=' + coin + '&account=*',
}, function (error, response, body) { method: 'GET'
if (response.statusCode && response.statusCode === 200) { }, function (error, response, body) {
outObj.basilisk[coin].addresses = JSON.parse(body).result; if (response.statusCode && response.statusCode === 200) {
writeCache(); outObj.basilisk[coin].addresses = JSON.parse(body).result;
outObj.basilisk[coin].addresses.forEach(function(address) {
console.log('KMD address ' + address);
outObj.basilisk[coin][address] = {};
writeCache(); writeCache();
// get listunspent async.each(outObj.basilisk[coin].addresses, function(address) {
request({ console.log('KMD address ' + address);
url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listunspent?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, outObj.basilisk[coin][address] = {};
method: 'GET' writeCache();
}, function (error, response, body) {
if (response.statusCode && response.statusCode === 200) { // get listunspent
outObj.basilisk[coin][address].listunspent = JSON.parse(body); console.log('http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listunspent' + (coin !== 'BTC' ? '2' : '') + '?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address);
console.log(body); request({
url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listunspent' + (coin !== 'BTC' ? '2' : '') + '?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address,
writeCache(); method: 'GET'
} }, function (error, response, body) {
}); if (response.statusCode && response.statusCode === 200) {
outObj.basilisk[coin][address].listunspent = JSON.parse(body);
// get listtransactions console.log(body);
request({
url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listtransactions?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address, writeCache();
method: 'GET' }
}, function (error, response, body) { });
if (response.statusCode && response.statusCode === 200) {
outObj.basilisk[coin][address].listtransactions = JSON.parse(body); // get listtransactions
console.log(body); request({
url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listtransactions' + (coin !== 'BTC' ? '2' : '') + '?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address,
writeCache(); method: 'GET'
} }, function (error, response, body) {
}); if (response.statusCode && response.statusCode === 200) {
outObj.basilisk[coin][address].listtransactions = JSON.parse(body);
// get listtransactions2 console.log(body);
request({
url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listtransactions2?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address, writeCache();
method: 'GET' }
}, function (error, response, body) { });
if (response.statusCode && response.statusCode === 200) {
outObj.basilisk[coin][address].listtransactions2 = JSON.parse(body); if (coin !== 'BTC') {
console.log(body); // get refresh
request({
writeCache(); url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address,
method: 'GET'
}, function (error, response, body) {
if (response.statusCode && response.statusCode === 200) {
outObj.basilisk[coin][address].refresh = JSON.parse(body);
console.log(body);
writeCache();
}
});
} }
}); });
// get refresh console.timeEnd('allcoins'); // !not an actual time taken by the call!
request({ } else {
url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address, // TODO: error
method: 'GET' }
}, function (error, response, body) { });
if (response.statusCode && response.statusCode === 200) {
outObj.basilisk[coin][address].refresh = JSON.parse(body);
console.log(body);
writeCache();
}
});
});
console.timeEnd('allcoins'); // !not an actual time taken by the call!
} else {
// TODO: error
}
}); });
}); } else {
// TODO: error
}
} else { } else {
// TODO: error // TODO: error
} }
} else { });
// TODO: error } else {
} // TODO: error
}); }
} else { });*/
// TODO: error } else {
} res.end(JSON.stringify({
}); 'msg': 'error',
'result': 'another call is in progress already'
}));
}
}); });
shepherd.post('/debuglog', function(req, res) { shepherd.post('/debuglog', function(req, res) {

Loading…
Cancel
Save