-
Notifications
You must be signed in to change notification settings - Fork 176
fix(bridge): hide stale price impact while a new quote is loading #7711
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
base: develop
Are you sure you want to change the base?
Changes from 3 commits
4294834
bad39f2
d790ffd
29f16f8
f706d5e
c25c58b
6354b1a
cd9dd32
601890b
accd586
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ import { Fraction, Percent } from '@cowprotocol/currency' | |
| import ms from 'ms.macro' | ||
|
|
||
| import { useDerivedTradeState } from 'modules/trade' | ||
| import { useTradeQuote } from 'modules/tradeQuote' | ||
| import { useTradeUsdAmounts } from 'modules/usdAmount' | ||
|
|
||
| import { useSafeMemo } from 'common/hooks/useSafeMemo' | ||
|
|
@@ -31,7 +32,12 @@ export function useFiatValuePriceImpact(): { priceImpact: Percent | undefined; i | |
| outputAmount: { value: fiatValueOutput, isLoading: outputIsLoading }, | ||
| } = useTradeUsdAmounts(inputCurrencyAmount, outputCurrencyAmount, inputToken, outputToken) | ||
|
|
||
| const isLoading = inputIsLoading || outputIsLoading | ||
| const { isLoading: isQuoteLoading, hasParamsChanged: quoteParamsChanged } = useTradeQuote() | ||
|
|
||
| // Trade-quote signals indicate the current output amount is stale (token just changed | ||
| // or a fresh quote is in flight). Compute price impact only once the quote catches up, | ||
| // otherwise we'd display a huge nonsense % derived from mismatched in/out amounts. | ||
| const isLoading = inputIsLoading || outputIsLoading || isQuoteLoading || quoteParamsChanged | ||
| const [hasLoadingTimedOut, setHasLoadingTimedOut] = useState(false) | ||
|
|
||
| useEffect(() => { | ||
|
|
@@ -51,12 +57,20 @@ export function useFiatValuePriceImpact(): { priceImpact: Percent | undefined; i | |
| // Don't calculate price impact if trade is not set up (both trade assets are not set) | ||
| if (!isTradeSetUp) return null | ||
|
|
||
| const stillLoading = isLoading && !hasLoadingTimedOut | ||
|
|
||
| // While a fresh quote is loading, don't expose the stale value at all — consumers | ||
| // hide the percentage when `priceImpact` is undefined, leaving just the spinner. | ||
| if (stillLoading) { | ||
| return { priceImpact: undefined, isLoading: true } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This never happens because after the first quote is being loaded, the second quote (the fresh quote as you say) has So this code and comment are incorrect |
||
| } | ||
|
|
||
| const priceImpact = computeFiatValuePriceImpact( | ||
| fiatValueInput ? FractionUtils.fractionLikeToFraction(fiatValueInput) : null, | ||
| fiatValueOutput ? FractionUtils.fractionLikeToFraction(fiatValueOutput) : null, | ||
| ) | ||
|
|
||
| return { priceImpact, isLoading: isLoading && !hasLoadingTimedOut } | ||
| return { priceImpact, isLoading: false } | ||
| }, [isTradeSetUp, fiatValueInput, fiatValueOutput, isLoading, hasLoadingTimedOut]) | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.