XRPLF / xrpl.js

A JavaScript/TypeScript API for interacting with the XRP Ledger in Node.js and the browser
https://xrpl.org/
1.21k stars 512 forks source link

Update UINT64 ctor to accept `bigint | string` types #2796

Open ckeshava opened 1 day ago

ckeshava commented 1 day ago

High Level Overview of Change

UINT64 type must support values in the range of [0, 18446744073709551615], both inclusive. Javascript's number type does not have a compatible range of values, due to double-precision floating point format.

If users specify a large input (say number(18446744073709551615), well-beyond the limits of number type) into a UINT64 type field, the xrpl.js SDK parses the input as 0 . No error is raised by xrpl.js , although the VSCode editor displays a red-squiggly warning about the loss-of-precision. We can prevent such unexpected surprises by enforcing a stricter type bigint.

This PR specifies the change in UINT64.from method. It also updates relevant Transaction fields to use bigint type, instead of number type. These transactions pertain to the PriceOracle and the XChainBridge amendments.

Context of Change

Type of Change

Did you update HISTORY.md?

Test Plan

Existing tests should suffice for validating the serialization/de-serialization of the UINT64 type fields.

coderabbitai[bot] commented 1 day ago

Walkthrough

The changes in this pull request focus on enhancing type safety and precision in the handling of numeric values across various interfaces and methods related to the UInt64 type and its interactions within the XRPL. Key modifications include restricting the accepted types in the UInt64.from method, updating various interfaces to utilize bigint instead of number, and refining validation logic to accommodate these changes. Additionally, the test cases have been updated to reflect these new type requirements, ensuring consistent handling of large integer values.

Changes

File Path Change Summary
packages/ripple-binary-codec/src/types/uint-64.ts Updated UInt64.from method signature to accept only UInt64, string, or bigint. Removed handling for number.
packages/ripple-binary-codec/test/binary-serializer.test.ts Updated tests for UInt64 serialization to enforce type checks for string and bigint. Added new tests for undefined fields.
packages/xrpl/src/models/common/index.ts Changed AssetPrice type from number | string to bigint | string.
packages/xrpl/src/models/ledger/XChainOwnedCreateAccountClaimID.ts Changed XChainAccountCreateCount type from number to bigint.
packages/xrpl/src/models/transactions/XChainAddAccountCreateAttestation.ts Updated XChainAccountCreateCount type to bigint | string and adjusted validation logic.
packages/xrpl/src/models/transactions/XChainAddClaimAttestation.ts Changed XChainClaimID type to bigint | string and updated validation logic.
packages/xrpl/src/models/transactions/XChainClaim.ts Updated XChainClaimID type to bigint | string and modified validation logic accordingly.
packages/xrpl/src/models/transactions/XChainCommit.ts Changed XChainClaimID type to bigint | string and updated validation logic.
packages/xrpl/src/models/transactions/common.ts Added isBigInt function for type checking of bigint.
packages/xrpl/src/models/transactions/oracleSet.ts Updated PriceData validation to use isBigInt for AssetPrice.
packages/xrpl/test/integration/requests/getAggregatePrice.test.ts Changed AssetPrice in tests from number to BigInt.
packages/xrpl/test/integration/transactions/oracleDelete.test.ts Updated AssetPrice in tests from number to BigInt.
packages/xrpl/test/integration/transactions/oracleSet.test.ts Changed AssetPrice in tests from number to BigInt.
packages/xrpl/test/integration/transactions/xchainAddAccountCreateAttestation.test.ts Updated XChainAccountCreateCount in tests from number to BigInt.
packages/xrpl/test/integration/transactions/xchainAddClaimAttestation.test.ts Changed XChainClaimID in tests from number to BigInt.
packages/xrpl/test/integration/transactions/xchainClaim.test.ts Updated XChainClaimID in tests from number to BigInt.
packages/xrpl/test/integration/transactions/xchainCommit.test.ts Changed XChainClaimID in tests from number to BigInt.
packages/xrpl/test/models/oracleSet.test.ts Updated AssetPrice in tests from number to BigInt.
packages/xrpl/tools/generateModels.js Updated type mapping for UINT64 from 'number | string' to 'string' and enhanced model generation logic.

Possibly related issues

🐰 In the meadow where numbers play,
BigInts hop and dance all day.
With types so strict, we leap with cheer,
Precision's here, no need to fear!
From number to bigint, we make the switch,
In our code, we found the perfect pitch! 🌼✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❀️ Share - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)
πŸͺ§ Tips ### Chat There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai): - Review comments: Directly reply to a review comment made by CodeRabbit. Example: - `I pushed a fix in commit , please review it.` - `Generate unit testing code for this file.` - `Open a follow-up GitHub issue for this discussion.` - Files and specific lines of code (under the "Files changed" tab): Tag `@coderabbitai` in a new review comment at the desired location with your query. Examples: - `@coderabbitai generate unit testing code for this file.` - `@coderabbitai modularize this function.` - PR comments: Tag `@coderabbitai` in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples: - `@coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.` - `@coderabbitai read src/utils.ts and generate unit testing code.` - `@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.` - `@coderabbitai help me debug CodeRabbit configuration file.` Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. ### CodeRabbit Commands (Invoked using PR comments) - `@coderabbitai pause` to pause the reviews on a PR. - `@coderabbitai resume` to resume the paused reviews. - `@coderabbitai review` to trigger an incremental review. This is useful when automatic reviews are disabled for the repository. - `@coderabbitai full review` to do a full review from scratch and review all the files again. - `@coderabbitai summary` to regenerate the summary of the PR. - `@coderabbitai resolve` resolve all the CodeRabbit review comments. - `@coderabbitai configuration` to show the current CodeRabbit configuration for the repository. - `@coderabbitai help` to get help. ### Other keywords and placeholders - Add `@coderabbitai ignore` anywhere in the PR description to prevent this PR from being reviewed. - Add `@coderabbitai summary` to generate the high-level summary at a specific location in the PR description. - Add `@coderabbitai` anywhere in the PR title to generate the title automatically. ### Documentation and Community - Visit our [Documentation](https://coderabbit.ai/docs) for detailed information on how to use CodeRabbit. - Join our [Discord Community](http://discord.gg/coderabbit) to get help, request features, and share feedback. - Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.