loopdb: withdrawal table and queries

This commit is contained in:
Slyghtning 2025-05-13 11:30:18 +02:00
parent b780a9ae91
commit 94aeae81ed
No known key found for this signature in database
GPG key ID: F82D456EA023C9BF
6 changed files with 282 additions and 0 deletions

View file

@ -0,0 +1,2 @@
DROP TABLE IF EXISTS withdrawals;
DROP TABLE IF EXISTS withdrawal_deposits;

View file

@ -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)
);

View file

@ -208,3 +208,20 @@ type SweepsOld struct {
Amt int64
Completed bool
}
type Withdrawal struct {
ID int32
WithdrawalID []byte
WithdrawalTxID sql.NullString
TotalDepositAmount int64
WithdrawnAmount sql.NullInt64
ChangeAmount sql.NullInt64
InitiationTime time.Time
ConfirmationHeight sql.NullInt64
}
type WithdrawalDeposit struct {
ID int32
WithdrawalID []byte
DepositID []byte
}

View file

@ -16,8 +16,11 @@ type Querier interface {
CreateDeposit(ctx context.Context, arg CreateDepositParams) error
CreateReservation(ctx context.Context, arg CreateReservationParams) error
CreateStaticAddress(ctx context.Context, arg CreateStaticAddressParams) error
CreateWithdrawal(ctx context.Context, arg CreateWithdrawalParams) error
CreateWithdrawalDeposit(ctx context.Context, arg CreateWithdrawalDepositParams) error
DropBatch(ctx context.Context, id int32) error
FetchLiquidityParams(ctx context.Context) ([]byte, error)
GetAllWithdrawals(ctx context.Context) ([]Withdrawal, error)
GetBatchSweeps(ctx context.Context, batchID int32) ([]Sweep, error)
GetBatchSweptAmount(ctx context.Context, batchID int32) (int64, error)
GetDeposit(ctx context.Context, depositID []byte) (Deposit, error)
@ -42,6 +45,8 @@ type Querier interface {
GetSwapUpdates(ctx context.Context, swapHash []byte) ([]SwapUpdate, error)
GetSweepStatus(ctx context.Context, outpoint string) (bool, error)
GetUnconfirmedBatches(ctx context.Context) ([]SweepBatch, error)
GetWithdrawalDeposits(ctx context.Context, withdrawalID []byte) ([][]byte, error)
GetWithdrawalIDByDepositID(ctx context.Context, depositID []byte) ([]byte, error)
InsertBatch(ctx context.Context, arg InsertBatchParams) (int32, error)
InsertDepositUpdate(ctx context.Context, arg InsertDepositUpdateParams) error
InsertHtlcKeys(ctx context.Context, arg InsertHtlcKeysParams) error
@ -64,6 +69,7 @@ type Querier interface {
UpdateLoopOutAssetOffchainPayments(ctx context.Context, arg UpdateLoopOutAssetOffchainPaymentsParams) error
UpdateReservation(ctx context.Context, arg UpdateReservationParams) error
UpdateStaticAddressLoopIn(ctx context.Context, arg UpdateStaticAddressLoopInParams) error
UpdateWithdrawal(ctx context.Context, arg UpdateWithdrawalParams) error
UpsertLiquidityParams(ctx context.Context, params []byte) error
UpsertSweep(ctx context.Context, arg UpsertSweepParams) error
}

View file

@ -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;

View file

@ -0,0 +1,168 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.25.0
// source: static_address_withdrawals.sql
package sqlc
import (
"context"
"database/sql"
"time"
)
const createWithdrawal = `-- name: CreateWithdrawal :exec
INSERT INTO withdrawals (
withdrawal_id,
total_deposit_amount,
initiation_time
) VALUES (
$1, $2, $3
)
`
type CreateWithdrawalParams struct {
WithdrawalID []byte
TotalDepositAmount int64
InitiationTime time.Time
}
func (q *Queries) CreateWithdrawal(ctx context.Context, arg CreateWithdrawalParams) error {
_, err := q.db.ExecContext(ctx, createWithdrawal, arg.WithdrawalID, arg.TotalDepositAmount, arg.InitiationTime)
return err
}
const createWithdrawalDeposit = `-- name: CreateWithdrawalDeposit :exec
INSERT INTO withdrawal_deposits (
withdrawal_id,
deposit_id
) VALUES (
$1, $2
)
`
type CreateWithdrawalDepositParams struct {
WithdrawalID []byte
DepositID []byte
}
func (q *Queries) CreateWithdrawalDeposit(ctx context.Context, arg CreateWithdrawalDepositParams) error {
_, err := q.db.ExecContext(ctx, createWithdrawalDeposit, arg.WithdrawalID, arg.DepositID)
return err
}
const getAllWithdrawals = `-- name: GetAllWithdrawals :many
SELECT
id, withdrawal_id, withdrawal_tx_id, total_deposit_amount, withdrawn_amount, change_amount, initiation_time, confirmation_height
FROM
withdrawals
ORDER BY
initiation_time DESC
`
func (q *Queries) GetAllWithdrawals(ctx context.Context) ([]Withdrawal, error) {
rows, err := q.db.QueryContext(ctx, getAllWithdrawals)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Withdrawal
for rows.Next() {
var i Withdrawal
if err := rows.Scan(
&i.ID,
&i.WithdrawalID,
&i.WithdrawalTxID,
&i.TotalDepositAmount,
&i.WithdrawnAmount,
&i.ChangeAmount,
&i.InitiationTime,
&i.ConfirmationHeight,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getWithdrawalDeposits = `-- name: GetWithdrawalDeposits :many
SELECT
deposit_id
FROM
withdrawal_deposits
WHERE
withdrawal_id = $1
`
func (q *Queries) GetWithdrawalDeposits(ctx context.Context, withdrawalID []byte) ([][]byte, error) {
rows, err := q.db.QueryContext(ctx, getWithdrawalDeposits, withdrawalID)
if err != nil {
return nil, err
}
defer rows.Close()
var items [][]byte
for rows.Next() {
var deposit_id []byte
if err := rows.Scan(&deposit_id); err != nil {
return nil, err
}
items = append(items, deposit_id)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getWithdrawalIDByDepositID = `-- name: GetWithdrawalIDByDepositID :one
SELECT withdrawal_id
FROM withdrawal_deposits
WHERE deposit_id = $1
`
func (q *Queries) GetWithdrawalIDByDepositID(ctx context.Context, depositID []byte) ([]byte, error) {
row := q.db.QueryRowContext(ctx, getWithdrawalIDByDepositID, depositID)
var withdrawal_id []byte
err := row.Scan(&withdrawal_id)
return withdrawal_id, err
}
const updateWithdrawal = `-- name: UpdateWithdrawal :exec
UPDATE withdrawals
SET
withdrawal_tx_id = $2,
withdrawn_amount = $3,
change_amount = $4,
confirmation_height = $5
WHERE
withdrawal_id = $1
`
type UpdateWithdrawalParams struct {
WithdrawalID []byte
WithdrawalTxID sql.NullString
WithdrawnAmount sql.NullInt64
ChangeAmount sql.NullInt64
ConfirmationHeight sql.NullInt64
}
func (q *Queries) UpdateWithdrawal(ctx context.Context, arg UpdateWithdrawalParams) error {
_, err := q.db.ExecContext(ctx, updateWithdrawal,
arg.WithdrawalID,
arg.WithdrawalTxID,
arg.WithdrawnAmount,
arg.ChangeAmount,
arg.ConfirmationHeight,
)
return err
}