2021-12-30 00:08:41 +01: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 getTransactions = ( req , res , next ) => {
logger . log ( { selectedNode : req . session . selectedNode , level : 'INFO' , fileName : 'Transactions' , msg : 'Getting Transactions..' } ) ;
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/transactions' ;
request ( options ) . then ( ( body ) => {
2022-01-16 21:55:50 +01:00
logger . log ( { selectedNode : req . session . selectedNode , level : 'DEBUG' , fileName : 'Transactions' , msg : 'Transactions List Received' , data : body } ) ;
2021-12-30 00:08:41 +01:00
if ( body . transactions && body . transactions . length > 0 ) {
body . transactions = common . sortDescByKey ( body . transactions , 'time_stamp' ) ;
}
2022-01-16 21:55:50 +01:00
logger . log ( { selectedNode : req . session . selectedNode , level : 'INFO' , fileName : 'Transactions' , msg : 'Sorted Transactions List Received' , data : body . transactions } ) ;
2021-12-30 00:08:41 +01:00
res . status ( 200 ) . json ( body . transactions ) ;
} ) . catch ( ( errRes ) => {
const err = common . handleError ( errRes , 'Transactions' , 'List Transactions Error' , req . session . selectedNode ) ;
return res . status ( err . statusCode ) . json ( { message : err . message , error : err . error } ) ;
} ) ;
} ;
export const postTransactions = ( req , res , next ) => {
logger . log ( { selectedNode : req . session . selectedNode , level : 'INFO' , fileName : 'Transactions' , msg : 'Sending Transaction..' } ) ;
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/transactions' ;
options . form = {
amount : req . body . amount ,
addr : req . body . address ,
sat _per _byte : req . body . fees ,
target _conf : req . body . blocks
} ;
if ( req . body . sendAll ) {
options . form . send _all = req . body . sendAll ;
}
options . form = JSON . stringify ( options . form ) ;
request . post ( options ) . then ( ( body ) => {
2022-01-16 21:55:50 +01:00
logger . log ( { selectedNode : req . session . selectedNode , level : 'INFO' , fileName : 'Transactions' , msg : 'Transaction Sent' , data : body } ) ;
2021-12-30 00:08:41 +01:00
res . status ( 201 ) . json ( body ) ;
} ) . catch ( ( errRes ) => {
const err = common . handleError ( errRes , 'Transactions' , 'Send Transaction Error' , req . session . selectedNode ) ;
return res . status ( err . statusCode ) . json ( { message : err . message , error : err . error } ) ;
} ) ;
} ;