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

data is never updated if view method reverts #3933

Closed
1 task done
0x33dm opened this issue May 15, 2024 · 9 comments
Closed
1 task done

data is never updated if view method reverts #3933

0x33dm opened this issue May 15, 2024 · 9 comments

Comments

@0x33dm
Copy link

0x33dm commented May 15, 2024

Describe the bug

Problem

I'm using useReadContract in order to have live updates from a variable called account_debt, in order to fetch that data I read a "view" method on a smart contract called get_pending_market_state_for_account.

The way the view functions works is:

  1. If the user has no debt the view will revert.
  2. If the user has debt the view will return account_debt
  3. If the user closes his debt the view will revert again.

The natural user flow on the application goes as this:

  1. User starts with no debt
  2. User creates debt
  3. User pays debt
  4. User has no debt again UI should return to (1)

The problem is when a user pays the debt on step (3) the UI never returns to (1), because:

  1. While the user has no debt the value of account_debt is null
  2. When debt is created it becomes a bigint
  3. When debt is closed the value never turns back to null because ( I assume ) Wagmi won't update the data if the contract starts reverting with the same params?

Debugging

When I look at the ReactQueryDevTools on the stuck page I can see this and it NEVER changes even though I see the "fetch" happening often, the cached data is always here.

image

If I press the blue button "Trigger Loading" then the data is updated and becomes "null" again ( which is the original state ).

image

Questions


  • Is there a recommended way of dealing with this problem?
  • Is there a way of overriding this problem, for instance have a hook where I can check if the view reverted for a given reason I can return some "empty state" to my frontend?
  • Why clicking "Trigger Loading" has a different behaviour than "Refetch" and would it be possible to have "refetch" working as "trigger loading" by itself?

Link to Minimal Reproducible Example

https://stackblitz.com/edit/new-wagmi?file=src%2FApp.tsx

Steps To Reproduce

Described on the preview comment

Wagmi Version

2.8.1

Viem Version

2.10.2

TypeScript Version

5.4.5

Check existing issues

Anything else?

To reproduce the error I would need to send some test tokens on FTM + FTM for gas and I think it's not needed in this case as the problem is well explained.

Thanks a lot for your hard work on the library!

@0x33dm
Copy link
Author

0x33dm commented May 17, 2024

I tried to make a new reproductionable example on https://new.wagmi.sh/ but no matter what I do the page is rendered blank.

Tried the latest OPERA and latest CHROME both ended up with a blank page.

@tmm
Copy link
Member

tmm commented May 21, 2024

Can you share the contract address and ABI so someone that helps doesn't need to manually type the address and look for the ABI?

@0x33dm
Copy link
Author

0x33dm commented May 22, 2024

Can you share the contract address and ABI so someone that helps doesn't need to manually type the address and look for the ABI?

I believe deploying this contract on a test net / local network would be a little to complicated, so would be better to write a simple contract with a parameter that triggers the failed assert.

@ajeetgill
Copy link

I tried to make a new reproductionable example on https://new.wagmi.sh/ but no matter what I do the page is rendered blank.

Tried the latest OPERA and latest CHROME both ended up with a blank page.

This is a silly thing but try using VPN, or different internet connections as well.
Been in similar situation, turned out the ISP/WiFi was blocking those APIs (try ping-ing).

@0x33dm
Copy link
Author

0x33dm commented Jun 1, 2024

This is a silly thing but try using VPN, or different internet connections as well. Been in similar situation, turned out the ISP/WiFi was blocking those APIs (try ping-ing).

I could understand how ad-blockers could potentially block something. and i tried on incognito browser..

would be really weird if it was my ISP blocking something

@tmm
Copy link
Member

tmm commented Jul 9, 2024

Wasn't able to reproduce. TanStack Query has aggressive caching defaults so likely something you need to tune there.

@tmm tmm closed this as completed Jul 9, 2024
@tmm tmm reopened this Jul 9, 2024
@tmm tmm closed this as not planned Won't fix, can't repro, duplicate, stale Jul 9, 2024
@tmm
Copy link
Member

tmm commented Jul 9, 2024

With refetchOnWindowFocus switch on, can trigger refetching automatically when window refocuses.

useReadContract({
  // ...
  query: {
    refetchOnWindowFocus: true,
  },
})
001102_Firefox_2024-07-09-15.37.45.mp4

@0x33dm
Copy link
Author

0x33dm commented Jul 9, 2024

With refetchOnWindowFocus switch on, can trigger refetching automatically when window refocuses.

useReadContract({
  // ...
  query: {
    refetchOnWindowFocus: true,
  },
})

sure, I know that, but I don't understand how this is related to my issue.

Anyway, I understand that it's probably possible to overwrite this behaviour, for instance, by manually checking if it reverts, and if it does, then setting the cache to an empty state of some sort directly via RQuery API instead of wagmi.

I'm just wondering if that shouldn't be wagmi default behaviour, to overwrite the cache with some variation of "undefined" instead of having the previous contract response.

@tmm
Copy link
Member

tmm commented Jul 9, 2024

Can you provide a GitHub repo with a minimal reproduction (with clear steps) that you are experiencing this with? (You can use pnpm create wagmi to kickstart the repo.)

We might be talking about different things so want to make sure we are aligned before I spend anymore time on this. Wagmi doesn't do anything special other than pass through options to Viem via TanStack Query.

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

No branches or pull requests

3 participants