Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

JSON RPC API using (u)int64 whereas JS can only handle int53 #1116

Closed
linuskendall opened this issue Jan 20, 2022 · 9 comments
Closed

JSON RPC API using (u)int64 whereas JS can only handle int53 #1116

linuskendall opened this issue Jan 20, 2022 · 9 comments
Assignees
Labels
enhancement New feature or request

Comments

@linuskendall
Copy link

linuskendall commented Jan 20, 2022

Problem

The JSON API uses int64 for fields like lamports. However, the maximum safe integer in browsers is 2^53-1

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER

Proposed Solution

Make lamports a string in the HTTP/RPC API and parse with BigInt on the web3.js client side?

@linuskendall linuskendall changed the title JSON RPC using int64 whereas JS can only handle int53 JSON RPC API using (u)int64 whereas JS can only handle int53 Jan 20, 2022
@vpontis
Copy link
Contributor

vpontis commented Jan 20, 2022

@linuskendall thanks for opening this!

Yea, we've switched to passing around lamports and token amounts as strings internally and then parsing with BN.js. But we still get number from most of the JSON RPC endpoints.

While it's much nicer to get a number, I fear this will lead to some bad bugs...

@mvines
Copy link
Contributor

mvines commented Jan 20, 2022

We've switch over to strings in a couple of the methods already, and marked the Number field as deprecated. Example: https://docs.solana.com/developing/clients/jsonrpc-api#token-balances-structure

It would be helpful to enumerate the remaining problematic methods to make this issue easier to pick up by somebody willing to clean them up too!

@vpontis
Copy link
Contributor

vpontis commented Jan 20, 2022

@mvines The biggest offender right now is lamports which is consistently a number in the web3.js API.

@emdoyle
Copy link

emdoyle commented Feb 26, 2022

@mvines This also occurs when using getBalance, and if I understand correctly it's because the value is coerced by superstruct into a number. Took me a long time to realize this was the reason balances were off by ~40 lamports in my tests client-side :/

This also happens with getAccountInfo (lamports as @vpontis mentioned) so I don't think there is a safe way to retrieve native SOL balances in web3.js at the moment unless I'm missing some other method.

@mvines mvines added the good first issue Good for newcomers label Mar 15, 2022
@steveluscher
Copy link
Contributor

The use of JS numbers is terrifying, and has been added as a principle in #1111 as we consider a rewrite of web3.js.

@steveluscher
Copy link
Contributor

I removed the ‘good first issue’ tag. I think this is rather extremely complicated, and will require a months' (years'?) long deprecation strategy for the old fields, and possibly a version bump of the RPC itself.

Some of the groundwork is already being laid in the new web3 rewrite through the use of bigint at the outer edge of the interface. This should let people who program apps using the new RPC to seamlessly upgrade to the new RPC methods (when we cut them over) without changing their app's code or upgrading their other dependencies.

@github-actions github-actions bot added the stale label May 7, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale May 15, 2024
@steveluscher steveluscher added the do-not-close Add this tag to exempt an issue/PR from being closed by the stalebot label May 15, 2024
@steveluscher steveluscher reopened this May 15, 2024
@github-actions github-actions bot removed the stale label May 16, 2024
@steveluscher
Copy link
Contributor

@lorisleiva has a devilishly clever solution for this, coming to @solana/web3.js 2.0 (see #3196).

@steveluscher steveluscher added enhancement New feature or request and removed do-not-close Add this tag to exempt an issue/PR from being closed by the stalebot labels Oct 21, 2024
@steveluscher
Copy link
Contributor

I can't believe this is done.

Copy link
Contributor

Because there has been no activity on this issue for 7 days since it was closed, it has been automatically locked. Please open a new issue if it requires a follow up.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants