Skip to content

Commit

Permalink
Merge pull request #832 from EdgeApp/sam/fix-token-amount-merging
Browse files Browse the repository at this point in the history
Merge duplicate token transaction data from evmscan
  • Loading branch information
samholmes authored Sep 12, 2024
2 parents 10b2c0b + f086594 commit 85f4c54
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 21 additions & 5 deletions src/ethereum/networkAdapters/EvmScanAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ export class EvmScanAdapter extends NetworkAdapter<EvmScanAdapterConfig> {
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
Expand Down Expand Up @@ -339,7 +339,7 @@ export class EvmScanAdapter extends NetworkAdapter<EvmScanAdapterConfig> {
cleanedTx,
l1RollupFee
)
allTransactions.push(tx)
allTransactions = mergeEdgeTransactions([...allTransactions, tx])
} catch (e: any) {
this.ethEngine.error(
`getAllTxsEthscan ${cleanerFunc.name}\n${safeErrorMessage(
Expand Down Expand Up @@ -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`
)
Expand All @@ -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,
Expand All @@ -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())
Expand Down

0 comments on commit 85f4c54

Please sign in to comment.