Skip to content

Commit 1bacf26

Browse files
committed
staticaddr: show deposits as part of cli swap output
1 parent c1643ba commit 1bacf26

File tree

4 files changed

+501
-423
lines changed

4 files changed

+501
-423
lines changed

loopd/swapclient_server.go

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,6 +1744,35 @@ func (s *swapClientServer) ListStaticAddressSwaps(ctx context.Context,
17441744
return &looprpc.ListStaticAddressSwapsResponse{}, nil
17451745
}
17461746

1747+
// Query lnd's info to get the current block height.
1748+
lndInfo, err := s.lnd.Client.GetInfo(ctx)
1749+
if err != nil {
1750+
return nil, err
1751+
}
1752+
1753+
addrParams, err := s.staticAddressManager.GetStaticAddressParameters(
1754+
ctx,
1755+
)
1756+
if err != nil {
1757+
return nil, err
1758+
}
1759+
1760+
// Fetch all deposits once and index them by swap hash for quick lookup.
1761+
allDeposits, err := s.depositManager.GetAllDeposits(ctx)
1762+
if err != nil {
1763+
return nil, err
1764+
}
1765+
1766+
depositsBySwap := make(map[lntypes.Hash][]*deposit.Deposit, len(swaps))
1767+
for _, d := range allDeposits {
1768+
if len(d.SwapHash) == 0 {
1769+
continue
1770+
}
1771+
depositsBySwap[*d.SwapHash] = append(
1772+
depositsBySwap[*d.SwapHash], d,
1773+
)
1774+
}
1775+
17471776
var clientSwaps []*looprpc.StaticAddressLoopInSwap
17481777
for _, swp := range swaps {
17491778
chainParams, err := s.network.ChainParams()
@@ -1752,19 +1781,43 @@ func (s *swapClientServer) ListStaticAddressSwaps(ctx context.Context,
17521781
}
17531782
swapPayReq, err := zpay32.Decode(swp.SwapInvoice, chainParams)
17541783
if err != nil {
1755-
return nil, fmt.Errorf("error decoding swap invoice: "+
1756-
"%v", err)
1784+
return nil, fmt.Errorf("error decoding swap "+
1785+
"invoice: %v", err)
1786+
}
1787+
1788+
// Assemble the deposits associated with this swap, if any.
1789+
var protoDeposits []*looprpc.Deposit
1790+
if ds, ok := depositsBySwap[swp.SwapHash]; ok {
1791+
protoDeposits = make([]*looprpc.Deposit, 0, len(ds))
1792+
for _, d := range ds {
1793+
state := toClientDepositState(d.GetState())
1794+
blocksUntilExpiry := d.ConfirmationHeight +
1795+
int64(addrParams.Expiry) -
1796+
int64(lndInfo.BlockHeight)
1797+
1798+
pd := &looprpc.Deposit{
1799+
Id: d.ID[:],
1800+
State: state,
1801+
Outpoint: d.OutPoint.String(),
1802+
Value: int64(d.Value),
1803+
ConfirmationHeight: d.ConfirmationHeight,
1804+
SwapHash: d.SwapHash[:],
1805+
BlocksUntilExpiry: blocksUntilExpiry,
1806+
}
1807+
protoDeposits = append(protoDeposits, pd)
1808+
}
17571809
}
1810+
1811+
state := toClientStaticAddressLoopInState(swp.GetState())
1812+
swapAmount := int64(swp.TotalDepositAmount())
1813+
payReqAmount := int64(swapPayReq.MilliSat.ToSatoshis())
17581814
swap := &looprpc.StaticAddressLoopInSwap{
1759-
SwapHash: swp.SwapHash[:],
1760-
DepositOutpoints: swp.DepositOutpoints,
1761-
State: toClientStaticAddressLoopInState(
1762-
swp.GetState(),
1763-
),
1764-
SwapAmountSatoshis: int64(swp.TotalDepositAmount()),
1765-
PaymentRequestAmountSatoshis: int64(
1766-
swapPayReq.MilliSat.ToSatoshis(),
1767-
),
1815+
SwapHash: swp.SwapHash[:],
1816+
DepositOutpoints: swp.DepositOutpoints,
1817+
State: state,
1818+
SwapAmountSatoshis: swapAmount,
1819+
PaymentRequestAmountSatoshis: payReqAmount,
1820+
Deposits: protoDeposits,
17681821
}
17691822

17701823
clientSwaps = append(clientSwaps, swap)

0 commit comments

Comments
 (0)