Skip to content

Commit

Permalink
Improve error checking in coin functions
Browse files Browse the repository at this point in the history
  • Loading branch information
adaxi committed Feb 18, 2018
1 parent 43ed584 commit f873852
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 81 deletions.
50 changes: 23 additions & 27 deletions lib/coins/aeon.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ const debug = require('debug')('coinFuncs');

let hexChars = new RegExp("[0-9a-f]+");

function handleRpcError(err, body, callback) {
console.error(JSON.stringify(body));
callback(new Error(`${err} RPC: ${JSON.stringify(body)}`))
}

function Coin(data){
this.bestExchange = global.config.payout.bestExchange;
this.data = data;
Expand All @@ -28,37 +33,28 @@ function Coin(data){

this.niceHashDiff = 400000;

this.getBlockHeaderByID = function(blockId, callback){
global.support.rpcDaemon('getblockheaderbyheight', {"height": blockId}, function (body) {
if (body.hasOwnProperty('result')){
return callback(null, body.result.block_header);
} else {
console.error(JSON.stringify(body));
return callback(true, body);
}
});
this.getBlockHeaderByHeight = (height, callback) => { // unused
global.support.rpcDaemon('getblockheaderbyheight', { height }, (body) =>
(body && body,result && body.result.status === 'OK' && body.result.block_header)
? callback(null, body.result.block_header)
: handleRpcError(`RPC 'getblockheaderbyheight' to aeon daemon failed to return the block header.`, body, callback)
);
};

this.getBlockHeaderByHash = function(blockHash, callback){
global.support.rpcDaemon('getblockheaderbyhash', {"hash": blockHash}, function (body) {
if (typeof(body) !== 'undefined' && body.hasOwnProperty('result')){
return callback(null, body.result.block_header);
} else {
console.error(JSON.stringify(body));
return callback(true, body);
}
});
this.getBlockHeaderByHash = (hash, callback) => {
global.support.rpcDaemon('getblockheaderbyhash', { hash }, (body) =>
(body && body,result && body.result.status === 'OK' && body.result.block_header)
? callback(null, body.result.block_header)
: handleRpcError(`RPC 'getblockheaderbyhash' to aeon daemon failed to return the block header.`, body, callback)
);
};

this.getLastBlockHeader = function(callback){
global.support.rpcDaemon('getlastblockheader', [], function (body) {
if (typeof(body) !== 'undefined' && body.hasOwnProperty('result')){
return callback(null, body.result.block_header);
} else {
console.error(JSON.stringify(body));
return callback(true, body);
}
});
this.getLastBlockHeader = (callback) => {
global.support.rpcDaemon('getlastblockheader', {}, (body) =>
(body && body.result && body.result.status === 'OK' && body.result.block_header)
? callback(null, body.result.block_header)
: handleRpcError(`RPC 'getlastblockheader' to aeon daemon failed to return the block header.`, body, callback)
);
};

this.getBlockTemplate = function(walletAddress, callback){
Expand Down
50 changes: 23 additions & 27 deletions lib/coins/krb.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ const debug = require('debug')('coinFuncs');

let hexChars = new RegExp("[0-9a-f]+");

This comment has been minimized.

Copy link
@irfanersu

irfanersu Dec 20, 2018

Yu hi

function handleRpcError(err, body, callback) {
console.error(JSON.stringify(body));
callback(new Error(`${err} RPC: ${JSON.stringify(body)}`))
}

function Coin(data){
this.bestExchange = global.config.payout.bestExchange;
this.data = data;
Expand All @@ -28,37 +33,28 @@ function Coin(data){

this.niceHashDiff = 200000;

this.getBlockHeaderByID = function(blockId, callback){
global.support.rpcDaemon('getblockheaderbyheight', {"height": blockId}, function (body) {
if (body.hasOwnProperty('result')){
return callback(null, body.result.block_header);
} else {
console.error(JSON.stringify(body));
return callback(true, body);
}
});
this.getBlockHeaderByHeight = (height, callback) => { // unused
global.support.rpcDaemon('getblockheaderbyheight', { height }, (body) =>
(body && body,result && body.result.status === 'OK' && body.result.block_header)
? callback(null, body.result.block_header)
: handleRpcError(`RPC 'getblockheaderbyheight' to krb daemon failed to return the block header.`, body, callback)
);
};

this.getBlockHeaderByHash = function(blockHash, callback){
global.support.rpcDaemon('getblockheaderbyhash', {"hash": blockHash}, function (body) {
if (typeof(body) !== 'undefined' && body.hasOwnProperty('result')){
return callback(null, body.result.block_header);
} else {
console.error(JSON.stringify(body));
return callback(true, body);
}
});
this.getBlockHeaderByHash = (hash, callback) => {
global.support.rpcDaemon('getblockheaderbyhash', { hash }, (body) =>
(body && body,result && body.result.status === 'OK' && body.result.block_header)
? callback(null, body.result.block_header)
: handleRpcError(`RPC 'getblockheaderbyhash' to krb daemon failed to return the block header.`, body, callback)
);
};

this.getLastBlockHeader = function(callback){
global.support.rpcDaemon('getlastblockheader', [], function (body) {
if (typeof(body) !== 'undefined' && body.hasOwnProperty('result')){
return callback(null, body.result.block_header);
} else {
console.error(JSON.stringify(body));
return callback(true, body);
}
});
this.getLastBlockHeader = (callback) => {
global.support.rpcDaemon('getlastblockheader', {}, (body) =>
(body && body.result && body.result.status === 'OK' && body.result.block_header)
? callback(null, body.result.block_header)
: handleRpcError(`RPC 'getlastblockheader' to krb daemon failed to return the block header.`, body, callback)
);
};

this.getBlockTemplate = function(walletAddress, callback){
Expand Down
50 changes: 23 additions & 27 deletions lib/coins/xmr.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ const debug = require('debug')('coinFuncs');

let hexChars = new RegExp("[0-9a-f]+");

function handleRpcError(err, body, callback) {
console.error(JSON.stringify(body));
callback(new Error(`${err} RPC: ${JSON.stringify(body)}`))
}

function Coin(data){
this.bestExchange = global.config.payout.bestExchange;
this.data = data;
Expand Down Expand Up @@ -42,37 +47,28 @@ function Coin(data){

this.niceHashDiff = 400000;

this.getBlockHeaderByID = function(blockId, callback){
global.support.rpcDaemon('getblockheaderbyheight', {"height": blockId}, function (body) {
if (body.hasOwnProperty('result')){
return callback(null, body.result.block_header);
} else {
console.error(JSON.stringify(body));
return callback(true, body);
}
});
this.getBlockHeaderByHeight = (height, callback) => { // unused
global.support.rpcDaemon('getblockheaderbyheight', { height }, (body) =>
(body && body,result && body.result.status === 'OK' && body.result.block_header)
? callback(null, body.result.block_header)
: handleRpcError(`RPC 'getblockheaderbyheight' to monero daemon failed to return the block header.`, body, callback)
);
};

this.getBlockHeaderByHash = function(blockHash, callback){
global.support.rpcDaemon('getblockheaderbyhash', {"hash": blockHash}, function (body) {
if (typeof(body) !== 'undefined' && body.hasOwnProperty('result')){
return callback(null, body.result.block_header);
} else {
console.error(JSON.stringify(body));
return callback(true, body);
}
});
this.getBlockHeaderByHash = (hash, callback) => {
global.support.rpcDaemon('getblockheaderbyhash', { hash }, (body) =>
(body && body,result && body.result.status === 'OK' && body.result.block_header)
? callback(null, body.result.block_header)
: handleRpcError(`RPC 'getblockheaderbyhash' to monero daemon failed to return the block header.`, body, callback)
);
};

this.getLastBlockHeader = function(callback){
global.support.rpcDaemon('getlastblockheader', [], function (body) {
if (typeof(body) !== 'undefined' && body.hasOwnProperty('result')){
return callback(null, body.result.block_header);
} else {
console.error(JSON.stringify(body));
return callback(true, body);
}
});
this.getLastBlockHeader = (callback) => {
global.support.rpcDaemon('getlastblockheader', {}, (body) =>
(body && body.result && body.result.status === 'OK' && body.result.block_header)
? callback(null, body.result.block_header)
: handleRpcError(`RPC 'getlastblockheader' to monero daemon failed to return the block header.`, body, callback)
);
};

this.getBlockTemplate = function(walletAddress, callback){
Expand Down

0 comments on commit f873852

Please sign in to comment.