var request = require ( 'request-promise' ) ;
var common = require ( '../../common' ) ;
var options = { } ;
exports . getGraphInfo = ( req , res , next ) => {
options = common . getOptions ( ) ;
options . url = common . getSelLNServerUrl ( ) + '/v1/graph/info' ;
request ( options ) . then ( ( body ) => {
const body _str = ( ! body ) ? '' : JSON . stringify ( body ) ;
const search _idx = ( ! body ) ? - 1 : body _str . search ( 'Not Found' ) ;
if ( ! body || search _idx > - 1 || body . error ) {
logger . error ( { fileName : 'GraphInfo' , lineNum : 12 , msg : 'Fetch Network Info Error: ' + ( ( ! body || ! body . error ) ? 'Error From Server!' : JSON . stringify ( body . error ) ) } ) ;
res . status ( 500 ) . json ( {
message : "Fetching network Info failed!" ,
error : ( ! body || search _idx > - 1 ) ? 'Error From Server!' : body . error
} ) ;
} else {
body . btc _total _network _capacity = ( ! body . total _network _capacity ) ? 0 : common . convertToBTC ( body . total _network _capacity ) ;
body . btc _avg _channel _size = ( ! body . avg _channel _size ) ? 0 : common . convertToBTC ( body . avg _channel _size ) ;
body . btc _min _channel _size = ( ! body . min _channel _size ) ? 0 : common . convertToBTC ( body . min _channel _size ) ;
body . btc _max _channel _size = ( ! body . max _channel _size ) ? 0 : common . convertToBTC ( body . max _channel _size ) ;
res . status ( 200 ) . json ( body ) ;
}
} )
. catch ( errRes => {
let err = JSON . parse ( JSON . stringify ( errRes ) ) ;
if ( err . options && err . options . headers && err . options . headers [ 'Grpc-Metadata-macaroon' ] ) {
delete err . options . headers [ 'Grpc-Metadata-macaroon' ] ;
}
if ( err . response && err . response . request && err . response . request . headers && err . response . request . headers [ 'Grpc-Metadata-macaroon' ] ) {
delete err . response . request . headers [ 'Grpc-Metadata-macaroon' ] ;
}
logger . error ( { fileName : 'GraphInfo' , lineNum : 32 , msg : 'Fetch Network Info Error: ' + JSON . stringify ( err ) } ) ;
return res . status ( 500 ) . json ( {
message : "Fetching Network Info Failed!" ,
error : err . error
} ) ;
} ) ;
} ;