1

I'm using WAGMI in a web app and I'm seeing some slowness in having read values updated in my app when the blockchain state changes. Looking at the docs, I see two potentially relevant options in the useContractReads method: 'watch' and 'cacheOnBlock'.

My guess is that watch automatically updates the value whenever a new block arrives and the value has changed, and cacheOnBlock says that the data returned is cached based on the current block number, so it seems that it would immediately be considered 'stale' but I guess you have to manually fetch an update?

Could anyone provide some additional context or scenarios on when each of these options are useful?

1 Answer 1

1

When using the useContractRead() hook, there are a few options that tell it how often to fetch data from the blockchain.

The documentation which you linked outlines it pretty well.

watch is going to check per-block changes, which on fast chains can lead to rate-limit issues with your RPC endpoint if you don't write a custom hook or conditional logic (more on that below). Unless otherwise stated, you're setting it to "always read." This would be helpful if you are setting up an app that looks for arbitrage opportunities or something that needs to be acted on, but would dramatically slow down something like fetching the last token ID on an NFT minting site.

cacheOnBlock again does exactly what the documentation says it does. It writes the data to the cache in block-time, after that block it is considered "stale". Unless a refresh is triggered, it will simply be read, and stored to cache. cacheTime or 'staleTime' can be used to determine how long the data should be kept in the cache (in ms). If no other logic is set up, the cacheTime defaults to 0, meaning that you would need to set enabled to false or write a custom hook to prevent it from continuously reading it to cache. staleTime is how long (in ms) the data is considered stale for, and can be set between 0 and Infinity.

There are a lot of variables in wagmi that determine how often the chain should be queried, and they can be used together to fine tune your app.

Another important variable to consider if you're looking to limit how often data is queried is the refetch variable, which can conditionally query logic from the read hook.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.