From 94aeae81edb9a385dfe535a30734effd81c3ae83 Mon Sep 17 00:00:00 2001 From: Slyghtning Date: Tue, 13 May 2025 11:30:18 +0200 Subject: [PATCH] loopdb: withdrawal table and queries --- ...000015_static_address_withdrawals.down.sql | 2 + .../000015_static_address_withdrawals.up.sql | 42 +++++ loopdb/sqlc/models.go | 17 ++ loopdb/sqlc/querier.go | 6 + .../queries/static_address_withdrawals.sql | 47 +++++ loopdb/sqlc/static_address_withdrawals.sql.go | 168 ++++++++++++++++++ 6 files changed, 282 insertions(+) create mode 100644 loopdb/sqlc/migrations/000015_static_address_withdrawals.down.sql create mode 100644 loopdb/sqlc/migrations/000015_static_address_withdrawals.up.sql create mode 100644 loopdb/sqlc/queries/static_address_withdrawals.sql create mode 100644 loopdb/sqlc/static_address_withdrawals.sql.go diff --git a/loopdb/sqlc/migrations/000015_static_address_withdrawals.down.sql b/loopdb/sqlc/migrations/000015_static_address_withdrawals.down.sql new file mode 100644 index 00000000..03f4d868 --- /dev/null +++ b/loopdb/sqlc/migrations/000015_static_address_withdrawals.down.sql @@ -0,0 +1,2 @@ +DROP TABLE IF EXISTS withdrawals; +DROP TABLE IF EXISTS withdrawal_deposits; diff --git a/loopdb/sqlc/migrations/000015_static_address_withdrawals.up.sql b/loopdb/sqlc/migrations/000015_static_address_withdrawals.up.sql new file mode 100644 index 00000000..5400be7f --- /dev/null +++ b/loopdb/sqlc/migrations/000015_static_address_withdrawals.up.sql @@ -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) +); diff --git a/loopdb/sqlc/models.go b/loopdb/sqlc/models.go index c2e617aa..c71f0239 100644 --- a/loopdb/sqlc/models.go +++ b/loopdb/sqlc/models.go @@ -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 +} diff --git a/loopdb/sqlc/querier.go b/loopdb/sqlc/querier.go index d5283b86..9b600727 100644 --- a/loopdb/sqlc/querier.go +++ b/loopdb/sqlc/querier.go @@ -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 } diff --git a/loopdb/sqlc/queries/static_address_withdrawals.sql b/loopdb/sqlc/queries/static_address_withdrawals.sql new file mode 100644 index 00000000..5cff29a7 --- /dev/null +++ b/loopdb/sqlc/queries/static_address_withdrawals.sql @@ -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; \ No newline at end of file diff --git a/loopdb/sqlc/static_address_withdrawals.sql.go b/loopdb/sqlc/static_address_withdrawals.sql.go new file mode 100644 index 00000000..1c2134c1 --- /dev/null +++ b/loopdb/sqlc/static_address_withdrawals.sql.go @@ -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 +}