Skip to content

staticaddr: persist withdrawal info #938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions cmd/loop/staticaddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var staticAddressCommands = cli.Command{
newStaticAddressCommand,
listUnspentCommand,
listDepositsCommand,
listWithdrawalsCommand,
listStaticAddressSwapsCommand,
withdrawalCommand,
summaryCommand,
Expand Down Expand Up @@ -312,6 +313,38 @@ func listDeposits(ctx *cli.Context) error {
return nil
}

var listWithdrawalsCommand = cli.Command{
Name: "listwithdrawals",
Usage: "Display a summary of past withdrawals.",
Description: `
`,
Action: listWithdrawals,
}

func listWithdrawals(ctx *cli.Context) error {
ctxb := context.Background()
if ctx.NArg() > 0 {
return cli.ShowCommandHelp(ctx, "withdrawals")
}

client, cleanup, err := getClient(ctx)
if err != nil {
return err
}
defer cleanup()

resp, err := client.ListStaticAddressWithdrawals(
ctxb, &looprpc.ListStaticAddressWithdrawalRequest{},
)
if err != nil {
return err
}

printRespJSON(resp)

return nil
}

var listStaticAddressSwapsCommand = cli.Command{
Name: "listswaps",
Usage: "Shows a list of finalized static address swaps.",
Expand Down
5 changes: 5 additions & 0 deletions loopd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,10 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
depositManager = deposit.NewManager(depoCfg)

// Static address deposit withdrawal manager setup.
withdrawalStore := withdraw.NewSqlStore(
loopdb.NewTypedStore[withdraw.Querier](baseDb),
depositStore,
)
withdrawalCfg := &withdraw.ManagerConfig{
StaticAddressServerClient: staticAddressClient,
AddressManager: staticAddressManager,
Expand All @@ -612,6 +616,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
ChainParams: d.lnd.ChainParams,
ChainNotifier: d.lnd.ChainNotifier,
Signer: d.lnd.Signer,
Store: withdrawalStore,
}
withdrawalManager = withdraw.NewManager(withdrawalCfg, blockHeight)

Expand Down
47 changes: 47 additions & 0 deletions loopd/swapclient_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1673,6 +1673,53 @@ func (s *swapClientServer) ListStaticAddressDeposits(ctx context.Context,
}, nil
}

// ListStaticAddressWithdrawals returns a list of all finalized withdrawal
// transactions.
func (s *swapClientServer) ListStaticAddressWithdrawals(ctx context.Context,
_ *looprpc.ListStaticAddressWithdrawalRequest) (
*looprpc.ListStaticAddressWithdrawalResponse, error) {

withdrawals, err := s.withdrawalManager.GetAllWithdrawals(ctx)
if err != nil {
return nil, err
}

if len(withdrawals) == 0 {
return &looprpc.ListStaticAddressWithdrawalResponse{}, nil
}

clientWithdrawals := make(
[]*looprpc.StaticAddressWithdrawal, 0, len(withdrawals),
)
for _, w := range withdrawals {
deposits := make([]*looprpc.Deposit, 0, len(w.Deposits))
for _, d := range w.Deposits {
deposits = append(deposits, &looprpc.Deposit{
Id: d.ID[:],
Outpoint: d.OutPoint.String(),
Value: int64(d.Value),
ConfirmationHeight: d.ConfirmationHeight,
State: toClientDepositState(
d.GetState(),
),
})
}
withdrawal := &looprpc.StaticAddressWithdrawal{
TxId: w.TxID.String(),
Deposits: deposits,
TotalDepositAmountSatoshis: int64(w.TotalDepositAmount),
WithdrawnAmountSatoshis: int64(w.WithdrawnAmount),
ChangeAmountSatoshis: int64(w.ChangeAmount),
ConfirmationHeight: uint32(w.ConfirmationHeight),
}
clientWithdrawals = append(clientWithdrawals, withdrawal)
}

return &looprpc.ListStaticAddressWithdrawalResponse{
Withdrawals: clientWithdrawals,
}, nil
}

// ListStaticAddressSwaps returns a list of all swaps that are currently pending
// or previously succeeded.
func (s *swapClientServer) ListStaticAddressSwaps(ctx context.Context,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP TABLE IF EXISTS withdrawals;
DROP TABLE IF EXISTS withdrawal_deposits;
42 changes: 42 additions & 0 deletions loopdb/sqlc/migrations/000015_static_address_withdrawals.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
-- withdrawals stores finalized static address withdrawals.
CREATE TABLE IF NOT EXISTS withdrawals (
-- id is the auto-incrementing primary key for a withdrawal.
id INTEGER PRIMARY KEY,

-- withdrawal_id is the unique identifier for the withdrawal.
withdrawal_id BLOB NOT NULL UNIQUE,

-- withdrawal_tx_id is the transaction tx id of the withdrawal.
withdrawal_tx_id TEXT UNIQUE,

-- total_deposit_amount is the total amount of the deposits in satoshis.
total_deposit_amount BIGINT NOT NULL,

-- withdrawn_amount is the total amount of the withdrawal. It amounts
-- to the total amount of the deposits minus the fees and optional change.
withdrawn_amount BIGINT,

-- change_amount is the optional change that the user selected.
change_amount BIGINT,

-- initiation_time is the creation of the withdrawal.
initiation_time TIMESTAMP NOT NULL,

-- confirmation_height is the block height at which the withdrawal was first
-- confirmed.
confirmation_height BIGINT
);

CREATE TABLE IF NOT EXISTS withdrawal_deposits (
-- id is the auto-incrementing primary key.
id INTEGER PRIMARY KEY,

-- withdrawal_id references the withdrawals table.
withdrawal_id BLOB NOT NULL REFERENCES withdrawals(withdrawal_id),

-- deposit_id references the deposits table.
deposit_id BLOB NOT NULL REFERENCES deposits(deposit_id),

-- Ensure that each deposit is used only once per withdrawal.
UNIQUE(deposit_id, withdrawal_id)
);
17 changes: 17 additions & 0 deletions loopdb/sqlc/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions loopdb/sqlc/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions loopdb/sqlc/queries/static_address_withdrawals.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
-- name: CreateWithdrawal :exec
INSERT INTO withdrawals (
withdrawal_id,
total_deposit_amount,
initiation_time
) VALUES (
$1, $2, $3
);

-- name: CreateWithdrawalDeposit :exec
INSERT INTO withdrawal_deposits (
withdrawal_id,
deposit_id
) VALUES (
$1, $2
);

-- name: GetWithdrawalIDByDepositID :one
SELECT withdrawal_id
FROM withdrawal_deposits
WHERE deposit_id = $1;

-- name: UpdateWithdrawal :exec
UPDATE withdrawals
SET
withdrawal_tx_id = $2,
withdrawn_amount = $3,
change_amount = $4,
confirmation_height = $5
WHERE
withdrawal_id = $1;

-- name: GetWithdrawalDeposits :many
SELECT
deposit_id
FROM
withdrawal_deposits
WHERE
withdrawal_id = $1;

-- name: GetAllWithdrawals :many
SELECT
*
FROM
withdrawals
ORDER BY
initiation_time DESC;
Loading