Skip to content

Commit 80828c2

Browse files
authored
Merge pull request #938 from hieblmi/withdraw-info
staticaddr: persist withdrawal info
2 parents ca8610e + 06bbb90 commit 80828c2

20 files changed

+1854
-638
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,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
DROP TABLE IF EXISTS withdrawals;
2+
DROP TABLE IF EXISTS withdrawal_deposits;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
-- withdrawals stores finalized static address withdrawals.
2+
CREATE TABLE IF NOT EXISTS withdrawals (
3+
-- id is the auto-incrementing primary key for a withdrawal.
4+
id INTEGER PRIMARY KEY,
5+
6+
-- withdrawal_id is the unique identifier for the withdrawal.
7+
withdrawal_id BLOB NOT NULL UNIQUE,
8+
9+
-- withdrawal_tx_id is the transaction tx id of the withdrawal.
10+
withdrawal_tx_id TEXT UNIQUE,
11+
12+
-- total_deposit_amount is the total amount of the deposits in satoshis.
13+
total_deposit_amount BIGINT NOT NULL,
14+
15+
-- withdrawn_amount is the total amount of the withdrawal. It amounts
16+
-- to the total amount of the deposits minus the fees and optional change.
17+
withdrawn_amount BIGINT,
18+
19+
-- change_amount is the optional change that the user selected.
20+
change_amount BIGINT,
21+
22+
-- initiation_time is the creation of the withdrawal.
23+
initiation_time TIMESTAMP NOT NULL,
24+
25+
-- confirmation_height is the block height at which the withdrawal was first
26+
-- confirmed.
27+
confirmation_height BIGINT
28+
);
29+
30+
CREATE TABLE IF NOT EXISTS withdrawal_deposits (
31+
-- id is the auto-incrementing primary key.
32+
id INTEGER PRIMARY KEY,
33+
34+
-- withdrawal_id references the withdrawals table.
35+
withdrawal_id BLOB NOT NULL REFERENCES withdrawals(withdrawal_id),
36+
37+
-- deposit_id references the deposits table.
38+
deposit_id BLOB NOT NULL REFERENCES deposits(deposit_id),
39+
40+
-- Ensure that each deposit is used only once per withdrawal.
41+
UNIQUE(deposit_id, withdrawal_id)
42+
);

loopdb/sqlc/models.go

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

loopdb/sqlc/querier.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
-- name: CreateWithdrawal :exec
2+
INSERT INTO withdrawals (
3+
withdrawal_id,
4+
total_deposit_amount,
5+
initiation_time
6+
) VALUES (
7+
$1, $2, $3
8+
);
9+
10+
-- name: CreateWithdrawalDeposit :exec
11+
INSERT INTO withdrawal_deposits (
12+
withdrawal_id,
13+
deposit_id
14+
) VALUES (
15+
$1, $2
16+
);
17+
18+
-- name: GetWithdrawalIDByDepositID :one
19+
SELECT withdrawal_id
20+
FROM withdrawal_deposits
21+
WHERE deposit_id = $1;
22+
23+
-- name: UpdateWithdrawal :exec
24+
UPDATE withdrawals
25+
SET
26+
withdrawal_tx_id = $2,
27+
withdrawn_amount = $3,
28+
change_amount = $4,
29+
confirmation_height = $5
30+
WHERE
31+
withdrawal_id = $1;
32+
33+
-- name: GetWithdrawalDeposits :many
34+
SELECT
35+
deposit_id
36+
FROM
37+
withdrawal_deposits
38+
WHERE
39+
withdrawal_id = $1;
40+
41+
-- name: GetAllWithdrawals :many
42+
SELECT
43+
*
44+
FROM
45+
withdrawals
46+
ORDER BY
47+
initiation_time DESC;

0 commit comments

Comments
 (0)