2021-12-29 18:08:41 -05:00
import request from 'request-promise' ;
import { Logger } from '../../utils/logger.js' ;
import { Common } from '../../utils/common.js' ;
let options = null ;
const logger = Logger ;
const common = Common ;
export const listChannels = ( req , res , next ) => {
logger . log ( { selectedNode : req . session . selectedNode , level : 'INFO' , fileName : 'Channels' , msg : 'Getting Channels..' } ) ;
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/channel/listChannels' ;
request ( options ) . then ( ( body ) => {
2022-08-17 15:48:04 -07:00
body === null || body === void 0 ? void 0 : body . map ( ( channel ) => {
2021-12-29 18:08:41 -05:00
if ( ! channel . alias || channel . alias === '' ) {
channel . alias = channel . id . substring ( 0 , 20 ) ;
}
const local = ( channel . msatoshi _to _us ) ? channel . msatoshi _to _us : 0 ;
const remote = ( channel . msatoshi _to _them ) ? channel . msatoshi _to _them : 0 ;
const total = channel . msatoshi _total ? channel . msatoshi _total : 0 ;
channel . balancedness = ( total === 0 ) ? 1 : ( 1 - Math . abs ( ( local - remote ) / total ) ) . toFixed ( 3 ) ;
return channel ;
} ) ;
2022-01-16 15:55:50 -05:00
logger . log ( { selectedNode : req . session . selectedNode , level : 'INFO' , fileName : 'Channels' , msg : 'Channels List Received' , data : body } ) ;
2021-12-29 18:08:41 -05:00
res . status ( 200 ) . json ( body ) ;
} ) . catch ( ( errRes ) => {
const err = common . handleError ( errRes , 'Channels' , 'List Channels Error' , req . session . selectedNode ) ;
return res . status ( err . statusCode ) . json ( { message : err . message , error : err . error } ) ;
} ) ;
} ;
export const openChannel = ( req , res , next ) => {
logger . log ( { selectedNode : req . session . selectedNode , level : 'INFO' , fileName : 'Channels' , msg : 'Opening Channel..' } ) ;
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/channel/openChannel' ;
options . body = req . body ;
logger . log ( { selectedNode : req . session . selectedNode , level : 'DEBUG' , fileName : 'Channels' , msg : 'Open Channel Options' , data : options . body } ) ;
request . post ( options ) . then ( ( body ) => {
2022-01-16 15:55:50 -05:00
logger . log ( { selectedNode : req . session . selectedNode , level : 'INFO' , fileName : 'Channels' , msg : 'Channel Opened' , data : body } ) ;
2021-12-29 18:08:41 -05:00
res . status ( 201 ) . json ( body ) ;
} ) . catch ( ( errRes ) => {
const err = common . handleError ( errRes , 'Channels' , 'Open Channel Error' , req . session . selectedNode ) ;
return res . status ( err . statusCode ) . json ( { message : err . message , error : err . error } ) ;
} ) ;
} ;
export const setChannelFee = ( req , res , next ) => {
logger . log ( { selectedNode : req . session . selectedNode , level : 'INFO' , fileName : 'Channels' , msg : 'Setting Channel Fee..' } ) ;
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/channel/setChannelFee' ;
options . body = req . body ;
logger . log ( { selectedNode : req . session . selectedNode , level : 'DEBUG' , fileName : 'Channels' , msg : 'Update Channel Policy Options' , data : options . body } ) ;
request . post ( options ) . then ( ( body ) => {
2022-01-16 15:55:50 -05:00
logger . log ( { selectedNode : req . session . selectedNode , level : 'INFO' , fileName : 'Channels' , msg : 'Updated Channel Policy' , data : body } ) ;
2021-12-29 18:08:41 -05:00
res . status ( 201 ) . json ( body ) ;
} ) . catch ( ( errRes ) => {
const err = common . handleError ( errRes , 'Channels' , 'Update Channel Policy Error' , req . session . selectedNode ) ;
return res . status ( err . statusCode ) . json ( { message : err . message , error : err . error } ) ;
} ) ;
} ;
export const closeChannel = ( req , res , next ) => {
logger . log ( { selectedNode : req . session . selectedNode , level : 'INFO' , fileName : 'Channels' , msg : 'Closing Channel..' } ) ;
req . setTimeout ( 60000 * 10 ) ; // timeout 10 mins
options = common . getOptions ( req ) ;
if ( options . error ) {
return res . status ( options . statusCode ) . json ( { message : options . message , error : options . error } ) ;
}
const unilateralTimeoutQuery = req . query . force ? '?unilateralTimeout=1' : '' ;
options . url = req . session . selectedNode . ln _server _url + '/v1/channel/closeChannel/' + req . params . channelId + unilateralTimeoutQuery ;
logger . log ( { selectedNode : req . session . selectedNode , level : 'DEBUG' , fileName : 'Channels' , msg : 'Closing Channel' , data : options . url } ) ;
request . delete ( options ) . then ( ( body ) => {
2022-01-16 15:55:50 -05:00
logger . log ( { selectedNode : req . session . selectedNode , level : 'INFO' , fileName : 'Channels' , msg : 'Channel Closed' , data : body } ) ;
2021-12-29 18:08:41 -05:00
res . status ( 204 ) . json ( body ) ;
} ) . catch ( ( errRes ) => {
const err = common . handleError ( errRes , 'Channels' , 'Close Channel Error' , req . session . selectedNode ) ;
return res . status ( err . statusCode ) . json ( { message : err . message , error : err . error } ) ;
} ) ;
} ;
export const getLocalRemoteBalance = ( req , res , next ) => {
logger . log ( { selectedNode : req . session . selectedNode , level : 'INFO' , fileName : 'Channels' , msg : 'Getting Local & Remote Balances..' } ) ;
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/channel/localremotebal' ;
request ( options ) . then ( ( body ) => {
if ( ! body . localBalance ) {
body . localBalance = 0 ;
}
if ( ! body . remoteBalance ) {
body . remoteBalance = 0 ;
}
2022-01-16 15:55:50 -05:00
logger . log ( { selectedNode : req . session . selectedNode , level : 'INFO' , fileName : 'Channels' , msg : 'Local Remote Balance Received' , data : body } ) ;
2021-12-29 18:08:41 -05:00
res . status ( 200 ) . json ( body ) ;
} ) . catch ( ( errRes ) => {
const err = common . handleError ( errRes , 'Channels' , 'Local Remote Balance Error' , req . session . selectedNode ) ;
return res . status ( err . statusCode ) . json ( { message : err . message , error : err . error } ) ;
} ) ;
} ;
export const listForwards = ( req , res , next ) => {
logger . log ( { selectedNode : req . session . selectedNode , level : 'INFO' , fileName : 'Channels' , msg : 'Getting Channel List Forwards..' } ) ;
options = common . getOptions ( req ) ;
if ( options . error ) {
return res . status ( options . statusCode ) . json ( { message : options . message , error : options . error } ) ;
}
2022-05-24 20:54:57 -04:00
options . url = req . session . selectedNode . ln _server _url + '/v1/channel/listForwards?status=' + ( req . query . status ? req . query . status : 'settled' ) ;
2021-12-29 18:08:41 -05:00
request . get ( options ) . then ( ( body ) => {
2022-08-10 23:42:04 -07:00
logger . log ( { selectedNode : req . session . selectedNode , level : 'DEBUG' , fileName : 'Channels' , msg : 'Forwarding History Received For Status ' + req . query . status , data : body } ) ;
2021-12-29 18:08:41 -05:00
res . status ( 200 ) . json ( body ) ;
} ) . catch ( ( errRes ) => {
const err = common . handleError ( errRes , 'Channels' , 'Forwarding History Error' , req . session . selectedNode ) ;
return res . status ( err . statusCode ) . json ( { message : err . message , error : err . error } ) ;
} ) ;
} ;
2022-05-13 17:24:37 -04:00
export const funderUpdatePolicy = ( req , res , next ) => {
logger . log ( { selectedNode : req . session . selectedNode , level : 'INFO' , fileName : 'Channels' , msg : 'Getting or Updating Funder Policy..' } ) ;
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/channel/funderUpdate' ;
if ( req . body && req . body . policy ) {
options . body = req . body ;
}
logger . log ( { selectedNode : req . session . selectedNode , level : 'DEBUG' , fileName : 'Channels' , msg : 'Funder Update Body' , data : options . body } ) ;
request . post ( options ) . then ( ( body ) => {
2022-08-17 15:48:04 -07:00
var _a , _b ;
2022-05-13 17:24:37 -04:00
logger . log ( { selectedNode : req . session . selectedNode , level : 'INFO' , fileName : 'Channels' , msg : 'Funder Policy Received' , data : body } ) ;
2022-08-17 15:48:04 -07:00
body . channel _fee _max _base _msat = ( body . channel _fee _max _base _msat && typeof body . channel _fee _max _base _msat === 'string' && body . channel _fee _max _base _msat . includes ( 'msat' ) ) ? + ( ( _a = body . channel _fee _max _base _msat ) === null || _a === void 0 ? void 0 : _a . replace ( 'msat' , '' ) ) : body . channel _fee _max _base _msat ;
body . lease _fee _base _msat = ( body . lease _fee _base _msat && typeof body . lease _fee _base _msat === 'string' && body . lease _fee _base _msat . includes ( 'msat' ) ) ? + ( ( _b = body . lease _fee _base _msat ) === null || _b === void 0 ? void 0 : _b . replace ( 'msat' , '' ) ) : body . channel _fee _max _base _msat ;
2022-05-13 17:24:37 -04:00
res . status ( 200 ) . json ( body ) ;
} ) . catch ( ( errRes ) => {
const err = common . handleError ( errRes , 'Channels' , 'Funder Policy Error' , req . session . selectedNode ) ;
return res . status ( err . statusCode ) . json ( { message : err . message , error : err . error } ) ;
} ) ;
} ;
2022-05-20 18:07:58 -04:00
export const listForwardsPaginated = ( req , res , next ) => {
logger . log ( { selectedNode : req . session . selectedNode , level : 'INFO' , fileName : 'Channels' , msg : 'Getting Paginated List Forwards..' } ) ;
options = common . getOptions ( req ) ;
if ( options . error ) {
return res . status ( options . statusCode ) . json ( { message : options . message , error : options . error } ) ;
}
2022-08-07 16:43:17 -07:00
const { status , maxLen , offset } = req . query ;
2022-05-24 15:52:10 -04:00
let queryStr = '?status=' + ( status ? status : 'settled' ) ;
queryStr = queryStr + '&maxLen=' + ( maxLen ? maxLen : '10' ) ;
queryStr = queryStr + '&offset=' + ( offset ? offset : '0' ) ;
2022-05-23 20:57:22 -04:00
options . url = req . session . selectedNode . ln _server _url + '/v1/channel/listForwardsPaginated' + queryStr ;
2022-05-20 18:07:58 -04:00
logger . log ( { selectedNode : req . session . selectedNode , level : 'DEBUG' , fileName : 'Channels' , msg : 'Paginated Forwarding History url' + options . url } ) ;
request . get ( options ) . then ( ( body ) => {
2022-08-10 23:42:04 -07:00
logger . log ( { selectedNode : req . session . selectedNode , level : 'DEBUG' , fileName : 'Channels' , msg : 'Paginated Forwarding History Received For Status ' + req . query . status , data : body } ) ;
2022-05-20 18:07:58 -04:00
res . status ( 200 ) . json ( body ) ;
} ) . catch ( ( errRes ) => {
const err = common . handleError ( errRes , 'Channels' , 'Paginated Forwarding History Error' , req . session . selectedNode ) ;
return res . status ( err . statusCode ) . json ( { message : err . message , error : err . error } ) ;
} ) ;
} ;