diff --git a/lib/coins/aeon.js b/lib/coins/aeon.js index c68082dc..462ccd59 100644 --- a/lib/coins/aeon.js +++ b/lib/coins/aeon.js @@ -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; @@ -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){ diff --git a/lib/coins/krb.js b/lib/coins/krb.js index 1494da8f..868bd898 100644 --- a/lib/coins/krb.js +++ b/lib/coins/krb.js @@ -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; @@ -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){ diff --git a/lib/coins/xmr.js b/lib/coins/xmr.js index a4521110..07a8086f 100644 --- a/lib/coins/xmr.js +++ b/lib/coins/xmr.js @@ -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; @@ -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){