-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(combinedBalances): Optimize balance diff calculations (#5082)
* chore: create state for balance combined and optimize applyBalanceDiffs function * fix: avoid negative user balance * fix: remove refetch tendernly simulation on mount * fix: build error * feat: add simulation link on order review
- Loading branch information
1 parent
20f543a
commit 38aae71
Showing
10 changed files
with
118 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 3 additions & 41 deletions
44
apps/cowswap-frontend/src/modules/combinedBalances/hooks/useTokensBalancesCombined.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,7 @@ | ||
import { useMemo } from 'react' | ||
import { useAtomValue } from 'jotai' | ||
|
||
import { BalancesState, useTokensBalances } from '@cowprotocol/balances-and-allowances' | ||
import { useWalletInfo } from '@cowprotocol/wallet' | ||
|
||
import { BigNumber } from 'ethers' | ||
|
||
import { usePreHookBalanceDiff } from 'modules/hooksStore/hooks/useBalancesDiff' | ||
import { useIsHooksTradeType } from 'modules/trade' | ||
import { balancesCombinedAtom } from '../state/balanceCombinedAtom' | ||
|
||
export function useTokensBalancesCombined() { | ||
const { account } = useWalletInfo() | ||
const preHooksBalancesDiff = usePreHookBalanceDiff() | ||
const tokenBalances = useTokensBalances() | ||
const isHooksTradeType = useIsHooksTradeType() | ||
|
||
return useMemo(() => { | ||
if (!account || !isHooksTradeType) return tokenBalances | ||
const accountBalancesDiff = preHooksBalancesDiff[account.toLowerCase()] || {} | ||
return applyBalanceDiffs(tokenBalances, accountBalancesDiff) | ||
}, [account, preHooksBalancesDiff, tokenBalances, isHooksTradeType]) | ||
} | ||
|
||
function applyBalanceDiffs(currentBalances: BalancesState, balanceDiff: Record<string, string>): BalancesState { | ||
// Get all unique addresses from both objects | ||
const allAddresses = [...new Set([...Object.keys(currentBalances.values), ...Object.keys(balanceDiff)])] | ||
|
||
const normalizedValues = allAddresses.reduce( | ||
(acc, address) => { | ||
const currentBalance = currentBalances.values[address] || BigNumber.from(0) | ||
const diff = balanceDiff[address] ? BigNumber.from(balanceDiff[address]) : BigNumber.from(0) | ||
|
||
return { | ||
...acc, | ||
[address]: currentBalance.add(diff), | ||
} | ||
}, | ||
{} as Record<string, BigNumber>, | ||
) | ||
|
||
return { | ||
isLoading: currentBalances.isLoading, | ||
values: normalizedValues, | ||
} | ||
return useAtomValue(balancesCombinedAtom) | ||
} |
5 changes: 5 additions & 0 deletions
5
apps/cowswap-frontend/src/modules/combinedBalances/state/balanceCombinedAtom.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { atomWithReset } from 'jotai/utils' | ||
|
||
import { BalancesState } from '@cowprotocol/balances-and-allowances' | ||
|
||
export const balancesCombinedAtom = atomWithReset<BalancesState>({ isLoading: false, values: {} }) |
55 changes: 55 additions & 0 deletions
55
apps/cowswap-frontend/src/modules/combinedBalances/updater/BalancesCombinedUpdater.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { useEffect } from 'react' | ||
|
||
import { BalancesState, useTokensBalances } from '@cowprotocol/balances-and-allowances' | ||
import { useWalletInfo } from '@cowprotocol/wallet' | ||
|
||
import { BigNumber } from 'ethers' | ||
|
||
import { useHooks } from 'modules/hooksStore' | ||
import { usePreHookBalanceDiff } from 'modules/hooksStore/hooks/useBalancesDiff' | ||
import { useIsHooksTradeType } from 'modules/trade' | ||
import { useSetAtom } from 'jotai' | ||
import { balancesCombinedAtom } from '../state/balanceCombinedAtom' | ||
|
||
export function BalancesCombinedUpdater() { | ||
const { account } = useWalletInfo() | ||
const setBalancesCombined = useSetAtom(balancesCombinedAtom) | ||
const preHooksBalancesDiff = usePreHookBalanceDiff() | ||
const { preHooks } = useHooks() | ||
const tokenBalances = useTokensBalances() | ||
const isHooksTradeType = useIsHooksTradeType() | ||
|
||
useEffect(() => { | ||
if (!account || !isHooksTradeType || !preHooks.length) { | ||
setBalancesCombined(tokenBalances) | ||
return | ||
} | ||
const accountBalancesDiff = preHooksBalancesDiff[account.toLowerCase()] || {} | ||
setBalancesCombined(applyBalanceDiffs(tokenBalances, accountBalancesDiff)) | ||
}, [account, preHooksBalancesDiff, isHooksTradeType, tokenBalances]) | ||
|
||
return null | ||
} | ||
|
||
function applyBalanceDiffs(currentBalances: BalancesState, balanceDiff: Record<string, string>): BalancesState { | ||
const normalizedValues = { ...currentBalances.values } | ||
|
||
// Only process addresses that have balance differences | ||
// This optimizes since the balances diff object is usually smaller than the balances object | ||
Object.entries(balanceDiff).forEach(([address, diff]) => { | ||
const currentBalance = normalizedValues[address] | ||
if (currentBalance === undefined) return | ||
const balanceWithDiff = currentBalance.add(BigNumber.from(diff)) | ||
|
||
// If the balance with diff is negative, set the balance to 0 | ||
// This avoid the UI crashing in case of some error | ||
normalizedValues[address] = balanceWithDiff.isNegative() | ||
? BigNumber.from(0) | ||
: currentBalance.add(BigNumber.from(diff)) | ||
}) | ||
|
||
return { | ||
isLoading: currentBalances.isLoading, | ||
values: normalizedValues, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
apps/cowswap-frontend/src/modules/tenderly/hooks/useSimulationData.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { useMemo } from 'react' | ||
|
||
import { useTenderlyBundleSimulation } from './useTenderlyBundleSimulation' | ||
|
||
export function useSimulationData(hookUuid?: string) { | ||
const { data } = useTenderlyBundleSimulation() | ||
|
||
return useMemo(() => { | ||
if (!data || !hookUuid) return | ||
return data[hookUuid] | ||
}, [data, hookUuid]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters