Skip to content

Commit 0b699c2

Browse files
committed
staticaddr: show deposits as part of cli swap output
1 parent 1407a6e commit 0b699c2

File tree

4 files changed

+500
-423
lines changed

4 files changed

+500
-423
lines changed

loopd/swapclient_server.go

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

1747+
lndInfo, err := s.lnd.Client.GetInfo(ctx)
1748+
if err != nil {
1749+
return nil, err
1750+
}
1751+
1752+
addrParams, err := s.staticAddressManager.GetStaticAddressParameters(
1753+
ctx,
1754+
)
1755+
if err != nil {
1756+
return nil, err
1757+
}
1758+
1759+
// Fetch all deposits once and index them by swap hash for quick lookup.
1760+
allDeposits, err := s.depositManager.GetAllDeposits(ctx)
1761+
if err != nil {
1762+
return nil, err
1763+
}
1764+
1765+
depositsBySwap := make(map[string][]*deposit.Deposit, len(swaps))
1766+
for _, d := range allDeposits {
1767+
if len(d.SwapHash) == 0 {
1768+
continue
1769+
}
1770+
swapHash := hex.EncodeToString(d.SwapHash)
1771+
depositsBySwap[swapHash] = append(depositsBySwap[swapHash], d)
1772+
}
1773+
17471774
var clientSwaps []*looprpc.StaticAddressLoopInSwap
17481775
for _, swp := range swaps {
17491776
chainParams, err := s.network.ChainParams()
@@ -1752,19 +1779,44 @@ func (s *swapClientServer) ListStaticAddressSwaps(ctx context.Context,
17521779
}
17531780
swapPayReq, err := zpay32.Decode(swp.SwapInvoice, chainParams)
17541781
if err != nil {
1755-
return nil, fmt.Errorf("error decoding swap invoice: "+
1756-
"%v", err)
1782+
return nil, fmt.Errorf("error decoding swap "+
1783+
"invoice: %v", err)
1784+
}
1785+
1786+
// Assemble the deposits associated with this swap, if any.
1787+
var protoDeposits []*looprpc.Deposit
1788+
swapHash := hex.EncodeToString(swp.SwapHash[:])
1789+
if ds, ok := depositsBySwap[swapHash]; ok {
1790+
protoDeposits = make([]*looprpc.Deposit, 0, len(ds))
1791+
for _, d := range ds {
1792+
state := toClientDepositState(d.GetState())
1793+
blocksUntilExpiry := d.ConfirmationHeight +
1794+
int64(addrParams.Expiry) -
1795+
int64(lndInfo.BlockHeight)
1796+
1797+
pd := &looprpc.Deposit{
1798+
Id: d.ID[:],
1799+
State: state,
1800+
Outpoint: d.OutPoint.String(),
1801+
Value: int64(d.Value),
1802+
ConfirmationHeight: d.ConfirmationHeight,
1803+
SwapHash: d.SwapHash,
1804+
BlocksUntilExpiry: blocksUntilExpiry,
1805+
}
1806+
protoDeposits = append(protoDeposits, pd)
1807+
}
17571808
}
1809+
1810+
state := toClientStaticAddressLoopInState(swp.GetState())
1811+
swapAmount := int64(swp.TotalDepositAmount())
1812+
payReqAmount := int64(swapPayReq.MilliSat.ToSatoshis())
17581813
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-
),
1814+
SwapHash: swp.SwapHash[:],
1815+
DepositOutpoints: swp.DepositOutpoints,
1816+
State: state,
1817+
SwapAmountSatoshis: swapAmount,
1818+
PaymentRequestAmountSatoshis: payReqAmount,
1819+
Deposits: protoDeposits,
17681820
}
17691821

17701822
clientSwaps = append(clientSwaps, swap)

0 commit comments

Comments
 (0)