@@ -58,6 +58,7 @@ import {
5858 TLspLogPayload ,
5959 TLspLogEvent ,
6060 TChannelMonitor ,
61+ TCreateChannelReq ,
6162} from './utils/types' ;
6263import {
6364 appendPath ,
@@ -170,10 +171,10 @@ class LightningManager {
170171 this . onBroadcastTransaction . bind ( this ) ,
171172 ) ;
172173 //Channel manager handle events:
173- ldk . onEvent (
174- EEventTypes . channel_manager_funding_generation_ready ,
175- this . onChannelManagerFundingGenerationReady . bind ( this ) ,
176- ) ;
174+ // ldk.onEvent(
175+ // EEventTypes.channel_manager_funding_generation_ready,
176+ // this.onChannelManagerFundingGenerationReady.bind(this),
177+ // );
177178 ldk . onEvent (
178179 EEventTypes . channel_manager_payment_claimable ,
179180 this . onChannelManagerPaymentClaimable . bind ( this ) ,
@@ -2050,13 +2051,13 @@ class LightningManager {
20502051 //LDK channel manager events
20512052 //All events and their values: https://docs.rs/lightning/latest/lightning/util/events/enum.Event.html
20522053 //Sample node for examples on how to handle events: https://github.com/lightningdevkit/ldk-sample/blob/c0a722430b8fbcb30310d64487a32aae839da3e8/src/main.rs#L600
2053- private onChannelManagerFundingGenerationReady (
2054- res : TChannelManagerFundingGenerationReady ,
2055- ) : void {
2056- console . log (
2057- `onChannelManagerFundingGenerationReady: ${ JSON . stringify ( res ) } ` ,
2058- ) ; //TODO
2059- }
2054+ // private onChannelManagerFundingGenerationReady(
2055+ // res: TChannelManagerFundingGenerationReady,
2056+ // ): void {
2057+ // console.log(
2058+ // `onChannelManagerFundingGenerationReady: ${JSON.stringify(res)}`,
2059+ // ); //TODO
2060+ // }
20602061
20612062 private async onChannelManagerPaymentClaimable (
20622063 res : TChannelManagerClaim ,
@@ -2437,6 +2438,39 @@ class LightningManager {
24372438 remotePersist : true ,
24382439 } ) ;
24392440 }
2441+
2442+ /**
2443+ * Creates a new channel with the provided peer and then listens for the channel manager funding generation ready event.
2444+ * Once event is triggered, those details can be used to create the funding transaction.
2445+ */
2446+ async createChannel ( req : TCreateChannelReq ) : Promise < Result < TChannelManagerFundingGenerationReady > > {
2447+ const res = await ldk . createChannel ( req ) ;
2448+ if ( res . isErr ( ) ) {
2449+ return err ( res . error ) ;
2450+ }
2451+
2452+ return new Promise ( ( resolve , reject ) => {
2453+ // Channel funding ready event should be instant but if it fails and we don't get the event, we should reject.
2454+ const timeout = setTimeout ( ( ) => {
2455+ reject ( err ( new Error ( "Event not triggered within 5 seconds" ) ) ) ;
2456+ } , 5000 ) ;
2457+
2458+ // Listen for the event for the channel funding details
2459+ ldk . onEvent ( EEventTypes . channel_manager_funding_generation_ready , ( eventRes : TChannelManagerFundingGenerationReady ) => {
2460+ clearTimeout ( timeout ) ;
2461+ resolve ( ok ( eventRes ) ) ;
2462+ } ) ;
2463+
2464+ ldk . onEvent ( EEventTypes . channel_manager_channel_closed , ( eventRes : TChannelManagerChannelClosed ) => {
2465+ if ( eventRes . channel_id === res . value ) {
2466+ clearTimeout ( timeout ) ;
2467+ reject ( err ( "Channel closed before funding" ) ) ;
2468+ }
2469+ } ) ;
2470+ } ) ;
2471+ }
2472+
2473+ //TODO: fund channel
24402474}
24412475
24422476export default new LightningManager ( ) ;
0 commit comments