-
Notifications
You must be signed in to change notification settings - Fork 55
Add fast price fetching #2477
Add fast price fetching #2477
Changes from 3 commits
16c42f0
a95eec6
ad7213c
e1d6fad
36524aa
7fc7885
c4f8610
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { useCallback } from 'react' | ||
|
||
import { FeeQuoteParams, getBestQuote, QuoteParams, QuoteResult } from 'utils/price' | ||
import { FeeQuoteParams, getBestQuote, getFastQuote, QuoteParams, QuoteResult } from 'utils/price' | ||
import { isValidOperatorError, ApiErrorCodes } from 'api/gnosisProtocol/errors/OperatorError' | ||
import GpQuoteError, { | ||
GpQuoteErrorCodes, | ||
|
@@ -19,7 +19,7 @@ import { QuoteInformationObject } from 'state/price/reducer' | |
import { useQuoteDispatchers } from 'state/price/hooks' | ||
import { AddGpUnsupportedTokenParams } from 'state/lists/actions' | ||
import { QuoteError } from 'state/price/actions' | ||
import { onlyResolvesLast } from 'utils/async' | ||
import { CancelableResult, onlyResolvesLast } from 'utils/async' | ||
import useGetGpPriceStrategy from 'hooks/useGetGpPriceStrategy' | ||
import { calculateValidTo } from 'hooks/useSwapCallback' | ||
import { useUserTransactionTTL } from 'state/user/hooks' | ||
|
@@ -109,6 +109,7 @@ export function handleQuoteError({ quoteData, error, addUnsupportedToken }: Hand | |
} | ||
|
||
const getBestQuoteResolveOnlyLastCall = onlyResolvesLast<QuoteResult>(getBestQuote) | ||
const getFastQuoteResolveOnlyLastCall = onlyResolvesLast<QuoteResult>(getFastQuote) | ||
|
||
/** | ||
* @returns callback that fetches a new quote and update the state | ||
|
@@ -140,24 +141,10 @@ export function useRefetchQuoteCallback() { | |
|
||
let quoteData: FeeQuoteParams | QuoteInformationObject = quoteParams | ||
|
||
const { sellToken, buyToken, chainId } = quoteData | ||
try { | ||
// Start action: Either new quote or refreshing quote | ||
if (isPriceRefresh) { | ||
// Refresh the quote | ||
refreshQuote({ sellToken, chainId }) | ||
} else { | ||
// Get new quote | ||
getNewQuote(quoteParams) | ||
} | ||
|
||
registerOnWindow({ | ||
getBestQuote: async () => getBestQuoteResolveOnlyLastCall({ ...params, strategy: priceStrategy }), | ||
}) | ||
// price can be null if fee > price | ||
const handleResponse = (response: CancelableResult<QuoteResult>, isBestQuote: boolean) => { | ||
const { cancelled, data } = response | ||
|
||
// Get the quote | ||
// price can be null if fee > price | ||
const { cancelled, data } = await getBestQuoteResolveOnlyLastCall({ ...params, strategy: priceStrategy }) | ||
if (cancelled) { | ||
// Cancellation can happen if a new request is made, then any ongoing query is canceled | ||
console.debug('[useRefetchPriceCallback] Canceled get quote price for', params) | ||
|
@@ -170,6 +157,7 @@ export function useRefetchQuoteCallback() { | |
...quoteParams, | ||
fee: getPromiseFulfilledValue(fee, undefined), | ||
price: getPromiseFulfilledValue(price, undefined), | ||
isBestQuote, | ||
} | ||
// check the promise fulfilled values | ||
// handle if rejected | ||
|
@@ -202,7 +190,9 @@ export function useRefetchQuoteCallback() { | |
|
||
// Update quote | ||
updateQuote(quoteData) | ||
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. Do we ALWAY update the price? no matter if its worse or better? 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. Addressed in the comment up ☝️ |
||
} catch (error) { | ||
} | ||
|
||
const handleError = (error: Error) => { | ||
// handle any errors in quote fetch | ||
// we re-use the quoteData object in scope to save values into state | ||
const quoteError = handleQuoteError({ | ||
|
@@ -217,6 +207,38 @@ export function useRefetchQuoteCallback() { | |
error: quoteError, | ||
}) | ||
} | ||
|
||
const { sellToken, buyToken, chainId } = quoteData | ||
// Start action: Either new quote or refreshing quote | ||
if (isPriceRefresh) { | ||
// Refresh the quote | ||
refreshQuote({ sellToken, chainId }) | ||
} else { | ||
// Get new quote | ||
getNewQuote(quoteParams) | ||
} | ||
|
||
// Init get quote methods params | ||
const bestQuoteParams = { ...params, strategy: priceStrategy } | ||
const fastQuoteParams = { quoteParams: { ...quoteParams, priceQuality: 'fast' } } | ||
|
||
// Register get best and fast quote methods on window | ||
registerOnWindow({ | ||
getBestQuote: async () => getBestQuoteResolveOnlyLastCall(bestQuoteParams), | ||
getFastQuote: async () => getFastQuoteResolveOnlyLastCall(fastQuoteParams), | ||
}) | ||
|
||
// Get the fast quote | ||
if (!isPriceRefresh) { | ||
getFastQuoteResolveOnlyLastCall(fastQuoteParams) | ||
.then((res) => handleResponse(res, false)) | ||
.catch(handleError) | ||
} | ||
|
||
// Get the best quote | ||
getBestQuoteResolveOnlyLastCall(bestQuoteParams) | ||
.then((res) => handleResponse(res, true)) | ||
.catch(handleError) | ||
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. Can there be a race condition?
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. It would, the solution for this is in latest commit and I tested it in a way by using setTimeout on fast price update action. And the update will check inside of update action code, if there is already a quote price amount, which means that the best quote request was already done and if the current action params are bestQuote update or not. |
||
}, | ||
[ | ||
deadline, | ||
|
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.
curious why we need this? u end up leaving the nose picking GIF right?
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.
Good catch, reverted this also