@@ -16,6 +16,7 @@ import (
1616 proxy "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
1717 "github.com/lightninglabs/lndclient"
1818 "github.com/lightninglabs/loop"
19+ "github.com/lightninglabs/loop/instantout"
1920 "github.com/lightninglabs/loop/loopd/perms"
2021 "github.com/lightninglabs/loop/loopdb"
2122 "github.com/lightninglabs/loop/sweepbatcher"
@@ -24,6 +25,7 @@ import (
2425 loop_looprpc "github.com/lightninglabs/loop/looprpc"
2526
2627 loop_swaprpc "github.com/lightninglabs/loop/swapserverrpc"
28+ "github.com/lightningnetwork/lnd/clock"
2729 "github.com/lightningnetwork/lnd/lntypes"
2830 "github.com/lightningnetwork/lnd/macaroons"
2931 "google.golang.org/grpc"
@@ -67,10 +69,6 @@ type Daemon struct {
6769 // same process.
6870 swapClientServer
6971
70- // reservationManager is the manager that handles all reservation state
71- // machines.
72- reservationManager * reservation.Manager
73-
7472 // ErrChan is an error channel that users of the Daemon struct must use
7573 // to detect runtime errors and also whether a shutdown is fully
7674 // completed.
@@ -429,6 +427,11 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
429427 swapClient .Conn ,
430428 )
431429
430+ // Create an instantout server client.
431+ instantOutClient := loop_swaprpc .NewInstantSwapServerClient (
432+ swapClient .Conn ,
433+ )
434+
432435 // Both the client RPC server and the swap server client should stop
433436 // on main context cancel. So we create it early and pass it down.
434437 d .mainCtx , d .mainCtxCancel = context .WithCancel (context .Background ())
@@ -486,7 +489,11 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
486489 }
487490 }
488491
489- // Create the reservation rpc server.
492+ var (
493+ reservationManager * reservation.Manager
494+ instantOutManager * instantout.Manager
495+ )
496+ // Create the reservation and instantout managers.
490497 if d .cfg .EnableExperimental {
491498 reservationStore := reservation .NewSQLStore (baseDb )
492499 reservationConfig := & reservation.Config {
@@ -497,9 +504,30 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
497504 FetchL402 : swapClient .Server .FetchL402 ,
498505 }
499506
500- d . reservationManager = reservation .NewManager (
507+ reservationManager = reservation .NewManager (
501508 reservationConfig ,
502509 )
510+
511+ // Create the instantout services.
512+ instantOutStore := instantout .NewSQLStore (
513+ baseDb , clock .NewDefaultClock (), reservationStore ,
514+ d .lnd .ChainParams ,
515+ )
516+ instantOutConfig := & instantout.Config {
517+ Store : instantOutStore ,
518+ LndClient : d .lnd .Client ,
519+ RouterClient : d .lnd .Router ,
520+ ChainNotifier : d .lnd .ChainNotifier ,
521+ Signer : d .lnd .Signer ,
522+ Wallet : d .lnd .WalletKit ,
523+ ReservationManager : reservationManager ,
524+ InstantOutClient : instantOutClient ,
525+ Network : d .lnd .ChainParams ,
526+ }
527+
528+ instantOutManager = instantout .NewInstantOutManager (
529+ instantOutConfig ,
530+ )
503531 }
504532
505533 // Now finally fully initialize the swap client RPC server instance.
@@ -513,7 +541,8 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
513541 subscribers : make (map [int ]chan <- interface {}),
514542 statusChan : make (chan loop.SwapInfo ),
515543 mainCtx : d .mainCtx ,
516- reservationManager : d .reservationManager ,
544+ reservationManager : reservationManager ,
545+ instantOutManager : instantOutManager ,
517546 }
518547
519548 // Retrieve all currently existing swaps from the database.
@@ -606,6 +635,43 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
606635 }()
607636 }
608637
638+ // Start the instant out manager.
639+ if d .instantOutManager != nil {
640+ d .wg .Add (1 )
641+ initChan := make (chan struct {})
642+ go func () {
643+ defer d .wg .Done ()
644+
645+ getInfo , err := d .lnd .Client .GetInfo (d .mainCtx )
646+ if err != nil {
647+ d .internalErrChan <- err
648+ return
649+ }
650+
651+ log .Info ("Starting instantout manager" )
652+ defer log .Info ("Instantout manager stopped" )
653+
654+ err = d .instantOutManager .Run (
655+ d .mainCtx , initChan , int32 (getInfo .BlockHeight ),
656+ )
657+ if err != nil && ! errors .Is (err , context .Canceled ) {
658+ d .internalErrChan <- err
659+ }
660+ }()
661+
662+ // Wait for the instantout server to be ready before starting the
663+ // grpc server.
664+ timeOutCtx , cancel := context .WithTimeout (d .mainCtx , 10 * time .Second )
665+ select {
666+ case <- timeOutCtx .Done ():
667+ cancel ()
668+ return fmt .Errorf ("reservation server not ready: %v" ,
669+ timeOutCtx .Err ())
670+ case <- initChan :
671+ cancel ()
672+ }
673+ }
674+
609675 // Last, start our internal error handler. This will return exactly one
610676 // error or nil on the main error channel to inform the caller that
611677 // something went wrong or that shutdown is complete. We don't add to
0 commit comments