Skip to content

Commit 48e6c15

Browse files
committed
tapdb/sqlc: add new supply_commit.sql queries
1 parent b8979da commit 48e6c15

File tree

1 file changed

+192
-0
lines changed

1 file changed

+192
-0
lines changed

tapdb/sqlc/queries/supply_commit.sql

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
-- name: UpsertSupplyCommitStateMachine :one
2+
WITH target_state AS (
3+
-- Select the ID for the provided state name, if it exists.
4+
SELECT id
5+
FROM supply_commit_states s1
6+
WHERE s1.state_name = sqlc.narg('state_name')
7+
), default_state AS (
8+
-- Select the ID for the 'DefaultState'.
9+
SELECT id
10+
FROM supply_commit_states s2
11+
WHERE s2.state_name = 'DefaultState'
12+
)
13+
INSERT INTO supply_commit_state_machines (
14+
group_key, current_state_id, latest_commitment_id
15+
) VALUES (
16+
@group_key,
17+
-- Use the target state ID if found, otherwise use the default state ID.
18+
coalesce((SELECT id FROM target_state), (SELECT id FROM default_state)),
19+
sqlc.narg('latest_commitment_id')
20+
)
21+
ON CONFLICT (group_key)
22+
DO UPDATE SET
23+
-- Update state ID only if a target state ID was found, otherwise keep existing.
24+
current_state_id = coalesce((SELECT id FROM target_state), supply_commit_state_machines.current_state_id),
25+
latest_commitment_id = coalesce(sqlc.narg('latest_commitment_id'), supply_commit_state_machines.latest_commitment_id)
26+
-- Return the ID of the state that was actually set (either inserted or updated),
27+
-- and the latest commitment ID that was set.
28+
RETURNING current_state_id, latest_commitment_id;
29+
30+
-- name: InsertSupplyCommitment :one
31+
INSERT INTO supply_commitments (
32+
group_key, chain_txn_id,
33+
output_index, internal_key_id, output_key, -- Core fields
34+
block_height, block_header, merkle_proof, -- Nullable chain details
35+
supply_root_hash, supply_root_sum -- Nullable root details
36+
) VALUES (
37+
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10
38+
) RETURNING commit_id;
39+
40+
-- name: UpdateSupplyCommitmentChainDetails :exec
41+
UPDATE supply_commitments
42+
SET merkle_proof = @merkle_proof,
43+
output_index = @output_index,
44+
block_header = @block_header,
45+
chain_txn_id = @chain_txn_id,
46+
block_height = @block_height
47+
WHERE commit_id = @commit_id;
48+
49+
-- name: UpdateSupplyCommitmentRoot :exec
50+
UPDATE supply_commitments
51+
SET supply_root_hash = @supply_root_hash,
52+
supply_root_sum = @supply_root_sum
53+
WHERE commit_id = @commit_id;
54+
55+
-- name: InsertSupplyCommitTransition :one
56+
INSERT INTO supply_commit_transitions (
57+
state_machine_group_key, old_commitment_id, new_commitment_id,
58+
pending_commit_txn_id, finalized, creation_time
59+
) VALUES (
60+
$1, $2, $3, $4, $5, $6
61+
) RETURNING transition_id;
62+
63+
-- name: FinalizeSupplyCommitTransition :exec
64+
UPDATE supply_commit_transitions
65+
SET finalized = TRUE
66+
WHERE transition_id = @transition_id;
67+
68+
-- name: InsertSupplyUpdateEvent :exec
69+
INSERT INTO supply_update_events (
70+
transition_id, update_type_id, event_data
71+
) VALUES (
72+
$1, $2, $3
73+
);
74+
75+
-- name: QuerySupplyCommitStateMachine :one
76+
SELECT
77+
sm.group_key,
78+
sm.current_state_id,
79+
states.state_name,
80+
sm.latest_commitment_id
81+
FROM supply_commit_state_machines sm
82+
JOIN supply_commit_states states
83+
ON sm.current_state_id = states.id
84+
WHERE sm.group_key = @group_key;
85+
86+
-- name: QueryPendingSupplyCommitTransition :one
87+
WITH target_machine AS (
88+
SELECT group_key
89+
FROM supply_commit_state_machines
90+
WHERE group_key = @group_key
91+
)
92+
SELECT
93+
t.transition_id,
94+
t.state_machine_group_key,
95+
t.old_commitment_id,
96+
t.new_commitment_id,
97+
t.pending_commit_txn_id,
98+
t.finalized,
99+
t.creation_time
100+
FROM supply_commit_transitions t
101+
JOIN target_machine tm
102+
ON t.state_machine_group_key = tm.group_key
103+
WHERE t.finalized = FALSE
104+
ORDER BY t.creation_time DESC
105+
LIMIT 1;
106+
107+
-- name: QuerySupplyUpdateEvents :many
108+
SELECT
109+
ue.event_id,
110+
ue.transition_id,
111+
ue.update_type_id,
112+
types.update_type_name,
113+
ue.event_data
114+
FROM supply_update_events ue
115+
JOIN supply_commit_update_types types
116+
ON ue.update_type_id = types.id
117+
WHERE ue.transition_id = @transition_id
118+
ORDER BY ue.event_id ASC;
119+
120+
-- name: QuerySupplyCommitment :one
121+
SELECT *
122+
FROM supply_commitments
123+
WHERE commit_id = @commit_id;
124+
125+
-- name: UpdateSupplyCommitTransitionCommitment :exec
126+
UPDATE supply_commit_transitions
127+
SET new_commitment_id = @new_commitment_id,
128+
pending_commit_txn_id = @pending_commit_txn_id
129+
WHERE transition_id = @transition_id;
130+
131+
-- name: DeleteSupplyCommitTransition :exec
132+
DELETE FROM supply_commit_transitions
133+
WHERE transition_id = @transition_id;
134+
135+
-- name: DeleteSupplyUpdateEvents :exec
136+
DELETE FROM supply_update_events
137+
WHERE transition_id = @transition_id;
138+
139+
-- name: FetchUnspentPrecommits :many
140+
SELECT
141+
mac.tx_output_index,
142+
ik.raw_key AS taproot_internal_key,
143+
mac.group_key,
144+
mint_txn.block_height,
145+
mint_txn.raw_tx
146+
FROM mint_anchor_uni_commitments mac
147+
JOIN asset_minting_batches amb ON mac.batch_id = amb.batch_id
148+
JOIN genesis_points gp ON amb.genesis_id = gp.genesis_id
149+
JOIN chain_txns mint_txn ON gp.anchor_tx_id = mint_txn.txn_id
150+
JOIN internal_keys ik ON mac.taproot_internal_key_id = ik.key_id
151+
LEFT JOIN supply_commitments sc ON mac.spent_by = sc.commit_id
152+
LEFT JOIN chain_txns commit_txn ON sc.chain_txn_id = commit_txn.txn_id
153+
WHERE
154+
mac.group_key = @group_key AND
155+
(mac.spent_by IS NULL OR commit_txn.block_hash IS NULL);
156+
157+
-- name: FetchSupplyCommit :one
158+
SELECT
159+
sc.commit_id,
160+
sc.output_index,
161+
sc.output_key,
162+
ik.raw_key AS internal_key,
163+
txn.raw_tx,
164+
sc.supply_root_hash AS root_hash,
165+
sc.supply_root_sum AS root_sum
166+
FROM supply_commit_state_machines sm
167+
JOIN supply_commitments sc
168+
ON sm.latest_commitment_id = sc.commit_id
169+
JOIN chain_txns txn
170+
ON sc.chain_txn_id = txn.txn_id
171+
JOIN internal_keys ik
172+
ON sc.internal_key_id = ik.key_id
173+
WHERE
174+
sm.group_key = @group_key AND
175+
txn.block_hash IS NOT NULL;
176+
177+
-- name: QueryExistingPendingTransition :one
178+
-- Find the ID of an existing non-finalized transition for the group key
179+
SELECT transition_id
180+
FROM supply_commit_transitions sct
181+
WHERE sct.state_machine_group_key = @group_key AND finalized = FALSE
182+
LIMIT 1;
183+
184+
-- name: FetchInternalKeyByID :one
185+
SELECT raw_key, key_family, key_index
186+
FROM internal_keys
187+
WHERE key_id = @key_id;
188+
189+
-- name: FetchChainTxByID :one
190+
SELECT raw_tx, block_height -- Include block_height needed by FetchState
191+
FROM chain_txns
192+
WHERE txn_id = @txn_id;

0 commit comments

Comments
 (0)