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

fix: Rpc request increase #9611

Merged
merged 8 commits into from Apr 18, 2024
Merged

Conversation

memoyil
Copy link
Collaborator

@memoyil memoyil commented Apr 16, 2024


PR-Codex overview

This PR updates dependencies, adds new hooks, and refactors existing ones in the wagmi package.

Detailed summary

  • Updated dependencies in package.json
  • Added new hooks in useBalance.ts
  • Refactored existing hooks in various files

The following files were skipped due to too many changes: apps/web/src/views/CakeStaking/hooks/useProxyVeCakeBalance.ts, packages/wagmi/src/hooks/useFeeData.ts, apps/web/src/state/nftMarket/hooks.ts, apps/web/src/views/Farms/components/YieldBooster/hooks/useYieldBoosterState.ts, apps/web/src/views/CakeStaking/hooks/useProxyVeCakeBalanceOfAtTime.ts, apps/web/src/views/FixedStaking/hooks/useStakedPools.ts, packages/wagmi/src/hooks/useReadContracts.ts, packages/wagmi/src/hooks/useReadContract.ts, apps/web/src/hooks/v3/useV3Positions.ts, apps/gamification/hooks/useTokenBalance.ts, apps/web/src/hooks/useTokenBalance.ts

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

Copy link

changeset-bot bot commented Apr 16, 2024

⚠️ No Changeset found

Latest commit: 7997737

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.

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

💥 An error occurred when fetching the changed packages and changesets in this PR
Some errors occurred when validating the changesets config:
The package "gamification" depends on the ignored package "@pancakeswap/ui-wallets", but "gamification" is not being ignored. Please add "gamification" to the `ignore` option.

Copy link

vercel bot commented Apr 16, 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 Apr 17, 2024 2:37pm
5 Ignored Deployments
Name Status Preview Comments Updated (UTC)
aptos-web ⬜️ Ignored (Inspect) Visit Preview Apr 17, 2024 2:37pm
blog ⬜️ Ignored (Inspect) Visit Preview Apr 17, 2024 2:37pm
bridge ⬜️ Ignored (Inspect) Visit Preview Apr 17, 2024 2:37pm
games ⬜️ Ignored (Inspect) Visit Preview Apr 17, 2024 2:37pm
uikit ⬜️ Ignored (Inspect) Visit Preview Apr 17, 2024 2:37pm

@@ -28,8 +30,9 @@ export const useTokenBalanceByChain = (tokenAddress: Address, chainIdOverride?:
})

useEffect(() => {
refetch()
}, [blockNumber, refetch])
queryClient.invalidateQueries({ queryKey })
Copy link
Collaborator

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

Copy link
Collaborator Author

@memoyil memoyil Apr 16, 2024

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

TanStack/query#608 (reply in thread)

Copy link
Collaborator Author

@memoyil memoyil Apr 16, 2024

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

TanStack/query#2468 (reply in thread)

Copy link
Collaborator

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

Copy link
Collaborator

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

Copy link
Collaborator Author

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

Copy link
Collaborator Author

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)

@memoyil memoyil force-pushed the feature/fix_rpc_request_increase branch from 0116675 to e1552ac Compare April 17, 2024 07:26
Copy link

socket-security bot commented Apr 17, 2024

New and removed dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/ajv@6.12.6 eval +2 962 kB esp
npm/big.js@5.2.2 None 0 63.9 kB mikemcl
npm/bottleneck@2.19.5 eval 0 629 kB sgrondin
npm/clsx@1.2.1 None 0 5.67 kB lukeed
npm/crypto-js@4.2.0 None 0 487 kB evanvosberg
npm/csstype@3.1.2 None 0 1.22 MB faddee
npm/deepmerge@4.3.1 None 0 31.2 kB tehshrike
npm/ethers@5.7.2 Transitive: network +37 16.5 MB ricmoo
npm/eventemitter3@4.0.7 None 0 38 kB lpinca
npm/fast-json-stable-stringify@2.1.0 None 0 17 kB esp
npm/lodash@4.17.21 None 0 1.41 MB bnjmnt4n
npm/qs@6.11.2 None 0 241 kB ljharb
npm/react-dom@18.2.0 environment +3 4.62 MB gnoff
npm/react-is@17.0.2 environment 0 24.8 kB gaearon
npm/react@18.2.0 environment +2 337 kB gnoff

🚮 Removed packages: npm/@0xsquid/widget@1.6.16, npm/@babel/core@7.23.3, npm/@babel/preset-env@7.23.3, npm/@babel/preset-react@7.23.3, npm/@binance/w3w-utils@1.1.4, npm/@binance/w3w-wagmi-connector-v2@1.1.6, npm/@binance/w3w-wagmi-connector@1.1.4, npm/@blocto/sdk@0.5.5, npm/@changesets/cli@2.27.1, npm/@cloudflare/workers-types@3.19.0, npm/@cyberlab/cyber-app-sdk@3.0.0-beta.5, npm/@datadog/browser-logs@5.8.0, npm/@datadog/browser-rum@5.8.0, npm/@gelatonetwork/limit-orders-lib@4.5.0, npm/@gnosis.pm/safe-apps-wagmi@1.2.0, npm/@layerzerolabs/scan-client@0.0.6, npm/@material-ui/core@4.12.4, npm/@msafe/aptos-wallet@3.0.6, npm/@next/bundle-analyzer@13.0.7, npm/@next/eslint-plugin-next@13.4.2, npm/@orbs-network/twap-ui-pancake@0.9.98, npm/@orbs-network/twap-ui@0.9.98, npm/@pancakeswap/aptos-swap-sdk@1.0.4, npm/@pancakeswap/awgmi@1.0.1, npm/@pancakeswap/chains@0.4.2, npm/@pancakeswap/eslint-config-pancake@1.2.0, npm/@pancakeswap/gauges@1.1.8, npm/@pancakeswap/multicall@3.5.1, npm/@pancakeswap/pcsx-sdk@0.0.3, npm/@pancakeswap/permit2-sdk@1.0.3, npm/@pancakeswap/pools@5.1.18, npm/@pancakeswap/price-api-sdk@1.2.1, npm/@pancakeswap/sdk@5.8.1, npm/@pancakeswap/smart-router@6.0.2, npm/@pancakeswap/stable-swap-sdk@1.0.4, npm/@pancakeswap/swap-sdk-core@1.1.0, npm/@pancakeswap/token-lists@0.0.11, npm/@pancakeswap/tokens@0.6.12, npm/@pancakeswap/universal-router-sdk@1.0.15, npm/@pancakeswap/v3-sdk@3.8.1, npm/@pancakeswap/wagmi@3.0.1, npm/@popperjs/core@2.11.8, npm/@pythnetwork/pyth-evm-js@1.29.2, npm/@radix-ui/react-dismissable-layer@1.0.5, npm/@radix-ui/react-slot@1.0.2, npm/@reduxjs/toolkit@1.9.7, npm/@sentry/nextjs@7.80.0, npm/@snapshot-labs/snapshot.js@0.4.110, npm/@storybook/addon-a11y@7.5.3, npm/@storybook/addon-actions@7.5.3, npm/@storybook/addon-essentials@7.5.3, npm/@storybook/addon-links@7.5.3, npm/@storybook/builder-vite@7.5.3, npm/@storybook/react-vite@7.5.3, npm/@storybook/react@7.5.3, npm/@styled-system/should-forward-prop@5.1.5, npm/@stylelint/postcss-css-in-js@0.37.3, npm/@tanstack/react-query@5.29.2, npm/@testing-library/jest-dom@5.17.0, npm/@testing-library/react-hooks@8.0.1, npm/@testing-library/react@12.1.5, npm/@testing-library/react@13.4.0

View full report↗︎

Copy link

socket-security bot commented Apr 17, 2024

🚨 Potential security issues detected. Learn more about Socket for GitHub ↗︎

To accept the risk, merge this PR and you will not be notified again.

Alert Package NoteSource
Filesystem access npm/resolve-from@5.0.0
  • orphan: npm/resolve-from@5.0.0
Environment variable access npm/argparse@2.0.1
  • orphan: npm/argparse@2.0.1
Filesystem access npm/argparse@2.0.1
  • orphan: npm/argparse@2.0.1
Filesystem access npm/rimraf@3.0.2
  • orphan: npm/rimraf@3.0.2
Filesystem access npm/mkdirp@1.0.4
  • orphan: npm/mkdirp@1.0.4
Environment variable access npm/mkdirp@1.0.4
  • orphan: npm/mkdirp@1.0.4
Environment variable access npm/mkdirp@1.0.4
  • orphan: npm/mkdirp@1.0.4
Environment variable access npm/npm-run-path@2.0.2
  • orphan: npm/npm-run-path@2.0.2
Environment variable access npm/npm-run-path@2.0.2
  • orphan: npm/npm-run-path@2.0.2
Filesystem access npm/commander@2.20.3
  • orphan: npm/commander@2.20.3
Filesystem access npm/glob@7.2.3
  • orphan: npm/glob@7.2.3
Filesystem access npm/glob@7.2.3
  • orphan: npm/glob@7.2.3
Filesystem access npm/source-map-support@0.5.21
  • orphan: npm/source-map-support@0.5.21
Environment variable access npm/chalk@2.4.2
  • orphan: npm/chalk@2.4.2
Filesystem access npm/mkdirp@0.5.6
  • orphan: npm/mkdirp@0.5.6
Filesystem access npm/rimraf@2.6.3
  • orphan: npm/rimraf@2.6.3
Filesystem access npm/fs.realpath@1.0.0
  • orphan: npm/fs.realpath@1.0.0
Environment variable access npm/fs.realpath@1.0.0
  • orphan: npm/fs.realpath@1.0.0
Filesystem access npm/resolve-from@4.0.0
  • orphan: npm/resolve-from@4.0.0
Environment variable access npm/external-editor@3.1.0
  • orphan: npm/external-editor@3.1.0
Filesystem access npm/external-editor@3.1.0
  • orphan: npm/external-editor@3.1.0
Environment variable access npm/external-editor@3.1.0
  • orphan: npm/external-editor@3.1.0
Filesystem access npm/chardet@0.7.0
  • orphan: npm/chardet@0.7.0
Environment variable access npm/graceful-fs@4.2.11
  • orphan: npm/graceful-fs@4.2.11
Filesystem access npm/graceful-fs@4.2.11
  • orphan: npm/graceful-fs@4.2.11
Environment variable access npm/graceful-fs@4.2.11
  • orphan: npm/graceful-fs@4.2.11
Environment variable access npm/graceful-fs@4.2.11
  • orphan: npm/graceful-fs@4.2.11
Filesystem access npm/convert-source-map@1.9.0
  • orphan: npm/convert-source-map@1.9.0
Filesystem access npm/get-package-type@0.1.0
  • orphan: npm/get-package-type@0.1.0
Filesystem access npm/locate-path@5.0.0
  • orphan: npm/locate-path@5.0.0
Filesystem access npm/path-exists@4.0.0
  • orphan: npm/path-exists@4.0.0
Environment variable access npm/cross-spawn@7.0.3
  • orphan: npm/cross-spawn@7.0.3
Filesystem access npm/cross-spawn@7.0.3
  • orphan: npm/cross-spawn@7.0.3
Environment variable access npm/cross-spawn@7.0.3
  • orphan: npm/cross-spawn@7.0.3
Environment variable access npm/npm-run-path@4.0.1
  • orphan: npm/npm-run-path@4.0.1
Environment variable access npm/npm-run-path@4.0.1
  • orphan: npm/npm-run-path@4.0.1
Filesystem access npm/isexe@2.0.0
  • orphan: npm/isexe@2.0.0
Environment variable access npm/isexe@2.0.0
  • orphan: npm/isexe@2.0.0
Filesystem access npm/pump@3.0.0
  • orphan: npm/pump@3.0.0
Environment variable access npm/ci-info@2.0.0
  • orphan: npm/ci-info@2.0.0
Environment variable access npm/fb-watchman@2.0.2
  • orphan: npm/fb-watchman@2.0.2
Filesystem access npm/make-dir@3.1.0
  • orphan: npm/make-dir@3.1.0
Environment variable access npm/argparse@1.0.10
  • orphan: npm/argparse@1.0.10
Filesystem access npm/argparse@1.0.10
  • orphan: npm/argparse@1.0.10
Environment variable access npm/request@2.88.2
  • orphan: npm/request@2.88.2
Filesystem access npm/request@2.88.2
  • orphan: npm/request@2.88.2
Mild CVE npm/request@2.88.2
  • orphan: npm/request@2.88.2
Deprecated npm/request@2.88.2
  • orphan: npm/request@2.88.2
Environment variable access npm/readable-stream@3.6.2
  • orphan: npm/readable-stream@3.6.2
Filesystem access npm/is-wsl@2.2.0
  • orphan: npm/is-wsl@2.2.0
Environment variable access npm/is-wsl@2.2.0
  • orphan: npm/is-wsl@2.2.0
Filesystem access npm/is-docker@2.2.1
  • orphan: npm/is-docker@2.2.1
Environment variable access npm/react-is@16.13.1
  • orphan: npm/react-is@16.13.1
Environment variable access npm/aws4@1.12.0
  • orphan: npm/aws4@1.12.0
Filesystem access npm/form-data@2.3.3
  • orphan: npm/form-data@2.3.3
Deprecated npm/har-validator@5.1.5
  • Reason: this library is no longer supported
  • orphan: npm/har-validator@5.1.5
Environment variable access npm/assert-plus@1.0.0
  • orphan: npm/assert-plus@1.0.0
Environment variable access npm/execa@1.0.0
  • orphan: npm/execa@1.0.0
Environment variable access npm/execa@1.0.0
  • orphan: npm/execa@1.0.0
Filesystem access npm/debug@2.6.9
  • orphan: npm/debug@2.6.9
Environment variable access npm/debug@2.6.9
  • orphan: npm/debug@2.6.9
Environment variable access npm/debug@2.6.9
  • orphan: npm/debug@2.6.9
Environment variable access npm/debug@2.6.9
  • orphan: npm/debug@2.6.9
Environment variable access npm/debug@2.6.9
  • orphan: npm/debug@2.6.9
Environment variable access npm/bluebird@3.7.2
  • orphan: npm/bluebird@3.7.2
Environment variable access npm/bluebird@3.7.2
  • orphan: npm/bluebird@3.7.2
Environment variable access npm/open@7.4.2
  • orphan: npm/open@7.4.2
Filesystem access npm/open@7.4.2


const { status, refetch, data } = useBalance({
const { status, refetch, data, queryKey } = useBalance({
Copy link
Collaborator

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?

Copy link
Collaborator Author

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])
Copy link
Collaborator

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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

chefjackson
chefjackson previously approved these changes Apr 17, 2024
@chefjackson chefjackson merged commit 1d973b2 into develop Apr 18, 2024
17 of 18 checks passed
@chefjackson chefjackson deleted the feature/fix_rpc_request_increase branch April 18, 2024 02:47
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.

None yet

2 participants