Browse Source

added new shepherd cache-one route (wip)

all-modes
pbca26 8 years ago
parent
commit
24cd55a350
  1. 181
      routes/shepherd.js

181
routes/shepherd.js

@ -131,7 +131,7 @@ var allcoinsInProgress = false;
/* /*
* params: userpass, pubkey * params: userpass, pubkey
*/ */
shepherd.get('/allcoins', function(req, res, next) { shepherd.get('/cache-all', function(req, res, next) {
if (!allcoinsInProgress) { if (!allcoinsInProgress) {
allcoinsInProgress = true; allcoinsInProgress = true;
@ -171,7 +171,7 @@ shepherd.get('/allcoins', function(req, res, next) {
'result': 'call is initiated' 'result': 'call is initiated'
})); }));
console.log('allcoins call started'); console.log('cache-all call started');
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,
@ -207,7 +207,7 @@ shepherd.get('/allcoins', function(req, res, next) {
'getbalance': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/getbalance?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, 'getbalance': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/getbalance?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address,
'refresh': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&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' && coin === 'SYS') { if (coin === 'BTC' || coin === 'SYS') {
delete dexUrls.refresh; delete dexUrls.refresh;
delete dexUrls.getbalance; delete dexUrls.getbalance;
} }
@ -257,62 +257,137 @@ shepherd.get('/allcoins', function(req, res, next) {
/* /*
* params: userpass, pubkey, coin, address * params: userpass, pubkey, coin, address
*/ */
shepherd.get('/refresh', function(req, res, next) { shepherd.get('/cache-one', function(req, res, next) {
var sessionKey = req.query.userpass, if (!allcoinsInProgress) {
coin = req.query.coin, // TODO: add check to allow only one cache call/sequence in progress
address = req.query.address, var sessionKey = req.query.userpass,
pubkey = req.query.pubkey, coin = req.query.coin,
errorObj = { address = req.query.address,
'msg': 'error', pubkey = req.query.pubkey,
'result': 'error' callsArray = req.query.calls.split(':'),
}, errorObj = {
outObj, 'msg': 'error',
pubkey, 'result': 'error'
writeCache = function() { },
fs.writeFile(iguanaDir + '/cache-' + pubkey + '.json', JSON.stringify(outObj), function(err) { outObj,
if (err) { pubkey,
return console.log(err); 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(callsArray);
console.log('file ' + iguanaDir + '/cache-' + pubkey + '.json is updated'); res.end(JSON.stringify({
}); 'msg': 'success',
}; 'result': 'call is initiated'
}));
if (fs.existsSync(iguanaDir + '/cache-' + pubkey + '.json')) { console.log('cache-one call started');
outObj = JSON.parse(fs.readFileSync(iguanaDir + '/cache-' + pubkey + '.json', 'utf8'));
if (outObj && !outObj.basilisk) { if (fs.existsSync(iguanaDir + '/cache-' + pubkey + '.json')) {
outObj['basilisk'] = {}; outObj = JSON.parse(fs.readFileSync(iguanaDir + '/cache-' + pubkey + '.json', 'utf8'));
outObj['basilisk'][coin] = {};
} else { if (!outObj || !outObj.basilisk) {
if (!outObj[coin]) { outObj['basilisk'] = {};
outObj['basilisk'][coin][address] = {}; outObj['basilisk'][coin] = {};
} else {
if (!outObj[coin]) {
outObj['basilisk'][coin] = {};
if (address) {
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 // update all available coin addresses
if (!address) {
request({ request({
url: refreshUrl, 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 && response.statusCode && response.statusCode === 200) { if (response && response.statusCode && response.statusCode === 200) {
outObj.basilisk[coin][address].refresh = JSON.parse(body); outObj.basilisk[coin].addresses = JSON.parse(body).result;
console.log(refreshUrl); writeCache();
console.log(body); //callStack[coin] = callStack[coin] + outObj.basilisk[coin].addresses.length * (coin === 'BTC' ? 2 : 3);
//console.log(coin + ' stack len ' + callStack[coin]);
writeCache();
res.end(JSON.stringify({ async.each(outObj.basilisk[coin].addresses, function(address) {
'msg': 'success', var dexUrls = {
'result': iguanaDir + '/cache-' + pubkey + '.json updated' 'listunspent': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listunspent' + (coin !== 'BTC' && coin !== 'SYS' ? '2' : '') + '?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address,
})); 'listtransactions': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listtransactions' + (coin !== 'BTC' && coin !== 'SYS' ? '2' : '') + '?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address,
} 'getbalance': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/getbalance?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address,
}); 'refresh': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address
},
_dexUrls = [];
for (var a = 0; a < callsArray.length; a++) {
_dexUrls.push(dexUrls[callsArray[a]]);
}
if (coin === 'BTC' || coin === 'SYS') {
delete dexUrls.refresh;
delete dexUrls.getbalance;
}
console.log(_dexUrls);
//console.log(JSON.stringify(dexUrls));
console.log(coin + ' address ' + address);
/*outObj.basilisk[coin][address] = {};
writeCache();
async.forEachOf(dexUrls, function(dexUrl, key) {
request({
url: dexUrl,
method: 'GET'
}, function (error, response, body) {
if (response && response.statusCode && response.statusCode === 200) {
outObj.basilisk[coin][address][key] = JSON.parse(body);
console.log(dexUrl);
console.log(body);
callStack[coin]--;
console.log(coin + ' _stack len ' + callStack[coin]);
checkCallStack();
writeCache();
}
});
});*/
});
} else {
// TODO: error
}
});
} else {
/*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'
}));
}
});*/
}
} 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