Skip to content

Commit 94aeae8

Browse files
committed
loopdb: withdrawal table and queries
1 parent b780a9a commit 94aeae8

File tree

6 files changed

+282
-0
lines changed

6 files changed

+282
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
DROP TABLE IF EXISTS withdrawals;
2+
DROP TABLE IF EXISTS withdrawal_deposits;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
-- withdrawals stores finalized static address withdrawals.
2+
CREATE TABLE IF NOT EXISTS withdrawals (
3+
-- id is the auto-incrementing primary key for a withdrawal.
4+
id INTEGER PRIMARY KEY,
5+
6+
-- withdrawal_id is the unique identifier for the withdrawal.
7+
withdrawal_id BLOB NOT NULL UNIQUE,
8+
9+
-- withdrawal_tx_id is the transaction tx id of the withdrawal.
10+
withdrawal_tx_id TEXT UNIQUE,
11+
12+
-- total_deposit_amount is the total amount of the deposits in satoshis.
13+
total_deposit_amount BIGINT NOT NULL,
14+
15+
-- withdrawn_amount is the total amount of the withdrawal. It amounts
16+
-- to the total amount of the deposits minus the fees and optional change.
17+
withdrawn_amount BIGINT,
18+
19+
-- change_amount is the optional change that the user selected.
20+
change_amount BIGINT,
21+
22+
-- initiation_time is the creation of the withdrawal.
23+
initiation_time TIMESTAMP NOT NULL,
24+
25+
-- confirmation_height is the block height at which the withdrawal was first
26+
-- confirmed.
27+
confirmation_height BIGINT
28+
);
29+
30+
CREATE TABLE IF NOT EXISTS withdrawal_deposits (
31+
-- id is the auto-incrementing primary key.
32+
id INTEGER PRIMARY KEY,
33+
34+
-- withdrawal_id references the withdrawals table.
35+
withdrawal_id BLOB NOT NULL REFERENCES withdrawals(withdrawal_id),
36+
37+
-- deposit_id references the deposits table.
38+
deposit_id BLOB NOT NULL REFERENCES deposits(deposit_id),
39+
40+
-- Ensure that each deposit is used only once per withdrawal.
41+
UNIQUE(deposit_id, withdrawal_id)
42+
);

loopdb/sqlc/models.go

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

loopdb/sqlc/querier.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
-- name: CreateWithdrawal :exec
2+
INSERT INTO withdrawals (
3+
withdrawal_id,
4+
total_deposit_amount,
5+
initiation_time
6+
) VALUES (
7+
$1, $2, $3
8+
);
9+
10+
-- name: CreateWithdrawalDeposit :exec
11+
INSERT INTO withdrawal_deposits (
12+
withdrawal_id,
13+
deposit_id
14+
) VALUES (
15+
$1, $2
16+
);
17+
18+
-- name: GetWithdrawalIDByDepositID :one
19+
SELECT withdrawal_id
20+
FROM withdrawal_deposits
21+
WHERE deposit_id = $1;
22+
23+
-- name: UpdateWithdrawal :exec
24+
UPDATE withdrawals
25+
SET
26+
withdrawal_tx_id = $2,
27+
withdrawn_amount = $3,
28+
change_amount = $4,
29+
confirmation_height = $5
30+
WHERE
31+
withdrawal_id = $1;
32+
33+
-- name: GetWithdrawalDeposits :many
34+
SELECT
35+
deposit_id
36+
FROM
37+
withdrawal_deposits
38+
WHERE
39+
withdrawal_id = $1;
40+
41+
-- name: GetAllWithdrawals :many
42+
SELECT
43+
*
44+
FROM
45+
withdrawals
46+
ORDER BY
47+
initiation_time DESC;

loopdb/sqlc/static_address_withdrawals.sql.go

Lines changed: 168 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)