2020-03-16 15:57:57 +00:00
var request = require ( 'request-promise' ) ;
var common = require ( '../../common' ) ;
2021-02-21 19:02:31 +00:00
var logger = require ( './logger' ) ;
2020-03-16 15:57:57 +00:00
var options = { } ;
exports . loopOut = ( req , res , next ) => {
2020-10-12 22:43:16 +00:00
options = common . getSwapServerOptions ( ) ;
if ( options . url === '' ) { return res . status ( 500 ) . json ( { message : "Loop Out Failed!" , error : { message : 'Loop Server URL is missing in the configuration.' } } ) ; }
options . url = options . url + '/v1/loop/out' ;
options . body = {
2020-03-16 15:57:57 +00:00
amt : req . body . amount ,
sweep _conf _target : req . body . targetConf ,
max _swap _routing _fee : req . body . swapRoutingFee ,
max _miner _fee : req . body . minerFee ,
max _prepay _routing _fee : req . body . prepayRoutingFee ,
max _prepay _amt : req . body . prepayAmt ,
max _swap _fee : req . body . swapFee ,
2020-12-20 23:36:04 +00:00
swap _publication _deadline : req . body . swapPublicationDeadline ,
label : 'RTL'
2020-03-16 15:57:57 +00:00
} ;
2020-10-12 22:43:16 +00:00
if ( req . body . chanId !== '' ) { options . body [ 'loop_out_channel' ] = req . body . chanId ; }
if ( req . body . destAddress !== '' ) { options . body [ 'dest' ] = req . body . destAddress ; }
logger . info ( { fileName : 'Loop' , msg : 'Loop Out Body: ' + JSON . stringify ( options . body ) } ) ;
request . post ( options ) . then ( loopOutRes => {
logger . info ( { fileName : 'Loop' , msg : 'Loop Out: ' + JSON . stringify ( loopOutRes ) } ) ;
if ( ! loopOutRes || loopOutRes . error ) {
logger . error ( { fileName : 'Loop' , lineNum : 28 , msg : 'Loop Out Error: ' + JSON . stringify ( loopOutRes . error ) } ) ;
2020-03-16 15:57:57 +00:00
res . status ( 500 ) . json ( {
message : 'Loop Out Failed!' ,
2020-10-12 22:43:16 +00:00
error : ( ! loopOutRes ) ? 'Error From Server!' : loopOutRes . error . message
2020-03-16 15:57:57 +00:00
} ) ;
} else {
2020-10-12 22:43:16 +00:00
res . status ( 201 ) . json ( loopOutRes ) ;
2020-03-16 15:57:57 +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 [ '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 : 'Loop' , lineNum : 44 , msg : 'Loop Out Error: ' + JSON . stringify ( err ) } ) ;
2020-03-16 15:57:57 +00:00
return res . status ( 500 ) . json ( {
message : "Loop Out Failed!" ,
2020-05-03 19:52:38 +00:00
error : ( err . error && err . error . error ) ? err . error . error : ( err . error ) ? err . error : err
2020-03-16 15:57:57 +00:00
} ) ;
} ) ;
} ;
exports . loopOutTerms = ( req , res , next ) => {
2020-10-12 22:43:16 +00:00
options = common . getSwapServerOptions ( ) ;
if ( options . url === '' ) { return res . status ( 500 ) . json ( { message : "Loop Out Terms Failed!" , error : { message : 'Loop Server URL is missing in the configuration.' } } ) ; }
options . url = options . url + '/v1/loop/out/terms' ;
2020-03-16 15:57:57 +00:00
request ( options ) . then ( function ( body ) {
logger . info ( { fileName : 'Loop' , msg : 'Loop Out Terms: ' + JSON . stringify ( body ) } ) ;
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 [ '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 : 'Loop' , lineNum : 67 , msg : 'Loop Out Terms Error: ' + JSON . stringify ( err ) } ) ;
2020-03-16 15:57:57 +00:00
return res . status ( 500 ) . json ( {
message : "Loop Out Terms Failed!" ,
2020-05-03 19:52:38 +00:00
error : ( err . error && err . error . error ) ? err . error . error : ( err . error ) ? err . error : err
2020-03-16 15:57:57 +00:00
} ) ;
} ) ;
} ;
exports . loopOutQuote = ( req , res , next ) => {
2020-10-12 22:43:16 +00:00
options = common . getSwapServerOptions ( ) ;
if ( options . url === '' ) { return res . status ( 500 ) . json ( { message : "Loop Out Quote Failed!" , error : { message : 'Loop Server URL is missing in the configuration.' } } ) ; }
options . url = options . url + '/v1/loop/out/quote/' + req . params . amount + '?conf_target=' + ( req . query . targetConf ? req . query . targetConf : '2' ) + '&swap_publication_deadline=' + req . query . swapPublicationDeadline ;
2020-03-16 15:57:57 +00:00
logger . info ( { fileName : 'Loop' , msg : 'Loop Out Quote URL: ' + options . url } ) ;
2020-10-12 22:43:16 +00:00
request ( options ) . then ( function ( quoteRes ) {
logger . info ( { fileName : 'Loop' , msg : 'Loop Out Quote: ' + JSON . stringify ( quoteRes ) } ) ;
quoteRes . amount = + req . params . amount ;
quoteRes . swap _payment _dest = quoteRes . swap _payment _dest ? Buffer . from ( quoteRes . swap _payment _dest , 'base64' ) . toString ( 'hex' ) : '' ;
res . status ( 200 ) . json ( quoteRes ) ;
2020-03-16 15:57:57 +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 [ '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 : 'Loop' , lineNum : 94 , msg : 'Loop Out Quote Error: ' + JSON . stringify ( err ) } ) ;
2020-03-16 15:57:57 +00:00
return res . status ( 500 ) . json ( {
message : "Loop Out Quote Failed!" ,
2020-05-03 19:52:38 +00:00
error : ( err . error && err . error . error ) ? err . error . error : ( err . error ) ? err . error : err
2020-03-16 15:57:57 +00:00
} ) ;
} ) ;
} ;
exports . loopOutTermsAndQuotes = ( req , res , next ) => {
2020-10-12 22:43:16 +00:00
options = common . getSwapServerOptions ( ) ;
if ( options . url === '' ) { return res . status ( 500 ) . json ( { message : "Loop Out Terms And Quotes Failed!" , error : { message : 'Loop Server URL is missing in the configuration.' } } ) ; }
options . url = options . url + '/v1/loop/out/terms' ;
2020-03-16 15:57:57 +00:00
request ( options ) . then ( function ( terms ) {
logger . info ( { fileName : 'Loop' , msg : 'Loop Out Terms: ' + JSON . stringify ( terms ) } ) ;
2020-10-12 22:43:16 +00:00
const options1 = common . getSwapServerOptions ( ) ; const options2 = common . getSwapServerOptions ( ) ;
options1 . url = options1 . url + '/v1/loop/out/quote/' + terms . min _swap _amount + '?conf_target=' + ( req . query . targetConf ? req . query . targetConf : '2' ) + '&swap_publication_deadline=' + req . query . swapPublicationDeadline ;
options2 . url = options2 . url + '/v1/loop/out/quote/' + terms . max _swap _amount + '?conf_target=' + ( req . query . targetConf ? req . query . targetConf : '2' ) + '&swap_publication_deadline=' + req . query . swapPublicationDeadline ;
2020-03-16 15:57:57 +00:00
logger . info ( { fileName : 'Loop' , msg : 'Loop Out Min Quote Options: ' + JSON . stringify ( options1 ) } ) ;
logger . info ( { fileName : 'Loop' , msg : 'Loop Out Max Quote Options: ' + JSON . stringify ( options2 ) } ) ;
Promise . all ( [ request ( options1 ) , request ( options2 ) ] ) . then ( function ( values ) {
values [ 0 ] . amount = + terms . min _swap _amount ;
values [ 1 ] . amount = + terms . max _swap _amount ;
values [ 0 ] . swap _payment _dest = values [ 0 ] . swap _payment _dest ? Buffer . from ( values [ 0 ] . swap _payment _dest , 'base64' ) . toString ( 'hex' ) : '' ;
values [ 1 ] . swap _payment _dest = values [ 1 ] . swap _payment _dest ? Buffer . from ( values [ 1 ] . swap _payment _dest , 'base64' ) . toString ( 'hex' ) : '' ;
logger . info ( { fileName : 'Loop' , msg : 'Loop Out Quotes 1: ' + JSON . stringify ( values [ 0 ] ) } ) ;
logger . info ( { fileName : 'Loop' , msg : 'Loop Out Quotes 2: ' + JSON . stringify ( values [ 1 ] ) } ) ;
res . status ( 200 ) . json ( values ) ;
} )
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 [ '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 : 'Loop' , lineNum : 132 , msg : 'Loop Out Quotes Error: ' + JSON . stringify ( err ) } ) ;
2020-03-16 15:57:57 +00:00
return res . status ( 500 ) . json ( {
message : "Loop Out Quotes Failed!" ,
2020-05-03 19:52:38 +00:00
error : ( err . error && err . error . error ) ? err . error . error : ( err . error ) ? err . error : err
2020-03-16 15:57:57 +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 [ '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 : 'Loop' , lineNum : 146 , msg : 'Loop Out Terms Error: ' + JSON . stringify ( err ) } ) ;
2020-03-16 15:57:57 +00:00
return res . status ( 500 ) . json ( {
message : "Loop Out Terms Failed!" ,
2020-05-03 19:52:38 +00:00
error : ( err . error && err . error . error ) ? err . error . error : ( err . error ) ? err . error : err
2020-03-16 15:57:57 +00:00
} ) ;
} ) ;
} ;
exports . loopIn = ( req , res , next ) => {
2020-10-12 22:43:16 +00:00
options = common . getSwapServerOptions ( ) ;
if ( options . url === '' ) { return res . status ( 500 ) . json ( { message : "Loop In Failed!" , error : { message : 'Loop Server URL is missing in the configuration.' } } ) ; }
options . url = options . url + '/v1/loop/in' ;
options . body = {
2020-03-16 15:57:57 +00:00
amt : req . body . amount ,
max _swap _fee : req . body . swapFee ,
2020-12-20 23:36:04 +00:00
max _miner _fee : req . body . minerFee ,
label : 'RTL'
2020-10-12 22:43:16 +00:00
} ;
2020-12-20 23:36:04 +00:00
logger . info ( { fileName : 'Loop' , msg : 'Loop In Body: ' + JSON . stringify ( options . body ) } ) ;
2020-03-16 15:57:57 +00:00
request . post ( options ) . then ( function ( body ) {
logger . info ( { fileName : 'Loop' , msg : 'Loop In: ' + JSON . stringify ( body ) } ) ;
if ( ! body || body . error ) {
2020-05-03 19:52:38 +00:00
logger . error ( { fileName : 'Loop' , lineNum : 166 , msg : 'Loop In Error: ' + JSON . stringify ( body . error ) } ) ;
2020-03-16 15:57:57 +00:00
res . status ( 500 ) . json ( {
message : 'Loop In Failed!' ,
2020-05-03 19:52:38 +00:00
error : ( body . error && body . error . message ) ? body . error . message : 'Error From Server!'
2020-03-16 15:57:57 +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 [ '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 : 'Loop' , lineNum : 182 , msg : 'Loop In Error: ' + JSON . stringify ( err ) } ) ;
2020-03-16 15:57:57 +00:00
return res . status ( 500 ) . json ( {
message : "Loop In Failed!" ,
2020-05-03 19:52:38 +00:00
error : ( err . error && err . error . error ) ? err . error . error : ( err . error ) ? err . error : err
2020-03-16 15:57:57 +00:00
} ) ;
} ) ;
} ;
exports . loopInTerms = ( req , res , next ) => {
2020-10-12 22:43:16 +00:00
options = common . getSwapServerOptions ( ) ;
if ( options . url === '' ) { return res . status ( 500 ) . json ( { message : "Loop In Terms Failed!" , error : { message : 'Loop Server URL is missing in the configuration.' } } ) ; }
options . url = options . url + '/v1/loop/in/terms' ;
2020-03-16 15:57:57 +00:00
request ( options ) . then ( function ( body ) {
logger . info ( { fileName : 'Loop' , msg : 'Loop In Terms: ' + JSON . stringify ( body ) } ) ;
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 [ '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 : 'Loop' , lineNum : 205 , msg : 'Loop In Terms Error: ' + JSON . stringify ( err ) } ) ;
2020-03-16 15:57:57 +00:00
return res . status ( 500 ) . json ( {
message : "Loop In Terms Failed!" ,
2020-05-03 19:52:38 +00:00
error : ( err . error && err . error . error ) ? err . error . error : ( err . error ) ? err . error : err
2020-03-16 15:57:57 +00:00
} ) ;
} ) ;
} ;
exports . loopInQuote = ( req , res , next ) => {
2020-10-12 22:43:16 +00:00
options = common . getSwapServerOptions ( ) ;
if ( options . url === '' ) { return res . status ( 500 ) . json ( { message : "Loop In Quote Failed!" , error : { message : 'Loop Server URL is missing in the configuration.' } } ) ; }
options . url = options . url + '/v1/loop/in/quote/' + req . params . amount + '?conf_target=' + ( req . query . targetConf ? req . query . targetConf : '2' ) + '&swap_publication_deadline=' + req . query . swapPublicationDeadline ;
2020-03-16 15:57:57 +00:00
logger . info ( { fileName : 'Loop' , msg : 'Loop In Quote Options: ' + options . url } ) ;
request ( options ) . then ( function ( body ) {
logger . info ( { fileName : 'Loop' , msg : 'Loop In Quote: ' + JSON . stringify ( body ) } ) ;
body . amount = + req . params . amount ;
body . swap _payment _dest = body . swap _payment _dest ? Buffer . from ( body . swap _payment _dest , 'base64' ) . toString ( 'hex' ) : '' ;
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 [ '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 : 'Loop' , lineNum : 232 , msg : 'Loop In Quote Error: ' + JSON . stringify ( err ) } ) ;
2020-03-16 15:57:57 +00:00
return res . status ( 500 ) . json ( {
message : "Loop In Quote Failed!" ,
2020-05-03 19:52:38 +00:00
error : ( err . error && err . error . error ) ? err . error . error : ( err . error ) ? err . error : err
2020-03-16 15:57:57 +00:00
} ) ;
} ) ;
} ;
exports . loopInTermsAndQuotes = ( req , res , next ) => {
2020-10-12 22:43:16 +00:00
options = common . getSwapServerOptions ( ) ;
if ( options . url === '' ) { return res . status ( 500 ) . json ( { message : "Loop In Terms And Quotes Failed!" , error : { message : 'Loop Server URL is missing in the configuration.' } } ) ; }
options . url = options . url + '/v1/loop/in/terms' ;
2020-03-16 15:57:57 +00:00
request ( options ) . then ( function ( terms ) {
logger . info ( { fileName : 'Loop' , msg : 'Loop In Terms: ' + JSON . stringify ( terms ) } ) ;
2020-10-12 22:43:16 +00:00
const options1 = common . getSwapServerOptions ( ) ; const options2 = common . getSwapServerOptions ( ) ;
options1 . url = options1 . url + '/v1/loop/in/quote/' + terms . min _swap _amount + '?conf_target=' + ( req . query . targetConf ? req . query . targetConf : '2' ) + '&swap_publication_deadline=' + req . query . swapPublicationDeadline ;
options2 . url = options2 . url + '/v1/loop/in/quote/' + terms . max _swap _amount + '?conf_target=' + ( req . query . targetConf ? req . query . targetConf : '2' ) + '&swap_publication_deadline=' + req . query . swapPublicationDeadline ;
2020-03-16 15:57:57 +00:00
logger . info ( { fileName : 'Loop' , msg : 'Loop In Min Quote Options: ' + JSON . stringify ( options1 ) } ) ;
logger . info ( { fileName : 'Loop' , msg : 'Loop In Max Quote Options: ' + JSON . stringify ( options2 ) } ) ;
Promise . all ( [ request ( options1 ) , request ( options2 ) ] ) . then ( function ( values ) {
values [ 0 ] . amount = + terms . min _swap _amount ;
values [ 1 ] . amount = + terms . max _swap _amount ;
values [ 0 ] . swap _payment _dest = values [ 0 ] . swap _payment _dest ? Buffer . from ( values [ 0 ] . swap _payment _dest , 'base64' ) . toString ( 'hex' ) : '' ;
values [ 1 ] . swap _payment _dest = values [ 1 ] . swap _payment _dest ? Buffer . from ( values [ 1 ] . swap _payment _dest , 'base64' ) . toString ( 'hex' ) : '' ;
logger . info ( { fileName : 'Loop' , msg : 'Loop In Quotes 1: ' + JSON . stringify ( values [ 0 ] ) } ) ;
logger . info ( { fileName : 'Loop' , msg : 'Loop In Quotes 2: ' + JSON . stringify ( values [ 1 ] ) } ) ;
res . status ( 200 ) . json ( values ) ;
} )
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 [ '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 : 'Loop' , lineNum : 270 , msg : 'Loop In Quotes Error: ' + JSON . stringify ( err ) } ) ;
2020-03-16 15:57:57 +00:00
return res . status ( 500 ) . json ( {
message : "Loop In Quotes Failed!" ,
2020-05-03 19:52:38 +00:00
error : ( err . error && err . error . error ) ? err . error . error : ( err . error ) ? err . error : err
2020-03-16 15:57:57 +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 [ '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 : 'Loop' , lineNum : 284 , msg : 'Loop In Terms Error: ' + JSON . stringify ( err ) } ) ;
2020-03-16 15:57:57 +00:00
return res . status ( 500 ) . json ( {
message : "Loop In Terms Failed!" ,
2020-05-03 19:52:38 +00:00
error : ( err . error && err . error . error ) ? err . error . error : ( err . error ) ? err . error : err
2020-03-16 15:57:57 +00:00
} ) ;
} ) ;
} ;
exports . swaps = ( req , res , next ) => {
2020-10-12 22:43:16 +00:00
options = common . getSwapServerOptions ( ) ;
if ( options . url === '' ) { return res . status ( 500 ) . json ( { message : "Loop Out Failed!" , error : { message : 'Loop Server URL is missing in the configuration.' } } ) ; }
options . url = options . url + '/v1/loop/swaps' ;
2020-03-16 15:57:57 +00:00
request ( options ) . then ( function ( body ) {
2020-10-12 22:43:16 +00:00
logger . info ( { fileName : 'Loop' , msg : 'Loop Swaps: ' + JSON . stringify ( body ) } ) ;
2020-03-16 15:57:57 +00:00
if ( body . swaps && body . swaps . length > 0 ) {
body . swaps . forEach ( swap => {
swap . initiation _time _str = ( ! swap . initiation _time ) ? '' : common . convertTimestampToDate ( Math . round ( swap . initiation _time / 1000000000 ) ) ;
swap . last _update _time _str = ( ! swap . last _update _time ) ? '' : common . convertTimestampToDate ( Math . round ( swap . last _update _time / 1000000000 ) ) ;
} ) ;
body . swaps = common . sortDescByKey ( body . swaps , 'initiation_time' ) ;
logger . info ( { fileName : 'Loop' , msg : 'Loop Swaps after Sort: ' + JSON . stringify ( body ) } ) ;
}
res . status ( 200 ) . json ( body . swaps ) ;
} )
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 [ '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' ] ;
}
2020-10-12 22:43:16 +00:00
logger . error ( { fileName : 'Loop' , lineNum : 327 , msg : 'List Swaps Error: ' + JSON . stringify ( err ) } ) ;
2020-03-16 15:57:57 +00:00
return res . status ( 500 ) . json ( {
message : "Loop Swaps Failed!" ,
2020-05-03 19:52:38 +00:00
error : ( err . error && err . error . error ) ? err . error . error : ( err . error ) ? err . error : err
2020-03-16 15:57:57 +00:00
} ) ;
} ) ;
} ;
exports . swap = ( req , res , next ) => {
2020-10-12 22:43:16 +00:00
options = common . getSwapServerOptions ( ) ;
if ( options . url === '' ) { return res . status ( 500 ) . json ( { message : "Loop Out Failed!" , error : { message : 'Loop Server URL is missing in the configuration.' } } ) ; }
options . url = options . url + '/v1/loop/swap/' + req . params . id ;
2020-03-16 15:57:57 +00:00
request ( options ) . then ( function ( body ) {
logger . info ( { fileName : 'Loop' , msg : 'Loop Swap: ' + body } ) ;
body . initiation _time _str = ( ! body . initiation _time ) ? '' : common . convertTimestampToDate ( Math . round ( body . initiation _time / 1000000000 ) ) ;
body . last _update _time _str = ( ! body . last _update _time ) ? '' : common . convertTimestampToDate ( Math . round ( body . last _update _time / 1000000000 ) ) ;
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 [ '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 : 'Loop' , lineNum : 342 , msg : 'Swap Info Error: ' + JSON . stringify ( err ) } ) ;
2020-03-16 15:57:57 +00:00
return res . status ( 500 ) . json ( {
message : "Loop Swap Failed!" ,
2020-05-03 19:52:38 +00:00
error : ( err . error && err . error . error ) ? err . error . error : ( err . error ) ? err . error : err
2020-03-16 15:57:57 +00:00
} ) ;
} ) ;
} ;