fix: preserve multiple token transfers per tx hash in transaction history
All checks were successful
check / check (push) Successful in 22s
All checks were successful
check / check (push) Successful in 22s
When a swap produces multiple ERC-20 token transfers with the same tx hash (e.g. send WETH + receive USDC), only the last transfer was kept because txsByHash was keyed by bare tx hash. This caused the address-token view to show no transactions for tokens obtained via swap. Use a composite key (hash:contractAddress) so all token transfers are preserved. The bare-hash normal-tx entry is removed when token transfers replace it to avoid duplication. Closes #72
This commit is contained in:
@@ -153,9 +153,14 @@ async function fetchRecentTransactions(address, blockscoutUrl, count = 25) {
|
|||||||
|
|
||||||
// When a token transfer shares a hash with a normal tx, the normal tx
|
// When a token transfer shares a hash with a normal tx, the normal tx
|
||||||
// is the contract call (0 ETH) and the token transfer has the real
|
// is the contract call (0 ETH) and the token transfer has the real
|
||||||
// amount and symbol. Replace the normal tx with the token transfer,
|
// amount and symbol. Preserve contract call metadata (direction, label,
|
||||||
// but preserve contract call metadata (direction, label, method) so
|
// method) so swaps and other contract interactions display correctly.
|
||||||
// swaps and other contract interactions display correctly.
|
//
|
||||||
|
// A single tx hash can produce multiple token transfers (e.g. a swap
|
||||||
|
// sends token A and receives token B). Use a composite key
|
||||||
|
// (hash:contractAddress) so every transfer is preserved. The original
|
||||||
|
// normal-tx entry (keyed by bare hash) is removed when at least one
|
||||||
|
// token transfer replaces it.
|
||||||
for (const tt of ttJson.items || []) {
|
for (const tt of ttJson.items || []) {
|
||||||
const parsed = parseTokenTransfer(tt, addrLower);
|
const parsed = parseTokenTransfer(tt, addrLower);
|
||||||
const existing = txsByHash.get(parsed.hash);
|
const existing = txsByHash.get(parsed.hash);
|
||||||
@@ -164,8 +169,12 @@ async function fetchRecentTransactions(address, blockscoutUrl, count = 25) {
|
|||||||
parsed.directionLabel = existing.directionLabel;
|
parsed.directionLabel = existing.directionLabel;
|
||||||
parsed.isContractCall = true;
|
parsed.isContractCall = true;
|
||||||
parsed.method = existing.method;
|
parsed.method = existing.method;
|
||||||
|
// Remove the bare-hash normal tx so it isn't duplicated
|
||||||
|
txsByHash.delete(parsed.hash);
|
||||||
}
|
}
|
||||||
txsByHash.set(parsed.hash, parsed);
|
// Use composite key so multiple token transfers per tx are kept
|
||||||
|
const compositeKey = parsed.hash + ":" + (parsed.contractAddress || "");
|
||||||
|
txsByHash.set(compositeKey, parsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
const txs = [...txsByHash.values()];
|
const txs = [...txsByHash.values()];
|
||||||
|
|||||||
Reference in New Issue
Block a user