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

chore: faster prediction prices #10154

Merged
merged 13 commits into from
Aug 20, 2024
Merged

Conversation

thechefpenguin
Copy link
Contributor

@thechefpenguin thechefpenguin commented Jul 9, 2024


PR-Codex overview

This PR introduces an useAlternateSource option in the AI prediction config to fetch price data from a different source. It also updates UI elements and refactors code for better performance.

Detailed summary

  • Added MutableRefObject interface for useAlternateSource in AI prediction config
  • Updated cache control headers in the API response
  • Modified UI elements in PhishingWarningBanner and AILiveRoundCard
  • Refactored code in PredictionConfigProviders and usePredictionPrice to support alternate data source for price fetching

✨ Ask PR-Codex anything about this PR by commenting with /codex {your question}

Copy link

vercel bot commented Jul 9, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
web ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 19, 2024 6:49pm
6 Skipped Deployments
Name Status Preview Comments Updated (UTC)
aptos-web ⬜️ Ignored (Inspect) Visit Preview Aug 19, 2024 6:49pm
blog ⬜️ Ignored (Inspect) Visit Preview Aug 19, 2024 6:49pm
bridge ⬜️ Ignored (Inspect) Visit Preview Aug 19, 2024 6:49pm
games ⬜️ Ignored (Inspect) Visit Preview Aug 19, 2024 6:49pm
gamification ⬜️ Ignored (Inspect) Visit Preview Aug 19, 2024 6:49pm
uikit ⬜️ Ignored (Inspect) Visit Preview Aug 19, 2024 6:49pm

Copy link

changeset-bot bot commented Jul 9, 2024

⚠️ No Changeset found

Latest commit: 5b5f01d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

export const usePredictionPriceMutation = () => {
const queryClient = useQueryClient()

return useMutation({
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why useMutation hook is used since it is not sending any data to change to api?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@memoyil The mutation function will call Binance API directly to go around our own APIs caching.
This data is set in query data and cached so will be reflected everywhere on the UI where the same queryKey exists

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, but useMutation hook is meant for post/put api's where it change some resources on the server side, since it is a get call this can be done with useQuery by using refetch function of it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I want to use a different data source and refetch function would use the existing api.

I have updated to make the update function a normal async call instead of a mutation

queryClient.setQueryData(['price', currencyA, currencyB], data)
},
})
const updatePriceFromSource = async ({
Copy link
Collaborator

@memoyil memoyil Jul 9, 2024

Choose a reason for hiding this comment

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

usecallback to not return new function on each render, otherwise settimeout will be cleared before getting triggered

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks. added

export const usePredictionPriceUpdate = () => {
const queryClient = useQueryClient()

const updatePriceFromSource = useCallback(
Copy link
Collaborator

@memoyil memoyil Jul 9, 2024

Choose a reason for hiding this comment

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

I think it is better to convert this hook to util function that returns promise of this function, the caller side can do the query client update afterwards when there is an answer. In that way multiple components can use this util based on their own needs. Wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Usually other components would use wallet api for prices and only prediction will use this price source, which is why this isn't a global hook either. I think if we really need this api for other components then can abstract away but for now should be good scoped to prediction

Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you help approve

@memoyil
Copy link
Collaborator

memoyil commented Jul 10, 2024

@thechefpenguin I am thinking that this approach might have issues, imagine :

  1. usePredictionPrice hook refetchInterval close to be triggered
  2. price updated from updatePriceFromSource and query data is set
  3. Then usePredictionPrice hook triggered and price back to old/obsolete value

I think we should create another query hook (which usePredictionPrice uses it) where we can set query function or the link where we can fetch the data, change the link or queryFn when we want to change the resource and then use usePredictionPrice's hooks refetch in the refreshPriceTimeout function. After calling the refetch link/queryfn can be set back to original value.

wdyt?

@memoyil memoyil force-pushed the chore/faster-prediction-prices branch from 1932723 to 86084bc Compare August 19, 2024 10:18
enabled = true,
}: UsePredictionPriceParameters = {}) => {
const config = useConfig()

return useQuery<PriceResponse>({
queryKey: ['price', currencyA, currencyB],
Copy link
Collaborator

@memoyil memoyil Aug 19, 2024

Choose a reason for hiding this comment

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

to ensure smooth price transition and to see the new price all the components that use this hook, the same querykey will be used

@thechefpenguin thechefpenguin merged commit 9176f25 into develop Aug 20, 2024
19 checks passed
@thechefpenguin thechefpenguin deleted the chore/faster-prediction-prices branch August 20, 2024 06:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants