2021-12-29 23:08:41 +00:00
import request from 'request-promise' ;
import { Logger , LoggerService } from '../../utils/logger.js' ;
import { Common , CommonService } from '../../utils/common.js' ;
let options = null ;
const logger : LoggerService = Logger ;
const common : CommonService = Common ;
export const getRoute = ( req , res , next ) = > {
logger . log ( { selectedNode : req.session.selectedNode , level : 'INFO' , fileName : 'Network' , msg : 'Getting Network Routes..' } ) ;
options = common . getOptions ( req ) ;
if ( options . error ) { return res . status ( options . statusCode ) . json ( { message : options.message , error : options.error } ) ; }
options . url = req . session . selectedNode . ln_server_url + '/v1/network/getRoute/' + req . params . destPubkey + '/' + req . params . amount ;
2023-10-18 02:22:08 +00:00
request . post ( options ) . then ( ( body ) = > {
2022-01-16 20:55:50 +00:00
logger . log ( { selectedNode : req.session.selectedNode , level : 'INFO' , fileName : 'Network' , msg : 'Network Routes Received' , data : body } ) ;
2021-12-29 23:08:41 +00:00
res . status ( 200 ) . json ( { routes : body } ) ;
} ) . catch ( ( errRes ) = > {
const err = common . handleError ( errRes , 'Network' , 'Query Routes Error' , req . session . selectedNode ) ;
return res . status ( err . statusCode ) . json ( { message : err.message , error : err.error } ) ;
} ) ;
} ;
export const listNode = ( req , res , next ) = > {
logger . log ( { selectedNode : req.session.selectedNode , level : 'INFO' , fileName : 'Network' , msg : 'Node Lookup..' } ) ;
options = common . getOptions ( req ) ;
if ( options . error ) { return res . status ( options . statusCode ) . json ( { message : options.message , error : options.error } ) ; }
options . url = req . session . selectedNode . ln_server_url + '/v1/network/listNode/' + req . params . id ;
2023-10-18 02:22:08 +00:00
request . post ( options ) . then ( ( body ) = > {
2022-01-16 20:55:50 +00:00
logger . log ( { selectedNode : req.session.selectedNode , level : 'INFO' , fileName : 'Network' , msg : 'Node Lookup Finished' , data : body } ) ;
2021-12-29 23:08:41 +00:00
res . status ( 200 ) . json ( body ) ;
} ) . catch ( ( errRes ) = > {
const err = common . handleError ( errRes , 'Network' , 'Node Lookup Error' , req . session . selectedNode ) ;
return res . status ( err . statusCode ) . json ( { message : err.message , error : err.error } ) ;
} ) ;
} ;
export const listChannel = ( req , res , next ) = > {
logger . log ( { selectedNode : req.session.selectedNode , level : 'INFO' , fileName : 'Network' , msg : 'Channel Lookup..' } ) ;
options = common . getOptions ( req ) ;
if ( options . error ) { return res . status ( options . statusCode ) . json ( { message : options.message , error : options.error } ) ; }
options . url = req . session . selectedNode . ln_server_url + '/v1/network/listChannel/' + req . params . channelShortId ;
2023-10-18 02:22:08 +00:00
request . post ( options ) . then ( ( body ) = > {
2022-01-16 20:55:50 +00:00
logger . log ( { selectedNode : req.session.selectedNode , level : 'INFO' , fileName : 'Network' , msg : 'Channel Lookup Finished' , data : body } ) ;
2021-12-29 23:08:41 +00:00
res . status ( 200 ) . json ( body ) ;
} ) . catch ( ( errRes ) = > {
const err = common . handleError ( errRes , 'Network' , 'Channel Lookup Error' , req . session . selectedNode ) ;
return res . status ( err . statusCode ) . json ( { message : err.message , error : err.error } ) ;
} ) ;
} ;
export const feeRates = ( req , res , next ) = > {
logger . log ( { selectedNode : req.session.selectedNode , level : 'INFO' , fileName : 'Network' , msg : 'Getting Network Fee Rates..' } ) ;
options = common . getOptions ( req ) ;
if ( options . error ) { return res . status ( options . statusCode ) . json ( { message : options.message , error : options.error } ) ; }
options . url = req . session . selectedNode . ln_server_url + '/v1/network/feeRates/' + req . params . feeRateStyle ;
2023-10-18 02:22:08 +00:00
request . post ( options ) . then ( ( body ) = > {
2022-01-16 20:55:50 +00:00
logger . log ( { selectedNode : req.session.selectedNode , level : 'INFO' , fileName : 'Network' , msg : 'Network Fee Rates Received for ' + req . params . feeRateStyle , data : body } ) ;
2021-12-29 23:08:41 +00:00
res . status ( 200 ) . json ( body ) ;
} ) . catch ( ( errRes ) = > {
const err = common . handleError ( errRes , 'Network' , 'Fee Rates Error' , req . session . selectedNode ) ;
return res . status ( err . statusCode ) . json ( { message : err.message , error : err.error } ) ;
} ) ;
} ;
2022-05-17 02:53:13 +00:00
export const listNodes = ( req , res , next ) = > {
logger . log ( { selectedNode : req.session.selectedNode , level : 'INFO' , fileName : 'Network' , msg : 'List Nodes..' } ) ;
options = common . getOptions ( req ) ;
if ( options . error ) { return res . status ( options . statusCode ) . json ( { message : options.message , error : options.error } ) ; }
2022-05-20 22:07:58 +00:00
const queryStr = req . query . liquidity_ads ? '?liquidity_ads=' + req . query . liquidity_ads : '' ;
options . url = req . session . selectedNode . ln_server_url + '/v1/network/listNodes' + queryStr ;
2022-05-19 19:47:41 +00:00
logger . log ( { selectedNode : req.session.selectedNode , level : 'DEBUG' , fileName : 'Network' , msg : 'List Nodes URL' + options . url } ) ;
2023-10-18 02:22:08 +00:00
request . post ( options ) . then ( ( body ) = > {
2022-05-17 02:53:13 +00:00
logger . log ( { selectedNode : req.session.selectedNode , level : 'INFO' , fileName : 'Network' , msg : 'List Nodes Finished' , data : body } ) ;
2022-05-19 19:47:41 +00:00
body . forEach ( ( node ) = > {
if ( node . option_will_fund ) {
2022-11-15 20:55:44 +00:00
node . option_will_fund . lease_fee_base_msat = ( node . option_will_fund . lease_fee_base_msat && typeof node . option_will_fund . lease_fee_base_msat === 'string' &&
node . option_will_fund . lease_fee_base_msat . includes ( 'msat' ) ) ? node . option_will_fund . lease_fee_base_msat ? . replace ( 'msat' , '' ) : node . option_will_fund . lease_fee_base_msat ;
node . option_will_fund . channel_fee_max_base_msat = ( node . option_will_fund . channel_fee_max_base_msat && typeof node . option_will_fund . channel_fee_max_base_msat === 'string' &&
node . option_will_fund . channel_fee_max_base_msat . includes ( 'msat' ) ) ? node . option_will_fund . channel_fee_max_base_msat ? . replace ( 'msat' , '' ) : node . option_will_fund . channel_fee_max_base_msat ;
2022-05-19 19:47:41 +00:00
}
2022-05-17 02:53:13 +00:00
return node ;
} ) ;
res . status ( 200 ) . json ( body ) ;
} ) . catch ( ( errRes ) = > {
const err = common . handleError ( errRes , 'Network' , 'Node Lookup Error' , req . session . selectedNode ) ;
return res . status ( err . statusCode ) . json ( { message : err.message , error : err.error } ) ;
} ) ;
} ;