Skip to content
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

[USD Price] SWR-ise the USD fetching logic #539

Merged
merged 6 commits into from
May 25, 2022
Merged

Conversation

W3stside
Copy link
Contributor

@W3stside W3stside commented May 6, 2022

Summary

Attempts to SWR-ise the USD querying logic

As a bonus - stops 2 faulty calls to quote happening on initial load - app was querying "USDC" in price quote

@W3stside W3stside requested review from a team May 6, 2022 11:42
@github-actions
Copy link
Contributor

github-actions bot commented May 6, 2022

CLA Assistant Lite All Contributors have signed the CLA.

() => (strategy && quoteParams ? getGpUsdcPriceResolveOnlyLastCall({ strategy, quoteParams }) : null)
)

useEffect(() => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was considering using useMemo being returned directly but I like having the previous price in state remaining vs null if returning useMemo

could be convinced otherwise. the useMemo approach is way cleaner since i dont need useState and can return the error directly from SWR

@elena-zh
Copy link
Contributor

elena-zh commented May 6, 2022

Could you please provide the PR preview link?

@github-actions
Copy link
Contributor

  • 🔭 GP Swap: CoW Protocol v2 Swap UI

@elena-zh
Copy link
Contributor

Hey @W3stside , is it OK that even if I select ETH-WETH or WXDAI-XDAI token pairs, quote requests for WETH-USDC, WXDAI-USDXC are still running in the console for Rinkeby and GC networks?
image

@elena-zh
Copy link
Contributor

Also, I noticed, that USD estimation for ETH token can disappear when swap tokens in the From/TO fields
image
image

@alfetopito
Copy link
Collaborator

Not sure the caching is working as expected, at least not how I would expect.
Using Elena's use case (switching between sell/buy order), every swap sends a new quote api endpoint request.
I would expect it to not be sent unless caching is expired.

Screen.Recording.2022-05-16.at.15.05.54.mov

BTW, what is the cache expiring time?

@W3stside
Copy link
Contributor Author

W3stside commented May 20, 2022

Not sure the caching is working as expected, at least not how I would expect. Using Elena's use case (switching between sell/buy order), every swap sends a new quote api endpoint request. I would expect it to not be sent unless caching is expired.

Screen.Recording.2022-05-16.at.15.05.54.mov
BTW, what is the cache expiring time?

Well here you're looking at price quotes. We're talking USD estimation, which isn't being called AFAICT. also any new swap = different swap params = invalidated cache, or did u mean sth else?

Not sure the cache time, think it's 2s (2000) which is default

Copy link
Contributor

@anxolin anxolin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice initiative.

My only problem with this PR is where you are adding caches.

In principle, the cache should be added in quering of the data. Adding it there feels like mixing logic with cache strategies. The logics for the strategy quering should be fast. Why not just wrapping the API calls with a simple custom hook that uses SWR

}, [account, baseAmountRaw, sellTokenAddress, sellTokenDecimals, stablecoin, supportedChain])

// SWR cache cow usd requests
const { data: priceResponse, error: errorResponse } = useSWR<CancelableResult<string | null> | null>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how often this cached result will be revalidated? when does it become stale?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's currently revalidated using default options, as shown:
image

i added custom options that are in a comment below

src/custom/hooks/useUSDCPrice/index.ts Outdated Show resolved Hide resolved
@elena-zh
Copy link
Contributor

USD estimation for ETH token work fine now, but the issue reported in #539 (comment) is still here

@alfetopito
Copy link
Collaborator

Not sure the caching is working as expected, at least not how I would expect. Using Elena's use case (switching between sell/buy order), every swap sends a new quote api endpoint request. I would expect it to not be sent unless caching is expired.
Screen.Recording.2022-05-16.at.15.05.54.mov
BTW, what is the cache expiring time?

Well here you're looking at price quotes. We're talking USD estimation, which isn't being called AFAICT. also any new swap = different swap params = invalidated cache, or did u mean sth else?

Not sure the cache time, think it's 2s (2000) which is default

Ah sorry, my mistake.
Was watching for our price quotes not the USD quotes - which is what you changed 🤦

@alfetopito
Copy link
Collaborator

Filtering for coingecko requests though, they all seem to be failing due to CORS issues
Screen Shot 2022-05-20 at 13 37 23

@alfetopito
Copy link
Collaborator

Filtering for coingecko requests though, they all seem to be failing due to CORS issues Screen Shot 2022-05-20 at 13 37 23

Seemed to be a temporary fluke locally. All is well now

@W3stside
Copy link
Contributor Author

added SWR options to constants:

const SWR_OPTIONS = {
  refreshInterval: ms`30s`,
  dedupingInterval: ms`10s`,
  // don't revalidate data on focus, can cause too many re-renders
  // see https://koba04.medium.com/revalidating-options-of-swr-4d9f08bee813
  revalidateOnFocus: false,
}
  • refreshes the data every 30 seconds
  • doesn't revalidate on focus as that can cause too many revalidations
  • dedupes requests within a 10s window

@anxolin @alfetopito thoughts?

@W3stside W3stside requested a review from anxolin May 23, 2022 09:44
@alfetopito
Copy link
Collaborator

added SWR options to constants:

const SWR_OPTIONS = {
  refreshInterval: ms`30s`,
  dedupingInterval: ms`10s`,
  // don't revalidate data on focus, can cause too many re-renders
  // see https://koba04.medium.com/revalidating-options-of-swr-4d9f08bee813
  revalidateOnFocus: false,
}
* refreshes the data every 30 seconds

* doesn't revalidate on focus as that can cause too many revalidations

* dedupes requests within a 10s window

@anxolin @alfetopito thoughts?

Looks good.

@W3stside
Copy link
Contributor Author

ok im merging this

@W3stside W3stside merged commit 4f57bf4 into develop May 25, 2022
@github-actions github-actions bot locked and limited conversation to collaborators May 25, 2022
@alfetopito alfetopito deleted the usd-hook-rework branch May 25, 2022 16:54
} else {
price = new Price({
baseAmount,
quoteAmount: stringToCurrency(quote, currency),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here is the relevant change that was NOT in 1.14.2 and only added to prod in 1.15.0

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants