2020-03-22 14:17:56 -06:00
var request = require ( 'postman-request' )
2019-05-27 10:33:22 -07:00
, settings = require ( './settings' )
, Address = require ( '../models/address' ) ;
var base _server = 'http://127.0.0.1:' + settings . port + "/" ;
var base _url = base _server + 'api/' ;
2020-11-21 20:30:20 -07:00
const onode = require ( './node' ) ;
const client = new onode . Client ( settings . wallet ) ;
2020-11-20 18:43:30 -07:00
2019-05-27 10:33:22 -07:00
// returns coinbase total sent as current coin supply
function coinbase _supply ( cb ) {
Address . findOne ( { a _id : 'coinbase' } , function ( err , address ) {
if ( address ) {
return cb ( address . sent ) ;
} else {
2020-11-23 20:22:40 -07:00
return cb ( 0 ) ;
2019-05-27 10:33:22 -07:00
}
} ) ;
}
2020-11-22 16:36:22 -07:00
function rpcCommand ( params , cb ) {
2020-12-05 15:09:33 -07:00
client . cmd ( [ { method : params [ 0 ] . method , params : params [ 0 ] . parameters } ] , function ( err , response ) {
2020-11-22 16:36:22 -07:00
if ( err )
return cb ( 'There was an error. Check your console.' ) ;
else
return cb ( response ) ;
} ) ;
}
2019-05-27 10:33:22 -07:00
2020-12-07 21:32:43 -07:00
function prepareRpcCommand ( cmd , addParams ) {
var method _name = '' ;
var params = addParams || [ ] ;
// Check for null/blank string
if ( cmd != null && cmd . trim ( ) != '' ) {
// Split cmd by spaces
var split = cmd . split ( ' ' ) ;
for ( i = 0 ; i < split . length ; i ++ ) {
if ( i == 0 )
method _name = split [ i ] ;
else
params . push ( split [ i ] ) ;
}
}
return { method : method _name , parameters : params } ;
}
function convertHashUnits ( hashes ) {
if ( settings . nethash _units == 'K' ) {
// return units in KH/s
return ( hashes / 1000 ) . toFixed ( 4 ) ;
} else if ( settings . nethash _units == 'M' ) {
// return units in MH/s
return ( hashes / 1000000 ) . toFixed ( 4 ) ;
} else if ( settings . nethash _units == 'G' ) {
// return units in GH/s
return ( hashes / 1000000000 ) . toFixed ( 4 ) ;
} else if ( settings . nethash _units == 'T' ) {
// return units in TH/s
return ( hashes / 1000000000000 ) . toFixed ( 4 ) ;
} else if ( settings . nethash _units == 'P' ) {
// return units in PH/s
return ( hashes / 1000000000000000 ) . toFixed ( 4 ) ;
} else {
// return units in H/s
return hashes . toFixed ( 4 ) ;
}
}
2020-11-22 16:36:22 -07:00
module . exports = {
2019-05-27 10:33:22 -07:00
convert _to _satoshi : function ( amount , cb ) {
// fix to 8dp & convert to string
var fixed = amount . toFixed ( 8 ) . toString ( ) ;
// remove decimal (.) and return integer
return cb ( parseInt ( fixed . replace ( '.' , '' ) ) ) ;
} ,
get _hashrate : function ( cb ) {
2020-12-07 21:32:43 -07:00
// check if hash rate should be hidden
2019-05-27 10:33:22 -07:00
if ( settings . index . show _hashrate == false ) return cb ( '-' ) ;
2020-12-07 21:32:43 -07:00
// check how to acquire network hashrate
if ( settings . nethash == 'netmhashps' ) {
// load getmininginfo rpc call from settings
var cmd = prepareRpcCommand ( settings . api _cmds . getmininginfo ) ;
// check if the rpc cmd is valid
if ( ! ( cmd . method == '' && cmd . parameters . length == 0 ) ) {
// check if getting data from wallet rpc or web api request
if ( settings . use _rpc ) {
// get data from wallet via rpc cmd
rpcCommand ( [ { method : cmd . method , parameters : cmd . parameters } ] , function ( response ) {
// check if an error msg was received from the rpc server
if ( response == 'There was an error. Check your console.' ) return cb ( '-' ) ;
var net _hash = null ;
// check for different implementations of the net has value
if ( response . netmhashps ) {
// value returned in MH/s so convert to H/s
net _hash = ( response . netmhashps * 1000000 ) ;
} else if ( response . networkhashps )
net _hash = response . networkhashps ;
else if ( response . hashespersec )
net _hash = response . hashespersec ;
// check if netmhashps has a value
if ( net _hash ) {
// return hash value with proper units
return cb ( convertHashUnits ( net _hash ) ) ;
2020-11-22 16:06:53 -07:00
} else {
2020-12-07 21:32:43 -07:00
// netmhashps is blank/null
return cb ( '-' ) ;
2020-11-22 16:06:53 -07:00
}
2020-12-07 21:32:43 -07:00
} ) ;
} else {
// get data via internal web api request
var uri = base _url + 'getmininginfo' ;
request ( { uri : uri , json : true , headers : { 'User-Agent' : 'eiquidus' } } , function ( error , response , body ) {
// check if an error msg was received from the web api server
if ( body == 'There was an error. Check your console.' ) {
// return a blank value
return cb ( '-' ) ;
} else {
var net _hash = null ;
// check for different implementations of the net has value
if ( body . netmhashps ) {
// value returned in MH/s so convert to H/s
net _hash = ( body . netmhashps * 1000000 ) ;
} else if ( body . networkhashps )
net _hash = body . networkhashps ;
else if ( body . hashespersec )
net _hash = body . hashespersec ;
// check if there is a net hash value
if ( net _hash ) {
// return hash value with proper units
return cb ( convertHashUnits ( net _hash ) ) ;
2020-11-22 16:06:53 -07:00
} else {
2020-12-07 21:32:43 -07:00
// netmhashps is blank/null
return cb ( '-' ) ;
2020-11-22 16:06:53 -07:00
}
}
2020-12-07 21:32:43 -07:00
} ) ;
}
} else {
// getmininginfo cmd not set
return cb ( '-' ) ;
2020-11-22 16:06:53 -07:00
}
2020-12-07 21:32:43 -07:00
} else if ( settings . nethash == 'getnetworkhashps' ) {
// load getnetworkhashps rpc call from settings
var cmd = prepareRpcCommand ( settings . api _cmds . getnetworkhashps ) ;
// check if the rpc cmd is valid
if ( ! ( cmd . method == '' && cmd . parameters . length == 0 ) ) {
// check if getting data from wallet rpc or web api request
if ( settings . use _rpc ) {
// get data from wallet via rpc cmd
rpcCommand ( [ { method : cmd . method , parameters : cmd . parameters } ] , function ( response ) {
// check if an error msg was received from the rpc server
if ( response == 'There was an error. Check your console.' ) return cb ( '-' ) ;
// check if the response has a value
if ( response ) {
// return hash value with proper units
return cb ( convertHashUnits ( response ) ) ;
2020-11-22 16:06:53 -07:00
} else {
2020-12-07 21:32:43 -07:00
// response is blank/null
return cb ( '-' ) ;
2020-11-22 16:06:53 -07:00
}
2020-12-07 21:32:43 -07:00
} ) ;
} else {
// get data via internal web api request
var uri = base _url + 'getnetworkhashps' ;
request ( { uri : uri , json : true , headers : { 'User-Agent' : 'eiquidus' } } , function ( error , response , body ) {
// check if an error msg was received from the web api server
if ( body == 'There was an error. Check your console.' ) {
// return a blank value
return cb ( '-' ) ;
2020-11-22 16:06:53 -07:00
} else {
2020-12-07 21:32:43 -07:00
// return hash value with proper units
return cb ( convertHashUnits ( body ) ) ;
2020-11-22 16:06:53 -07:00
}
2020-12-07 21:32:43 -07:00
} ) ;
}
} else {
// getnetworkhashps cmd not set
return cb ( '-' ) ;
2020-11-22 16:06:53 -07:00
}
2020-12-07 21:32:43 -07:00
} else {
// Invalid network hashrate setting value
return cb ( '-' ) ;
2019-05-27 10:33:22 -07:00
}
} ,
get _difficulty : function ( cb ) {
2020-12-07 21:32:43 -07:00
var cmd = prepareRpcCommand ( settings . api _cmds . getdifficulty ) ;
if ( ! ( cmd . method == '' && cmd . parameters . length == 0 ) ) {
if ( settings . use _rpc ) {
rpcCommand ( [ { method : cmd . method , parameters : cmd . parameters } ] , function ( response ) {
// check if an error msg was received from the rpc server
if ( response == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( response ) ;
} ) ;
} else {
var uri = base _url + 'getdifficulty' ;
request ( { uri : uri , json : true , headers : { 'User-Agent' : 'eiquidus' } } , function ( error , response , body ) {
// check if an error msg was received from the web api server
if ( body == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( body ) ;
} ) ;
}
2020-11-22 16:06:53 -07:00
} else {
2020-12-07 21:32:43 -07:00
// cmd not in use. return null.
return cb ( null ) ;
2020-11-22 16:06:53 -07:00
}
2019-05-27 10:33:22 -07:00
} ,
get _connectioncount : function ( cb ) {
2020-12-07 21:32:43 -07:00
var cmd = prepareRpcCommand ( settings . api _cmds . getconnectioncount ) ;
if ( ! ( cmd . method == '' && cmd . parameters . length == 0 ) ) {
if ( settings . use _rpc ) {
rpcCommand ( [ { method : cmd . method , parameters : cmd . parameters } ] , function ( response ) {
// check if an error msg was received from the rpc server
if ( response == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( response ) ;
} ) ;
} else {
var uri = base _url + 'getconnectioncount' ;
request ( { uri : uri , json : true , headers : { 'User-Agent' : 'eiquidus' } } , function ( error , response , body ) {
// check if an error msg was received from the web api server
if ( body == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( body ) ;
} ) ;
}
2020-11-22 16:06:53 -07:00
} else {
2020-12-07 21:32:43 -07:00
// cmd not in use. return null.
return cb ( null ) ;
2020-11-22 16:06:53 -07:00
}
2019-05-27 10:33:22 -07:00
} ,
get _masternodecount : function ( cb ) {
2020-12-07 21:32:43 -07:00
var cmd = prepareRpcCommand ( settings . api _cmds . getmasternodecount ) ;
if ( ! ( cmd . method == '' && cmd . parameters . length == 0 ) ) {
2020-11-22 16:06:53 -07:00
if ( settings . use _rpc ) {
2020-12-07 21:32:43 -07:00
rpcCommand ( [ { method : cmd . method , parameters : cmd . parameters } ] , function ( response ) {
// check if an error msg was received from the rpc server
if ( response == 'There was an error. Check your console.' )
return cb ( null ) ;
2020-11-22 16:06:53 -07:00
else
2020-12-07 21:32:43 -07:00
return cb ( response ) ;
2020-11-22 16:06:53 -07:00
} ) ;
} else {
var uri = base _url + 'getmasternodecount' ;
request ( { uri : uri , json : true , headers : { 'User-Agent' : 'eiquidus' } } , function ( error , response , body ) {
2020-12-07 21:32:43 -07:00
// check if an error msg was received from the web api server
if ( body == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( body ) ;
2020-11-22 16:06:53 -07:00
} ) ;
}
2020-12-07 21:32:43 -07:00
} else {
// cmd not in use. return null.
return cb ( null ) ;
}
2019-05-27 10:33:22 -07:00
} ,
get _blockcount : function ( cb ) {
2020-12-07 21:32:43 -07:00
var cmd = prepareRpcCommand ( settings . api _cmds . getblockcount ) ;
if ( ! ( cmd . method == '' && cmd . parameters . length == 0 ) ) {
if ( settings . use _rpc ) {
rpcCommand ( [ { method : cmd . method , parameters : cmd . parameters } ] , function ( response ) {
// check if an error msg was received from the rpc server
if ( response == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( response ) ;
} ) ;
} else {
var uri = base _url + 'getblockcount' ;
request ( { uri : uri , json : true , headers : { 'User-Agent' : 'eiquidus' } } , function ( error , response , body ) {
// check if an error msg was received from the web api server
if ( body == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( body ) ;
} ) ;
}
2020-11-22 16:06:53 -07:00
} else {
2020-12-07 21:32:43 -07:00
// cmd not in use. return null.
return cb ( null ) ;
2020-11-22 16:06:53 -07:00
}
2019-05-27 10:33:22 -07:00
} ,
get _blockhash : function ( height , cb ) {
2020-12-07 21:32:43 -07:00
var cmd = prepareRpcCommand ( settings . api _cmds . getblockhash , ( height ? [ parseInt ( height ) ] : [ ] ) ) ;
if ( ! ( cmd . method == '' && cmd . parameters . length == 0 ) ) {
if ( settings . use _rpc ) {
rpcCommand ( [ { method : cmd . method , parameters : cmd . parameters } ] , function ( response ) {
// check if an error msg was received from the rpc server
if ( response == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( response ) ;
} ) ;
} else {
var uri = base _url + 'getblockhash?height=' + ( height ? height : '' ) ;
request ( { uri : uri , json : true , headers : { 'User-Agent' : 'eiquidus' } } , function ( error , response , body ) {
// check if an error msg was received from the web api server
if ( body == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( body ) ;
} ) ;
}
2020-11-22 16:36:22 -07:00
} else {
2020-12-07 21:32:43 -07:00
// cmd not in use. return null.
return cb ( null ) ;
2020-11-20 18:43:30 -07:00
}
2019-05-27 10:33:22 -07:00
} ,
get _block : function ( hash , cb ) {
2020-12-07 21:32:43 -07:00
var cmd = prepareRpcCommand ( settings . api _cmds . getblock , ( hash ? [ hash ] : [ ] ) ) ;
if ( ! ( cmd . method == '' && cmd . parameters . length == 0 ) ) {
if ( settings . use _rpc ) {
rpcCommand ( [ { method : cmd . method , parameters : cmd . parameters } ] , function ( response ) {
// check if an error msg was received from the rpc server
if ( response == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( response ) ;
} ) ;
} else {
var uri = base _url + 'getblock?hash=' + hash ;
request ( { uri : uri , json : true , headers : { 'User-Agent' : 'eiquidus' } } , function ( error , response , body ) {
// check if an error msg was received from the web api server
if ( body == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( body ) ;
} ) ;
}
2020-11-22 16:06:53 -07:00
} else {
2020-12-07 21:32:43 -07:00
// cmd not in use. return null.
return cb ( null ) ;
2020-11-20 18:43:30 -07:00
}
2019-05-27 10:33:22 -07:00
} ,
get _rawtransaction : function ( hash , cb ) {
2020-12-07 21:32:43 -07:00
var cmd = prepareRpcCommand ( settings . api _cmds . getrawtransaction , ( hash ? [ hash , 1 ] : [ ] ) ) ;
if ( ! ( cmd . method == '' && cmd . parameters . length == 0 ) ) {
if ( settings . use _rpc ) {
rpcCommand ( [ { method : cmd . method , parameters : cmd . parameters } ] , function ( response ) {
// check if an error msg was received from the rpc server
if ( response == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( response ) ;
} ) ;
} else {
var uri = base _url + 'getrawtransaction?txid=' + hash + '&decrypt=1' ;
request ( { uri : uri , json : true , headers : { 'User-Agent' : 'eiquidus' } } , function ( error , response , body ) {
// check if an error msg was received from the web api server
if ( body == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( body ) ;
} ) ;
}
2020-11-22 16:06:53 -07:00
} else {
2020-12-07 21:32:43 -07:00
// cmd not in use. return null.
return cb ( null ) ;
2020-11-20 18:56:25 -07:00
}
2019-05-27 10:33:22 -07:00
} ,
get _maxmoney : function ( cb ) {
2020-12-07 21:32:43 -07:00
var cmd = prepareRpcCommand ( settings . api _cmds . getmaxmoney ) ;
if ( ! ( cmd . method == '' && cmd . parameters . length == 0 ) ) {
if ( settings . use _rpc ) {
rpcCommand ( [ { method : cmd . method , parameters : cmd . parameters } ] , function ( response ) {
// check if an error msg was received from the rpc server
if ( response == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( response ) ;
} ) ;
} else {
var uri = base _url + 'getmaxmoney' ;
request ( { uri : uri , json : true , headers : { 'User-Agent' : 'eiquidus' } } , function ( error , response , body ) {
// check if an error msg was received from the web api server
if ( body == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( body ) ;
} ) ;
}
2020-11-22 16:06:53 -07:00
} else {
2020-12-07 21:32:43 -07:00
// cmd not in use. return null.
return cb ( null ) ;
2020-11-22 16:06:53 -07:00
}
2019-05-27 10:33:22 -07:00
} ,
get _maxvote : function ( cb ) {
2020-12-07 21:32:43 -07:00
var cmd = prepareRpcCommand ( settings . api _cmds . getmaxvote ) ;
if ( ! ( cmd . method == '' && cmd . parameters . length == 0 ) ) {
if ( settings . use _rpc ) {
rpcCommand ( [ { method : cmd . method , parameters : cmd . parameters } ] , function ( response ) {
// check if an error msg was received from the rpc server
if ( response == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( response ) ;
} ) ;
} else {
var uri = base _url + 'getmaxvote' ;
request ( { uri : uri , json : true , headers : { 'User-Agent' : 'eiquidus' } } , function ( error , response , body ) {
// check if an error msg was received from the web api server
if ( body == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( body ) ;
} ) ;
}
2020-11-22 16:06:53 -07:00
} else {
2020-12-07 21:32:43 -07:00
// cmd not in use. return null.
return cb ( null ) ;
2020-11-22 16:06:53 -07:00
}
2019-05-27 10:33:22 -07:00
} ,
get _vote : function ( cb ) {
2020-12-07 21:32:43 -07:00
var cmd = prepareRpcCommand ( settings . api _cmds . getvote ) ;
if ( ! ( cmd . method == '' && cmd . parameters . length == 0 ) ) {
if ( settings . use _rpc ) {
rpcCommand ( [ { method : cmd . method , parameters : cmd . parameters } ] , function ( response ) {
// check if an error msg was received from the rpc server
if ( response == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( response ) ;
} ) ;
} else {
var uri = base _url + 'getvote' ;
request ( { uri : uri , json : true , headers : { 'User-Agent' : 'eiquidus' } } , function ( error , response , body ) {
// check if an error msg was received from the web api server
if ( body == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( body ) ;
} ) ;
}
2020-11-22 16:06:53 -07:00
} else {
2020-12-07 21:32:43 -07:00
// cmd not in use. return null.
return cb ( null ) ;
2020-11-22 16:06:53 -07:00
}
2019-05-27 10:33:22 -07:00
} ,
get _phase : function ( cb ) {
2020-12-07 21:32:43 -07:00
var cmd = prepareRpcCommand ( settings . api _cmds . getphase ) ;
if ( ! ( cmd . method == '' && cmd . parameters . length == 0 ) ) {
if ( settings . use _rpc ) {
rpcCommand ( [ { method : cmd . method , parameters : cmd . parameters } ] , function ( response ) {
// check if an error msg was received from the rpc server
if ( response == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( response ) ;
} ) ;
} else {
var uri = base _url + 'getphase' ;
request ( { uri : uri , json : true , headers : { 'User-Agent' : 'eiquidus' } } , function ( error , response , body ) {
// check if an error msg was received from the web api server
if ( body == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( body ) ;
} ) ;
}
2020-11-22 16:06:53 -07:00
} else {
2020-12-07 21:32:43 -07:00
// cmd not in use. return null.
return cb ( null ) ;
2020-11-22 16:06:53 -07:00
}
2019-05-27 10:33:22 -07:00
} ,
get _reward : function ( cb ) {
2020-12-07 21:32:43 -07:00
var cmd = prepareRpcCommand ( settings . api _cmds . getreward ) ;
if ( ! ( cmd . method == '' && cmd . parameters . length == 0 ) ) {
if ( settings . use _rpc ) {
rpcCommand ( [ { method : cmd . method , parameters : cmd . parameters } ] , function ( response ) {
// check if an error msg was received from the rpc server
if ( response == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( response ) ;
} ) ;
} else {
var uri = base _url + 'getreward' ;
request ( { uri : uri , json : true , headers : { 'User-Agent' : 'eiquidus' } } , function ( error , response , body ) {
// check if an error msg was received from the web api server
if ( body == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( body ) ;
} ) ;
}
2020-11-22 16:06:53 -07:00
} else {
2020-12-07 21:32:43 -07:00
// cmd not in use. return null.
return cb ( null ) ;
2020-11-22 16:06:53 -07:00
}
2019-05-27 10:33:22 -07:00
} ,
get _estnext : function ( cb ) {
2020-12-07 21:32:43 -07:00
var cmd = prepareRpcCommand ( settings . api _cmds . getnextrewardestimate ) ;
if ( ! ( cmd . method == '' && cmd . parameters . length == 0 ) ) {
if ( settings . use _rpc ) {
rpcCommand ( [ { method : cmd . method , parameters : cmd . parameters } ] , function ( response ) {
// check if an error msg was received from the rpc server
if ( response == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( response ) ;
} ) ;
} else {
var uri = base _url + 'getnextrewardestimate' ;
request ( { uri : uri , json : true , headers : { 'User-Agent' : 'eiquidus' } } , function ( error , response , body ) {
// check if an error msg was received from the web api server
if ( body == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( body ) ;
} ) ;
}
2020-11-22 16:06:53 -07:00
} else {
2020-12-07 21:32:43 -07:00
// cmd not in use. return null.
return cb ( null ) ;
2020-11-22 16:06:53 -07:00
}
2019-05-27 10:33:22 -07:00
} ,
get _nextin : function ( cb ) {
2020-12-07 21:32:43 -07:00
var cmd = prepareRpcCommand ( settings . api _cmds . getnextrewardwhenstr ) ;
if ( ! ( cmd . method == '' && cmd . parameters . length == 0 ) ) {
if ( settings . use _rpc ) {
rpcCommand ( [ { method : cmd . method , parameters : cmd . parameters } ] , function ( response ) {
// check if an error msg was received from the rpc server
if ( response == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( response ) ;
} ) ;
} else {
var uri = base _url + 'getnextrewardwhenstr' ;
request ( { uri : uri , json : true , headers : { 'User-Agent' : 'eiquidus' } } , function ( error , response , body ) {
// check if an error msg was received from the web api server
if ( body == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( body ) ;
} ) ;
}
2020-11-22 16:06:53 -07:00
} else {
2020-12-07 21:32:43 -07:00
// cmd not in use. return null.
return cb ( null ) ;
2020-11-22 16:06:53 -07:00
}
2019-05-27 10:33:22 -07:00
} ,
// synchonous loop used to interate through an array,
// avoid use unless absolutely neccessary
syncLoop : function ( iterations , process , exit ) {
var index = 0 ,
done = false ,
shouldExit = false ;
var loop = {
next : function ( ) {
if ( done ) {
if ( shouldExit && exit ) {
exit ( ) ; // Exit if we're done
}
return ; // Stop the loop if we're done
}
// If we're not finished
if ( index < iterations ) {
index ++ ; // Increment our index
if ( index % 100 === 0 ) { //clear stack
setTimeout ( function ( ) {
process ( loop ) ; // Run our process, pass in the loop
} , 1 ) ;
} else {
process ( loop ) ; // Run our process, pass in the loop
}
// Otherwise we're done
} else {
done = true ; // Make sure we say we're done
if ( exit ) exit ( ) ; // Call the callback on exit
}
} ,
iteration : function ( ) {
return index - 1 ; // Return the loop number we're on
} ,
break : function ( end ) {
done = true ; // End the loop
shouldExit = end ; // Passing end as true means we still call the exit callback
}
} ;
loop . next ( ) ;
return loop ;
} ,
balance _supply : function ( cb ) {
Address . find ( { } , 'balance' ) . where ( 'balance' ) . gt ( 0 ) . exec ( function ( err , docs ) {
var count = 0 ;
module . exports . syncLoop ( docs . length , function ( loop ) {
var i = loop . iteration ( ) ;
count = count + docs [ i ] . balance ;
loop . next ( ) ;
} , function ( ) {
return cb ( count ) ;
} ) ;
} ) ;
} ,
get _supply : function ( cb ) {
2020-12-07 21:32:43 -07:00
if ( settings . supply == 'HEAVY' ) {
// attempt to get the supply from the getsupply or similar api cmd that returns the current money supply as a single positive decimal value
var cmd = prepareRpcCommand ( settings . api _cmds . getsupply ) ;
if ( ! ( cmd . method == '' && cmd . parameters . length == 0 ) ) {
if ( settings . use _rpc ) {
rpcCommand ( [ { method : cmd . method , parameters : cmd . parameters } ] , function ( response ) {
// check if an error msg was received from the rpc server
if ( response == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( response ) ;
} ) ;
} else {
var uri = base _url + 'getsupply' ;
request ( { uri : uri , json : true , headers : { 'User-Agent' : 'eiquidus' } } , function ( error , response , body ) {
// check if an error msg was received from the web api server
if ( body == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( body ) ;
} ) ;
}
2020-11-22 16:06:53 -07:00
} else {
2020-12-07 21:32:43 -07:00
// cmd not in use. return null.
return cb ( null ) ;
2020-11-22 16:06:53 -07:00
}
2020-12-07 21:32:43 -07:00
} else if ( settings . supply == 'GETINFO' ) {
// attempt to get the supply from the getinfo or similar api cmd that returns and object containing various state info. Must include a value called "moneysupply" which represents the current running total of coins
var cmd = prepareRpcCommand ( settings . api _cmds . getinfo ) ;
if ( ! ( cmd . method == '' && cmd . parameters . length == 0 ) ) {
if ( settings . use _rpc ) {
rpcCommand ( [ { method : cmd . method , parameters : cmd . parameters } ] , function ( response ) {
// check if an error msg was received from the rpc server
if ( ! response || ! response . moneysupply || response == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( response . moneysupply ) ;
} ) ;
} else {
var uri = base _url + 'getinfo' ;
request ( { uri : uri , json : true , headers : { 'User-Agent' : 'eiquidus' } } , function ( error , response , body ) {
// check if an error msg was received from the web api server
if ( ! body || ! body . moneysupply || body == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( body . moneysupply ) ;
} ) ;
}
2020-11-22 16:06:53 -07:00
} else {
2020-12-07 21:32:43 -07:00
// cmd not in use. return null.
return cb ( null ) ;
2020-11-22 16:06:53 -07:00
}
2020-12-07 21:32:43 -07:00
} else if ( settings . supply == 'BALANCES' ) {
// get the supply by running a query on the addresses collection and summing up all positive balances (potentially a long running query for blockchains with tons of addresses)
module . exports . balance _supply ( function ( supply ) {
return cb ( supply / 100000000 ) ;
} ) ;
} else if ( settings . supply == 'TXOUTSET' ) {
// attempt to get the supply from the gettxoutsetinfo or similar api cmd that returns an object with statistics about the unspent transaction output set. Must include a value called "total_amount" which represents the current running total of coins
var cmd = prepareRpcCommand ( settings . api _cmds . gettxoutsetinfo ) ;
if ( ! ( cmd . method == '' && cmd . parameters . length == 0 ) ) {
if ( settings . use _rpc ) {
rpcCommand ( [ { method : cmd . method , parameters : cmd . parameters } ] , function ( response ) {
// check if an error msg was received from the rpc server
if ( ! response || ! response . total _amount || response == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( response . total _amount ) ;
} ) ;
} else {
var uri = base _url + 'gettxoutsetinfo' ;
request ( { uri : uri , json : true , headers : { 'User-Agent' : 'eiquidus' } } , function ( error , response , body ) {
// check if an error msg was received from the web api server
if ( ! body || ! body . total _amount || body == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( body . total _amount ) ;
} ) ;
}
} else {
// cmd not in use. return null.
return cb ( null ) ;
}
} else {
// returns coinbase total sent as current coin supply
coinbase _supply ( function ( supply ) {
return cb ( supply / 100000000 ) ;
} ) ;
2019-05-27 10:33:22 -07:00
}
} ,
2020-12-21 16:19:14 -07:00
get _peerinfo : function ( cb ) {
var cmd = prepareRpcCommand ( settings . api _cmds . getpeerinfo ) ;
if ( ! ( cmd . method == '' && cmd . parameters . length == 0 ) ) {
if ( settings . use _rpc ) {
rpcCommand ( [ { method : cmd . method , parameters : cmd . parameters } ] , function ( response ) {
// check if an error msg was received from the rpc server
if ( response == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( response ) ;
} ) ;
} else {
var uri = base _url + 'getpeerinfo' ;
request ( { uri : uri , json : true , headers : { 'User-Agent' : 'eiquidus' } } , function ( error , response , body ) {
// check if an error msg was received from the web api server
if ( body == 'There was an error. Check your console.' )
return cb ( null ) ;
else
return cb ( body ) ;
} ) ;
}
} else {
// cmd not in use. return null.
return cb ( null ) ;
}
} ,
get _geo _location : function ( address , cb ) {
request ( { uri : 'https://freegeoip.app/json/' + address , json : true , headers : { 'User-Agent' : 'eiquidus' } } , function ( error , response , geo ) {
return cb ( error , geo ) ;
} ) ;
} ,
2019-05-27 10:33:22 -07:00
is _unique : function ( array , object , cb ) {
var unique = true ;
var index = null ;
module . exports . syncLoop ( array . length , function ( loop ) {
var i = loop . iteration ( ) ;
if ( array [ i ] . addresses == object ) {
unique = false ;
index = i ;
loop . break ( true ) ;
loop . next ( ) ;
} else {
loop . next ( ) ;
}
} , function ( ) {
return cb ( unique , index ) ;
} ) ;
} ,
calculate _total : function ( vout , cb ) {
var total = 0 ;
module . exports . syncLoop ( vout . length , function ( loop ) {
var i = loop . iteration ( ) ;
//module.exports.convert_to_satoshi(parseFloat(vout[i].amount), function(amount_sat){
total = total + vout [ i ] . amount ;
loop . next ( ) ;
//});
} , function ( ) {
return cb ( total ) ;
} ) ;
} ,
2019-10-17 22:06:30 -06:00
prepare _vout : function ( vout , txid , vin , vhidden , cb ) {
2019-05-27 10:33:22 -07:00
var arr _vout = [ ] ;
var arr _vin = [ ] ;
arr _vin = vin ;
module . exports . syncLoop ( vout . length , function ( loop ) {
var i = loop . iteration ( ) ;
// make sure vout has an address
if ( vout [ i ] . scriptPubKey . type != 'nonstandard' && vout [ i ] . scriptPubKey . type != 'nulldata' ) {
// check if tx is public or private (private = if no out address)
if ( vout [ i ] . scriptPubKey . type != 'zerocoinmint' && typeof vout [ i ] . scriptPubKey . addresses != 'undefined' ) {
// check if vout address is unique, if so add it array, if not add its amount to existing index
module . exports . is _unique ( arr _vout , vout [ i ] . scriptPubKey . addresses [ 0 ] , function ( unique , index ) {
if ( unique == true ) {
// unique vout
module . exports . convert _to _satoshi ( parseFloat ( vout [ i ] . value ) , function ( amount _sat ) {
arr _vout . push ( { addresses : vout [ i ] . scriptPubKey . addresses [ 0 ] , amount : amount _sat } ) ;
loop . next ( ) ;
} ) ;
} else {
// already exists
module . exports . convert _to _satoshi ( parseFloat ( vout [ i ] . value ) , function ( amount _sat ) {
arr _vout [ index ] . amount = arr _vout [ index ] . amount + amount _sat ;
loop . next ( ) ;
} ) ;
}
} ) ;
} else {
// private tx
// TODO: save this data to be able to show an anon tx
loop . next ( ) ;
}
} else {
// no address, move to next vout
loop . next ( ) ;
}
} , function ( ) {
2019-10-17 22:06:30 -06:00
// check for hidden/anonymous outputs
vhidden . forEach ( function ( vanon , i ) {
if ( vanon . vpub _old > 0 ) {
module . exports . convert _to _satoshi ( parseFloat ( vanon . vpub _old ) , function ( amount _sat ) {
arr _vout . push ( { addresses : "private_tx" , amount : amount _sat } ) ;
} ) ;
} else {
module . exports . convert _to _satoshi ( parseFloat ( vanon . vpub _new ) , function ( amount _sat ) {
if ( vhidden . length > 0 && ( ! vout || vout . length == 0 ) && ( ! vin || vin . length == 0 ) ) {
2019-10-18 23:04:46 -06:00
// hidden sender is sending to hidden recipient
// the sent and received values are not known in this case. only the fee paid is known and subtracted from the sender.
arr _vout . push ( { addresses : "private_tx" , amount : 0 } ) ;
2019-10-17 22:06:30 -06:00
}
2019-10-18 23:04:46 -06:00
// add a private send address with the known amount sent
2019-10-17 22:06:30 -06:00
arr _vin . push ( { addresses : "private_tx" , amount : amount _sat } ) ;
} ) ;
}
} ) ;
if ( typeof vout [ 0 ] !== 'undefined' && vout [ 0 ] . scriptPubKey . type == 'nonstandard' ) {
2019-05-27 10:33:22 -07:00
if ( arr _vin . length > 0 && arr _vout . length > 0 ) {
if ( arr _vin [ 0 ] . addresses == arr _vout [ 0 ] . addresses ) {
//PoS
arr _vout [ 0 ] . amount = arr _vout [ 0 ] . amount - arr _vin [ 0 ] . amount ;
arr _vin . shift ( ) ;
return cb ( arr _vout , arr _vin ) ;
} else {
return cb ( arr _vout , arr _vin ) ;
}
} else {
return cb ( arr _vout , arr _vin ) ;
}
} else {
return cb ( arr _vout , arr _vin ) ;
}
} ) ;
} ,
get _input _addresses : function ( input , vout , cb ) {
var addresses = [ ] ;
if ( input . coinbase ) {
var amount = 0 ;
module . exports . syncLoop ( vout . length , function ( loop ) {
var i = loop . iteration ( ) ;
amount = amount + parseFloat ( vout [ i ] . value ) ;
loop . next ( ) ;
} , function ( ) {
addresses . push ( { hash : 'coinbase' , amount : amount } ) ;
return cb ( addresses ) ;
} ) ;
} else {
module . exports . get _rawtransaction ( input . txid , function ( tx ) {
if ( tx ) {
module . exports . syncLoop ( tx . vout . length , function ( loop ) {
var i = loop . iteration ( ) ;
if ( tx . vout [ i ] . n == input . vout ) {
//module.exports.convert_to_satoshi(parseFloat(tx.vout[i].value), function(amount_sat){
if ( tx . vout [ i ] . scriptPubKey . addresses ) {
addresses . push ( { hash : tx . vout [ i ] . scriptPubKey . addresses [ 0 ] , amount : tx . vout [ i ] . value } ) ;
}
loop . break ( true ) ;
loop . next ( ) ;
//});
} else {
loop . next ( ) ;
}
} , function ( ) {
return cb ( addresses ) ;
} ) ;
} else {
return cb ( ) ;
}
} ) ;
}
} ,
prepare _vin : function ( tx , cb ) {
var arr _vin = [ ] ;
module . exports . syncLoop ( tx . vin . length , function ( loop ) {
var i = loop . iteration ( ) ;
module . exports . get _input _addresses ( tx . vin [ i ] , tx . vout , function ( addresses ) {
if ( addresses && addresses . length ) {
//console.log('vin');
module . exports . is _unique ( arr _vin , addresses [ 0 ] . hash , function ( unique , index ) {
if ( unique == true ) {
module . exports . convert _to _satoshi ( parseFloat ( addresses [ 0 ] . amount ) , function ( amount _sat ) {
arr _vin . push ( { addresses : addresses [ 0 ] . hash , amount : amount _sat } ) ;
loop . next ( ) ;
} ) ;
} else {
module . exports . convert _to _satoshi ( parseFloat ( addresses [ 0 ] . amount ) , function ( amount _sat ) {
arr _vin [ index ] . amount = arr _vin [ index ] . amount + amount _sat ;
loop . next ( ) ;
} ) ;
}
} ) ;
} else {
loop . next ( ) ;
}
} ) ;
} , function ( ) {
return cb ( arr _vin ) ;
} ) ;
}
} ;