@ -7,7 +7,7 @@ import { Channel, PaymentReceived, PaymentRelayed } from '../../shared/models/ec
export const ECLReducer = createReducer ( initECLState ,
export const ECLReducer = createReducer ( initECLState ,
on ( updateECLAPICallStatus , ( state , { payload } ) = > {
on ( updateECLAPICallStatus , ( state , { payload } ) = > {
const updatedApisCallStatus = JSON . parse ( JSON . stringify ( state . apisCallStatus ) ) ;
const updatedApisCallStatus = JSON . parse ( JSON . stringify ( state . apisCallStatus ) ) ;
updatedApisCallStatus [ payload . action ] = {
updatedApisCallStatus [ payload . action || 0 ] = {
status : payload.status ,
status : payload.status ,
statusCode : payload.statusCode ,
statusCode : payload.statusCode ,
message : payload.message ,
message : payload.message ,
@ -88,11 +88,11 @@ export const ECLReducer = createReducer(initECLState,
on ( setPayments , ( state , { payload } ) = > {
on ( setPayments , ( state , { payload } ) = > {
if ( payload && payload . sent ) {
if ( payload && payload . sent ) {
const storedChannels = [ . . . state . activeChannels , . . . state . pendingChannels , . . . state . inactiveChannels ] ;
const storedChannels = [ . . . state . activeChannels , . . . state . pendingChannels , . . . state . inactiveChannels ] ;
payload . sent . map ( ( sentPayment ) = > {
payload . sent ? . map ( ( sentPayment ) = > {
const peerFound = state . peers . find ( ( peer ) = > peer . nodeId === sentPayment . recipientNodeId ) ;
const peerFound = state . peers . find ( ( peer ) = > peer . nodeId === sentPayment . recipientNodeId ) ;
sentPayment . recipientNodeAlias = peerFound ? peerFound.alias : sentPayment.recipientNodeId ;
sentPayment . recipientNodeAlias = peerFound ? peerFound.alias : sentPayment.recipientNodeId ;
if ( sentPayment . parts ) {
if ( sentPayment . parts ) {
sentPayment . parts . map ( ( part ) = > {
sentPayment . parts ? . map ( ( part ) = > {
const channelFound = storedChannels . find ( ( channel ) = > channel . channelId === part . toChannelId ) ;
const channelFound = storedChannels . find ( ( channel ) = > channel . channelId === part . toChannelId ) ;
part . toChannelAlias = channelFound ? channelFound.alias : part.toChannelId ;
part . toChannelAlias = channelFound ? channelFound.alias : part.toChannelId ;
return sentPayment . parts ;
return sentPayment . parts ;
@ -118,7 +118,7 @@ export const ECLReducer = createReducer(initECLState,
} ) ) ,
} ) ) ,
on ( addInvoice , ( state , { payload } ) = > {
on ( addInvoice , ( state , { payload } ) = > {
const newInvoices = state . invoices ;
const newInvoices = state . invoices ;
newInvoices . unshift ( payload ) ;
newInvoices ? . unshift ( payload ) ;
return {
return {
. . . state ,
. . . state ,
invoices : newInvoices
invoices : newInvoices
@ -130,12 +130,12 @@ export const ECLReducer = createReducer(initECLState,
} ) ) ,
} ) ) ,
on ( updateInvoice , ( state , { payload } ) = > {
on ( updateInvoice , ( state , { payload } ) = > {
let modifiedInvoices = state . invoices ;
let modifiedInvoices = state . invoices ;
modifiedInvoices = modifiedInvoices . map ( ( invoice ) = > {
modifiedInvoices = modifiedInvoices ? . map ( ( invoice ) = > {
if ( invoice . paymentHash === payload . paymentHash ) {
if ( invoice . paymentHash === payload . paymentHash ) {
if ( payload . hasOwnProperty ( 'type' ) ) {
if ( payload . hasOwnProperty ( 'type' ) ) {
const updatedInvoice = JSON . parse ( JSON . stringify ( invoice ) ) ;
const updatedInvoice = JSON . parse ( JSON . stringify ( invoice ) ) ;
updatedInvoice . amountSettled = ( ( < PaymentReceived > payload ) . parts && ( < PaymentReceived > payload ) . parts . length && ( < PaymentReceived > payload ) . parts . length > 0 && ( < PaymentReceived > payload ) . parts [ 0 ] . amount ) ? ( <PaymentReceived > payload ) . parts [ 0 ] . amount / 1000 : 0 ;
updatedInvoice . amountSettled = ( ( < PaymentReceived > payload ) . parts && ( < PaymentReceived > payload ) . parts . length && ( < PaymentReceived > payload ) . parts . length > 0 && ( < PaymentReceived > payload ) . parts [ 0 ] . amount ) ? ( ( <PaymentReceived > payload ) . parts [ 0 ] . amount || 0 ) / 1000 : 0 ;
updatedInvoice . receivedAt = ( ( < PaymentReceived > payload ) . parts && ( < PaymentReceived > payload ) . parts . length && ( < PaymentReceived > payload ) . parts . length > 0 && ( < PaymentReceived > payload ) . parts [ 0 ] . timestamp ) ? Math . round ( ( <PaymentReceived > payload ) . parts [ 0 ] . timestamp / 1000 ) : 0 ;
updatedInvoice . receivedAt = ( ( < PaymentReceived > payload ) . parts && ( < PaymentReceived > payload ) . parts . length && ( < PaymentReceived > payload ) . parts . length > 0 && ( < PaymentReceived > payload ) . parts [ 0 ] . timestamp ) ? Math . round ( ( ( <PaymentReceived > payload ) . parts [ 0 ] . timestamp || 0 ) / 1000 ) : 0 ;
updatedInvoice . status = 'received' ;
updatedInvoice . status = 'received' ;
return updatedInvoice ;
return updatedInvoice ;
} else {
} else {
@ -151,9 +151,9 @@ export const ECLReducer = createReducer(initECLState,
} ) ,
} ) ,
on ( updateChannelState , ( state , { payload } ) = > {
on ( updateChannelState , ( state , { payload } ) = > {
let modifiedPendingChannels = state . pendingChannels ;
let modifiedPendingChannels = state . pendingChannels ;
modifiedPendingChannels = modifiedPendingChannels . map ( ( pendingChannel ) = > {
modifiedPendingChannels = modifiedPendingChannels ? . map ( ( pendingChannel ) = > {
if ( pendingChannel . channelId === payload . channelId && pendingChannel . nodeId === payload . remoteNodeId ) {
if ( pendingChannel . channelId === payload . channelId && pendingChannel . nodeId === payload . remoteNodeId ) {
payload . currentState = payload . currentState . replace ( /_/g , ' ' ) ;
payload . currentState = payload . currentState ? . replace ( /_/g , ' ' ) ;
pendingChannel . state = payload . currentState ;
pendingChannel . state = payload . currentState ;
}
}
return pendingChannel ;
return pendingChannel ;
@ -166,17 +166,19 @@ export const ECLReducer = createReducer(initECLState,
on ( updateRelayedPayment , ( state , { payload } ) = > {
on ( updateRelayedPayment , ( state , { payload } ) = > {
const modifiedPayments = state . payments ;
const modifiedPayments = state . payments ;
const updatedPayload = mapAliases ( payload , [ . . . state . activeChannels , . . . state . pendingChannels , . . . state . inactiveChannels ] ) ;
const updatedPayload = mapAliases ( payload , [ . . . state . activeChannels , . . . state . pendingChannels , . . . state . inactiveChannels ] ) ;
updatedPayload . amountIn = Math . round ( payload . amountIn / 1000 ) ;
updatedPayload . amountIn = Math . round ( ( payload . amountIn || 0 ) / 1000 ) ;
updatedPayload . amountOut = Math . round ( payload . amountOut / 1000 ) ;
updatedPayload . amountOut = Math . round ( ( payload . amountOut || 0 ) / 1000 ) ;
modifiedPayments . relayed . unshift ( updatedPayload ) ;
modifiedPayments . relayed ? . unshift ( updatedPayload ) ;
const feeSats = payload . amountIn - payload . amountOut ;
const feeSats = ( payload . amountIn ||0 ) - ( payload . amountOut || 0 ) ;
const modifiedLightningBalance = { localBalance : ( state . lightningBalance . localBalance + feeSats ) , remoteBalance : ( state . lightningBalance . remoteBalance - feeSats ) } ;
const modifiedLightningBalance = { localBalance : ( state . lightningBalance . localBalance + feeSats ) , remoteBalance : ( state . lightningBalance . remoteBalance - feeSats ) } ;
const modifiedChannelStatus = state . channelsStatus ;
const modifiedChannelStatus = state . channelsStatus ;
modifiedChannelStatus . active . capacity = state . channelsStatus . active . capacity + feeSats ;
if ( modifiedChannelStatus . active ) {
modifiedChannelStatus . active . capacity = ( state . channelsStatus ? . active ? . capacity || 0 ) + feeSats ;
}
const modifiedFees = {
const modifiedFees = {
daily_fee : ( state . fees . daily_fee + feeSats ) , daily_txs : ( state . fees . daily_txs + 1 ) ,
daily_fee : ( ( state . fees . daily_fee || 0 ) + feeSats ) , daily_txs : ( ( state . fees . daily_txs || 0 ) + 1 ) ,
weekly_fee : ( state . fees . weekly_fee + feeSats ) , weekly_txs : ( state . fees . weekly_txs + 1 ) ,
weekly_fee : ( ( state . fees . weekly_fee || 0 ) + feeSats ) , weekly_txs : ( ( state . fees . weekly_txs || 0 ) + 1 ) ,
monthly_fee : ( state . fees . monthly_fee + feeSats ) , monthly_txs : ( state . fees . monthly_txs + 1 )
monthly_fee : ( ( state . fees . monthly_fee || 0 ) + feeSats ) , monthly_txs : ( ( state . fees . monthly_txs || 0 ) + 1 )
} ;
} ;
const modifiedActiveChannels = state . activeChannels ;
const modifiedActiveChannels = state . activeChannels ;
let foundFrom = false ;
let foundFrom = false ;
@ -184,16 +186,16 @@ export const ECLReducer = createReducer(initECLState,
for ( const channel of modifiedActiveChannels ) {
for ( const channel of modifiedActiveChannels ) {
if ( channel . channelId === payload . fromChannelId ) {
if ( channel . channelId === payload . fromChannelId ) {
foundFrom = true ;
foundFrom = true ;
const channelTotal = channel . toLocal + channel . toRemote ;
const channelTotal = ( channel . toLocal || 0 ) + ( channel . toRemote || 0 ) ;
channel . toLocal = channel . toLocal + updatedPayload . amountIn ;
channel . toLocal = ( channel . toLocal || 0 ) + updatedPayload . amountIn ;
channel . toRemote = channel . toRemote - updatedPayload . amountIn ;
channel . toRemote = ( channel . toRemote || 0 ) - updatedPayload . amountIn ;
channel . balancedness = ( channelTotal === 0 ) ? 1 : + ( 1 - Math . abs ( ( channel . toLocal - channel . toRemote ) / channelTotal ) ) . toFixed ( 3 ) ;
channel . balancedness = ( channelTotal === 0 ) ? 1 : + ( 1 - Math . abs ( ( channel . toLocal - channel . toRemote ) / channelTotal ) ) . toFixed ( 3 ) ;
}
}
if ( channel . channelId === payload . toChannelId ) {
if ( channel . channelId === payload . toChannelId ) {
foundTo = true ;
foundTo = true ;
const channelTotal = channel . toLocal + channel . toRemote ;
const channelTotal = ( channel . toLocal || 0 ) + ( channel . toRemote || 0 ) ;
channel . toLocal = channel . toLocal - updatedPayload . amountOut ;
channel . toLocal = ( channel . toLocal || 0 ) - updatedPayload . amountOut ;
channel . toRemote = channel . toRemote + updatedPayload . amountOut ;
channel . toRemote = ( channel . toRemote || 0 ) + updatedPayload . amountOut ;
channel . balancedness = ( channelTotal === 0 ) ? 1 : + ( 1 - Math . abs ( ( channel . toLocal - channel . toRemote ) / channelTotal ) ) . toFixed ( 3 ) ;
channel . balancedness = ( channelTotal === 0 ) ? 1 : + ( 1 - Math . abs ( ( channel . toLocal - channel . toRemote ) / channelTotal ) ) . toFixed ( 3 ) ;
}
}
if ( foundTo && foundFrom ) {
if ( foundTo && foundFrom ) {
@ -215,14 +217,14 @@ const mapAliases = (rlEvent: PaymentRelayed, storedChannels: Channel[]) => {
if ( rlEvent . type === 'payment-relayed' ) {
if ( rlEvent . type === 'payment-relayed' ) {
if ( storedChannels && storedChannels . length > 0 ) {
if ( storedChannels && storedChannels . length > 0 ) {
for ( let idx = 0 ; idx < storedChannels . length ; idx ++ ) {
for ( let idx = 0 ; idx < storedChannels . length ; idx ++ ) {
if ( storedChannels [ idx ] . channelId . toString ( ) === rlEvent . fromChannelId ) {
if ( storedChannels [ idx ] . channelId ? . toString ( ) === rlEvent . fromChannelId ) {
rlEvent . fromChannelAlias = storedChannels [ idx ] . alias ? storedChannels [ idx ] . alias : rlEvent.fromChannelId ;
rlEvent . fromChannelAlias = storedChannels [ idx ] . alias ? storedChannels [ idx ] . alias : rlEvent.fromChannelId ;
rlEvent . fromShortChannelId = storedChannels [ idx ] . shortChannelId ? storedChannels [ idx ] . shortChannelId : '' ;
rlEvent . fromShortChannelId = storedChannels [ idx ] . shortChannelId ? storedChannels [ idx ] . shortChannelId : '' ;
if ( rlEvent . toChannelAlias ) {
if ( rlEvent . toChannelAlias ) {
return rlEvent ;
return rlEvent ;
}
}
}
}
if ( storedChannels [ idx ] . channelId . toString ( ) === rlEvent . toChannelId ) {
if ( storedChannels [ idx ] . channelId ? . toString ( ) === rlEvent . toChannelId ) {
rlEvent . toChannelAlias = storedChannels [ idx ] . alias ? storedChannels [ idx ] . alias : rlEvent.toChannelId ;
rlEvent . toChannelAlias = storedChannels [ idx ] . alias ? storedChannels [ idx ] . alias : rlEvent.toChannelId ;
rlEvent . toShortChannelId = storedChannels [ idx ] . shortChannelId ? storedChannels [ idx ] . shortChannelId : '' ;
rlEvent . toShortChannelId = storedChannels [ idx ] . shortChannelId ? storedChannels [ idx ] . shortChannelId : '' ;
if ( rlEvent . fromChannelAlias ) {
if ( rlEvent . fromChannelAlias ) {
@ -231,70 +233,70 @@ const mapAliases = (rlEvent: PaymentRelayed, storedChannels: Channel[]) => {
}
}
if ( idx === storedChannels . length - 1 ) {
if ( idx === storedChannels . length - 1 ) {
if ( ! rlEvent . fromChannelAlias ) {
if ( ! rlEvent . fromChannelAlias ) {
rlEvent . fromChannelAlias = rlEvent . fromChannelId . substring ( 0 , 17 ) + '...' ;
rlEvent . fromChannelAlias = rlEvent . fromChannelId ? . substring ( 0 , 17 ) + '...' ;
rlEvent . fromShortChannelId = '' ;
rlEvent . fromShortChannelId = '' ;
}
}
if ( ! rlEvent . toChannelAlias ) {
if ( ! rlEvent . toChannelAlias ) {
rlEvent . toChannelAlias = rlEvent . toChannelId . substring ( 0 , 17 ) + '...' ;
rlEvent . toChannelAlias = rlEvent . toChannelId ? . substring ( 0 , 17 ) + '...' ;
rlEvent . toShortChannelId = '' ;
rlEvent . toShortChannelId = '' ;
}
}
}
}
}
}
} else {
} else {
rlEvent . fromChannelAlias = rlEvent . fromChannelId . substring ( 0 , 17 ) + '...' ;
rlEvent . fromChannelAlias = rlEvent . fromChannelId ? . substring ( 0 , 17 ) + '...' ;
rlEvent . fromShortChannelId = '' ;
rlEvent . fromShortChannelId = '' ;
rlEvent . toChannelAlias = rlEvent . toChannelId . substring ( 0 , 17 ) + '...' ;
rlEvent . toChannelAlias = rlEvent . toChannelId ? . substring ( 0 , 17 ) + '...' ;
rlEvent . toShortChannelId = '' ;
rlEvent . toShortChannelId = '' ;
}
}
} else if ( rlEvent . type = 'trampoline-payment-relayed' ) {
} else if ( rlEvent . type = 'trampoline-payment-relayed' ) {
if ( storedChannels && storedChannels . length > 0 ) {
if ( storedChannels && storedChannels . length > 0 ) {
for ( let idx = 0 ; idx < storedChannels . length ; idx ++ ) {
for ( let idx = 0 ; idx < storedChannels . length ; idx ++ ) {
rlEvent . incoming . forEach ( ( incomingEvent ) = > {
rlEvent . incoming ? . forEach ( ( incomingEvent ) = > {
if ( storedChannels [ idx ] . channelId . toString ( ) === incomingEvent . channelId ) {
if ( storedChannels [ idx ] . channelId ? . toString ( ) === incomingEvent . channelId ) {
incomingEvent . channelAlias = storedChannels [ idx ] . alias ? storedChannels [ idx ] . alias : incomingEvent.channelId ;
incomingEvent . channelAlias = storedChannels [ idx ] . alias ? storedChannels [ idx ] . alias : incomingEvent.channelId ;
incomingEvent . shortChannelId = storedChannels [ idx ] . shortChannelId ? storedChannels [ idx ] . shortChannelId : '' ;
incomingEvent . shortChannelId = storedChannels [ idx ] . shortChannelId ? storedChannels [ idx ] . shortChannelId : '' ;
}
}
} ) ;
} ) ;
rlEvent . outgoing . forEach ( ( outgoingEvent ) = > {
rlEvent . outgoing ? . forEach ( ( outgoingEvent ) = > {
if ( storedChannels [ idx ] . channelId . toString ( ) === outgoingEvent . channelId ) {
if ( storedChannels [ idx ] . channelId ? . toString ( ) === outgoingEvent . channelId ) {
outgoingEvent . channelAlias = storedChannels [ idx ] . alias ? storedChannels [ idx ] . alias : outgoingEvent.channelId ;
outgoingEvent . channelAlias = storedChannels [ idx ] . alias ? storedChannels [ idx ] . alias : outgoingEvent.channelId ;
outgoingEvent . shortChannelId = storedChannels [ idx ] . shortChannelId ? storedChannels [ idx ] . shortChannelId : '' ;
outgoingEvent . shortChannelId = storedChannels [ idx ] . shortChannelId ? storedChannels [ idx ] . shortChannelId : '' ;
}
}
} ) ;
} ) ;
if ( idx === storedChannels . length - 1 ) {
if ( idx === storedChannels . length - 1 ) {
if ( ! rlEvent . incoming [ 0 ] . channelAlias ) {
if ( rlEvent . incoming && rlEvent . incoming . length && rlEvent . incoming . length > 0 && ! rlEvent . incoming [ 0 ] . channelAlias ) {
rlEvent . incoming . forEach ( ( incomingEvent ) = > {
rlEvent . incoming ? . forEach ( ( incomingEvent ) = > {
incomingEvent . channelAlias = incomingEvent . channelId . substring ( 0 , 17 ) + '...' ;
incomingEvent . channelAlias = incomingEvent . channelId ? . substring ( 0 , 17 ) + '...' ;
incomingEvent . shortChannelId = '' ;
incomingEvent . shortChannelId = '' ;
} ) ;
} ) ;
}
}
if ( ! rlEvent . outgoing [ 0 ] . channelAlias ) {
if ( rlEvent . outgoing && rlEvent . outgoing . length && rlEvent . outgoing . length > 0 && ! rlEvent . outgoing [ 0 ] . channelAlias ) {
rlEvent . outgoing . forEach ( ( outgoingEvent ) = > {
rlEvent . outgoing ? . forEach ( ( outgoingEvent ) = > {
outgoingEvent . channelAlias = outgoingEvent . channelId . substring ( 0 , 17 ) + '...' ;
outgoingEvent . channelAlias = outgoingEvent . channelId ? . substring ( 0 , 17 ) + '...' ;
outgoingEvent . shortChannelId = '' ;
outgoingEvent . shortChannelId = '' ;
} ) ;
} ) ;
}
}
}
}
}
}
} else {
} else {
rlEvent . incoming . forEach ( ( incomingEvent ) = > {
rlEvent . incoming ? . forEach ( ( incomingEvent ) = > {
incomingEvent . channelAlias = incomingEvent . channelId . substring ( 0 , 17 ) + '...' ;
incomingEvent . channelAlias = incomingEvent . channelId ? . substring ( 0 , 17 ) + '...' ;
incomingEvent . shortChannelId = '' ;
incomingEvent . shortChannelId = '' ;
} ) ;
} ) ;
rlEvent . outgoing . forEach ( ( outgoingEvent ) = > {
rlEvent . outgoing ? . forEach ( ( outgoingEvent ) = > {
outgoingEvent . channelAlias = outgoingEvent . channelId . substring ( 0 , 17 ) + '...' ;
outgoingEvent . channelAlias = outgoingEvent . channelId ? . substring ( 0 , 17 ) + '...' ;
outgoingEvent . shortChannelId = '' ;
outgoingEvent . shortChannelId = '' ;
} ) ;
} ) ;
}
}
rlEvent . amountIn = rlEvent . incoming . reduce ( ( acc , curr ) = > acc + curr . amount , 0 ) ;
rlEvent . amountIn = rlEvent . incoming ? . reduce ( ( acc , curr ) = > acc + curr . amount , 0 ) ;
rlEvent . fromChannelId = rlEvent . incoming [ 0 ] . channelId ;
rlEvent . fromChannelId = rlEvent . incoming && rlEvent . incoming . length ? rlEvent . incoming [ 0 ] . channelId : '' ;
rlEvent . fromChannelAlias = rlEvent . incoming [ 0 ] . channelAlias ;
rlEvent . fromChannelAlias = rlEvent . incoming && rlEvent . incoming . length ? rlEvent . incoming [ 0 ] . channelAlias : '' ;
rlEvent . fromShortChannelId = rlEvent . incoming [ 0 ] . shortChannelId ;
rlEvent . fromShortChannelId = rlEvent . incoming && rlEvent . incoming . length ? rlEvent . incoming [ 0 ] . shortChannelId : '' ;
rlEvent . amountOut = rlEvent . outgoing . reduce ( ( acc , curr ) = > acc + curr . amount , 0 ) ;
rlEvent . amountOut = rlEvent . outgoing ? . reduce ( ( acc , curr ) = > acc + curr . amount , 0 ) ;
rlEvent . toChannelId = rlEvent . outgoing [ 0 ] . channelId ;
rlEvent . toChannelId = rlEvent . outgoing && rlEvent . outgoing . length ? rlEvent . outgoing [ 0 ] . channelId : '' ;
rlEvent . toChannelAlias = rlEvent . outgoing [ 0 ] . channelAlias ;
rlEvent . toChannelAlias = rlEvent . outgoing && rlEvent . outgoing . length ? rlEvent . outgoing [ 0 ] . channelAlias : '' ;
rlEvent . toShortChannelId = rlEvent . outgoing [ 0 ] . shortChannelId ;
rlEvent . toShortChannelId = rlEvent . outgoing && rlEvent . outgoing . length ? rlEvent . outgoing [ 0 ] . shortChannelId : '' ;
}
}
return rlEvent ;
return rlEvent ;
} ;
} ;