@@ -3,6 +3,7 @@ import { address, networks } from "bitcoinjs-lib";
33import { OP_RETURN } from "./opcodes" ;
44import { MerkleTree } from "merkletreejs" ;
55import SHA256 from "crypto-js/sha256" ;
6+ import { SuiClient } from "./sui-client" ;
67
78const CONFIRMATION_DEPTH = 8 ;
89
@@ -23,19 +24,33 @@ export interface PendingTx {
2324 block_height : number ;
2425}
2526
27+ interface BlockRecord {
28+ tx_id : string ;
29+ block_hash : string ;
30+ block_height : number ;
31+ }
32+
2633export class Indexer {
2734 d1 : D1Database ; // SQL DB
2835 blocksDB : KVNamespace ;
2936 nbtcTxDB : KVNamespace ;
3037 nbtcScriptHex : string ;
3138 suiFallbackAddr : string ;
32-
33- constructor ( env : Env , nbtcAddr : string , fallbackAddr : string , network : networks . Network ) {
39+ suiClient : SuiClient ;
40+
41+ constructor (
42+ env : Env ,
43+ nbtcAddr : string ,
44+ fallbackAddr : string ,
45+ network : networks . Network ,
46+ suiClient : SuiClient ,
47+ ) {
3448 this . d1 = env . DB ;
3549 this . blocksDB = env . btc_blocks ;
3650 this . nbtcTxDB = env . nbtc_txs ;
3751 this . suiFallbackAddr = fallbackAddr ;
3852 this . nbtcScriptHex = address . toOutputScript ( nbtcAddr , network ) . toString ( "hex" ) ;
53+ this . suiClient = suiClient ;
3954 }
4055
4156 // returns number of processed and add blocks
@@ -153,14 +168,16 @@ export class Indexer {
153168
154169 async processFinalizedTransactions ( ) : Promise < void > {
155170 const finalizedTxs = await this . d1
156- . prepare ( "SELECT tx_id, block_hash FROM nbtc_txs WHERE status = 'finalized'" )
157- . all < { tx_id : string ; block_hash : string } > ( ) ;
171+ . prepare (
172+ "SELECT tx_id, block_hash, height FROM nbtc_txs WHERE status = 'finalized'" ,
173+ )
174+ . all < BlockRecord > ( ) ;
158175
159176 if ( ! finalizedTxs . results || finalizedTxs . results . length === 0 ) {
160177 return ;
161178 }
162179
163- const txsByBlock = new Map < string , { tx_id : string ; block_hash : string } [ ] > ( ) ;
180+ const txsByBlock = new Map < string , BlockRecord [ ] > ( ) ;
164181
165182 for ( const tx of finalizedTxs . results ) {
166183 if ( ! txsByBlock . has ( tx . block_hash ) ) {
@@ -172,8 +189,11 @@ export class Indexer {
172189
173190 const updates : D1PreparedStatement [ ] = [ ] ;
174191 // TODO: do we imidietly process it and check if it was succesful and just change the status to minted? This should be a matter of seconds at most.
175- const setMintingStmt = this . d1 . prepare (
176- "UPDATE nbtc_txs SET status = 'minting', updated_at = CURRENT_TIMESTAMP WHERE tx_id = ?" ,
192+ const setMintedStmt = this . d1 . prepare (
193+ "UPDATE nbtc_txs SET status = 'minted', updated_at = CURRENT_TIMESTAMP WHERE tx_id = ?" ,
194+ ) ;
195+ const setFailedStmt = this . d1 . prepare (
196+ "UPDATE nbtc_txs SET status = 'failed', updated_at = CURRENT_TIMESTAMP WHERE tx_id = ?" ,
177197 ) ;
178198
179199 for ( const [ block_hash , txsInBlock ] of txsByBlock . entries ( ) ) {
@@ -210,9 +230,17 @@ export class Indexer {
210230 continue ;
211231 }
212232
213- // TODO: Call the minting smart contract.
214- // await suiClient.mintNBTC();
215- updates . push ( setMintingStmt . bind ( txInfo . tx_id ) ) ;
233+ const isSuccess = await this . suiClient . tryMintNbtc (
234+ targetTx ,
235+ txInfo . block_height ,
236+ txIndex ,
237+ proof ,
238+ ) ;
239+ if ( isSuccess ) {
240+ updates . push ( setMintedStmt . bind ( txInfo . tx_id ) ) ;
241+ } else {
242+ updates . push ( setFailedStmt . bind ( txInfo . tx_id ) ) ;
243+ }
216244 }
217245 } catch ( e ) {
218246 console . error ( `Failed to process finalized txs in block ${ block_hash } :` , e ) ;
0 commit comments