Skip to content

Commit c835f14

Browse files
committed
staticaddr: retain historic withdrawal info
1 parent b6baa8a commit c835f14

File tree

13 files changed

+551
-262
lines changed

13 files changed

+551
-262
lines changed

cmd/loop/staticaddr.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ var staticAddressCommands = cli.Command{
3030
newStaticAddressCommand,
3131
listUnspentCommand,
3232
listDepositsCommand,
33+
listWithdrawalsCommand,
3334
listStaticAddressSwapsCommand,
3435
withdrawalCommand,
3536
summaryCommand,
@@ -312,6 +313,38 @@ func listDeposits(ctx *cli.Context) error {
312313
return nil
313314
}
314315

316+
var listWithdrawalsCommand = cli.Command{
317+
Name: "listwithdrawals",
318+
Usage: "Display a summary of past withdrawals.",
319+
Description: `
320+
`,
321+
Action: listWithdrawals,
322+
}
323+
324+
func listWithdrawals(ctx *cli.Context) error {
325+
ctxb := context.Background()
326+
if ctx.NArg() > 0 {
327+
return cli.ShowCommandHelp(ctx, "withdrawals")
328+
}
329+
330+
client, cleanup, err := getClient(ctx)
331+
if err != nil {
332+
return err
333+
}
334+
defer cleanup()
335+
336+
resp, err := client.ListStaticAddressWithdrawals(
337+
ctxb, &looprpc.ListStaticAddressWithdrawalRequest{},
338+
)
339+
if err != nil {
340+
return err
341+
}
342+
343+
printRespJSON(resp)
344+
345+
return nil
346+
}
347+
315348
var listStaticAddressSwapsCommand = cli.Command{
316349
Name: "listswaps",
317350
Usage: "Shows a list of finalized static address swaps.",

loopd/daemon.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,10 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
604604
depositManager = deposit.NewManager(depoCfg)
605605

606606
// Static address deposit withdrawal manager setup.
607+
withdrawalStore := withdraw.NewSqlStore(
608+
loopdb.NewTypedStore[withdraw.Querier](baseDb),
609+
depositStore,
610+
)
607611
withdrawalCfg := &withdraw.ManagerConfig{
608612
StaticAddressServerClient: staticAddressClient,
609613
AddressManager: staticAddressManager,
@@ -612,6 +616,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
612616
ChainParams: d.lnd.ChainParams,
613617
ChainNotifier: d.lnd.ChainNotifier,
614618
Signer: d.lnd.Signer,
619+
Store: withdrawalStore,
615620
}
616621
withdrawalManager = withdraw.NewManager(withdrawalCfg, blockHeight)
617622

loopd/swapclient_server.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,6 +1673,53 @@ func (s *swapClientServer) ListStaticAddressDeposits(ctx context.Context,
16731673
}, nil
16741674
}
16751675

1676+
// ListStaticAddressWithdrawals returns a list of all finalized withdrawal
1677+
// transactions.
1678+
func (s *swapClientServer) ListStaticAddressWithdrawals(ctx context.Context,
1679+
_ *looprpc.ListStaticAddressWithdrawalRequest) (
1680+
*looprpc.ListStaticAddressWithdrawalResponse, error) {
1681+
1682+
withdrawals, err := s.withdrawalManager.GetAllWithdrawals(ctx)
1683+
if err != nil {
1684+
return nil, err
1685+
}
1686+
1687+
if len(withdrawals) == 0 {
1688+
return &looprpc.ListStaticAddressWithdrawalResponse{}, nil
1689+
}
1690+
1691+
clientWithdrawals := make(
1692+
[]*looprpc.StaticAddressWithdrawal, 0, len(withdrawals),
1693+
)
1694+
for _, w := range withdrawals {
1695+
deposits := make([]*looprpc.Deposit, 0, len(w.Deposits))
1696+
for _, d := range w.Deposits {
1697+
deposits = append(deposits, &looprpc.Deposit{
1698+
Id: d.ID[:],
1699+
Outpoint: d.OutPoint.String(),
1700+
Value: int64(d.Value),
1701+
ConfirmationHeight: d.ConfirmationHeight,
1702+
State: toClientDepositState(
1703+
d.GetState(),
1704+
),
1705+
})
1706+
}
1707+
withdrawal := &looprpc.StaticAddressWithdrawal{
1708+
TxId: w.TxID.String(),
1709+
Deposits: deposits,
1710+
TotalDepositAmountSatoshis: int64(w.TotalDepositAmount),
1711+
WithdrawnAmountSatoshis: int64(w.WithdrawnAmount),
1712+
ChangeAmountSatoshis: int64(w.ChangeAmount),
1713+
ConfirmationHeight: uint32(w.ConfirmationHeight),
1714+
}
1715+
clientWithdrawals = append(clientWithdrawals, withdrawal)
1716+
}
1717+
1718+
return &looprpc.ListStaticAddressWithdrawalResponse{
1719+
Withdrawals: clientWithdrawals,
1720+
}, nil
1721+
}
1722+
16761723
// ListStaticAddressSwaps returns a list of all swaps that are currently pending
16771724
// or previously succeeded.
16781725
func (s *swapClientServer) ListStaticAddressSwaps(ctx context.Context,

loopdb/sqlc/querier.go

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 27 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,36 @@
11
-- name: CreateWithdrawal :exec
2-
WITH inserted_withdrawal AS (
3-
INSERT INTO withdrawals (
4-
withdrawal_id,
5-
withdrawal_tx_id,
6-
total_deposit_amount,
7-
withdrawn_amount,
8-
change_amount,
9-
initiation_time,
10-
confirmation_height
11-
) VALUES (
12-
$1, $2, $3, $4, $5, $6, $7
13-
)
14-
RETURNING withdrawal_id
15-
)
16-
INSERT INTO withdrawal_deposits (withdrawal_id, deposit_id)
17-
SELECT iw.withdrawal_id, did
18-
FROM inserted_withdrawal iw,
19-
unnest($8::BLOB[]) AS did;
2+
INSERT INTO withdrawals (
3+
withdrawal_id,
4+
withdrawal_tx_id,
5+
total_deposit_amount,
6+
withdrawn_amount,
7+
change_amount,
8+
initiation_time,
9+
confirmation_height
10+
) VALUES (
11+
$1, $2, $3, $4, $5, $6, $7
12+
);
2013

21-
-- name: GetWithdrawalWithDepositsJSON :one
22-
SELECT
23-
w.withdrawal_id,
24-
w.withdrawal_tx_id,
25-
w.total_deposit_amount,
26-
w.withdrawn_amount,
27-
w.change_amount,
28-
w.initiation_time,
29-
w.confirmation_height AS withdrawal_confirmation_height,
14+
-- name: CreateWithdrawalDeposit :exec
15+
INSERT INTO withdrawal_deposits (
16+
withdrawal_id,
17+
deposit_id
18+
) VALUES (
19+
$1, $2
20+
);
3021

31-
json_agg(
32-
json_build_object(
33-
'deposit_id', d.deposit_id,
34-
'tx_hash', d.tx_hash,
35-
'out_index', d.out_index,
36-
'amount', d.amount,
37-
'confirmation_height', d.confirmation_height,
38-
'timeout_sweep_pk_script', d.timeout_sweep_pk_script,
39-
'expiry_sweep_txid', d.expiry_sweep_txid,
40-
'finalized_withdrawal_tx', d.finalized_withdrawal_tx
41-
)
42-
) AS deposits
22+
-- name: GetWithdrawalDeposits :many
23+
SELECT
24+
deposit_id
4325
FROM
44-
withdrawals w
45-
JOIN
46-
withdrawal_deposits wd ON w.withdrawal_id = wd.withdrawal_id
47-
JOIN
48-
deposits d ON wd.deposit_id = d.deposit_id
26+
withdrawal_deposits
4927
WHERE
50-
w.withdrawal_tx_id = $1
51-
GROUP BY
52-
w.id, w.withdrawal_id, w.withdrawal_tx_id, w.total_deposit_amount,
53-
w.withdrawn_amount, w.change_amount, w.initiation_time, w.confirmation_height;
28+
withdrawal_id = $1;
5429

55-
-- name: GetAllWithdrawalsWithDepositsJSON :many
30+
-- name: GetAllWithdrawals :many
5631
SELECT
57-
w.withdrawal_id,
58-
w.withdrawal_tx_id,
59-
w.total_deposit_amount,
60-
w.withdrawn_amount,
61-
w.change_amount,
62-
w.initiation_time,
63-
w.confirmation_height AS withdrawal_confirmation_height,
64-
65-
json_agg(
66-
json_build_object(
67-
'deposit_id', d.deposit_id,
68-
'tx_hash', d.tx_hash,
69-
'out_index', d.out_index,
70-
'amount', d.amount,
71-
'confirmation_height', d.confirmation_height,
72-
'timeout_sweep_pk_script', d.timeout_sweep_pk_script,
73-
'expiry_sweep_txid', d.expiry_sweep_txid,
74-
'finalized_withdrawal_tx', d.finalized_withdrawal_tx
75-
)
76-
ORDER BY d.id
77-
) AS deposits
32+
*
7833
FROM
79-
withdrawals w
80-
JOIN
81-
withdrawal_deposits wd ON w.withdrawal_id = wd.withdrawal_id
82-
JOIN
83-
deposits d ON wd.deposit_id = d.deposit_id
84-
GROUP BY
85-
w.id, w.withdrawal_id, w.withdrawal_tx_id, w.total_deposit_amount,
86-
w.withdrawn_amount, w.change_amount, w.initiation_time, w.confirmation_height
34+
withdrawals
8735
ORDER BY
88-
w.initiation_time DESC;
89-
36+
initiation_time DESC;

0 commit comments

Comments
 (0)