@@ -20,6 +20,13 @@ import {
2020import { getSeedPhraseDerivationPath } from '../accounts/account-cryptography' ;
2121import { useAccounts , useAccountsCache } from '../accounts/account-hooks' ;
2222import { getDefaultUnit } from './currencies' ;
23+ import { getFeatureFlag } from './feature-flags' ;
24+
25+ export function sparkDebugLog ( message : string , data ?: Record < string , unknown > ) {
26+ if ( getFeatureFlag ( 'DEBUG_LOGGING_SPARK' ) ) {
27+ console . debug ( `[Spark] ${ message } ` , data ?? '' ) ;
28+ }
29+ }
2330
2431const seedDerivationPath = getSeedPhraseDerivationPath ( 'spark' , 12 ) ;
2532
@@ -134,15 +141,26 @@ export function useTrackAndUpdateSparkAccountBalances() {
134141 }
135142
136143 if ( ! account . isOnline ) {
144+ sparkDebugLog ( 'Skipping balance poll — account offline' , {
145+ accountId : account . id ,
146+ } ) ;
137147 return null ;
138148 }
139149
150+ sparkDebugLog ( 'Polling balance' , { accountId : account . id } ) ;
151+
140152 const { satsBalance } = await measureOperation (
141153 'SparkWallet.getBalance' ,
142154 ( ) => account . wallet . getBalance ( ) ,
143155 { accountId : account . id } ,
144156 ) ;
145157
158+ sparkDebugLog ( 'Balance fetched from Spark SDK' , {
159+ accountId : account . id ,
160+ owned : String ( satsBalance . owned ) ,
161+ available : String ( satsBalance . available ) ,
162+ } ) ;
163+
146164 // WORKAROUND: Spark SDK sometimes returns 0 for balance incorrectly.
147165 // The bug seems to be resolved after the wallet is reinitialized.
148166 // Reinitialize the wallet and re-check balance.
@@ -210,19 +228,32 @@ export function useTrackAndUpdateSparkAccountBalances() {
210228 }
211229 // END WORKAROUND
212230
231+ const newOwnedBalance = new Money ( {
232+ amount : Number ( effectiveOwnedBalance ) ,
233+ currency : account . currency as Currency ,
234+ unit : getDefaultUnit ( account . currency ) ,
235+ } ) ;
236+ const newAvailableBalance = new Money ( {
237+ amount : Number ( effectiveAvailableBalance ) ,
238+ currency : account . currency as Currency ,
239+ unit : getDefaultUnit ( account . currency ) ,
240+ } ) ;
241+
242+ sparkDebugLog ( 'Updating accounts cache' , {
243+ accountId : account . id ,
244+ prevOwned : account . ownedBalance ?. toString ( ) ?? 'null' ,
245+ newOwned : newOwnedBalance . toString ( ) ,
246+ prevAvailable : account . availableBalance ?. toString ( ) ?? 'null' ,
247+ newAvailable : newAvailableBalance . toString ( ) ,
248+ walletChanged : String ( effectiveWallet !== account . wallet ) ,
249+ accountVersion : String ( account . version ) ,
250+ } ) ;
251+
213252 accountCache . updateSparkAccountIfBalanceOrWalletChanged ( {
214253 ...account ,
215254 wallet : effectiveWallet ,
216- ownedBalance : new Money ( {
217- amount : Number ( effectiveOwnedBalance ) ,
218- currency : account . currency as Currency ,
219- unit : getDefaultUnit ( account . currency ) ,
220- } ) ,
221- availableBalance : new Money ( {
222- amount : Number ( effectiveAvailableBalance ) ,
223- currency : account . currency as Currency ,
224- unit : getDefaultUnit ( account . currency ) ,
225- } ) ,
255+ ownedBalance : newOwnedBalance ,
256+ availableBalance : newAvailableBalance ,
226257 } ) ;
227258
228259 return effectiveOwnedBalance ;
0 commit comments