-
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
feat(trade): cache quotes #5473
Changes from all commits
0774e8f
e3a748d
c7bf8e0
cc15319
73eb428
dd09f9e
dc28060
742d093
d70862d
1815a2d
9c99e27
20cd5b6
ba35dbb
f26a1af
925478b
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 |
---|---|---|
|
@@ -7,7 +7,6 @@ import { Field } from 'legacy/state/types' | |
|
||
import { useNavigateOnCurrencySelection, useSwitchTokensPlaces, useUpdateCurrencyAmount } from 'modules/trade' | ||
import { createDebouncedTradeAmountAnalytics } from 'modules/trade/utils/analytics' | ||
import { useResetTradeQuote } from 'modules/tradeQuote' | ||
|
||
import { useAdvancedOrdersDerivedState } from './useAdvancedOrdersDerivedState' | ||
import { useUpdateAdvancedOrdersRawState } from './useAdvancedOrdersRawState' | ||
|
@@ -21,25 +20,16 @@ export function useAdvancedOrdersActions() { | |
|
||
const naviageOnCurrencySelection = useNavigateOnCurrencySelection() | ||
const updateCurrencyAmount = useUpdateCurrencyAmount() | ||
const resetTradeQuote = useResetTradeQuote() | ||
const cowAnalytics = useCowAnalytics() | ||
const debouncedTradeAmountAnalytics = useMemo(() => createDebouncedTradeAmountAnalytics(cowAnalytics), [cowAnalytics]) | ||
|
||
const updateAdvancedOrdersState = useUpdateAdvancedOrdersRawState() | ||
|
||
const onCurrencySelection = useCallback( | ||
(field: Field, currency: Currency | null) => { | ||
// Reset the output field until we fetch quote for new selected token | ||
// This is to avoid displaying wrong amounts in output field | ||
updateCurrencyAmount({ | ||
amount: { isTyped: false, value: '' }, | ||
field: Field.OUTPUT, | ||
currency, | ||
}) | ||
naviageOnCurrencySelection(field, currency) | ||
resetTradeQuote() | ||
}, | ||
[naviageOnCurrencySelection, updateCurrencyAmount, resetTradeQuote], | ||
[naviageOnCurrencySelection], | ||
) | ||
|
||
const onUserInput = useCallback( | ||
|
@@ -61,12 +51,7 @@ export function useAdvancedOrdersActions() { | |
[updateAdvancedOrdersState], | ||
) | ||
|
||
const onSwitchTokensDefault = useSwitchTokensPlaces(onSwitchTradeOverride) | ||
|
||
const onSwitchTokens = useCallback(() => { | ||
onSwitchTokensDefault() | ||
resetTradeQuote() | ||
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. Also an excessive quote resetting, this is already handled in |
||
}, [resetTradeQuote, onSwitchTokensDefault]) | ||
const onSwitchTokens = useSwitchTokensPlaces(onSwitchTradeOverride) | ||
|
||
return useMemo( | ||
() => ({ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import { ReactNode, useEffect } from 'react' | ||
import { ReactNode } from 'react' | ||
|
||
import { PriorityTokensUpdater } from '@cowprotocol/balances-and-allowances' | ||
import { useWalletInfo } from '@cowprotocol/wallet' | ||
|
||
import { TradeFormValidationUpdater } from 'modules/tradeFormValidation' | ||
import { TradeQuoteState, TradeQuoteUpdater, useUpdateTradeQuote } from 'modules/tradeQuote' | ||
import { TradeQuoteUpdater } from 'modules/tradeQuote' | ||
import { SmartSlippageUpdater } from 'modules/tradeSlippage' | ||
|
||
import { usePriorityTokenAddresses } from '../../hooks/usePriorityTokenAddresses' | ||
|
@@ -19,28 +19,19 @@ interface TradeWidgetUpdatersProps { | |
disableNativeSelling: boolean | ||
enableSmartSlippage?: boolean | ||
children: ReactNode | ||
tradeQuoteStateOverride?: TradeQuoteState | null | ||
onChangeRecipient: (recipient: string | null) => void | ||
} | ||
|
||
export function TradeWidgetUpdaters({ | ||
disableQuotePolling, | ||
disableNativeSelling, | ||
tradeQuoteStateOverride, | ||
enableSmartSlippage, | ||
onChangeRecipient, | ||
children, | ||
}: TradeWidgetUpdatersProps) { | ||
const { chainId, account } = useWalletInfo() | ||
const updateQuoteState = useUpdateTradeQuote() | ||
const priorityTokenAddresses = usePriorityTokenAddresses() | ||
|
||
useEffect(() => { | ||
if (disableQuotePolling && tradeQuoteStateOverride) { | ||
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.
|
||
updateQuoteState(tradeQuoteStateOverride) | ||
} | ||
}, [tradeQuoteStateOverride, disableQuotePolling, updateQuoteState]) | ||
|
||
useResetRecipient(onChangeRecipient) | ||
|
||
return ( | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { useSetAtom } from 'jotai/index' | ||
import { useMemo } from 'react' | ||
|
||
import { OrderQuoteResponse, PriceQuality } from '@cowprotocol/cow-sdk' | ||
|
||
import QuoteApiError, { QuoteApiErrorCodes } from 'api/cowProtocol/errors/QuoteError' | ||
import { FeeQuoteParams } from 'common/types' | ||
|
||
import { useProcessUnsupportedTokenError } from './useProcessUnsupportedTokenError' | ||
|
||
import { updateTradeQuoteAtom } from '../state/tradeQuoteAtom' | ||
import { SellTokenAddress } from '../state/tradeQuoteInputAtom' | ||
|
||
export interface TradeQuoteManager { | ||
setLoading(hasParamsChanged: boolean): void | ||
reset(): void | ||
onError(error: QuoteApiError, requestParams: FeeQuoteParams): void | ||
onResponse(data: OrderQuoteResponse, requestParams: FeeQuoteParams, fetchStartTimestamp: number): void | ||
} | ||
|
||
export function useTradeQuoteManager(sellTokenAddress: SellTokenAddress | undefined): TradeQuoteManager | null { | ||
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. Extracted the |
||
const update = useSetAtom(updateTradeQuoteAtom) | ||
const processUnsupportedTokenError = useProcessUnsupportedTokenError() | ||
|
||
return useMemo( | ||
() => | ||
sellTokenAddress | ||
? { | ||
setLoading(hasParamsChanged: boolean) { | ||
update(sellTokenAddress, { | ||
isLoading: true, | ||
hasParamsChanged, | ||
...(hasParamsChanged ? { response: null } : null), | ||
}) | ||
}, | ||
reset() { | ||
update(sellTokenAddress, { response: null, isLoading: false }) | ||
}, | ||
onError(error: QuoteApiError, requestParams: FeeQuoteParams) { | ||
update(sellTokenAddress, { error, quoteParams: requestParams, isLoading: false, hasParamsChanged: false }) | ||
|
||
if (error.type === QuoteApiErrorCodes.UnsupportedToken) { | ||
processUnsupportedTokenError(error, requestParams) | ||
} | ||
}, | ||
onResponse(data: OrderQuoteResponse, requestParams: FeeQuoteParams, fetchStartTimestamp: number) { | ||
const isOptimalQuote = requestParams.priceQuality === PriceQuality.OPTIMAL | ||
|
||
update(sellTokenAddress, { | ||
response: data, | ||
quoteParams: requestParams, | ||
...(isOptimalQuote ? { isLoading: false } : null), | ||
error: null, | ||
hasParamsChanged: false, | ||
fetchStartTimestamp, | ||
}) | ||
}, | ||
} | ||
: null, | ||
[update, processUnsupportedTokenError, sellTokenAddress], | ||
) | ||
} |
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.
This was an execessive amount reset which caused sell amount resetting to 1 when you change sell token