@@ -7,10 +7,18 @@ import {
77 toJS ,
88 values ,
99} from 'mobx' ;
10+ import { ChannelEventUpdate , ChannelPoint } from 'types/generated/lnd_pb' ;
1011import Big from 'big.js' ;
1112import { Store } from 'store' ;
1213import { Channel } from '../models' ;
1314
15+ const {
16+ OPEN_CHANNEL ,
17+ CLOSED_CHANNEL ,
18+ ACTIVE_CHANNEL ,
19+ INACTIVE_CHANNEL ,
20+ } = ChannelEventUpdate . UpdateType ;
21+
1422export default class ChannelStore {
1523 private _store : Store ;
1624
@@ -90,10 +98,52 @@ export default class ChannelStore {
9098 }
9199 }
92100
101+ /** update the channel list based on events from the API */
102+ @action . bound
103+ onChannelEvent ( event : ChannelEventUpdate . AsObject ) {
104+ this . _store . log . info ( 'handle incoming channel event' , event ) ;
105+ if ( event . type === INACTIVE_CHANNEL && event . inactiveChannel ) {
106+ // set the channel in state to inactive
107+ const point = this . _channelPointToString ( event . inactiveChannel ) ;
108+ values ( this . channels )
109+ . filter ( c => c . channelPoint === point )
110+ . forEach ( c => {
111+ c . active = false ;
112+ this . _store . log . info ( 'updated channel' , toJS ( c ) ) ;
113+ } ) ;
114+ } else if ( event . type === ACTIVE_CHANNEL && event . activeChannel ) {
115+ // set the channel in state to active
116+ const point = this . _channelPointToString ( event . activeChannel ) ;
117+ values ( this . channels )
118+ . filter ( c => c . channelPoint === point )
119+ . forEach ( c => {
120+ c . active = true ;
121+ this . _store . log . info ( 'updated channel' , toJS ( c ) ) ;
122+ } ) ;
123+ } else if ( event . type === CLOSED_CHANNEL && event . closedChannel ) {
124+ // delete the closed channel
125+ const channel = this . channels . get ( event . closedChannel . chanId ) ;
126+ this . channels . delete ( event . closedChannel . chanId ) ;
127+ this . _store . log . info ( 'removed closed channel' , toJS ( channel ) ) ;
128+ } else if ( event . type === OPEN_CHANNEL && event . openChannel ) {
129+ // add the new opened channel
130+ const channel = new Channel ( this . _store , event . openChannel ) ;
131+ this . channels . set ( channel . chanId , channel ) ;
132+ this . _store . log . info ( 'added new open channel' , toJS ( channel ) ) ;
133+ }
134+ }
135+
93136 /** exports the sorted list of channels to CSV file */
94137 @action . bound
95138 exportChannels ( ) {
96139 this . _store . log . info ( 'exporting Channels to a CSV file' ) ;
97140 this . _store . csv . export ( 'channels' , Channel . csvColumns , toJS ( this . sortedChannels ) ) ;
98141 }
142+
143+ /** converts a base64 encoded channel point to a hex encoded channel point */
144+ private _channelPointToString ( channelPoint : ChannelPoint . AsObject ) {
145+ const txidBytes = channelPoint . fundingTxidBytes as string ;
146+ const txid = Buffer . from ( txidBytes , 'base64' ) . reverse ( ) . toString ( 'hex' ) ;
147+ return `${ txid } :${ channelPoint . outputIndex } ` ;
148+ }
99149}
0 commit comments