Skip to content

Commit

Permalink
Remove duplicates when merging balances (#4413)
Browse files Browse the repository at this point in the history
* Remove duplicates when merging balances

* Update src/hooks/loadables/useLoadBalances.ts

Co-authored-by: Manuel Gellfart <[email protected]>

---------

Co-authored-by: Manuel Gellfart <[email protected]>
  • Loading branch information
mmv08 and schmanu authored Oct 24, 2024
1 parent c2192e4 commit 53bab84
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/hooks/loadables/useLoadBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,18 @@ export const useTokenListSetting = (): boolean | undefined => {
return isTrustedTokenList
}

const mergeBalances = (balance1: SafeBalanceResponse, balance2: SafeBalanceResponse): SafeBalanceResponse => {
const mergeBalances = (cgw: SafeBalanceResponse, sn: SafeBalanceResponse): SafeBalanceResponse => {
// Create a Map using token addresses as keys
const uniqueBalances = new Map(
// Process SafeNet items last so they take precedence by overwriting the CGW items
[...cgw.items, ...sn.items].map((item) => [item.tokenInfo.address, item]),
)

return {
fiatTotal: balance1.fiatTotal + balance2.fiatTotal,
items: [...balance1.items, ...balance2.items],
// We do not sum the fiatTotal as SafeNet doesn't return it
// And if it did, we would have to do something fancy with calculations so balances aren't double counted
fiatTotal: cgw.fiatTotal,
items: Array.from(uniqueBalances.values()),
}
}

Expand Down

0 comments on commit 53bab84

Please sign in to comment.