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

refactor: use NetworkID in faucet calculations #2950

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

mvadari
Copy link
Collaborator

@mvadari mvadari commented Mar 28, 2025

High Level Overview of Change

This PR refactors the faucet code to use the NetworkID to determine which faucet to use.

Context of Change

Resolves #2701

Previously, the code was making assumptions about what the URL would be, which isn't very robust.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • Refactor (non-breaking change that only restructures code)

Did you update HISTORY.md?

  • Yes

Test Plan

CI still passes.

Copy link

coderabbitai bot commented Mar 28, 2025

Walkthrough

This pull request updates the unreleased section in the HISTORY.md file by clarifying the OracleSet transaction fix and adding an entry for improved faucet support. In the wallet module, it introduces a new constant mapping network IDs to faucet networks, renames constants and functions (e.g., changing FaucetNetworkPaths to faucetNetworkPaths and getDefaultFaucetPath to getFaucetPath), and updates related import statements. The changes also streamline error handling and test cases, ensuring that the faucet network is derived from the client's network ID as outlined in issue [#2701].

Changes

File(s) Change Summary
packages/xrpl/HISTORY.md Updated "Unreleased" section: clarified OracleSet transaction fix and added "Better faucet support" entry.
packages/xrpl/src/Wallet/defaultFaucets.ts, packages/xrpl/src/Wallet/fundWallet.ts, packages/xrpl/test/wallet/fundWallet.test.ts Added faucetNetworkIDs constant; renamed FaucetNetworkPathsfaucetNetworkPaths and getDefaultFaucetPathgetFaucetPath; updated error handling, import statements, and tests accordingly.

Assessment against linked issues

Objective Addressed Explanation
Use Network ID to determine the correct faucet to use [#2701]

Suggested reviewers

  • justinr1234
  • anissa-ripple

Poem

I’m a little rabbit, hopping through the code,
Mapping network IDs down each digital road.
Constants renamed with a gentle twist,
A clearer fix I can’t resist.
With whiskers twitching in a code-filled spree,
Cheers to changes, as joyous as can be!
🐇✨


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 194ff00 and 6fc5b04.

📒 Files selected for processing (1)
  • packages/xrpl/src/Wallet/defaultFaucets.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/xrpl/src/Wallet/defaultFaucets.ts
⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: integration (18.x)
  • GitHub Check: integration (22.x)
  • GitHub Check: snippets (20.x)
  • GitHub Check: snippets (18.x)
  • GitHub Check: integration (20.x)
  • GitHub Check: snippets (22.x)
  • GitHub Check: browser (18.x)

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, 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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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 for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🔭 Outside diff range comments (1)
packages/xrpl/test/wallet/fundWallet.test.ts (1)

65-67: ⚠️ Potential issue

Test needs fixing to pass pipeline

This test is failing in the pipeline. The error indicates that the test expects a function to throw an error, but it's not throwing. With the new NetworkID check, the test logic may need to be updated.

To fix the test, make sure the client doesn't have a networkID set before testing:

it('throws if not connected to a known faucet host', function () {
  // Info: setupClient.setup creates a connection to 'localhost'
+ // @ts-expect-error Intentionally modifying private data for test
+ testContext.client.networkID = undefined
  assert.throws(() => getFaucetHost(testContext.client))
})
🧰 Tools
🪛 GitHub Actions: Node.js CI

[error] 66-66: Expected value to strictly be equal to: undefined. Received: undefined. expected [Function] to throw an error.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 066e670 and dcf566b.

📒 Files selected for processing (4)
  • packages/xrpl/HISTORY.md (1 hunks)
  • packages/xrpl/src/Wallet/defaultFaucets.ts (3 hunks)
  • packages/xrpl/src/Wallet/fundWallet.ts (2 hunks)
  • packages/xrpl/test/wallet/fundWallet.test.ts (2 hunks)
🧰 Additional context used
🧬 Code Definitions (2)
packages/xrpl/src/Wallet/fundWallet.ts (1)
packages/xrpl/src/Wallet/defaultFaucets.ts (1)
  • getFaucetPath (64-69)
packages/xrpl/test/wallet/fundWallet.test.ts (1)
packages/xrpl/src/Wallet/defaultFaucets.ts (3)
  • faucetNetworkPaths (19-22)
  • getFaucetPath (64-69)
  • getFaucetHost (36-55)
🪛 GitHub Actions: Node.js CI
packages/xrpl/test/wallet/fundWallet.test.ts

[error] 28-28: Expected value to strictly be equal to: 'faucet.devnet.rippletest.net'. Received: 'faucet.altnet.rippletest.net'.


[error] 66-66: Expected value to strictly be equal to: undefined. Received: undefined. expected [Function] to throw an error.

🔇 Additional comments (11)
packages/xrpl/src/Wallet/defaultFaucets.ts (5)

19-22: Good variable naming improvement

Renamed constant from FaucetNetworkPaths to faucetNetworkPaths to follow JavaScript naming conventions for non-class variables. This is consistent with standard naming practices.


24-27: Good improvement: Using NetworkID for faucet determination

Added a new faucetNetworkIDs Map that maps network IDs to faucet networks. This addresses issue #2701 by using NetworkID instead of relying on URL assumptions, making the code more robust.


39-47: Improved error handling and client NetworkID support

The code now properly checks for the client's networkID before attempting URL-based detection, making the faucet selection process more reliable. This change properly implements the fix requested in issue #2701.


49-52: Good backward compatibility

Maintained the URL-based checks as a fallback mechanism, ensuring backward compatibility with existing client code that may not provide a networkID.


64-69: Improved function naming

Renamed getDefaultFaucetPath to getFaucetPath for better clarity and consistency with other getter functions.

packages/xrpl/src/Wallet/fundWallet.ts (2)

6-6: Updated import statement correctly

The import statement has been updated to use the renamed function getFaucetPath instead of getDefaultFaucetPath, maintaining consistency with the changes in defaultFaucets.ts.


147-147: Function call updated correctly

The function call has been updated to use getFaucetPath instead of getDefaultFaucetPath, maintaining consistency with the renaming in defaultFaucets.ts.

packages/xrpl/test/wallet/fundWallet.test.ts (2)

5-8: Updated imports correctly

The import statements have been properly updated to use the renamed entities faucetNetworkPaths and getFaucetPath.


61-61: Updated function call correctly

The function call has been updated to use getFaucetPath instead of getDefaultFaucetPath.

packages/xrpl/HISTORY.md (2)

8-8: Improved documentation clarity

Updated the description to clarify that the OracleSet transaction change is a fix rather than a new feature, which better reflects the nature of the change.


11-11: Added entry for faucet support improvements

Added a history entry for "Better faucet support" which aligns with the changes made to use NetworkID for faucet determination rather than URL assumptions.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between dcf566b and fa0d8ee.

📒 Files selected for processing (3)
  • packages/xrpl/src/Wallet/defaultFaucets.ts (3 hunks)
  • packages/xrpl/src/Wallet/fundWallet.ts (3 hunks)
  • packages/xrpl/test/wallet/fundWallet.test.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/xrpl/test/wallet/fundWallet.test.ts
  • packages/xrpl/src/Wallet/fundWallet.ts
🧰 Additional context used
🪛 GitHub Actions: Node.js CI
packages/xrpl/src/Wallet/defaultFaucets.ts

[warning] 37-37: Unexpected console statement no-console

🔇 Additional comments (8)
packages/xrpl/src/Wallet/defaultFaucets.ts (8)

19-19: Good rename to follow camelCase conventions.

The constant has been appropriately renamed from FaucetNetworkPaths to faucetNetworkPaths to follow TypeScript naming conventions (camelCase for variables and constants), improving code consistency.


24-27: Good addition of NetworkID to FaucetNetwork mapping.

This new mapping enables looking up the appropriate faucet network based on network ID, which aligns with the PR objective of refactoring faucet code to use NetworkID instead of relying on URL assumptions. This will make the code more robust.


38-42: Improved error message with clearer context.

The error message has been updated to provide better context about what information is required to create a faucet URL.


44-47: Good implementation of NetworkID-based faucet lookup.

This is the core of the refactoring, looking up the faucet network using the client's networkID. This approach is more reliable than inferring from URLs as mentioned in the PR objective (issue #2701).


48-51: Good explicit handling for mainnet.

Adding a specific check for network ID 0 (mainnet) with a clear error message improves the API's usability by explicitly informing users that faucets aren't available for mainnet.


61-62: Updated documentation to match renamed constant.

The JSDoc has been correctly updated to reference the renamed constant faucetNetworkPaths.


63-63: Improved function name.

The function has been renamed from getDefaultFaucetPath to getFaucetPath, which is a better name as it's more concise and doesn't include the redundant "Default" qualifier.


67-68: Updated code to use the renamed constant.

The code now correctly references the renamed faucetNetworkPaths constant.

@mvadari mvadari requested review from khancode and ckeshava March 28, 2025 18:38
throw new XRPLFaucetError(
'Cannot fund an account on an issuing chain. Accounts must be created via the bridge.',
'Cannot create faucet URL without networkID or the faucet_host information',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'Cannot create faucet URL without networkID or the faucet_host information',
'Cannot create faucet URL without networkID',

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you want to remove the extra clause? If someone provides the faucet_host, the function doesn't need the network ID.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How will faucet_host information help? This method will throw an error if client.networkID == null

Comment on lines +203 to +206
`Unable to fund address with faucet after waiting ${
INTERVAL_SECONDS * MAX_ATTEMPTS
} seconds`,
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error message is not relevant for the processSuccessfulResponse method. I do not see any retry-mechanism (or) back-off algorithm used here.

However, it is relevant for getUpdatedBalance method. These two error messages appear to have been swapped.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the errors are correct. The faucet can also be used to add funds to an existing wallet, in which case the error should fall through to here, not throw in getUpdatedBalance.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you point me to the code where we do the waiting (or) retry mechanism? Most of the usages of the faucet have been to generate a new wallet.

Even while funding an existing wallet, the operation is tried exactly once. I don't see the retry-operation.

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

Successfully merging this pull request may close these issues.

Use Network ID to determine the correct faucet to use
2 participants