Refactored some of the rpc code for readability

This commit is contained in:
joeuhren
2020-11-22 16:36:22 -07:00
parent b3e9619c03
commit ad7f2f935d
+47 -72
View File
@@ -20,8 +20,16 @@ function coinbase_supply(cb) {
});
}
module.exports = {
function rpcCommand(params, cb) {
client.cmd([{method: params[0].method, params: params[0].parameters}], function(err, response){
if (err)
return cb('There was an error. Check your console.');
else
return cb(response);
});
}
module.exports = {
convert_to_satoshi: function(amount, cb) {
// fix to 8dp & convert to string
var fixed = amount.toFixed(8).toString();
@@ -33,34 +41,29 @@ module.exports = {
if (settings.index.show_hashrate == false) return cb('-');
if (settings.use_rpc) {
if (settings.nethash == 'netmhashps') {
client.cmd([{method:'getmininginfo', params: []}], function(err, response) {
if (err)
return cb('There was an error. Check your console.');
else {
if (response.netmhashps) {
if (settings.nethash_units == 'K') {
return cb((response.netmhashps * 1000).toFixed(4));
} else if (settings.nethash_units == 'G') {
return cb((response.netmhashps / 1000).toFixed(4));
} else if (settings.nethash_units == 'H') {
return cb((response.netmhashps * 1000000).toFixed(4));
} else if (settings.nethash_units == 'T') {
return cb((response.netmhashps / 1000000).toFixed(4));
} else if (settings.nethash_units == 'P') {
return cb((response.netmhashps / 1000000000).toFixed(4));
} else {
return cb(response.netmhashps.toFixed(4));
}
rpcCommand([{method:'getmininginfo', parameters: []}], function(response){
if (response == 'There was an error. Check your console.') { return cb(response);}
if (response.netmhashps) {
if (settings.nethash_units == 'K') {
return cb((response.netmhashps * 1000).toFixed(4));
} else if (settings.nethash_units == 'G') {
return cb((response.netmhashps / 1000).toFixed(4));
} else if (settings.nethash_units == 'H') {
return cb((response.netmhashps * 1000000).toFixed(4));
} else if (settings.nethash_units == 'T') {
return cb((response.netmhashps / 1000000).toFixed(4));
} else if (settings.nethash_units == 'P') {
return cb((response.netmhashps / 1000000000).toFixed(4));
} else {
return cb('-');
return cb(response.netmhashps.toFixed(4));
}
} else {
return cb('-');
}
});
} else {
client.cmd([{method:'getnetworkhashps', params: []}], function(err, response){
if (err)
return cb('There was an error. Check your console.');
else {
rpcCommand([{method:'getnetworkhashps', parameters: []}], function(response){
if (response == 'There was an error. Check your console.') { return cb(response);}
if (response) {
if (settings.nethash_units == 'K') {
return cb((response * 1000).toFixed(4));
@@ -78,7 +81,6 @@ module.exports = {
} else {
return cb('-');
}
}
});
}
}else{
@@ -130,11 +132,8 @@ module.exports = {
get_difficulty: function(cb) {
if (settings.use_rpc) {
client.cmd([{method:'getdifficulty', params: []}], function(err, response){
if (err)
return cb('There was an error. Check your console.');
else
return cb(response);
rpcCommand([{method:'getdifficulty', parameters: []}], function(response){
return cb(response);
});
} else {
var uri = base_url + 'getdifficulty';
@@ -146,11 +145,8 @@ module.exports = {
get_connectioncount: function(cb) {
if (settings.use_rpc) {
client.cmd([{method:'getconnectioncount', params: []}], function(err, response){
if (err)
return cb('There was an error. Check your console.');
else
return cb(response);
rpcCommand([{method:'getconnectioncount', parameters: []}], function(response){
return cb(response);
});
} else {
var uri = base_url + 'getconnectioncount';
@@ -173,11 +169,8 @@ module.exports = {
else
params.push(split[i]);
}
client.cmd([{method:method_name, params: params}], function(err, response){
if (err)
return cb('There was an error. Check your console.');
else
return cb(response);
rpcCommand([{method:method_name, parameters: params}], function(response){
return cb(response);
});
} else {
var uri = base_url + 'getmasternodecount';
@@ -191,11 +184,8 @@ module.exports = {
get_blockcount: function(cb) {
if (settings.use_rpc) {
client.cmd([{method:'getblockcount', params: []}], function(err, response){
if (err)
return cb('There was an error. Check your console.');
else
return cb(response);
rpcCommand([{method:'getblockcount', parameters: []}], function(response){
return cb(response);
});
} else {
var uri = base_url + 'getblockcount';
@@ -207,27 +197,21 @@ module.exports = {
get_blockhash: function(height, cb) {
if (settings.use_rpc) {
rpcCommand([{method:'getblockhash', parameters: [parseInt(height)]}], function(response){
return cb(response);
});
} else {
var uri = base_url + 'getblockhash?height=' + height;
request({uri: uri, json: true, headers: {'User-Agent': 'eiquidus'}}, function (error, response, body) {
return cb(body);
});
} else {
client.cmd([{method:'getblockhash', params: [parseInt(height)]}], function(err, response){
if (err)
return cb('There was an error. Check your console.');
else
return cb(response);
});
}
},
get_block: function(hash, cb) {
if (settings.use_rpc) {
client.cmd([{method:'getblock', params: [hash]}], function(err, response){
if (err)
return cb('There was an error. Check your console.');
else
return cb(response);
rpcCommand([{method:'getblock', parameters: [hash]}], function(response){
return cb(response);
});
} else {
var uri = base_url + 'getblock?hash=' + hash;
@@ -239,11 +223,8 @@ module.exports = {
get_rawtransaction: function(hash, cb) {
if (settings.use_rpc) {
client.cmd([{method:'getrawtransaction', params: [hash, 1]}], function(err, response){
if (err)
return cb('There was an error. Check your console.');
else
return cb(response);
rpcCommand([{method:'getrawtransaction', parameters: [hash, 1]}], function(response){
return cb(response);
});
} else {
var uri = base_url + 'getrawtransaction?txid=' + hash + '&decrypt=1';
@@ -255,11 +236,8 @@ module.exports = {
get_maxmoney: function(cb) {
if (settings.use_rpc) {
client.cmd([{method:'getmaxmoney', params: []}], function(err, response){
if (err)
return cb('There was an error. Check your console.');
else
return cb(response);
rpcCommand([{method:'getmaxmoney', parameters: []}], function(response){
return cb(response);
});
} else {
var uri = base_url + 'getmaxmoney';
@@ -271,11 +249,8 @@ module.exports = {
get_maxvote: function(cb) {
if (settings.use_rpc) {
client.cmd([{method:'getmaxvote', params: []}], function(err, response){
if (err)
return cb('There was an error. Check your console.');
else
return cb(response);
rpcCommand([{method:'getmaxvote', parameters: []}], function(response){
return cb(response);
});
} else {
var uri = base_url + 'getmaxvote';