Skip to content

Commit 45d9c7a

Browse files
refactor: improve nbtc_deposit_addresses usage (#328)
* refactor: improve nbtc_deposit_addresses usage Signed-off-by: Robert Zaremba <robert@zaremba.ch>
1 parent e0e7763 commit 45d9c7a

File tree

5 files changed

+38
-62
lines changed

5 files changed

+38
-62
lines changed

packages/btcindexer/db/migrations/0001_initial_schema.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ CREATE INDEX IF NOT EXISTS btc_blocks_is_scanned_height ON btc_blocks (is_scanne
1616
-- This table tracks the nBTC deposit txs (minting)
1717
CREATE TABLE IF NOT EXISTS nbtc_minting (
1818
tx_id TEXT NOT NULL PRIMARY KEY,
19-
address_id INTEGER NOT NULL, -- nbtc pkg is linked through address_id
19+
address_id INTEGER NOT NULL, -- nbtc pkg is linked through address_id -> nbtc_deposit_addresses.setup_id
2020
sender TEXT NOT NULL,
2121
vout INTEGER NOT NULL,
2222
block_hash TEXT,

packages/btcindexer/scripts/seed-config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ async function main() {
6767
continue;
6868
}
6969

70-
const checkAddrQuery = `SELECT id FROM nbtc_deposit_addresses WHERE setup_id = ${setupId} AND deposit_address = '${entry.btc_address}'`;
71-
const existingAddrId = await executeQuery<number>(checkAddrQuery, DB_NAME, local, "id");
70+
const checkAddrQuery = `SELECT 1 as "exists" FROM nbtc_deposit_addresses WHERE setup_id = ${setupId} AND deposit_address = '${entry.btc_address}'`;
71+
const existingAddrId = await executeQuery<number>(checkAddrQuery, DB_NAME, local, "exists");
7272

7373
if (existingAddrId) {
7474
continue;

packages/btcindexer/src/btcindexer.helpers.test.ts

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ export async function setupTestIndexerSuite(
261261
mockSuiClient.tryMintNbtcBatch.mockResolvedValue(result);
262262
};
263263

264-
const insertTx = async (options: {
264+
const insertTx = async (args: {
265265
txId: string;
266266
status: MintTxStatus | string;
267267
retryCount?: number;
@@ -276,39 +276,28 @@ export async function setupTestIndexerSuite(
276276
const defaultBlock = testData[329] || testData[327] || Object.values(testData)[0];
277277
if (!defaultBlock) throw new Error("No test data available for default values");
278278

279-
const depositAddr = options.depositAddress || defaultBlock.depositAddr;
280-
281-
// Validate that the deposit address exists in the database
282-
const addressResult = await db
283-
.prepare(`SELECT id FROM nbtc_deposit_addresses WHERE deposit_address = ?`)
284-
.bind(depositAddr)
285-
.first<{ id: number }>();
286-
287-
if (!addressResult) {
288-
throw new Error(
289-
`Deposit address '${depositAddr}' not found in database. ` +
290-
`Make sure to include it in the depositAddresses array during setupTestIndexer().`,
291-
);
292-
}
293-
279+
const depositAddr = args.depositAddress || defaultBlock.depositAddr;
294280
await db
295281
.prepare(
296282
`INSERT INTO nbtc_minting (tx_id, address_id, sender, vout, block_hash, block_height, sui_recipient, amount, status, created_at, updated_at, retry_count)
297-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
283+
VALUES (
284+
?,
285+
(SELECT id FROM nbtc_deposit_addresses WHERE deposit_address = ?),
286+
?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
298287
)
299288
.bind(
300-
options.txId,
301-
addressResult.id,
302-
options.sender || "sender_address",
303-
options.vout ?? 0,
304-
options.blockHash || defaultBlock.hash,
305-
options.blockHeight || defaultBlock.height,
306-
options.suiRecipient || "0xtest_recipient",
307-
options.amount || 10000,
308-
options.status,
289+
args.txId,
290+
depositAddr,
291+
args.sender || "sender_address",
292+
args.vout ?? 0,
293+
args.blockHash || defaultBlock.hash,
294+
args.blockHeight || defaultBlock.height,
295+
args.suiRecipient || "0xtest_recipient",
296+
args.amount || 10000,
297+
args.status,
309298
Date.now(),
310299
Date.now(),
311-
options.retryCount || 0,
300+
args.retryCount || 0,
312301
)
313302
.run();
314303
};

packages/btcindexer/src/cf-storage.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -185,22 +185,21 @@ export class CFStorage implements Storage {
185185
const now = Date.now();
186186
const insertOrUpdateNbtcTxStmt = this.d1.prepare(
187187
`INSERT INTO nbtc_minting (tx_id, address_id, sender, vout, block_hash, block_height, sui_recipient, amount, status, created_at, updated_at, sui_tx_id, retry_count)
188-
VALUES (?, (SELECT a.id FROM nbtc_deposit_addresses a JOIN setups p ON a.setup_id = p.id WHERE p.btc_network = ? AND p.sui_network = ? AND p.nbtc_pkg = ? AND a.deposit_address = ?), ?, ?, ?, ?, ?, ?, '${MintTxStatus.Confirming}', ?, ?, NULL, 0)
189-
ON CONFLICT(tx_id) DO UPDATE SET
190-
block_hash = excluded.block_hash,
191-
block_height = excluded.block_height,
192-
status = '${MintTxStatus.Confirming}',
193-
updated_at = excluded.updated_at,
194-
address_id = excluded.address_id,
195-
sender = excluded.sender`,
188+
VALUES (?,
189+
(SELECT a.id FROM nbtc_deposit_addresses a WHERE a.deposit_address = ?),
190+
?, ?, ?, ?, ?, ?, '${MintTxStatus.Confirming}', ?, ?, NULL, 0)
191+
ON CONFLICT(tx_id) DO UPDATE SET
192+
block_hash = excluded.block_hash,
193+
block_height = excluded.block_height,
194+
status = '${MintTxStatus.Confirming}',
195+
updated_at = excluded.updated_at,
196+
address_id = excluded.address_id,
197+
sender = excluded.sender`,
196198
);
197199
const statements = txs.map((tx) =>
198200
insertOrUpdateNbtcTxStmt.bind(
199201
tx.txId,
200-
tx.btcNetwork,
201-
tx.suiNetwork,
202-
tx.nbtcPkg,
203-
tx.depositAddress,
202+
tx.depositAddress, // inner select param
204203
tx.sender,
205204
tx.vout,
206205
tx.blockHash,
@@ -419,15 +418,14 @@ export class CFStorage implements Storage {
419418
const now = Date.now();
420419
const insertStmt = this.d1.prepare(
421420
`INSERT OR IGNORE INTO nbtc_minting (tx_id, address_id, sender, vout, sui_recipient, amount, status, created_at, updated_at, sui_tx_id, retry_count)
422-
VALUES (?, (SELECT a.id FROM nbtc_deposit_addresses a JOIN setups p ON a.setup_id = p.id WHERE p.btc_network = ? AND p.sui_network = ? AND p.nbtc_pkg = ? AND a.deposit_address = ?), ?, ?, ?, ?, '${MintTxStatus.Broadcasting}', ?, ?, NULL, 0)`,
421+
VALUES (?,
422+
(SELECT a.id FROM nbtc_deposit_addresses a WHERE a.deposit_address = ?),
423+
?, ?, ?, ?, '${MintTxStatus.Broadcasting}', ?, ?, NULL, 0)`,
423424
);
424425

425426
const statements = deposits.map((deposit) =>
426427
insertStmt.bind(
427428
deposit.txId,
428-
deposit.btcNetwork,
429-
deposit.suiNetwork,
430-
deposit.nbtcPkg,
431429
deposit.depositAddress,
432430
deposit.sender,
433431
deposit.vout,

packages/sui-indexer/src/storage.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -155,29 +155,18 @@ export class D1Storage {
155155
throw new Error(`Failed to derive address from script_pubkey: ${e}`);
156156
}
157157

158-
const addrRow = await this.db
159-
.prepare(
160-
"SELECT id FROM nbtc_deposit_addresses WHERE setup_id = ? AND deposit_address = ?",
161-
)
162-
.bind(u.setup_id, depositAddress)
163-
.first<{ id: number }>();
164-
165-
if (!addrRow) {
166-
throw new Error(
167-
`Deposit address not found for setup_id=${u.setup_id}, address=${depositAddress}`,
168-
);
169-
}
170-
171158
const stmt = this.db.prepare(
172159
`INSERT OR REPLACE INTO nbtc_utxos
173-
(nbtc_utxo_id, address_id, dwallet_id, txid, vout, amount, script_pubkey, status, locked_until)
174-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
160+
(nbtc_utxo_id, address_id, dwallet_id, txid, vout, amount, script_pubkey, status, locked_until)
161+
VALUES (?,
162+
(SELECT id FROM nbtc_deposit_addresses WHERE deposit_address = ?),
163+
?, ?, ?, ?, ?, ?, ?)`,
175164
);
176165
try {
177166
await stmt
178167
.bind(
179168
u.nbtc_utxo_id,
180-
addrRow.id,
169+
depositAddress,
181170
u.dwallet_id,
182171
u.txid,
183172
u.vout,

0 commit comments

Comments
 (0)