-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0001_initial_schema.sql
More file actions
134 lines (118 loc) · 5.23 KB
/
0001_initial_schema.sql
File metadata and controls
134 lines (118 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
-- This table tracks the blocks received from the relayer (queue for cron job)
CREATE TABLE IF NOT EXISTS btc_blocks (
hash TEXT NOT NULL,
height INTEGER NOT NULL,
network TEXT NOT NULL,
is_scanned INTEGER NOT NULL DEFAULT 0, -- 0 = not scanned, 1 = scanned
processed_at INTEGER, -- timestamp_ms
inserted_at INTEGER, -- timestamp_ms
PRIMARY KEY (height, network)
) STRICT;
CREATE INDEX IF NOT EXISTS btc_blocks_is_scanned_height ON btc_blocks (is_scanned, height);
---------- NBTC Minting and Withdrawal ----------
-- This table tracks the nBTC deposit txs (minting)
CREATE TABLE IF NOT EXISTS nbtc_minting (
tx_id TEXT NOT NULL PRIMARY KEY,
address_id INTEGER NOT NULL, -- nbtc pkg is linked through address_id -> nbtc_deposit_addresses.setup_id
sender TEXT NOT NULL,
vout INTEGER NOT NULL,
block_hash TEXT,
block_height INTEGER,
sui_recipient TEXT NOT NULL,
amount INTEGER NOT NULL,
status TEXT NOT NULL, -- 'broadcasting' | 'confirming' | 'finalized' | 'minting' | 'minted' | 'reorg'
created_at INTEGER NOT NULL, -- timestamp_ms
updated_at INTEGER NOT NULL, -- timestamp_ms
sui_tx_id TEXT,
retry_count INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (address_id) REFERENCES nbtc_deposit_addresses(id)
) STRICT;
CREATE INDEX IF NOT EXISTS nbtc_minting_status ON nbtc_minting (address_id, status);
CREATE INDEX IF NOT EXISTS nbtc_minting_sui_recipient ON nbtc_minting (sui_recipient, created_at);
CREATE INDEX IF NOT EXISTS nbtc_minting_sender ON nbtc_minting (sender);
-- This table holds the config for nBTC setups.
CREATE TABLE IF NOT EXISTS setups (
id INTEGER PRIMARY KEY,
btc_network TEXT NOT NULL,
sui_network TEXT NOT NULL,
nbtc_pkg TEXT NOT NULL,
nbtc_contract TEXT NOT NULL,
lc_pkg TEXT NOT NULL,
lc_contract TEXT NOT NULL,
nbtc_fallback_addr TEXT NOT NULL,
ika_pkg TEXT, -- Ika coordinator pkg
is_active INTEGER NOT NULL DEFAULT TRUE,
UNIQUE(sui_network, btc_network, nbtc_pkg)
) STRICT;
CREATE TABLE IF NOT EXISTS nbtc_deposit_addresses (
id INTEGER PRIMARY KEY,
setup_id INTEGER NOT NULL,
deposit_address TEXT NOT NULL,
is_active INTEGER NOT NULL DEFAULT 1,
FOREIGN KEY (setup_id) REFERENCES setups(id) ON DELETE CASCADE,
-- make sure we don't share bitcoin deposit address between setups
UNIQUE(deposit_address)
) STRICT;
CREATE TABLE IF NOT EXISTS nbtc_utxos (
nbtc_utxo_id INTEGER NOT NULL PRIMARY KEY, -- Sui ID asigned to this UTXO
-- TODO: This is an ID assigned by the smart contract. The primary key should be a combination of (setup_id, nbtc_utxo_id)
address_id INTEGER NOT NULL,
dwallet_id TEXT NOT NULL,
txid TEXT NOT NULL, -- Bitcoin transaction ID
vout INTEGER NOT NULL,
amount INTEGER NOT NULL,
script_pubkey BLOB NOT NULL,
status TEXT NOT NULL DEFAULT 'available', -- 'available', 'locked', 'spent' TODO: lets remove the 'spent' utxos after some time?
locked_until INTEGER,
FOREIGN KEY (address_id) REFERENCES nbtc_deposit_addresses(id)
) STRICT;
CREATE INDEX IF NOT EXISTS nbtc_utxos_selection ON nbtc_utxos(address_id, status, amount);
CREATE INDEX IF NOT EXISTS _nbtc_utxos_txid_vout ON nbtc_utxos(txid, vout);
CREATE TABLE IF NOT EXISTS nbtc_redeem_requests (
redeem_id INTEGER NOT NULL PRIMARY KEY,
setup_id INTEGER NOT NULL,
redeemer TEXT NOT NULL,
recipient_script BLOB NOT NULL, -- script pubkey
amount INTEGER NOT NULL,
created_at INTEGER NOT NULL,
sui_tx TEXT NOT NULL,
btc_tx TEXT, -- null if not broadcasted
status TEXT NOT NULL DEFAULT 'pending', -- 'pending', 'proposed', 'solved', 'signed', 'broadcasting', 'confirming'
btc_block_height INTEGER,
btc_block_hash TEXT,
btc_broadcasted_at INTEGER,
FOREIGN KEY (setup_id) REFERENCES setups(id)
) STRICT;
CREATE INDEX IF NOT EXISTS nbtc_redeem_requests_redeemer ON nbtc_redeem_requests (setup_id, redeemer);
CREATE INDEX IF NOT EXISTS nbtc_redeem_requests_btc_tx ON nbtc_redeem_requests (btc_tx);
CREATE TABLE IF NOT EXISTS nbtc_redeem_solutions (
redeem_id INTEGER NOT NULL, -- Reference to nbtc_redeem_requests (u64 from nbtc contract)
utxo_id INTEGER NOT NULL, -- Reference to nbtc_utxos
input_index INTEGER NOT NULL, -- The position of UTXO in the btc tx inputs
dwallet_id TEXT NOT NULL, -- The dWallet identifier this input
sign_id TEXT, -- Ika signature request identifier
verified INTEGER NOT NULL DEFAULT 0,
created_at INTEGER NOT NULL,
PRIMARY KEY (redeem_id, utxo_id),
FOREIGN KEY (redeem_id) REFERENCES nbtc_redeem_requests(redeem_id),
FOREIGN KEY (utxo_id) REFERENCES nbtc_utxos(nbtc_utxo_id)
) STRICT;
CREATE INDEX IF NOT EXISTS nbtc_redeem_solutions_redeem_id ON nbtc_redeem_solutions(redeem_id);
CREATE TABLE IF NOT EXISTS indexer_state (
setup_id INTEGER PRIMARY KEY,
nbtc_cursor TEXT NOT NULL, -- last processed cursor state
ika_cursor TEXT, -- IKA coordinator cursor
updated_at INTEGER, -- epoch time in ms
FOREIGN KEY (setup_id) REFERENCES setups(id)
) STRICT;
CREATE TABLE IF NOT EXISTS presign_objects (
presign_id TEXT NOT NULL PRIMARY KEY,
sui_network TEXT NOT NULL, -- for simplicity, we can reuse the presigns between the setups in the same network
created_at INTEGER NOT NULL
) STRICT;
CREATE INDEX IF NOT EXISTS presign_objects_sui_network_created_at ON presign_objects(sui_network, created_at);
CREATE TABLE IF NOT EXISTS cron_locks (
lock_name TEXT NOT NULL PRIMARY KEY,
acquired_at INTEGER NOT NULL,
expires_at INTEGER NOT NULL
) STRICT;