Browse Source

Merge pull request #30 from pbca26/master

shepherd cache api update
all-modes
Satinder Grewal 8 years ago
committed by GitHub
parent
commit
7ee895b0eb
  1. 216
      routes/shepherd.js

216
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');
@ -89,8 +90,53 @@ shepherd.get('/appconf', function(req, res, next) {
res.send(obj); res.send(obj);
}); });
shepherd.post('/allcoins', function(req, res, next) { /*
var sessionKey = req.body.sessionKey, * params: pubkey
*/
shepherd.get('/cache', function(req, res, next) {
var pubkey = req.query.pubkey;
if (pubkey) {
if (fs.existsSync(iguanaDir + '/cache-' + pubkey + '.json')) {
fs.readFile(iguanaDir + '/cache-' + pubkey + '.json', 'utf8', function (err, data) {
if (err) {
var errorObj = {
'msg': 'error',
'result': err
};
res.end(JSON.stringify(errorObj));
} else {
var successObj = {
'msg': 'success',
'result': JSON.parse(data)
};
res.end(JSON.stringify(successObj));
}
});
} else {
var errorObj = {
'msg': 'error',
'result': 'no pubkey provided'
};
res.end(JSON.stringify(errorObj));
}
}
});
var allcoinsInProgress = false;
/*
* params: userpass, pubkey
*/
shepherd.get('/allcoins', function(req, res, next) {
if (!allcoinsInProgress) {
allcoinsInProgress = true;
var sessionKey = req.query.userpass,
pubkey = req.query.pubkey,
_obj = { _obj = {
'msg': 'error', 'msg': 'error',
'result': 'error' 'result': 'error'
@ -98,7 +144,6 @@ shepherd.post('/allcoins', function(req, res, next) {
outObj = { outObj = {
basilisk: {} basilisk: {}
}, },
pubkey,
writeCache = function() { writeCache = function() {
fs.writeFile(iguanaDir + '/cache-' + pubkey + '.json', JSON.stringify(outObj), function(err) { fs.writeFile(iguanaDir + '/cache-' + pubkey + '.json', JSON.stringify(outObj), function(err) {
if (err) { if (err) {
@ -107,6 +152,18 @@ shepherd.post('/allcoins', function(req, res, next) {
console.log('file ' + iguanaDir + '/cache-' + pubkey + '.json is updated'); console.log('file ' + iguanaDir + '/cache-' + pubkey + '.json is updated');
}); });
},
callStack = {},
checkCallStack = function() {
var total = 0;
for (var coin in callStack) {
total =+ callStack[coin];
}
if (total / Object.keys(callStack).length === 1) {
allcoinsInProgress = false;
}
}; };
res.end(JSON.stringify({ res.end(JSON.stringify({
@ -114,24 +171,22 @@ shepherd.post('/allcoins', function(req, res, next) {
'result': 'call is initiated' 'result': 'call is initiated'
})); }));
console.time('allcoins'); console.log('allcoins call started');
request({
url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/SuperNET/activehandle?userpass=' + sessionKey,
method: 'GET'
}, function (error, response, body) {
if (response.statusCode && response.statusCode === 200) {
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 && response.statusCode && response.statusCode === 200) {
body = JSON.parse(body); body = JSON.parse(body);
// basilisk coins // basilisk coins
if (body.basilisk.length) { if (body.basilisk && body.basilisk.length) {
// get coin addresses // get coin addresses
body.basilisk.forEach(function(coin) { async.each(body.basilisk, function(coin) {
callStack[coin] = 1;
});
async.each(body.basilisk, function(coin) {
outObj.basilisk[coin] = {}; outObj.basilisk[coin] = {};
writeCache(); writeCache();
@ -139,69 +194,43 @@ shepherd.post('/allcoins', function(req, res, next) {
url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/bitcoinrpc/getaddressesbyaccount?userpass=' + sessionKey + '&coin=' + coin + '&account=*', url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/bitcoinrpc/getaddressesbyaccount?userpass=' + sessionKey + '&coin=' + coin + '&account=*',
method: 'GET' method: 'GET'
}, function (error, response, body) { }, function (error, response, body) {
if (response.statusCode && response.statusCode === 200) { if (response && response.statusCode && response.statusCode === 200) {
outObj.basilisk[coin].addresses = JSON.parse(body).result; outObj.basilisk[coin].addresses = JSON.parse(body).result;
writeCache(); writeCache();
callStack[coin] = callStack[coin] + outObj.basilisk[coin].addresses.length * (coin === 'BTC' ? 2 : 3);
outObj.basilisk[coin].addresses.forEach(function(address) { console.log(coin + ' stack len ' + callStack[coin]);
async.each(outObj.basilisk[coin].addresses, function(address) {
var dexUrls = {
'listunspent': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listunspent' + (coin !== 'BTC' ? '2' : '') + '?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address,
'listtransactions': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listtransactions' + (coin !== 'BTC' ? '2' : '') + '?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address,
'refresh': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address
};
if (coin === 'BTC') {
delete dexUrls.refresh;
}
console.log('KMD address ' + address); console.log('KMD address ' + address);
outObj.basilisk[coin][address] = {}; outObj.basilisk[coin][address] = {};
writeCache(); writeCache();
// get listunspent async.forEachOf(dexUrls, function(dexUrl, key) {
request({ request({
url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listunspent?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, url: dexUrl,
method: 'GET' method: 'GET'
}, function (error, response, body) { }, function (error, response, body) {
if (response.statusCode && response.statusCode === 200) { if (response && response.statusCode && response.statusCode === 200) {
outObj.basilisk[coin][address].listunspent = JSON.parse(body); outObj.basilisk[coin][address][key] = JSON.parse(body);
console.log(dexUrl);
console.log(body); console.log(body);
callStack[coin]--;
console.log(coin + ' _stack len ' + callStack[coin]);
checkCallStack();
writeCache(); writeCache();
} }
}); });
// get listtransactions
request({
url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listtransactions?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address,
method: 'GET'
}, function (error, response, body) {
if (response.statusCode && response.statusCode === 200) {
outObj.basilisk[coin][address].listtransactions = JSON.parse(body);
console.log(body);
writeCache();
}
}); });
// get listtransactions2
request({
url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listtransactions2?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address,
method: 'GET'
}, function (error, response, body) {
if (response.statusCode && response.statusCode === 200) {
outObj.basilisk[coin][address].listtransactions2 = JSON.parse(body);
console.log(body);
writeCache();
}
}); });
// get refresh
request({
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();
}
});
});
console.timeEnd('allcoins'); // !not an actual time taken by the call!
} else { } else {
// TODO: error // TODO: error
} }
@ -215,7 +244,70 @@ shepherd.post('/allcoins', function(req, res, next) {
} }
}); });
} else { } else {
// TODO: error res.end(JSON.stringify({
'msg': 'error',
'result': 'another call is in progress already'
}));
}
});
/*
* params: userpass, pubkey, coin, address
*/
shepherd.get('/refresh', function(req, res, next) {
var sessionKey = req.query.userpass,
coin = req.query.coin,
address = req.query.address,
pubkey = req.query.pubkey,
errorObj = {
'msg': 'error',
'result': 'error'
},
outObj,
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');
});
};
if (fs.existsSync(iguanaDir + '/cache-' + pubkey + '.json')) {
outObj = JSON.parse(fs.readFileSync(iguanaDir + '/cache-' + pubkey + '.json', 'utf8'));
if (outObj && !outObj.basilisk) {
outObj['basilisk'] = {};
outObj['basilisk'][coin] = {};
} else {
if (!outObj[coin]) {
outObj['basilisk'][coin][address] = {};
}
}
} else {
outObj = {
basilisk: {}
};
}
var refreshUrl = 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address
request({
url: refreshUrl,
method: 'GET'
}, function (error, response, body) {
if (response && response.statusCode && response.statusCode === 200) {
outObj.basilisk[coin][address].refresh = JSON.parse(body);
console.log(refreshUrl);
console.log(body);
writeCache();
res.end(JSON.stringify({
'msg': 'success',
'result': iguanaDir + '/cache-' + pubkey + '.json updated'
}));
} }
}); });
}); });

Loading…
Cancel
Save