1+ /**
2+ * @fileOverview actions to set channel state within the app and to
3+ * call the corresponding GRPC apis for channel management.
4+ */
5+
16import { toSatoshis , parseSat } from '../helper' ;
27import * as log from './log' ;
38
@@ -13,16 +18,31 @@ class ChannelAction {
1318 // Create channel actions
1419 //
1520
21+ /**
22+ * Initiate the create channel view by resetting input values
23+ * and then navigating to the view.
24+ * @return {undefined }
25+ */
1626 initCreate ( ) {
1727 this . _store . channel . pubkeyAtHost = '' ;
1828 this . _store . channel . amount = '' ;
1929 this . _nav . goChannelCreate ( ) ;
2030 }
2131
32+ /**
33+ * Set the amount input for the create channel view. This amount
34+ * is either in btc or fiat depending on user settings.
35+ * @param {string } options.amount The string formatted number
36+ */
2237 setAmount ( { amount } ) {
2338 this . _store . channel . amount = amount ;
2439 }
2540
41+ /**
42+ * Set the channel public key and hostname in a single variable
43+ * which can be parsed before calling the create channel grpc api.
44+ * @param {string } options.pubkeyAtHost The combined public key and host
45+ */
2646 setPubkeyAtHost ( { pubkeyAtHost } ) {
2747 this . _store . channel . pubkeyAtHost = pubkeyAtHost ;
2848 }
@@ -31,17 +51,33 @@ class ChannelAction {
3151 // Channel list actions
3252 //
3353
54+ /**
55+ * Initiate the channel list view by navigating to the view and updating
56+ * the app's channel state by calling all necessary grpc apis.
57+ * @return {undefined }
58+ */
3459 init ( ) {
3560 this . _nav . goChannels ( ) ;
3661 this . update ( ) ;
3762 }
3863
64+ /**
65+ * Select a channel item from the channel list view and then navigate
66+ * to the detail view to list channel parameters.
67+ * @param {Object } options.item The selected channel object
68+ * @return {undefined }
69+ */
3970 select ( { item } ) {
4071 this . _store . selectedChannel = item ;
4172 this . _nav . goChannelDetail ( ) ;
4273 this . update ( ) ;
4374 }
4475
76+ /**
77+ * Update the peers, channels, and pending channels in the app state
78+ * by querying all required grpc apis.
79+ * @return {Promise<undefined> }
80+ */
4581 async update ( ) {
4682 await Promise . all ( [
4783 this . getPeers ( ) ,
@@ -50,6 +86,15 @@ class ChannelAction {
5086 ] ) ;
5187 }
5288
89+ //
90+ // Generic channel actions
91+ //
92+
93+ /**
94+ * List the open channels by calling the respective grpc api and updating
95+ * the channels array in the global store.
96+ * @return {Promise<undefined> }
97+ */
5398 async getChannels ( ) {
5499 try {
55100 const { channels } = await this . _grpc . sendCommand ( 'listChannels' ) ;
@@ -69,6 +114,11 @@ class ChannelAction {
69114 }
70115 }
71116
117+ /**
118+ * List the pending channels by calling the respective grpc api and updating
119+ * the pendingChannels array in the global store.
120+ * @return {Promise<undefined> }
121+ */
72122 async getPendingChannels ( ) {
73123 try {
74124 const response = await this . _grpc . sendCommand ( 'pendingChannels' ) ;
@@ -112,6 +162,11 @@ class ChannelAction {
112162 }
113163 }
114164
165+ /**
166+ * List the peers by calling the respective grpc api and updating
167+ * the peers array in the global store.
168+ * @return {Promise<undefined> }
169+ */
115170 async getPeers ( ) {
116171 try {
117172 const { peers } = await this . _grpc . sendCommand ( 'listPeers' ) ;
@@ -131,6 +186,13 @@ class ChannelAction {
131186 }
132187 }
133188
189+ /**
190+ * Attempt to connect to a peer and open a channel in a single call.
191+ * If a connection already exists, just a channel will be opened.
192+ * This action can be called from a view event handler as does all
193+ * the necessary error handling and notification display.
194+ * @return {Promise<undefined> }
195+ */
134196 async connectAndOpen ( ) {
135197 try {
136198 const { channel, settings } = this . _store ;
@@ -149,6 +211,13 @@ class ChannelAction {
149211 }
150212 }
151213
214+ /**
215+ * Connect to peer and fail gracefully by catching exceptions and
216+ * logging their output.
217+ * @param {string } options.host The hostname of the peer
218+ * @param {string } options.pubkey The public key of the peer
219+ * @return {Promise<undefined> }
220+ */
152221 async connectToPeer ( { host, pubkey } ) {
153222 try {
154223 await this . _grpc . sendCommand ( 'connectPeer' , {
@@ -159,6 +228,13 @@ class ChannelAction {
159228 }
160229 }
161230
231+ /**
232+ * Open a channel to a peer without advertising it and update channel
233+ * state on data event from the streaming grpc api.
234+ * @param {string } options.pubkey The public key of the peer
235+ * @param {number } options.amount The amount in satoshis to fund the channel
236+ * @return {Promise<undefined> }
237+ */
162238 async openChannel ( { pubkey, amount } ) {
163239 const stream = this . _grpc . sendStreamCommand ( 'openChannel' , {
164240 node_pubkey : new Buffer ( pubkey , 'hex' ) ,
@@ -173,6 +249,12 @@ class ChannelAction {
173249 } ) ;
174250 }
175251
252+ /**
253+ * Close the selected channel by attempting a cooperative close.
254+ * This action can be called from a view event handler as does all
255+ * the necessary error handling and notification display.
256+ * @return {Promise<undefined> }
257+ */
176258 async closeSelectedChannel ( ) {
177259 try {
178260 const { selectedChannel } = this . _store ;
@@ -183,6 +265,14 @@ class ChannelAction {
183265 }
184266 }
185267
268+ /**
269+ * Close a channel using the grpc streaming api and update the state
270+ * on data events. Once the channel close is complete the channel will
271+ * be removed from the channels array in the store.
272+ * @param {string } options.channelPoint The channel identifier
273+ * @param {Boolean } options.force Force or cooperative close
274+ * @return {Promise<undefined> }
275+ */
186276 async closeChannel ( { channelPoint, force = false } ) {
187277 const stream = this . _grpc . sendStreamCommand ( 'closeChannel' , {
188278 channel_point : this . _parseChannelPoint ( channelPoint ) ,
0 commit comments