@@ -4,8 +4,9 @@ import type { NetworkConfig } from "./models";
44import { Processor } from "./processor" ;
55import { D1Storage } from "./storage" ;
66import { logError , logger } from "@gonative-cc/lib/logger" ;
7+ import { getMnemonic } from "@gonative-cc/lib/secrets" ;
78import { RedeemService } from "./redeem-service" ;
8- import { createSuiClients } from "./redeem-sui-client" ;
9+ import { createSuiClients , type SuiClient } from "./redeem-sui-client" ;
910import type { Service } from "@cloudflare/workers-types" ;
1011import type { WorkerEntrypoint } from "cloudflare:workers" ;
1112import type { BtcIndexerRpc } from "@gonative-cc/btcindexer/rpc-interface" ;
@@ -26,18 +27,26 @@ export default {
2627 const storage = new D1Storage ( env . DB ) ;
2728 const activeNetworks = await storage . getActiveNetworks ( ) ;
2829
30+ const mnemonic = await getMnemonic ( env . NBTC_MINTING_SIGNER_MNEMONIC ) ;
31+ if ( ! mnemonic ) return ;
32+ const suiClients = await createSuiClients ( activeNetworks , mnemonic ) ;
33+
2934 // Run both indexer and redeem solver tasks in parallel
3035 const results = await Promise . allSettled ( [
31- runSuiIndexer ( storage , env , activeNetworks ) ,
32- runRedeemSolver ( storage , env , activeNetworks ) ,
36+ runSuiIndexer ( storage , activeNetworks , suiClients ) ,
37+ runRedeemSolver ( storage , env , suiClients ) ,
3338 ] ) ;
3439
3540 // Check for any rejected promises and log errors
3641 reportErrors ( results , "scheduled" , "Scheduled task error" , [ "SuiIndexer" , "RedeemSolver" ] ) ;
3742 } ,
3843} satisfies ExportedHandler < Env > ;
3944
40- async function runSuiIndexer ( storage : D1Storage , env : Env , activeNetworks : SuiNet [ ] ) {
45+ async function runSuiIndexer (
46+ storage : D1Storage ,
47+ activeNetworks : SuiNet [ ] ,
48+ suiClients : Map < SuiNet , SuiClient > ,
49+ ) {
4150 if ( activeNetworks . length === 0 ) {
4251 logger . info ( { msg : "No active packages/networks found in database." } ) ;
4352 return ;
@@ -61,7 +70,9 @@ async function runSuiIndexer(storage: D1Storage, env: Env, activeNetworks: SuiNe
6170 networks : networksToProcess . map ( ( n ) => n . name ) ,
6271 } ) ;
6372
64- const networkJobs = networksToProcess . map ( ( netCfg ) => poolAndProcessEvents ( netCfg , storage ) ) ;
73+ const networkJobs = networksToProcess . map ( ( netCfg ) =>
74+ poolAndProcessEvents ( netCfg , storage , suiClients ) ,
75+ ) ;
6576 const results = await Promise . allSettled ( networkJobs ) ;
6677 reportErrors (
6778 results ,
@@ -72,9 +83,14 @@ async function runSuiIndexer(storage: D1Storage, env: Env, activeNetworks: SuiNe
7283 ) ;
7384}
7485
75- async function poolAndProcessEvents ( netCfg : NetworkConfig , storage : D1Storage ) {
86+ async function poolAndProcessEvents (
87+ netCfg : NetworkConfig ,
88+ storage : D1Storage ,
89+ suiClients : Map < SuiNet , SuiClient > ,
90+ ) {
7691 const client = new SuiGraphQLClient ( netCfg . url ) ;
77- const p = new Processor ( netCfg , storage , client ) ;
92+ const suiClient = suiClients . get ( netCfg . name ) ;
93+ const p = new Processor ( netCfg , storage , client , suiClient ) ;
7894
7995 const nbtcPkgs = await storage . getActiveNbtcPkgs ( netCfg . name ) ;
8096 if ( nbtcPkgs . length > 0 ) {
@@ -98,23 +114,11 @@ async function poolAndProcessEvents(netCfg: NetworkConfig, storage: D1Storage) {
98114 }
99115}
100116
101- async function runRedeemSolver ( storage : D1Storage , env : Env , activeNetworks : SuiNet [ ] ) {
117+ async function runRedeemSolver ( storage : D1Storage , env : Env , suiClients : Map < SuiNet , SuiClient > ) {
102118 logger . info ( { msg : "Running scheduled redeem solver task..." } ) ;
103- let mnemonic : string ;
104- try {
105- mnemonic = ( await env . NBTC_MINTING_SIGNER_MNEMONIC . get ( ) ) || "" ;
106- } catch ( error ) {
107- logger . error ( { msg : "Failed to retrieve NBTC_MINTING_SIGNER_MNEMONIC" , error } ) ;
108- return ;
109- }
110- if ( ! mnemonic ) {
111- logger . error ( { msg : "Missing NBTC_MINTING_SIGNER_MNEMONIC" } ) ;
112- return ;
113- }
114- const clients = await createSuiClients ( activeNetworks , mnemonic ) ;
115119 const service = new RedeemService (
116120 storage ,
117- clients ,
121+ suiClients ,
118122 env . BtcIndexer as unknown as Service < BtcIndexerRpc & WorkerEntrypoint > ,
119123 env . UTXO_LOCK_TIME ,
120124 env . REDEEM_DURATION_MS ,
0 commit comments