From 55f7625bc57c5ccb9a59205675313016f0d76a66 Mon Sep 17 00:00:00 2001 From: Slyghtning Date: Mon, 6 May 2024 14:07:36 +0200 Subject: [PATCH] sqlc: withdrawal address for deposits Deposit schema extension with a withdrawal sweep address. If the user selects a deposit for withdrawal the destination address of the sweep is stored here. --- .../000010_static_address_deposits.up.sql | 7 ++- loopdb/sqlc/models.go | 17 +++---- .../sqlc/queries/static_address_deposits.sql | 9 ++-- loopdb/sqlc/static_address_deposits.sql.go | 44 ++++++++++++------- 4 files changed, 48 insertions(+), 29 deletions(-) diff --git a/loopdb/sqlc/migrations/000010_static_address_deposits.up.sql b/loopdb/sqlc/migrations/000010_static_address_deposits.up.sql index 7898e608..2952e7e5 100644 --- a/loopdb/sqlc/migrations/000010_static_address_deposits.up.sql +++ b/loopdb/sqlc/migrations/000010_static_address_deposits.up.sql @@ -24,7 +24,12 @@ CREATE TABLE IF NOT EXISTS deposits ( timeout_sweep_pk_script BYTEA NOT NULL, -- expiry_sweep_txid is the transaction id of the expiry sweep. - expiry_sweep_txid BLOB + expiry_sweep_txid BLOB, + + -- finalized_withdrawal_tx is the coop signed tx that will be used to sweep + -- the deposit back to the clients wallet. It will be republished on block + -- arrival and after daemon restarts. + finalized_withdrawal_tx TEXT ); -- deposit_updates contains all the updates to a deposit. diff --git a/loopdb/sqlc/models.go b/loopdb/sqlc/models.go index 47f66ea1..59653882 100644 --- a/loopdb/sqlc/models.go +++ b/loopdb/sqlc/models.go @@ -10,14 +10,15 @@ import ( ) type Deposit struct { - ID int32 - DepositID []byte - TxHash []byte - OutIndex int32 - Amount int64 - ConfirmationHeight int64 - TimeoutSweepPkScript []byte - ExpirySweepTxid []byte + ID int32 + DepositID []byte + TxHash []byte + OutIndex int32 + Amount int64 + ConfirmationHeight int64 + TimeoutSweepPkScript []byte + ExpirySweepTxid []byte + FinalizedWithdrawalTx sql.NullString } type DepositUpdate struct { diff --git a/loopdb/sqlc/queries/static_address_deposits.sql b/loopdb/sqlc/queries/static_address_deposits.sql index d09ca3ff..bc782bbb 100644 --- a/loopdb/sqlc/queries/static_address_deposits.sql +++ b/loopdb/sqlc/queries/static_address_deposits.sql @@ -6,7 +6,8 @@ INSERT INTO deposits ( amount, confirmation_height, timeout_sweep_pk_script, - expiry_sweep_txid + expiry_sweep_txid, + finalized_withdrawal_tx ) VALUES ( $1, $2, @@ -14,7 +15,8 @@ INSERT INTO deposits ( $4, $5, $6, - $7 + $7, + $8 ); -- name: UpdateDeposit :exec @@ -23,7 +25,8 @@ SET tx_hash = $2, out_index = $3, confirmation_height = $4, - expiry_sweep_txid = $5 + expiry_sweep_txid = $5, + finalized_withdrawal_tx = $6 WHERE deposits.deposit_id = $1; diff --git a/loopdb/sqlc/static_address_deposits.sql.go b/loopdb/sqlc/static_address_deposits.sql.go index 8b4612d7..75645c8e 100644 --- a/loopdb/sqlc/static_address_deposits.sql.go +++ b/loopdb/sqlc/static_address_deposits.sql.go @@ -7,12 +7,13 @@ package sqlc import ( "context" + "database/sql" "time" ) const allDeposits = `-- name: AllDeposits :many SELECT - id, deposit_id, tx_hash, out_index, amount, confirmation_height, timeout_sweep_pk_script, expiry_sweep_txid + id, deposit_id, tx_hash, out_index, amount, confirmation_height, timeout_sweep_pk_script, expiry_sweep_txid, finalized_withdrawal_tx FROM deposits ORDER BY @@ -37,6 +38,7 @@ func (q *Queries) AllDeposits(ctx context.Context) ([]Deposit, error) { &i.ConfirmationHeight, &i.TimeoutSweepPkScript, &i.ExpirySweepTxid, + &i.FinalizedWithdrawalTx, ); err != nil { return nil, err } @@ -59,7 +61,8 @@ INSERT INTO deposits ( amount, confirmation_height, timeout_sweep_pk_script, - expiry_sweep_txid + expiry_sweep_txid, + finalized_withdrawal_tx ) VALUES ( $1, $2, @@ -67,18 +70,20 @@ INSERT INTO deposits ( $4, $5, $6, - $7 + $7, + $8 ) ` type CreateDepositParams struct { - DepositID []byte - TxHash []byte - OutIndex int32 - Amount int64 - ConfirmationHeight int64 - TimeoutSweepPkScript []byte - ExpirySweepTxid []byte + DepositID []byte + TxHash []byte + OutIndex int32 + Amount int64 + ConfirmationHeight int64 + TimeoutSweepPkScript []byte + ExpirySweepTxid []byte + FinalizedWithdrawalTx sql.NullString } func (q *Queries) CreateDeposit(ctx context.Context, arg CreateDepositParams) error { @@ -90,13 +95,14 @@ func (q *Queries) CreateDeposit(ctx context.Context, arg CreateDepositParams) er arg.ConfirmationHeight, arg.TimeoutSweepPkScript, arg.ExpirySweepTxid, + arg.FinalizedWithdrawalTx, ) return err } const getDeposit = `-- name: GetDeposit :one SELECT - id, deposit_id, tx_hash, out_index, amount, confirmation_height, timeout_sweep_pk_script, expiry_sweep_txid + id, deposit_id, tx_hash, out_index, amount, confirmation_height, timeout_sweep_pk_script, expiry_sweep_txid, finalized_withdrawal_tx FROM deposits WHERE @@ -115,6 +121,7 @@ func (q *Queries) GetDeposit(ctx context.Context, depositID []byte) (Deposit, er &i.ConfirmationHeight, &i.TimeoutSweepPkScript, &i.ExpirySweepTxid, + &i.FinalizedWithdrawalTx, ) return i, err } @@ -172,17 +179,19 @@ SET tx_hash = $2, out_index = $3, confirmation_height = $4, - expiry_sweep_txid = $5 + expiry_sweep_txid = $5, + finalized_withdrawal_tx = $6 WHERE deposits.deposit_id = $1 ` type UpdateDepositParams struct { - DepositID []byte - TxHash []byte - OutIndex int32 - ConfirmationHeight int64 - ExpirySweepTxid []byte + DepositID []byte + TxHash []byte + OutIndex int32 + ConfirmationHeight int64 + ExpirySweepTxid []byte + FinalizedWithdrawalTx sql.NullString } func (q *Queries) UpdateDeposit(ctx context.Context, arg UpdateDepositParams) error { @@ -192,6 +201,7 @@ func (q *Queries) UpdateDeposit(ctx context.Context, arg UpdateDepositParams) er arg.OutIndex, arg.ConfirmationHeight, arg.ExpirySweepTxid, + arg.FinalizedWithdrawalTx, ) return err }