Skip to content

staticaddr: show deposits and swap hashes in cli #991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 73 additions & 11 deletions loopd/swapclient_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1744,6 +1744,38 @@ func (s *swapClientServer) ListStaticAddressSwaps(ctx context.Context,
return &looprpc.ListStaticAddressSwapsResponse{}, nil
}

// Query lnd's info to get the current block height.
lndInfo, err := s.lnd.Client.GetInfo(ctx)
if err != nil {
return nil, err
}

addrParams, err := s.staticAddressManager.GetStaticAddressParameters(
ctx,
)
if err != nil {
return nil, err
}

// Fetch all deposits at once and index them by swap hash for a quick
// lookup.
allDeposits, err := s.depositManager.GetAllDeposits(ctx)
if err != nil {
return nil, err
}

depositsBySwap := make(map[lntypes.Hash][]*deposit.Deposit, len(swaps))
for _, d := range allDeposits {
if d.SwapHash == nil {
// This deposit is not associated with a swap, so we
// skip it.
continue
}
depositsBySwap[*d.SwapHash] = append(
depositsBySwap[*d.SwapHash], d,
)
}

var clientSwaps []*looprpc.StaticAddressLoopInSwap
for _, swp := range swaps {
chainParams, err := s.network.ChainParams()
Expand All @@ -1752,19 +1784,43 @@ func (s *swapClientServer) ListStaticAddressSwaps(ctx context.Context,
}
swapPayReq, err := zpay32.Decode(swp.SwapInvoice, chainParams)
if err != nil {
return nil, fmt.Errorf("error decoding swap invoice: "+
"%v", err)
return nil, fmt.Errorf("error decoding swap "+
"invoice: %v", err)
}

// Assemble the deposits associated with this swap, if any.
var protoDeposits []*looprpc.Deposit
if ds, ok := depositsBySwap[swp.SwapHash]; ok {
protoDeposits = make([]*looprpc.Deposit, 0, len(ds))
for _, d := range ds {
state := toClientDepositState(d.GetState())
blocksUntilExpiry := d.ConfirmationHeight +
int64(addrParams.Expiry) -
int64(lndInfo.BlockHeight)

pd := &looprpc.Deposit{
Id: d.ID[:],
State: state,
Outpoint: d.OutPoint.String(),
Value: int64(d.Value),
ConfirmationHeight: d.ConfirmationHeight,
SwapHash: d.SwapHash[:],
BlocksUntilExpiry: blocksUntilExpiry,
}
protoDeposits = append(protoDeposits, pd)
}
}

state := toClientStaticAddressLoopInState(swp.GetState())
swapAmount := int64(swp.TotalDepositAmount())
payReqAmount := int64(swapPayReq.MilliSat.ToSatoshis())
swap := &looprpc.StaticAddressLoopInSwap{
SwapHash: swp.SwapHash[:],
DepositOutpoints: swp.DepositOutpoints,
State: toClientStaticAddressLoopInState(
swp.GetState(),
),
SwapAmountSatoshis: int64(swp.TotalDepositAmount()),
PaymentRequestAmountSatoshis: int64(
swapPayReq.MilliSat.ToSatoshis(),
),
SwapHash: swp.SwapHash[:],
DepositOutpoints: swp.DepositOutpoints,
State: state,
SwapAmountSatoshis: swapAmount,
PaymentRequestAmountSatoshis: payReqAmount,
Deposits: protoDeposits,
}

clientSwaps = append(clientSwaps, swap)
Expand Down Expand Up @@ -1914,6 +1970,11 @@ func filter(deposits []*deposit.Deposit, f filterFunc) []*looprpc.Deposit {
continue
}

swapHash := make([]byte, 0, len(lntypes.Hash{}))
if d.SwapHash != nil {
swapHash = d.SwapHash[:]
}

hash := d.Hash
outpoint := wire.NewOutPoint(&hash, d.Index).String()
deposit := &looprpc.Deposit{
Expand All @@ -1924,6 +1985,7 @@ func filter(deposits []*deposit.Deposit, f filterFunc) []*looprpc.Deposit {
Outpoint: outpoint,
Value: int64(d.Value),
ConfirmationHeight: d.ConfirmationHeight,
SwapHash: swapHash,
}

clientDeposits = append(clientDeposits, deposit)
Expand Down
Loading