diff --git a/src/MoneroEngine.ts b/src/MoneroEngine.ts index 6027316..ca835e9 100644 --- a/src/MoneroEngine.ts +++ b/src/MoneroEngine.ts @@ -66,6 +66,9 @@ const SAVE_DATASTORE_MILLISECONDS = 10000 const PRIMARY_CURRENCY_TOKEN_ID = null +// Global throttle: max 1 onAddressesChecked per 500ms; ratio=1 always passes. +let lastSyncEmitTime = 0 + export class MoneroEngine implements EdgeCurrencyEngine { apiKey: string walletInfo: SafeWalletInfo @@ -169,6 +172,9 @@ export class MoneroEngine implements EdgeCurrencyEngine { return } if (numTx !== totalTxs) { + const now = Date.now() + if (now - lastSyncEmitTime < 500) return + lastSyncEmitTime = now const progress = numTx / totalTxs this.edgeTxLibCallbacks.onAddressesChecked(progress) } else { @@ -304,8 +310,10 @@ export class MoneroEngine implements EdgeCurrencyEngine { } this.saveTransactionState(PRIMARY_CURRENCY_TOKEN_ID, edgeTransaction) - this.edgeTxLibCallbacks.onTransactions(this.transactionEventArray) - this.transactionEventArray = [] + if (this.transactionEventArray.length > 0) { + this.edgeTxLibCallbacks.onTransactions(this.transactionEventArray) + this.transactionEventArray = [] + } return blockHeight } @@ -343,11 +351,13 @@ export class MoneroEngine implements EdgeCurrencyEngine { } this.updateOnAddressesChecked(transactions.length, transactions.length) - // Update the seenTxCheckpoint state: - this.seenTxCheckpoint = seenTxCheckpoint - this.edgeTxLibCallbacks.onSeenTxCheckpoint( - wasSeenTxCheckpoint(this.seenTxCheckpoint) - ) + // Only update the seenTxCheckpoint if it actually advanced: + const newCheckpoint = wasSeenTxCheckpoint(seenTxCheckpoint) + const oldCheckpoint = wasSeenTxCheckpoint(this.seenTxCheckpoint) + if (newCheckpoint !== oldCheckpoint) { + this.seenTxCheckpoint = seenTxCheckpoint + this.edgeTxLibCallbacks.onSeenTxCheckpoint(newCheckpoint) + } } catch (e) { this.log.error('checkTransactionsInnerLoop', e) } @@ -457,10 +467,8 @@ export class MoneroEngine implements EdgeCurrencyEngine { doInitialCallbacks(): void { for (const tokenId of this.walletLocalData.enabledTokens) { try { - this.edgeTxLibCallbacks.onTokenBalanceChanged( - tokenId, - this.walletLocalData.totalBalances.get(tokenId) ?? '0' - ) + const _bal = this.walletLocalData.totalBalances.get(tokenId) ?? '0' + this.edgeTxLibCallbacks.onTokenBalanceChanged(tokenId, _bal) } catch (e) { this.log.error('Error for currencyCode', tokenId, e) } diff --git a/test/engine/engine.ts b/test/engine/engine.ts index 922ec8f..3bd683c 100644 --- a/test/engine/engine.ts +++ b/test/engine/engine.ts @@ -73,7 +73,6 @@ for (const fixture of fixtures) { }, onUnactivatedTokenIdsChanged() {}, onStakingStatusChanged() {}, - onSubscribeAddresses() {}, onWcNewContractCall(payload) { emitter.emit('wcNewContractCall', payload) },