Skip to content

Commit 7c2f676

Browse files
authored
Merge pull request #991 from hieblmi/cli-show-deposits-and-swaphashes
staticaddr: show deposits and swap hashes in cli
2 parents 98fece4 + b2a4d7a commit 7c2f676

File tree

7 files changed

+667
-462
lines changed

7 files changed

+667
-462
lines changed

loopd/swapclient_server.go

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,6 +1744,38 @@ 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 at once and index them by swap hash for a quick
1761+
// lookup.
1762+
allDeposits, err := s.depositManager.GetAllDeposits(ctx)
1763+
if err != nil {
1764+
return nil, err
1765+
}
1766+
1767+
depositsBySwap := make(map[lntypes.Hash][]*deposit.Deposit, len(swaps))
1768+
for _, d := range allDeposits {
1769+
if d.SwapHash == nil {
1770+
// This deposit is not associated with a swap, so we
1771+
// skip it.
1772+
continue
1773+
}
1774+
depositsBySwap[*d.SwapHash] = append(
1775+
depositsBySwap[*d.SwapHash], d,
1776+
)
1777+
}
1778+
17471779
var clientSwaps []*looprpc.StaticAddressLoopInSwap
17481780
for _, swp := range swaps {
17491781
chainParams, err := s.network.ChainParams()
@@ -1752,19 +1784,43 @@ func (s *swapClientServer) ListStaticAddressSwaps(ctx context.Context,
17521784
}
17531785
swapPayReq, err := zpay32.Decode(swp.SwapInvoice, chainParams)
17541786
if err != nil {
1755-
return nil, fmt.Errorf("error decoding swap invoice: "+
1756-
"%v", err)
1787+
return nil, fmt.Errorf("error decoding swap "+
1788+
"invoice: %v", err)
1789+
}
1790+
1791+
// Assemble the deposits associated with this swap, if any.
1792+
var protoDeposits []*looprpc.Deposit
1793+
if ds, ok := depositsBySwap[swp.SwapHash]; ok {
1794+
protoDeposits = make([]*looprpc.Deposit, 0, len(ds))
1795+
for _, d := range ds {
1796+
state := toClientDepositState(d.GetState())
1797+
blocksUntilExpiry := d.ConfirmationHeight +
1798+
int64(addrParams.Expiry) -
1799+
int64(lndInfo.BlockHeight)
1800+
1801+
pd := &looprpc.Deposit{
1802+
Id: d.ID[:],
1803+
State: state,
1804+
Outpoint: d.OutPoint.String(),
1805+
Value: int64(d.Value),
1806+
ConfirmationHeight: d.ConfirmationHeight,
1807+
SwapHash: d.SwapHash[:],
1808+
BlocksUntilExpiry: blocksUntilExpiry,
1809+
}
1810+
protoDeposits = append(protoDeposits, pd)
1811+
}
17571812
}
1813+
1814+
state := toClientStaticAddressLoopInState(swp.GetState())
1815+
swapAmount := int64(swp.TotalDepositAmount())
1816+
payReqAmount := int64(swapPayReq.MilliSat.ToSatoshis())
17581817
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-
),
1818+
SwapHash: swp.SwapHash[:],
1819+
DepositOutpoints: swp.DepositOutpoints,
1820+
State: state,
1821+
SwapAmountSatoshis: swapAmount,
1822+
PaymentRequestAmountSatoshis: payReqAmount,
1823+
Deposits: protoDeposits,
17681824
}
17691825

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

1973+
swapHash := make([]byte, 0, len(lntypes.Hash{}))
1974+
if d.SwapHash != nil {
1975+
swapHash = d.SwapHash[:]
1976+
}
1977+
19171978
hash := d.Hash
19181979
outpoint := wire.NewOutPoint(&hash, d.Index).String()
19191980
deposit := &looprpc.Deposit{
@@ -1924,6 +1985,7 @@ func filter(deposits []*deposit.Deposit, f filterFunc) []*looprpc.Deposit {
19241985
Outpoint: outpoint,
19251986
Value: int64(d.Value),
19261987
ConfirmationHeight: d.ConfirmationHeight,
1988+
SwapHash: swapHash,
19271989
}
19281990

19291991
clientDeposits = append(clientDeposits, deposit)

0 commit comments

Comments
 (0)