From 401ff43365412d5da2e741bdec721de6a91f6d6b Mon Sep 17 00:00:00 2001 From: Slyghtning Date: Thu, 7 Mar 2024 17:25:07 +0100 Subject: [PATCH] sqlc: deposit queries, models and migrations --- .../000008_static_address_deposits.down.sql | 1 + .../000008_static_address_deposits.up.sql | 43 ++++ loopdb/sqlc/models.go | 18 ++ loopdb/sqlc/querier.go | 6 + .../sqlc/queries/static_address_deposits.sql | 66 ++++++ loopdb/sqlc/static_address_deposits.sql.go | 197 ++++++++++++++++++ loopdb/sqlc/static_addresses.sql.go | 2 +- 7 files changed, 332 insertions(+), 1 deletion(-) create mode 100644 loopdb/sqlc/migrations/000008_static_address_deposits.down.sql create mode 100644 loopdb/sqlc/migrations/000008_static_address_deposits.up.sql create mode 100644 loopdb/sqlc/queries/static_address_deposits.sql create mode 100644 loopdb/sqlc/static_address_deposits.sql.go diff --git a/loopdb/sqlc/migrations/000008_static_address_deposits.down.sql b/loopdb/sqlc/migrations/000008_static_address_deposits.down.sql new file mode 100644 index 0000000..1170fda --- /dev/null +++ b/loopdb/sqlc/migrations/000008_static_address_deposits.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS deposits; diff --git a/loopdb/sqlc/migrations/000008_static_address_deposits.up.sql b/loopdb/sqlc/migrations/000008_static_address_deposits.up.sql new file mode 100644 index 0000000..7898e60 --- /dev/null +++ b/loopdb/sqlc/migrations/000008_static_address_deposits.up.sql @@ -0,0 +1,43 @@ +-- deposits stores historic and unspent static address outputs. +CREATE TABLE IF NOT EXISTS deposits ( + -- id is the auto-incrementing primary key for a static address. + id INTEGER PRIMARY KEY, + + -- deposit_id is the unique identifier for the deposit. + deposit_id BLOB NOT NULL UNIQUE, + + -- tx_hash is the transaction hash of the deposit. + tx_hash BYTEA NOT NULL, + + -- output_index is the index of the output in the transaction. + out_index INT NOT NULL, + + -- amount is the amount of the deposit. + amount BIGINT NOT NULL, + + -- confirmation_height is the absolute height at which the deposit was + -- confirmed. + confirmation_height BIGINT NOT NULL, + + -- timeout_sweep_pk_script is the public key script that will be used to + -- sweep the deposit after has expired. + timeout_sweep_pk_script BYTEA NOT NULL, + + -- expiry_sweep_txid is the transaction id of the expiry sweep. + expiry_sweep_txid BLOB +); + +-- deposit_updates contains all the updates to a deposit. +CREATE TABLE IF NOT EXISTS deposit_updates ( + -- id is the auto incrementing primary key. + id INTEGER PRIMARY KEY, + + -- deposit_id is the unique identifier for the deposit. + deposit_id BLOB NOT NULL REFERENCES deposits(deposit_id), + + -- update_state is the state of the deposit at the time of the update. + update_state TEXT NOT NULL, + + -- update_timestamp is the timestamp of the update. + update_timestamp TIMESTAMP NOT NULL +); diff --git a/loopdb/sqlc/models.go b/loopdb/sqlc/models.go index 78d4828..3a9abe7 100644 --- a/loopdb/sqlc/models.go +++ b/loopdb/sqlc/models.go @@ -9,6 +9,24 @@ import ( "time" ) +type Deposit struct { + ID int32 + DepositID []byte + TxHash []byte + OutIndex int32 + Amount int64 + ConfirmationHeight int64 + TimeoutSweepPkScript []byte + ExpirySweepTxid []byte +} + +type DepositUpdate struct { + ID int32 + DepositID []byte + UpdateState string + UpdateTimestamp time.Time +} + type HtlcKey struct { SwapHash []byte SenderScriptPubkey []byte diff --git a/loopdb/sqlc/querier.go b/loopdb/sqlc/querier.go index 20f502e..a5f9393 100644 --- a/loopdb/sqlc/querier.go +++ b/loopdb/sqlc/querier.go @@ -9,16 +9,20 @@ import ( ) type Querier interface { + AllDeposits(ctx context.Context) ([]Deposit, error) AllStaticAddresses(ctx context.Context) ([]StaticAddress, error) ConfirmBatch(ctx context.Context, id int32) error + CreateDeposit(ctx context.Context, arg CreateDepositParams) error CreateReservation(ctx context.Context, arg CreateReservationParams) error CreateStaticAddress(ctx context.Context, arg CreateStaticAddressParams) error FetchLiquidityParams(ctx context.Context) ([]byte, error) GetBatchSweeps(ctx context.Context, batchID int32) ([]GetBatchSweepsRow, error) GetBatchSweptAmount(ctx context.Context, batchID int32) (int64, error) + GetDeposit(ctx context.Context, depositID []byte) (Deposit, error) GetInstantOutSwap(ctx context.Context, swapHash []byte) (GetInstantOutSwapRow, error) GetInstantOutSwapUpdates(ctx context.Context, swapHash []byte) ([]InstantoutUpdate, error) GetInstantOutSwaps(ctx context.Context) ([]GetInstantOutSwapsRow, error) + GetLatestDepositUpdate(ctx context.Context, depositID []byte) (DepositUpdate, error) GetLoopInSwap(ctx context.Context, swapHash []byte) (GetLoopInSwapRow, error) GetLoopInSwaps(ctx context.Context) ([]GetLoopInSwapsRow, error) GetLoopOutSwap(ctx context.Context, swapHash []byte) (GetLoopOutSwapRow, error) @@ -32,6 +36,7 @@ type Querier interface { GetSweepStatus(ctx context.Context, swapHash []byte) (bool, error) GetUnconfirmedBatches(ctx context.Context) ([]SweepBatch, error) InsertBatch(ctx context.Context, arg InsertBatchParams) (int32, error) + InsertDepositUpdate(ctx context.Context, arg InsertDepositUpdateParams) error InsertHtlcKeys(ctx context.Context, arg InsertHtlcKeysParams) error InsertInstantOut(ctx context.Context, arg InsertInstantOutParams) error InsertInstantOutUpdate(ctx context.Context, arg InsertInstantOutUpdateParams) error @@ -41,6 +46,7 @@ type Querier interface { InsertSwap(ctx context.Context, arg InsertSwapParams) error InsertSwapUpdate(ctx context.Context, arg InsertSwapUpdateParams) error UpdateBatch(ctx context.Context, arg UpdateBatchParams) error + UpdateDeposit(ctx context.Context, arg UpdateDepositParams) error UpdateInstantOut(ctx context.Context, arg UpdateInstantOutParams) error UpdateReservation(ctx context.Context, arg UpdateReservationParams) error UpsertLiquidityParams(ctx context.Context, params []byte) error diff --git a/loopdb/sqlc/queries/static_address_deposits.sql b/loopdb/sqlc/queries/static_address_deposits.sql new file mode 100644 index 0000000..d09ca3f --- /dev/null +++ b/loopdb/sqlc/queries/static_address_deposits.sql @@ -0,0 +1,66 @@ +-- name: CreateDeposit :exec +INSERT INTO deposits ( + deposit_id, + tx_hash, + out_index, + amount, + confirmation_height, + timeout_sweep_pk_script, + expiry_sweep_txid +) VALUES ( + $1, + $2, + $3, + $4, + $5, + $6, + $7 + ); + +-- name: UpdateDeposit :exec +UPDATE deposits +SET + tx_hash = $2, + out_index = $3, + confirmation_height = $4, + expiry_sweep_txid = $5 +WHERE + deposits.deposit_id = $1; + +-- name: InsertDepositUpdate :exec +INSERT INTO deposit_updates ( + deposit_id, + update_state, + update_timestamp +) VALUES ( + $1, + $2, + $3 + ); + +-- name: GetDeposit :one +SELECT + * +FROM + deposits +WHERE + deposit_id = $1; + +-- name: AllDeposits :many +SELECT + * +FROM + deposits +ORDER BY + id ASC; + +-- name: GetLatestDepositUpdate :one +SELECT + * +FROM + deposit_updates +WHERE + deposit_id = $1 +ORDER BY + update_timestamp DESC +LIMIT 1; \ No newline at end of file diff --git a/loopdb/sqlc/static_address_deposits.sql.go b/loopdb/sqlc/static_address_deposits.sql.go new file mode 100644 index 0000000..8b4612d --- /dev/null +++ b/loopdb/sqlc/static_address_deposits.sql.go @@ -0,0 +1,197 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.25.0 +// source: static_address_deposits.sql + +package sqlc + +import ( + "context" + "time" +) + +const allDeposits = `-- name: AllDeposits :many +SELECT + id, deposit_id, tx_hash, out_index, amount, confirmation_height, timeout_sweep_pk_script, expiry_sweep_txid +FROM + deposits +ORDER BY + id ASC +` + +func (q *Queries) AllDeposits(ctx context.Context) ([]Deposit, error) { + rows, err := q.db.QueryContext(ctx, allDeposits) + if err != nil { + return nil, err + } + defer rows.Close() + var items []Deposit + for rows.Next() { + var i Deposit + if err := rows.Scan( + &i.ID, + &i.DepositID, + &i.TxHash, + &i.OutIndex, + &i.Amount, + &i.ConfirmationHeight, + &i.TimeoutSweepPkScript, + &i.ExpirySweepTxid, + ); 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 createDeposit = `-- name: CreateDeposit :exec +INSERT INTO deposits ( + deposit_id, + tx_hash, + out_index, + amount, + confirmation_height, + timeout_sweep_pk_script, + expiry_sweep_txid +) VALUES ( + $1, + $2, + $3, + $4, + $5, + $6, + $7 + ) +` + +type CreateDepositParams struct { + DepositID []byte + TxHash []byte + OutIndex int32 + Amount int64 + ConfirmationHeight int64 + TimeoutSweepPkScript []byte + ExpirySweepTxid []byte +} + +func (q *Queries) CreateDeposit(ctx context.Context, arg CreateDepositParams) error { + _, err := q.db.ExecContext(ctx, createDeposit, + arg.DepositID, + arg.TxHash, + arg.OutIndex, + arg.Amount, + arg.ConfirmationHeight, + arg.TimeoutSweepPkScript, + arg.ExpirySweepTxid, + ) + 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 +FROM + deposits +WHERE + deposit_id = $1 +` + +func (q *Queries) GetDeposit(ctx context.Context, depositID []byte) (Deposit, error) { + row := q.db.QueryRowContext(ctx, getDeposit, depositID) + var i Deposit + err := row.Scan( + &i.ID, + &i.DepositID, + &i.TxHash, + &i.OutIndex, + &i.Amount, + &i.ConfirmationHeight, + &i.TimeoutSweepPkScript, + &i.ExpirySweepTxid, + ) + return i, err +} + +const getLatestDepositUpdate = `-- name: GetLatestDepositUpdate :one +SELECT + id, deposit_id, update_state, update_timestamp +FROM + deposit_updates +WHERE + deposit_id = $1 +ORDER BY + update_timestamp DESC +LIMIT 1 +` + +func (q *Queries) GetLatestDepositUpdate(ctx context.Context, depositID []byte) (DepositUpdate, error) { + row := q.db.QueryRowContext(ctx, getLatestDepositUpdate, depositID) + var i DepositUpdate + err := row.Scan( + &i.ID, + &i.DepositID, + &i.UpdateState, + &i.UpdateTimestamp, + ) + return i, err +} + +const insertDepositUpdate = `-- name: InsertDepositUpdate :exec +INSERT INTO deposit_updates ( + deposit_id, + update_state, + update_timestamp +) VALUES ( + $1, + $2, + $3 + ) +` + +type InsertDepositUpdateParams struct { + DepositID []byte + UpdateState string + UpdateTimestamp time.Time +} + +func (q *Queries) InsertDepositUpdate(ctx context.Context, arg InsertDepositUpdateParams) error { + _, err := q.db.ExecContext(ctx, insertDepositUpdate, arg.DepositID, arg.UpdateState, arg.UpdateTimestamp) + return err +} + +const updateDeposit = `-- name: UpdateDeposit :exec +UPDATE deposits +SET + tx_hash = $2, + out_index = $3, + confirmation_height = $4, + expiry_sweep_txid = $5 +WHERE + deposits.deposit_id = $1 +` + +type UpdateDepositParams struct { + DepositID []byte + TxHash []byte + OutIndex int32 + ConfirmationHeight int64 + ExpirySweepTxid []byte +} + +func (q *Queries) UpdateDeposit(ctx context.Context, arg UpdateDepositParams) error { + _, err := q.db.ExecContext(ctx, updateDeposit, + arg.DepositID, + arg.TxHash, + arg.OutIndex, + arg.ConfirmationHeight, + arg.ExpirySweepTxid, + ) + return err +} diff --git a/loopdb/sqlc/static_addresses.sql.go b/loopdb/sqlc/static_addresses.sql.go index cfe1a46..23902a2 100644 --- a/loopdb/sqlc/static_addresses.sql.go +++ b/loopdb/sqlc/static_addresses.sql.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.17.2 +// sqlc v1.25.0 // source: static_addresses.sql package sqlc