@@ -152,7 +152,12 @@ class WalletPayGeneric extends WalletPay {
152152 }
153153
154154 _listenToEvents ( ) {
155- this . provider . on ( 'subscribeAccount' , async ( res ) => {
155+ this . provider . on ( 'subscribeAccount' , async ( err , res ) => {
156+ if ( err ) {
157+ console . trace ( err )
158+ return
159+ }
160+
156161 if ( res . token ) {
157162 this . _eachToken ( async ( token ) => {
158163 if ( token . tokenContract . toLowerCase ( ) !== res ?. token . toLowerCase ( ) ) return
@@ -212,7 +217,7 @@ class WalletPayGeneric extends WalletPay {
212217 /**
213218 * Get balance of entire wallet or 1 address
214219 * @param {object } opts options
215- * @param {string } opts.token token name, for getting balance of token
220+ * @param {string= } opts.token token name, for getting balance of token
216221 * @param {string } addr Pass address to get balance of specific address
217222 * @returns {Promise<Balance> }
218223 */
@@ -246,7 +251,12 @@ class WalletPayGeneric extends WalletPay {
246251 return state
247252 }
248253
249- async _syncAddressPath ( addr , signal ) {
254+ /**
255+ * @param {string } addr
256+ * @param {any } signal
257+ * @param {StateDb= } stateInstance
258+ */
259+ async _syncAddressPath ( addr , signal , stateInstance = undefined ) {
250260 const provider = this . provider
251261 const path = addr . path
252262 const txs = await provider . getTransactionsByAddress ( { address : addr . address } )
@@ -257,34 +267,42 @@ class WalletPayGeneric extends WalletPay {
257267
258268 this . _hdWallet . addAddress ( addr )
259269 for ( const t of txs ) {
260- await this . _storeTx ( t )
270+ await this . _storeTx ( t , stateInstance )
261271 }
262- await this . _setAddrBalance ( addr . address )
272+ await this . _setAddrBalance ( addr . address , stateInstance )
273+
274+ const txIndex = await stateInstance . getTxIndex ( )
275+ console . log ( 'txIndex' , txIndex )
276+
263277 return txs . length > 0 ? signal . hasTx : signal . noTx
264278 }
265279
266- async _setAddrBalance ( addr ) {
267- const balances = await this . state . getBalances ( )
280+ async _setAddrBalance ( addr , stateInstance = undefined ) {
281+ const state = stateInstance ?? this . state
282+
283+ const balances = await state . getBalances ( )
268284 const bal = await this . getBalance ( { } , addr )
269285 await balances . setBal ( addr , bal . confirmed )
270286 }
271287
272- async _storeTx ( tx ) {
288+ async _storeTx ( tx , stateInstance = undefined ) {
289+ const state = stateInstance ?? this . state
290+
273291 const data = {
274292 from : tx . from ,
275293 to : tx . to ,
276294 value : new this . _Curr ( tx . value , 'base' ) ,
277295 height : tx . blockNumber ,
278296 txid : tx . hash
279297 }
280- await this . state . storeTxHistory ( data )
298+ await state . storeTxHistory ( data )
281299 return data
282300 }
283301
284302 /**
285303 * Crawl HD wallet path, collect transactions and calculate balance of all addresses.
286304 * @param {object } opts
287- * @param {bool = } opts.reset Reset all state and resync
305+ * @param {boolean = } opts.reset Reset all state and resync
288306 * @param {string= } opts.token Token name
289307 * @fires sync-path when a hd path is synced
290308 * @fires sync-end when entire HD path has been traversed, or when syncing is halted
@@ -309,7 +327,7 @@ class WalletPayGeneric extends WalletPay {
309327 return this . callToken ( 'syncPath' , opts . token , [ addr , signal ] )
310328 }
311329
312- const res = await this . _syncAddressPath ( addr , signal )
330+ const res = await this . _syncAddressPath ( addr , signal , state )
313331 this . emit ( 'synced-path' , syncState . _addrType , syncState . path , res === signal . hasTx , syncState . toJSON ( ) )
314332 return res
315333 } )
0 commit comments