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

BigInt value can't be serialized in JSON #3855

Closed
1 task done
tien opened this issue Apr 25, 2024 · 10 comments
Closed
1 task done

BigInt value can't be serialized in JSON #3855

tien opened this issue Apr 25, 2024 · 10 comments
Labels
Needs Reproduction Misc: Needs Reproduction

Comments

@tien
Copy link

tien commented Apr 25, 2024

Describe the bug

Using readContractQueryOptions with BigInt in args will result in a query key containing BigInt, crashing react-query serialization.

Link to Minimal Reproducible Example

No response

Steps To Reproduce

No response

Wagmi Version

2.5.20

Viem Version

2.9.26

TypeScript Version

No response

Check existing issues

Anything else?

No response

@tmm tmm added the Needs Reproduction Misc: Needs Reproduction label Apr 25, 2024
Copy link
Contributor

Hello @tien. Please provide a minimal reproduction using new.wagmi.sh for runtime issues or TypeScript Playground for type issues. Issues marked with "needs reproduction" will be closed if they have no activity within 3 days.

@tmm
Copy link
Member

tmm commented Apr 25, 2024

If you are using TanStack Query with a persister, you can use the built-in deserialize and serialize utils.

import { createSyncStoragePersister } from '@tanstack/query-sync-storage-persister'
import { deserialize, serialize } from 'wagmi'

const persister = createSyncStoragePersister({
  serialize,
  storage: window.localStorage,
  deserialize,
})

@tmm tmm closed this as not planned Won't fix, can't repro, duplicate, stale Apr 25, 2024
@tien
Copy link
Author

tien commented Apr 26, 2024

@tmm I'm not persisting the data though, the query key them self contains BigInt values.

@tien
Copy link
Author

tien commented Apr 26, 2024

@tmm this can be easily reproduced by doing

import { readContractQueryOptions } from 'wagmi/query';

// Observe that the query key which will be used by `react-query` contains BigInt 0n
// which will throw a serialization error
console.log(
  readContractQueryOptions(
    {},
    {
      args: [0n],
    }
  )
);

@tmm
Copy link
Member

tmm commented Apr 26, 2024

This test works fine 67ac1eb

Either attach a minimal reproduction or break the test.

@tien
Copy link
Author

tien commented Apr 26, 2024

@tmm the test show that it's incorrect. See how the 0n bigint is included in the query key? This will break if you try to use it with react-query useQuery or useSuspenseQuery (as instructed in wagmi docs on how to use suspense)

@tien
Copy link
Author

tien commented Apr 26, 2024

As in doing this will lead to a crash, because react query can't handle bigint key

useSuspenseQuery(readContractQueryOptions(config, {
   args: [0n],
});

This is the approach to do suspense from wagmi docs

On how react-query doesn't support bigint query:
TanStack/query#2450
TanStack/query#3082

@tmm
Copy link
Member

tmm commented Apr 26, 2024

the test show that it's incorrect

You said the bug could be reproduced by doing this. The test reflects that.


Either you need to handle serialization yourself or you can import hashFn from wagmi/query (used internally):

import { hashFn } from 'wagmi/query'

useSuspenseQuery(readContractQueryOptions(config, {
  args: [0n],
  queryKeyHashFn: hashFn, // for bigint support
});

@tien
Copy link
Author

tien commented Apr 26, 2024

@tmm thanks for clarifying the manual serialisation part. That's what I'm doing right now.

But I didn't know that you are expected to do that, and that the lib expose a utility function for that. Maybe the docs should includes that as well.

Copy link
Contributor

This issue has been locked since it has been closed for more than 14 days.

If you found a concrete bug or regression related to it, please open a new bug report with a reproduction against the latest wagmi version. If you have any other comments you can create a new discussion.

@github-actions github-actions bot locked and limited conversation to collaborators May 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Needs Reproduction Misc: Needs Reproduction
Projects
None yet
Development

No branches or pull requests

2 participants