-
Notifications
You must be signed in to change notification settings - Fork 111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(combinedBalances): Optimize balance diff calculations #5082
Merged
anxolin
merged 5 commits into
cowprotocol:develop
from
bleu:pedro/fix-hook-widget-optimization
Nov 14, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ffe08ff
chore: create state for balance combined and optimize applyBalanceDif…
yvesfracari c227ae6
fix: avoid negative user balance
yvesfracari 75a381d
fix: remove refetch tendernly simulation on mount
yvesfracari 37bf526
fix: build error
yvesfracari 824b94c
feat: add simulation link on order review
yvesfracari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you know why this is being re-computed still every few seconds?
Maybe this can help to figure it out https://github.com/simbathesailor/use-what-changed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC is because the
tokenBalances
state is updated every few seconds.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then our fault, we will need to look at this tomorrow, cause my balance were not changing, so probably is not correctly memorized and its likely there's a lot more parts of the app that gets redrawn because of this