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
fix: Rpc request increase #9611
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
5 Ignored Deployments
|
d9f108e
to
672e092
Compare
672e092
to
21f2bfd
Compare
21f2bfd
to
2028740
Compare
2028740
to
f864d41
Compare
@@ -28,8 +30,9 @@ export const useTokenBalanceByChain = (tokenAddress: Address, chainIdOverride?: | |||
}) | |||
|
|||
useEffect(() => { | |||
refetch() | |||
}, [blockNumber, refetch]) | |||
queryClient.invalidateQueries({ queryKey }) |
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.
After second thought, I'm a bit confused why refetch
is changing more frequently. Shouldn't the reference to refetch
changes whenever queryKey
is updated? Plus react query should have its own hash function to serialize the queryKey
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.
issue happens when multiple hooks refetching the same query key, when invalidation used it will taken into account the deduplication
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.
and refetch also bypasses enabled property, so even if the enabled is false when refetch is called it will fetch the query
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.
Just realized useReadContract
has this scopeKey
option
https://wagmi.sh/react/api/hooks/useReadContract#scopekey
Can we simply add blockNumber
to scopeKey? For example
const { data, status, queryKey, refetch, ...rest } = useReadContract({
chainId: chainIdOverride || chainId,
abi: erc20Abi,
address: tokenAddress,
functionName: 'balanceOf',
args: [account || '0x'],
query: {
enabled: !!account,
},
scopeKey: `useTokenBalanceByChain_${blockNumber}`,
})
So there's no need for effect
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.
and with placeholderData
we can keep the previous data visible
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.
@chefjackson Applied cancelRefetch: false to avoid deduplication
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.
@chefjackson When scopekey is used it will create a querykey like this
[ "readContract", { "chainId": 56, "address": "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c", "functionName": "balanceOf", "scopeKey": "useTokenBalanceByChain_37933594" } ]
So it doesn't affect query deduplication (wevm/wagmi#2031) and it is mainly used to refetch manually based on the scopekey (by using key filters instead of exact match)
0116675
to
e1552ac
Compare
🚨 Potential security issues detected. Learn more about Socket for GitHub ↗︎ To accept the risk, merge this PR and you will not be notified again.
|
|
||
const { status, refetch, data } = useBalance({ | ||
const { status, refetch, data, queryKey } = useBalance({ |
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.
do we still need queryKey here?
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.
Done
queryClient.invalidateQueries({ queryKey: readContractResult.queryKey }, { cancelRefetch: false }) | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [blockNumber, queryClient]) |
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.
watch should be one of the dependencies?
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.
Done
PR-Codex overview
This PR updates dependencies, adds new hooks, and refactors existing ones in the
wagmi
package.Detailed summary
package.json
useBalance.ts