Skip to content

refactor: simplify query combination typing #1155

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

Merged
merged 2 commits into from
Jul 14, 2025

Conversation

0xAlunara
Copy link
Collaborator

My mind is drained after this session and I no longer wish to spend further time on this nor the convoluted typing 😭

The useQueryMapping function and corresponding combineQueriesToObject typing were all kinds of fucked up and broken anyway; straight up not working. I've tried to keep the useQueryMapping function, but trying to do so gets you into type hell.

In short, instead of using

return useQueryMapping(
  uniqueAddresses.map((tokenAddress) => getTokenUsdRateQueryOptions({ chainId, tokenAddress })),
  uniqueAddresses,
)

you can use

return useQueries({
  queries: uniqueAddresses.map((tokenAddress) => getTokenUsdRateQueryOptions({ chainId, tokenAddress })),
  combine: (results) => combineQueriesToObject(results, uniqueAddresses),
})

In the case of token queries it correctly deduces the type now as Record<string, number>, and not Record<string, any>, so you dont need these ugly hacks like

const { data: usdRatesRaw } = useTokenUsdRates({ chainId, tokenAddresses: collateralTokenAddresses })
const usdRates = useMemo(() => usdRatesRaw ?? ({} as Record<string, number>), [usdRatesRaw])

but can simply use

const { data: usdRates } = useTokenUsdRates({ chainId, tokenAddresses })

Very useful for other cases where you want to execute multiple non-batched queries and group them back into a record (like user balances, which is why I started working on this)

Copy link

vercel bot commented Jul 12, 2025

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

Name Status Preview Comments Updated (UTC)
curve-dapp ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 12, 2025 5:41pm
curve-dapp-storybook ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 12, 2025 5:41pm

@0xAlunara 0xAlunara merged commit 6d5278b into refactor/token-query Jul 14, 2025
4 of 8 checks passed
@0xAlunara 0xAlunara deleted the refactor/remove-combine-queries branch July 14, 2025 11:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants