11import { Block } from "bitcoinjs-lib" ;
22import { toSerializableError } from "./errutils" ;
33import type { BlockInfo , FinalizedTxRow , NbtcTxRow , PendingTx } from "./models" ;
4- import { BlockStatus , BtcTxStatus } from "./models" ;
4+ import { BlockStatus , MintTxStatus } from "./models" ;
55import type { Storage } from "./storage" ;
66
77export class CFStorage implements Storage {
@@ -146,11 +146,11 @@ export class CFStorage implements Storage {
146146 const now = Date . now ( ) ;
147147 const insertOrUpdateNbtcTxStmt = this . d1 . prepare (
148148 `INSERT INTO nbtc_minting (tx_id, vout, block_hash, block_height, sui_recipient, amount_sats, status, created_at, updated_at)
149- VALUES (?, ?, ?, ?, ?, ?, '${ BtcTxStatus . Confirming } ', ?, ?)
149+ VALUES (?, ?, ?, ?, ?, ?, '${ MintTxStatus . Confirming } ', ?, ?)
150150 ON CONFLICT(tx_id, vout) DO UPDATE SET
151151 block_hash = excluded.block_hash,
152152 block_height = excluded.block_height,
153- status = '${ BtcTxStatus . Confirming } ',
153+ status = '${ MintTxStatus . Confirming } ',
154154 updated_at = excluded.updated_at` ,
155155 ) ;
156156 const statements = txs . map ( ( tx ) =>
@@ -176,17 +176,17 @@ export class CFStorage implements Storage {
176176 }
177177 }
178178
179- async getFinalizedTxs ( maxRetries : number ) : Promise < FinalizedTxRow [ ] > {
179+ async getNbtcFinalizedTxs ( maxRetries : number ) : Promise < FinalizedTxRow [ ] > {
180180 const finalizedTxs = await this . d1
181181 . prepare (
182- `SELECT tx_id, vout, block_hash, block_height, retry_count, nbtc_pkg, sui_network FROM nbtc_minting WHERE (status = '${ BtcTxStatus . Finalized } ' OR (status = '${ BtcTxStatus . MintFailed } ' AND retry_count <= ?)) AND status != '${ BtcTxStatus . FinalizedReorg } '` ,
182+ `SELECT tx_id, vout, block_hash, block_height, retry_count, nbtc_pkg, sui_network FROM nbtc_minting WHERE (status = '${ MintTxStatus . Finalized } ' OR (status = '${ MintTxStatus . MintFailed } ' AND retry_count <= ?)) AND status != '${ MintTxStatus . FinalizedReorg } '` ,
183183 )
184184 . bind ( maxRetries )
185185 . all < FinalizedTxRow > ( ) ;
186186 return finalizedTxs . results ?? [ ] ;
187187 }
188188
189- async updateTxsStatus ( txIds : string [ ] , status : BtcTxStatus ) : Promise < void > {
189+ async updateNbtcTxsStatus ( txIds : string [ ] , status : MintTxStatus ) : Promise < void > {
190190 if ( txIds . length === 0 ) {
191191 return ;
192192 }
@@ -201,7 +201,7 @@ export class CFStorage implements Storage {
201201 }
202202
203203 async batchUpdateNbtcTxs (
204- updates : { tx_id : string ; vout : number ; status : BtcTxStatus ; suiTxDigest ?: string } [ ] ,
204+ updates : { tx_id : string ; vout : number ; status : MintTxStatus ; suiTxDigest ?: string } [ ] ,
205205 ) : Promise < void > {
206206 const now = Date . now ( ) ;
207207 const setMintedStmt = this . d1 . prepare (
@@ -212,10 +212,10 @@ export class CFStorage implements Storage {
212212 ) ;
213213
214214 const statements = updates . map ( ( p ) => {
215- if ( p . status === BtcTxStatus . Minted ) {
216- return setMintedStmt . bind ( BtcTxStatus . Minted , p . suiTxDigest , now , p . tx_id , p . vout ) ;
215+ if ( p . status === MintTxStatus . Minted ) {
216+ return setMintedStmt . bind ( MintTxStatus . Minted , p . suiTxDigest , now , p . tx_id , p . vout ) ;
217217 } else {
218- return setFailedStmt . bind ( BtcTxStatus . MintFailed , now , p . tx_id , p . vout ) ;
218+ return setFailedStmt . bind ( MintTxStatus . MintFailed , now , p . tx_id , p . vout ) ;
219219 }
220220 } ) ;
221221 try {
@@ -236,7 +236,7 @@ export class CFStorage implements Storage {
236236 // This ensures we only try to verify blocks we know about.
237237 const blocksToVerify = await this . d1
238238 . prepare (
239- `SELECT DISTINCT block_hash FROM nbtc_minting WHERE status = '${ BtcTxStatus . Confirming } ' AND block_hash IS NOT NULL` ,
239+ `SELECT DISTINCT block_hash FROM nbtc_minting WHERE status = '${ MintTxStatus . Confirming } ' AND block_hash IS NOT NULL` ,
240240 )
241241 . all < { block_hash : string } > ( ) ;
242242 return blocksToVerify . results ?? [ ] ;
@@ -247,7 +247,7 @@ export class CFStorage implements Storage {
247247 const placeholders = blockHashes . map ( ( ) => "?" ) . join ( "," ) ;
248248 const updateStmt = this . d1
249249 . prepare (
250- `UPDATE nbtc_minting SET status = '${ BtcTxStatus . Reorg } ', updated_at = ? WHERE block_hash IN (${ placeholders } )` ,
250+ `UPDATE nbtc_minting SET status = '${ MintTxStatus . Reorg } ', updated_at = ? WHERE block_hash IN (${ placeholders } )` ,
251251 )
252252 . bind ( now , ...blockHashes ) ;
253253 await updateStmt . run ( ) ;
@@ -256,33 +256,32 @@ export class CFStorage implements Storage {
256256 async getConfirmingTxs ( ) : Promise < PendingTx [ ] > {
257257 const pendingTxs = await this . d1
258258 . prepare (
259- `SELECT tx_id, block_hash, block_height FROM nbtc_minting WHERE status = '${ BtcTxStatus . Confirming } '` ,
259+ `SELECT tx_id, block_hash, block_height FROM nbtc_minting WHERE status = '${ MintTxStatus . Confirming } '` ,
260260 )
261261 . all < PendingTx > ( ) ;
262262 return pendingTxs . results ?? [ ] ;
263263 }
264264
265- async finalizeTxs ( txIds : string [ ] ) : Promise < void > {
265+ async finalizeNbtcTxs ( txIds : string [ ] ) : Promise < void > {
266266 if ( txIds . length === 0 ) {
267267 return ;
268268 }
269269 const now = Date . now ( ) ;
270270 const finalizeStmt = this . d1 . prepare (
271- `UPDATE nbtc_minting SET status = '${ BtcTxStatus . Finalized } ', updated_at = ${ now } WHERE tx_id = ?` ,
271+ `UPDATE nbtc_minting SET status = '${ MintTxStatus . Finalized } ', updated_at = ${ now } WHERE tx_id = ?` ,
272272 ) ;
273273 const statements = txIds . map ( ( txId ) => finalizeStmt . bind ( txId ) ) ;
274274 await this . d1 . batch ( statements ) ;
275275 }
276276
277- // TODO: rename to getNbtcMineTx
278- async getStatusByTxid ( txid : string ) : Promise < NbtcTxRow | null > {
277+ async getNbtcMintTx ( txId : string ) : Promise < NbtcTxRow | null > {
279278 return this . d1
280279 . prepare ( "SELECT * FROM nbtc_minting WHERE tx_id = ?" )
281- . bind ( txid )
280+ . bind ( txId )
282281 . first < NbtcTxRow > ( ) ;
283282 }
284283
285- async getStatusBySuiAddress ( suiAddress : string ) : Promise < NbtcTxRow [ ] > {
284+ async getNbtcMintTxsBySuiAddr ( suiAddress : string ) : Promise < NbtcTxRow [ ] > {
286285 const dbResult = await this . d1
287286 . prepare ( "SELECT * FROM nbtc_minting WHERE sui_recipient = ? ORDER BY created_at DESC" )
288287 . bind ( suiAddress )
@@ -296,7 +295,7 @@ export class CFStorage implements Storage {
296295 const now = Date . now ( ) ;
297296 const insertStmt = this . d1 . prepare (
298297 `INSERT OR IGNORE INTO nbtc_minting (tx_id, vout, sui_recipient, amount_sats, status, created_at, updated_at)
299- VALUES (?, ?, ?, ?, '${ BtcTxStatus . Broadcasting } ', ?, ?)` ,
298+ VALUES (?, ?, ?, ?, '${ MintTxStatus . Broadcasting } ', ?, ?)` ,
300299 ) ;
301300
302301 const statements = deposits . map ( ( deposit ) =>
@@ -312,7 +311,7 @@ export class CFStorage implements Storage {
312311 await this . d1 . batch ( statements ) ;
313312 }
314313
315- async getDepositsBySender ( btcAddress : string ) : Promise < NbtcTxRow [ ] > {
314+ async getNbtcMintTxsByBtcSender ( btcAddress : string ) : Promise < NbtcTxRow [ ] > {
316315 const query = this . d1 . prepare ( `
317316 SELECT m.* FROM nbtc_minting m
318317 JOIN nbtc_sender_deposits s ON m.tx_id = s.tx_id
@@ -323,7 +322,7 @@ export class CFStorage implements Storage {
323322 return dbResult . results ?? [ ] ;
324323 }
325324
326- async insertSenderDeposits ( senders : { txId : string ; sender : string } [ ] ) : Promise < void > {
325+ async insertBtcDeposit ( senders : { txId : string ; sender : string } [ ] ) : Promise < void > {
327326 if ( senders . length === 0 ) {
328327 return ;
329328 }
0 commit comments