2019-09-02 04:11:37 +00:00
var request = require ( 'request-promise' ) ;
var common = require ( '../../common' ) ;
var logger = require ( '../logger' ) ;
var options = { } ;
2019-09-10 23:53:28 +00:00
exports . listChannels = ( req , res , next ) => {
options = common . getOptions ( ) ;
options . url = common . getSelLNServerUrl ( ) + '/channel/listChannels' ;
request ( options ) . then ( function ( body ) {
logger . info ( { fileName : 'Channels' , msg : 'List Channels: ' + JSON . stringify ( body ) } ) ;
2020-01-04 01:06:36 +00:00
body . map ( channel => {
2020-01-05 04:28:40 +00:00
local = ( channel . msatoshi _to _us ) ? channel . msatoshi _to _us : 0 ;
remote = ( channel . msatoshi _to _them ) ? channel . msatoshi _to _them : 0 ;
total = channel . msatoshi _total ? channel . msatoshi _total : 0 ;
2020-01-10 03:50:02 +00:00
channel . balancedness = ( total == 0 ) ? 1 : ( 1 - Math . abs ( ( local - remote ) / total ) ) . toFixed ( 3 ) ;
2020-01-04 01:06:36 +00:00
} )
2019-09-10 23:53:28 +00:00
res . status ( 200 ) . json ( body ) ;
} )
2020-05-03 19:52:38 +00:00
. catch ( errRes => {
let err = JSON . parse ( JSON . stringify ( errRes ) ) ;
if ( err . options && err . options . headers && err . options . headers . macaroon ) {
delete err . options . headers . macaroon ;
}
if ( err . response && err . response . request && err . response . request . headers && err . response . request . headers . macaroon ) {
delete err . response . request . headers . macaroon ;
}
logger . error ( { fileName : 'Channels' , lineNum : 26 , msg : 'List Channels: ' + JSON . stringify ( err ) } ) ;
2019-09-10 23:53:28 +00:00
return res . status ( 500 ) . json ( {
message : 'Fetching List Channels Failed!' ,
error : err . error
} ) ;
} ) ;
}
2019-09-14 19:58:21 +00:00
exports . openChannel = ( req , res , next ) => {
options = common . getOptions ( ) ;
options . url = common . getSelLNServerUrl ( ) + '/channel/openChannel' ;
options . body = req . body ;
2020-01-02 23:23:47 +00:00
logger . info ( { fileName : 'Channels' , msg : 'Open Channel Options: ' + JSON . stringify ( options . body ) } ) ;
2019-09-14 19:58:21 +00:00
request . post ( options ) . then ( ( body ) => {
logger . info ( { fileName : 'Channels' , msg : 'Open Channel Response: ' + JSON . stringify ( body ) } ) ;
2020-03-10 17:25:52 +00:00
if ( ! body || body . error ) {
2020-05-03 19:52:38 +00:00
logger . error ( { fileName : 'Channels' , lineNum : 42 , msg : 'Open Channel Error: ' + ( ( ! body || ! body . error ) ? 'Error From Server!' : JSON . stringify ( body . error ) ) } ) ;
2019-09-14 19:58:21 +00:00
res . status ( 500 ) . json ( {
message : 'Open Channel Failed!' ,
2020-03-10 17:25:52 +00:00
error : ( ! body ) ? 'Error From Server!' : body . error
2019-09-14 19:58:21 +00:00
} ) ;
} else {
res . status ( 201 ) . json ( body ) ;
}
} )
2020-05-03 19:52:38 +00:00
. catch ( errRes => {
let err = JSON . parse ( JSON . stringify ( errRes ) ) ;
if ( err . options && err . options . headers && err . options . headers . macaroon ) {
delete err . options . headers . macaroon ;
}
if ( err . response && err . response . request && err . response . request . headers && err . response . request . headers . macaroon ) {
delete err . response . request . headers . macaroon ;
}
logger . error ( { fileName : 'Channels' , lineNum : 58 , msg : 'Open Channel Failed: ' + JSON . stringify ( err ) } ) ;
2019-09-14 19:58:21 +00:00
return res . status ( 500 ) . json ( {
message : 'Open Channel Failed!' ,
error : err . error
} ) ;
} ) ;
}
2019-09-10 23:53:28 +00:00
exports . setChannelFee = ( req , res , next ) => {
options = common . getOptions ( ) ;
options . url = common . getSelLNServerUrl ( ) + '/channel/setChannelFee' ;
options . body = req . body ;
2020-01-02 23:23:47 +00:00
logger . info ( { fileName : 'Channels' , msg : 'Update Channel Policy Options: ' + JSON . stringify ( options . body ) } ) ;
2019-09-10 23:53:28 +00:00
request . post ( options ) . then ( ( body ) => {
logger . info ( { fileName : 'Channels' , msg : 'Update Channel Policy: ' + JSON . stringify ( body ) } ) ;
2020-03-10 17:25:52 +00:00
if ( ! body || body . error ) {
2020-05-03 19:52:38 +00:00
logger . error ( { fileName : 'Channels' , lineNum : 74 , msg : 'Update Channel Policy Error: ' + ( ( ! body || ! body . error ) ? 'Error From Server!' : JSON . stringify ( body . error ) ) } ) ;
2019-09-10 23:53:28 +00:00
res . status ( 500 ) . json ( {
message : 'Update Channel Policy Failed!' ,
2020-03-10 17:25:52 +00:00
error : ( ! body ) ? 'Error From Server!' : body . error
2019-09-10 23:53:28 +00:00
} ) ;
} else {
res . status ( 201 ) . json ( body ) ;
}
} )
2020-05-03 19:52:38 +00:00
. catch ( errRes => {
let err = JSON . parse ( JSON . stringify ( errRes ) ) ;
if ( err . options && err . options . headers && err . options . headers . macaroon ) {
delete err . options . headers . macaroon ;
}
if ( err . response && err . response . request && err . response . request . headers && err . response . request . headers . macaroon ) {
delete err . response . request . headers . macaroon ;
}
logger . error ( { fileName : 'Channels' , lineNum : 90 , msg : 'Update Channel Policy: ' + JSON . stringify ( err ) } ) ;
2019-09-10 23:53:28 +00:00
return res . status ( 500 ) . json ( {
message : 'Update Channel Policy Failed!' ,
error : err . error
} ) ;
} ) ;
}
exports . closeChannel = ( req , res , next ) => {
options = common . getOptions ( ) ;
const unilateralTimeoutQuery = req . query . unilateralTimeout ? '?unilateralTimeout=' + req . query . unilateralTimeout : '' ;
options . url = common . getSelLNServerUrl ( ) + '/channel/closeChannel/' + req . params . channelId + unilateralTimeoutQuery ;
logger . info ( { fileName : 'Channels' , msg : 'Closing Channel: ' + options . url } ) ;
request . delete ( options ) . then ( ( body ) => {
logger . info ( { fileName : 'Channels' , msg : 'Close Channel Response: ' + JSON . stringify ( body ) } ) ;
2020-03-10 17:25:52 +00:00
if ( ! body || body . error ) {
2020-05-03 19:52:38 +00:00
logger . error ( { fileName : 'Channels' , lineNum : 106 , msg : 'Close Channel Error: ' + ( ( ! body || ! body . error ) ? 'Error From Server!' : JSON . stringify ( body . error ) ) } ) ;
2019-09-10 23:53:28 +00:00
res . status ( 500 ) . json ( {
message : 'Close Channel Failed!' ,
2020-03-10 17:25:52 +00:00
error : ( ! body ) ? 'Error From Server!' : body . error
2019-09-10 23:53:28 +00:00
} ) ;
} else {
res . status ( 204 ) . json ( body ) ;
}
} )
2020-05-03 19:52:38 +00:00
. catch ( errRes => {
let err = JSON . parse ( JSON . stringify ( errRes ) ) ;
if ( err . options && err . options . headers && err . options . headers . macaroon ) {
delete err . options . headers . macaroon ;
}
if ( err . response && err . response . request && err . response . request . headers && err . response . request . headers . macaroon ) {
delete err . response . request . headers . macaroon ;
}
logger . error ( { fileName : 'Channels' , lineNum : 122 , msg : 'Close Channel Error: ' + JSON . stringify ( err ) } ) ;
2019-09-10 23:53:28 +00:00
return res . status ( 500 ) . json ( {
message : 'Close Channel Failed!' ,
error : err . error
} ) ;
} ) ;
}
2019-09-02 04:11:37 +00:00
exports . getLocalRemoteBalance = ( req , res , next ) => {
options = common . getOptions ( ) ;
options . url = common . getSelLNServerUrl ( ) + '/channel/localremotebal' ;
request ( options ) . then ( function ( body ) {
logger . info ( { fileName : 'Channels' , msg : 'Local Remote Balance: ' + JSON . stringify ( body ) } ) ;
2020-03-10 17:25:52 +00:00
if ( ! body . localBalance ) {
2019-09-02 04:11:37 +00:00
body . localBalance = 0 ;
body . btc _localBalance = 0 ;
} else {
body . btc _localBalance = common . convertToBTC ( body . localBalance ) ;
}
2020-03-10 17:25:52 +00:00
if ( ! body . remoteBalance ) {
2019-09-02 04:11:37 +00:00
body . remoteBalance = 0 ;
body . btc _remoteBalance = 0 ;
} else {
body . btc _remoteBalance = common . convertToBTC ( body . remoteBalance ) ;
}
res . status ( 200 ) . json ( body ) ;
} )
2020-05-03 19:52:38 +00:00
. catch ( errRes => {
let err = JSON . parse ( JSON . stringify ( errRes ) ) ;
if ( err . options && err . options . headers && err . options . headers . macaroon ) {
delete err . options . headers . macaroon ;
}
if ( err . response && err . response . request && err . response . request . headers && err . response . request . headers . macaroon ) {
delete err . response . request . headers . macaroon ;
}
logger . error ( { fileName : 'Channels' , lineNum : 156 , msg : 'Local Remote Balance Error: ' + JSON . stringify ( err ) } ) ;
2019-09-02 04:11:37 +00:00
return res . status ( 500 ) . json ( {
message : 'Fetching Local Remote Balance Failed!' ,
error : err . error
} ) ;
} ) ;
} ;
2019-09-08 19:09:00 +00:00
2019-09-10 23:53:28 +00:00
exports . listForwards = ( req , res , next ) => {
2019-09-08 19:09:00 +00:00
options = common . getOptions ( ) ;
2019-09-12 01:42:00 +00:00
options . url = common . getSelLNServerUrl ( ) + '/channel/listForwards/' ;
request . get ( options ) . then ( ( body ) => {
logger . info ( { fileName : 'Channels' , msg : 'Forwarding History Response: ' + JSON . stringify ( body ) } ) ;
2020-03-10 17:25:52 +00:00
if ( ! body || body . error ) {
2020-05-03 19:52:38 +00:00
logger . error ( { fileName : 'Channels' , lineNum : 170 , msg : 'Forwarding History Error: ' + ( ( ! body || ! body . error ) ? 'Error From Server!' : JSON . stringify ( body . error ) ) } ) ;
2019-09-08 19:09:00 +00:00
res . status ( 500 ) . json ( {
2019-09-12 01:42:00 +00:00
message : "Forwarding History Failed!" ,
2020-03-10 17:25:52 +00:00
error : ( ! body ) ? 'Error From Server!' : body . error
2019-09-08 19:09:00 +00:00
} ) ;
} else {
2019-10-27 20:39:40 +00:00
if ( body && body . length > 0 ) {
2019-09-12 01:42:00 +00:00
body . forEach ( event => {
2020-03-10 17:25:52 +00:00
event . received _time _str = ( ! event . received _time ) ? '' : common . convertTimestampToDate ( event . received _time ) ;
event . resolved _time _str = ( ! event . resolved _time ) ? '' : common . convertTimestampToDate ( event . resolved _time ) ;
2019-09-08 19:09:00 +00:00
} ) ;
2019-09-12 01:42:00 +00:00
body = common . sortDescByKey ( body , 'received_time' ) ;
2019-09-08 19:09:00 +00:00
}
2019-09-12 01:42:00 +00:00
logger . info ( { fileName : 'Channels' , msg : 'Forwarding History Received: ' + JSON . stringify ( body ) } ) ;
res . status ( 200 ) . json ( { last _offset _index : 0 , forwarding _events : body } ) ;
2019-09-08 19:09:00 +00:00
}
} )
2020-05-03 19:52:38 +00:00
. catch ( errRes => {
let err = JSON . parse ( JSON . stringify ( errRes ) ) ;
if ( err . options && err . options . headers && err . options . headers . macaroon ) {
delete err . options . headers . macaroon ;
}
if ( err . response && err . response . request && err . response . request . headers && err . response . request . headers . macaroon ) {
delete err . response . request . headers . macaroon ;
}
logger . error ( { fileName : 'Channels' , lineNum : 194 , msg : 'Forwarding History Error: ' + JSON . stringify ( err ) } ) ;
2019-09-08 19:09:00 +00:00
return res . status ( 500 ) . json ( {
2019-09-12 01:42:00 +00:00
message : "Forwarding History Failed!" ,
2019-09-08 19:09:00 +00:00
error : err . error
} ) ;
} ) ;
} ;