From b62fa6b1f052e29160b9762c87a52a07e98383e4 Mon Sep 17 00:00:00 2001 From: Sam Holmes Date: Fri, 28 Mar 2025 09:52:19 -0700 Subject: [PATCH 1/2] Move Monero specific tx update logic into `updateTransaction` The purpose of this is to avoid the foot-gun: calling `addTransaction` when the transaction exists in the database will result in `updateTransaction` being invoked without the Monero specific tx update logic. --- src/MoneroEngine.ts | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/MoneroEngine.ts b/src/MoneroEngine.ts index 0466050..eac67a0 100644 --- a/src/MoneroEngine.ts +++ b/src/MoneroEngine.ts @@ -267,7 +267,7 @@ export class MoneroEngine implements EdgeCurrencyEngine { }) } - let edgeTransaction: EdgeTransaction = { + const edgeTransaction: EdgeTransaction = { blockHeight, currencyCode: 'XMR', date, @@ -294,22 +294,9 @@ export class MoneroEngine implements EdgeCurrencyEngine { this.edgeTxLibCallbacks.onTransactions(this.transactionEventArray) this.transactionEventArray = [] } else { - // Already have this tx in the database. See if anything changed - const transactionsArray = this.getTxs(PRIMARY_CURRENCY_TOKEN_ID) - const edgeTx = transactionsArray[idx] - - if (edgeTx.blockHeight !== edgeTransaction.blockHeight) { - // The native amounts returned from the API take some time before they're accurate. We can trust the amounts we saved instead. - edgeTransaction = { - ...edgeTransaction, - nativeAmount: edgeTx.nativeAmount - } - - this.log(`Update transaction: ${tx.hash} height:${tx.height}`) - this.updateTransaction(PRIMARY_CURRENCY_TOKEN_ID, edgeTransaction, idx) - this.edgeTxLibCallbacks.onTransactions(this.transactionEventArray) - this.transactionEventArray = [] - } + this.updateTransaction(PRIMARY_CURRENCY_TOKEN_ID, edgeTransaction, idx) + this.edgeTxLibCallbacks.onTransactions(this.transactionEventArray) + this.transactionEventArray = [] } return blockHeight @@ -417,8 +404,23 @@ export class MoneroEngine implements EdgeCurrencyEngine { edgeTransaction: EdgeTransaction, idx: number ): void { - // Update the transaction + // Already have this tx in the database. See if anything changed const txs = this.getTxs(tokenId) + const edgeTx = txs[idx] + + // Already have this tx in the database. Consider a change if blockHeight changed + if (edgeTx.blockHeight === edgeTransaction.blockHeight) return + this.log( + `Update transaction: ${edgeTransaction.txid} height:${edgeTransaction.blockHeight}` + ) + // The native amounts returned from the API take some time before they're + // accurate. We can trust the amounts we saved instead. + edgeTransaction = { + ...edgeTransaction, + nativeAmount: edgeTx.nativeAmount + } + + // Update the transaction txs[idx] = edgeTransaction this.walletLocalDataDirty = true this.transactionEventArray.push({ From 683fab9f2a3f7615d16bf25eddc2273009b7032d Mon Sep 17 00:00:00 2001 From: Sam Holmes Date: Fri, 28 Mar 2025 09:48:21 -0700 Subject: [PATCH 2/2] Combine add/update transaction functions --- src/MoneroEngine.ts | 83 +++++++++++++++++++-------------------------- 1 file changed, 35 insertions(+), 48 deletions(-) diff --git a/src/MoneroEngine.ts b/src/MoneroEngine.ts index eac67a0..fd3385b 100644 --- a/src/MoneroEngine.ts +++ b/src/MoneroEngine.ts @@ -284,20 +284,9 @@ export class MoneroEngine implements EdgeCurrencyEngine { walletId: this.walletId } - const idx = this.findTransaction(PRIMARY_CURRENCY_TOKEN_ID, tx.hash) - if (idx === -1) { - this.log(`New transaction: ${tx.hash}`) - - // New transaction not in database - this.addTransaction(PRIMARY_CURRENCY_TOKEN_ID, edgeTransaction) - - this.edgeTxLibCallbacks.onTransactions(this.transactionEventArray) - this.transactionEventArray = [] - } else { - this.updateTransaction(PRIMARY_CURRENCY_TOKEN_ID, edgeTransaction, idx) - this.edgeTxLibCallbacks.onTransactions(this.transactionEventArray) - this.transactionEventArray = [] - } + this.saveTransactionState(PRIMARY_CURRENCY_TOKEN_ID, edgeTransaction) + this.edgeTxLibCallbacks.onTransactions(this.transactionEventArray) + this.transactionEventArray = [] return blockHeight } @@ -366,11 +355,15 @@ export class MoneroEngine implements EdgeCurrencyEngine { return txs as EdgeTransaction[] } - addTransaction(tokenId: EdgeTokenId, edgeTransaction: EdgeTransaction): void { + saveTransactionState( + tokenId: EdgeTokenId, + edgeTransaction: EdgeTransaction + ): void { // Add or update tx in transactionsObj const idx = this.findTransaction(tokenId, edgeTransaction.txid) if (idx === -1) { + this.log(`New transaction: ${edgeTransaction.txid}`) this.log.warn( 'addTransaction: adding and sorting:' + edgeTransaction.txid + @@ -395,41 +388,35 @@ export class MoneroEngine implements EdgeCurrencyEngine { transaction: edgeTransaction }) } else { - this.updateTransaction(tokenId, edgeTransaction, idx) - } - } + const txs = this.getTxs(tokenId) + const edgeTx = txs[idx] - updateTransaction( - tokenId: EdgeTokenId, - edgeTransaction: EdgeTransaction, - idx: number - ): void { - // Already have this tx in the database. See if anything changed - const txs = this.getTxs(tokenId) - const edgeTx = txs[idx] + // Already have this tx in the database. Consider a change if blockHeight changed + if (edgeTx.blockHeight === edgeTransaction.blockHeight) return + this.log( + `Update transaction: ${edgeTransaction.txid} height:${edgeTransaction.blockHeight}` + ) - // Already have this tx in the database. Consider a change if blockHeight changed - if (edgeTx.blockHeight === edgeTransaction.blockHeight) return - this.log( - `Update transaction: ${edgeTransaction.txid} height:${edgeTransaction.blockHeight}` - ) - // The native amounts returned from the API take some time before they're - // accurate. We can trust the amounts we saved instead. - edgeTransaction = { - ...edgeTransaction, - nativeAmount: edgeTx.nativeAmount - } + // The native amounts returned from the API take some time before they're + // accurate. We can trust the amounts we saved instead. + edgeTransaction = { + ...edgeTransaction, + nativeAmount: edgeTx.nativeAmount + } - // Update the transaction - txs[idx] = edgeTransaction - this.walletLocalDataDirty = true - this.transactionEventArray.push({ - isNew: false, - transaction: edgeTransaction - }) - this.log.warn( - 'updateTransaction' + edgeTransaction.txid + edgeTransaction.nativeAmount - ) + // Update the transaction + txs[idx] = edgeTransaction + this.walletLocalDataDirty = true + this.transactionEventArray.push({ + isNew: false, + transaction: edgeTransaction + }) + this.log.warn( + 'updateTransaction' + + edgeTransaction.txid + + edgeTransaction.nativeAmount + ) + } } // ************************************* @@ -748,7 +735,7 @@ export class MoneroEngine implements EdgeCurrencyEngine { } async saveTx(edgeTransaction: EdgeTransaction): Promise { - await this.addTransaction(edgeTransaction.tokenId, edgeTransaction) + await this.saveTransactionState(edgeTransaction.tokenId, edgeTransaction) } getDisplayPrivateSeed(privateKeys: JsonObject): string {