Skip to content

Commit

Permalink
staticaddr: get deposit block height from lnd client
Browse files Browse the repository at this point in the history
  • Loading branch information
hieblmi committed Dec 4, 2024
1 parent af503bd commit 37a6235
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 38 deletions.
1 change: 1 addition & 0 deletions loopd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
ChainParams: d.lnd.ChainParams,
ChainNotifier: d.lnd.ChainNotifier,
Signer: d.lnd.Signer,
LndClient: d.lnd.Client,
}
depositManager = deposit.NewManager(depoCfg)

Expand Down
48 changes: 10 additions & 38 deletions staticaddr/deposit/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ type ManagerConfig struct {

// Signer is the signer client that is used to sign transactions.
Signer lndclient.SignerClient

// LndClient is used to add invoices and select hop hints.
LndClient lndclient.LightningClient
}

// Manager manages the address state machines.
Expand Down Expand Up @@ -280,11 +283,6 @@ func (m *Manager) reconcileDeposits(ctx context.Context) error {
func (m *Manager) createNewDeposit(ctx context.Context,
utxo *lnwallet.Utxo) (*Deposit, error) {

blockHeight, err := m.getBlockHeight(ctx, utxo)
if err != nil {
return nil, err
}

// Get the sweep pk script.
addr, err := m.cfg.WalletKit.NextAddr(
ctx, lnwallet.DefaultAccountName,
Expand All @@ -303,12 +301,18 @@ func (m *Manager) createNewDeposit(ctx context.Context,
if err != nil {
return nil, err
}

info, err := m.cfg.LndClient.GetInfo(ctx)
if err != nil {
return nil, err
}

deposit := &Deposit{
ID: id,
state: Deposited,
OutPoint: utxo.OutPoint,
Value: utxo.Value,
ConfirmationHeight: int64(blockHeight),
ConfirmationHeight: int64(info.BlockHeight),
TimeOutSweepPkScript: timeoutSweepPkScript,
}

Expand All @@ -324,38 +328,6 @@ func (m *Manager) createNewDeposit(ctx context.Context,
return deposit, nil
}

// getBlockHeight retrieves the block height of a given utxo.
func (m *Manager) getBlockHeight(ctx context.Context,
utxo *lnwallet.Utxo) (uint32, error) {

addressParams, err := m.cfg.AddressManager.GetStaticAddressParameters(
ctx,
)
if err != nil {
return 0, fmt.Errorf("couldn't get confirmation height for "+
"deposit, %w", err)
}

notifChan, errChan, err := m.cfg.ChainNotifier.RegisterConfirmationsNtfn( //nolint:lll
ctx, &utxo.OutPoint.Hash, addressParams.PkScript, MinConfs,
int32(m.initiationHeight),
)
if err != nil {
return 0, err
}

select {
case tx := <-notifChan:
return tx.BlockHeight, nil

case err := <-errChan:
return 0, err

case <-ctx.Done():
return 0, ctx.Err()
}
}

// filterNewDeposits filters the given utxos for new deposits that we haven't
// seen before.
func (m *Manager) filterNewDeposits(utxos []*lnwallet.Utxo) []*lnwallet.Utxo {
Expand Down

0 comments on commit 37a6235

Please sign in to comment.