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

Migrate bn.js to bignumber #1542

Closed
arboleya opened this issue Dec 18, 2023 · 10 comments
Closed

Migrate bn.js to bignumber #1542

arboleya opened this issue Dec 18, 2023 · 10 comments
Assignees
Labels
feat Issue is a feature

Comments

@arboleya
Copy link
Member

arboleya commented Dec 18, 2023

Suggestions were made to replace one library with the other.

Ideally, we should have a pros and cons list before deciding.

Note

@arboleya arboleya added the feat Issue is a feature label Dec 18, 2023
@nedsalk
Copy link
Contributor

nedsalk commented Dec 19, 2023

Based on this discussion and ethers v6 using native bigint in their maths "package" (see here), it seems like ethers got rid of big-number libraries altogether and went completely in favor of native bigint.

@LuizAsFight
Copy link
Contributor

just to add more context: #280 #468

it's interesting to take a look at those to understand why bn was chosen at first place and what problems it solves

@nedsalk
Copy link
Contributor

nedsalk commented Jan 2, 2024

Related to the JSON.stringify, we could fix it by defining our own BigInt.prototype.toJSON (based on this comment):
image

It would solve the issue across the board. Although messing with the prototype is risky as it can cause collisions ... what's the chance that two libraries would be adding this same method? Also, as the comment I linked to above mentioned, MDN also recommends it, although in the image I'm serializing into hex, not a stringified number.

BN doesn't add the 0x prefix when stringifying so we could get rid of it here as well.

@LuizAsFight
Copy link
Contributor

Related to the JSON.stringify, we could fix it by defining our own BigInt.prototype.toJSON (based on this comment): image

It would solve the issue across the board. Although messing with the prototype is risky as it can cause collisions ... what's the chance that two libraries would be adding this same method? Also, as the comment I linked to above mentioned, MDN also recommends it, although in the image I'm serializing into hex, not a stringified number.

when you stringify again, it's not a BigInt anymore

@nedsalk nedsalk self-assigned this Jan 12, 2024
@nedsalk
Copy link
Contributor

nedsalk commented Jan 17, 2024

when you stringify again, it's not a BigInt anymore

Stringifying BN has the same behavior and we lose context that it was a BN before.

@danielbate
Copy link
Contributor

danielbate commented Jan 18, 2024

I was not there when BN was chosen, however from my understanding it solved problems for developers trying to build for environments where a native BigInt was not yet supported. As the ethers discussion reads (who migrated from BN to BigInt in v6), Node v12 was EOL in April 2022 (did not support BigInt) and all later versions of Node do, and all modern browsers do. Therefore the case for not using it for that reason is now pretty void.

For our own purposes, I believe this has come from reported inaccuracies when using BN for calculations (can't find anything to link but I believe @Torres-ssf reported this?). Additionally, there is an opportunity to reduce the SDK size through the removal of BN to a natively supported type, as @nedsalk is investigating in #1592.

I support @nedsalk implementation of BigInt.Prototype.toJson as also being the recommended implementation from MDN. I think using stringify and still expecting to always have maintained types is dangerous.

@nedsalk
Copy link
Contributor

nedsalk commented Jan 18, 2024

After taking a look at it with @danielbate, replacing BN with native bigint seems like a ton of relatively easy work, but it'll be a breaking change with all our consumers and we have to make that known.

The benefits, as mentioned by @danielbate above, are:

Besides that, we've already got a precedent for it with ethers so it wouldn't come as a big shock to anybody.

The cons at this moment are:

  • A breaking change for all ts-sdk consumers, most notably the Fuel's frontend team

@FuelLabs/frontend @FuelLabs/sdk-ts It would be great to get additional input from everyone.

@nedsalk
Copy link
Contributor

nedsalk commented Feb 22, 2024

As of the time of writing, there is no consensus in the team for moving forward with this issue so I'm removing my assignment from it. I won't be closing it as it's a valid issue and something that should probably be done in the future for package size benefits.

@nedsalk nedsalk removed their assignment Feb 22, 2024
@nedsalk
Copy link
Contributor

nedsalk commented Mar 14, 2024

Related to package size reduction, by doing an in-depth analysis of bn.js we could maybe exclude its unnecessary dependencies from our build via rollup and squeeze out some kilobytes that way. bn.js itself isn't tree-shakeable.

@danielbate
Copy link
Contributor

Closing this for now. Contextually tree-shaking is being discussed in #1592 so that can be resolved there. The proposal that this issue makes we have not got a consensus on so therefore doesn't seem right for us at the moment.

@danielbate danielbate closed this as not planned Won't fix, can't repro, duplicate, stale Jul 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat Issue is a feature
Projects
None yet
Development

No branches or pull requests

4 participants