From f0865940cbc8a1b7649d59196f4f7e39f0d954af Mon Sep 17 00:00:00 2001 From: Sam Holmes Date: Wed, 11 Sep 2024 16:28:37 -0700 Subject: [PATCH] Merge duplicate token transaction data from evmscan --- CHANGELOG.md | 2 ++ .../networkAdapters/EvmScanAdapter.ts | 26 +++++++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f64780d9a..be4074b17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- fixed: (EVM) Merge duplicate token transaction data from evmscan fixing incorrect transaction native amounts + ## 4.24.1 (2024-09-10) - fixed: (ADA) Correctly parse txid after broadcasting a transaction diff --git a/src/ethereum/networkAdapters/EvmScanAdapter.ts b/src/ethereum/networkAdapters/EvmScanAdapter.ts index d5c7d642e..7351c496d 100644 --- a/src/ethereum/networkAdapters/EvmScanAdapter.ts +++ b/src/ethereum/networkAdapters/EvmScanAdapter.ts @@ -287,7 +287,7 @@ export class EvmScanAdapter extends NetworkAdapter { const address = this.ethEngine.walletLocalData.publicKey let page = 1 - const allTransactions: EdgeTransaction[] = [] + let allTransactions: EdgeTransaction[] = [] let server: string | undefined while (true) { const offset = NUM_TRANSACTIONS_TO_QUERY @@ -339,7 +339,7 @@ export class EvmScanAdapter extends NetworkAdapter { cleanedTx, l1RollupFee ) - allTransactions.push(tx) + allTransactions = mergeEdgeTransactions([...allTransactions, tx]) } catch (e: any) { this.ethEngine.error( `getAllTxsEthscan ${cleanerFunc.name}\n${safeErrorMessage( @@ -533,7 +533,11 @@ export function mergeEdgeTransactions( } // Parent network is expected to always match for token transactions: - if (existingTransaction.parentNetworkFee !== transaction.parentNetworkFee) { + if ( + existingTransaction.parentNetworkFee != null && + transaction.parentNetworkFee != null && + existingTransaction.parentNetworkFee !== transaction.parentNetworkFee + ) { throw new Error( `Failed to merge transaction '${uniqueKey}': Mismatch parentNetworkFee` ) @@ -547,12 +551,19 @@ export function mergeEdgeTransactions( transaction.networkFee ) + // We can safely assume that the parentNetworkFees for each transaction + // are either the same or one or both are undefined. So, we can take the + // first non-undefined parentNetworkFee to get the merged parentNetworkFee: + const mergedParentNetworkFee = + existingTransaction.parentNetworkFee ?? transaction.parentNetworkFee + // Update the existing transaction: const nativeAmount = add( existingTransaction.nativeAmount, transaction.nativeAmount ) - txidToTransaction.set(uniqueKey, { + + const mergedTx = { ...existingTransaction, isSend: nativeAmount.startsWith('-'), nativeAmount, @@ -561,7 +572,12 @@ export function mergeEdgeTransactions( ...existingTransaction.ourReceiveAddresses, ...transaction.ourReceiveAddresses ] - }) + } + if (mergedParentNetworkFee != null) { + mergedTx.parentNetworkFee = mergedParentNetworkFee + } + + txidToTransaction.set(uniqueKey, mergedTx) } return Array.from(txidToTransaction.values())