Skip to content

Conversation

@alexandre-abrioux
Copy link
Contributor

@alexandre-abrioux alexandre-abrioux commented Apr 17, 2025

Description of the changes

The goal of this PR is to make it easier for builders to use TheGraph Network for payment detection with their own API key.

Summary by CodeRabbit

  • New Features

    • Added support for TheGraph Explorer as a new subgraph hosting option, including API key handling.
    • Enhanced network selection logic for subgraph endpoints, with improved modularity and clarity.
  • Tests

    • Introduced new tests to verify correct URL generation for various networks and configurations, including TheGraph Explorer support.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Apr 17, 2025

"""

Walkthrough

The changes introduce support for TheGraph Explorer as a new subgraph endpoint option in the payment detection module. This involves adding new constants, updating type definitions to include an optional API key for TheGraph Explorer, and refactoring the logic for selecting the appropriate subgraph URL into a new function. The main client creation function is simplified to use this new URL resolution function. Additionally, comprehensive tests are added to verify correct URL generation for various networks and configurations, including cases with and without TheGraph Explorer API keys.

Changes

File(s) Change Summary
packages/payment-detection/src/thegraph/client.ts Added support for TheGraph Explorer endpoint, new URL template and subgraph ID mappings, extended client options with API key, centralized URL selection logic, and simplified client instantiation.
packages/payment-detection/test/thegraph/client.test.ts Added tests for defaultGetTheGraphClientUrl verifying URL construction for multiple networks and configurations, including TheGraph Explorer API key scenarios.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant ClientModule as TheGraph Client Module

    Caller->>ClientModule: defaultGetTheGraphClient(network, options)
    ClientModule->>ClientModule: defaultGetTheGraphClientUrl(network, options)
    alt Network supported by TheGraph Explorer and API key provided
        ClientModule->>ClientModule: Construct Explorer URL with API key
    else Network supported by Alchemy
        ClientModule->>ClientModule: Construct Alchemy URL
    else Network is special (e.g., mantle, core)
        ClientModule->>ClientModule: Construct special network URL
    else
        ClientModule->>ClientModule: Construct Studio URL
    end
    ClientModule->>Caller: Return instantiated client or undefined
Loading

Possibly related PRs

  • RequestNetwork/requestNetwork#1449: Also modifies the URL selection logic for TheGraph clients, but focuses on expanding supported networks rather than introducing new endpoint types.

Suggested reviewers

  • rodrigopavezi
  • sstefdev
    """

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Lite

📥 Commits

Reviewing files that changed from the base of the PR and between f09c643 and da33c4a.

📒 Files selected for processing (1)
  • packages/payment-detection/src/thegraph/client.ts (5 hunks)
🔇 Additional comments (7)
packages/payment-detection/src/thegraph/client.ts (7)

12-14: Well-implemented TheGraph Explorer URL template

Good use of a template string with placeholders for the API key and subgraph ID. This makes it clear how the URL is structured and which parameters need to be replaced.


27-38: Clear enumeration of Alchemy-supported chains

This explicit array of supported chains for Alchemy is a good approach. It makes it easier to maintain and understand which chains are supported by the Alchemy endpoint.


40-58: Good implementation of Explorer subgraph ID mapping

Using Partial<Record<ChainName, string>> is a good choice for type safety while allowing for potentially undefined values. The mapping is comprehensive and well-organized.


83-84: Clear addition of TheGraph Explorer API key option

Good job adding the API key option with a descriptive comment that explains its purpose.


96-102: Proper extraction of client options

The destructuring approach here is clean and the explicit comment about ignoring the API key makes the intention clear. Good practice to use the ESLint disable comment for the unused variable.


137-172: Well-designed URL resolution function

The defaultGetTheGraphClientUrl function has a clear and logical structure:

  1. Handles chain normalization (particularly for Near chains)
  2. Extracts necessary parameters upfront
  3. Builds different URL options
  4. Determines which endpoint to use with clear conditionals
  5. Handles special cases via switch statement

The implementation properly prioritizes endpoints and handles edge cases appropriately.


178-181: Clean simplification of the client creation function

The refactored defaultGetTheGraphClient function is now more concise and focused on client creation, with URL resolution delegated to the specialized function. The early return pattern for undefined URLs is a good practice.


🪧 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 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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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
Contributor

@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

🧹 Nitpick comments (2)
packages/payment-detection/src/thegraph/client.ts (1)

40-58: Consider documenting subgraph IDs maintenance process

The mapping provides necessary subgraph IDs for TheGraph Explorer, but lacks documentation about how these IDs are determined or maintained. These IDs appear to be static identifiers, but if they change in the future, it would be helpful to know the process for updating them.

Consider adding a comment that explains:

  1. Where these IDs come from
  2. How stable these IDs are expected to be
  3. How/when they should be updated if needed
packages/payment-detection/test/thegraph/client.test.ts (1)

3-46: Comprehensive test coverage with room for improvement

The tests cover the main use cases for different networks and configurations, providing good verification of the URL construction logic. However, there are a few scenarios that should be tested for completeness:

  1. The 'private' network case which should return undefined
  2. The case where an API key is provided but the network has no subgraph ID
  3. Testing for 'core' network which has a special URL

Consider adding these additional test cases for complete coverage:

it('should return undefined for private network', () => {
  const url = defaultGetTheGraphClientUrl('private');
  expect(url).toBeUndefined();
});

it('should fallback to non-Explorer URL when network has no subgraph ID', () => {
  // Assuming a network that's not in THE_GRAPH_EXPLORER_SUBGRAPH_ID but in THE_GRAPH_ALCHEMY_CHAINS
  const url = defaultGetTheGraphClientUrl('unsupported-network', { theGraphExplorerApiKey: 'test' });
  expect(url).toBe(
    'https://api.studio.thegraph.com/query/67444/request-payments-unsupported-network/version/latest'
  );
});

it('should build the correct URL for Core', () => {
  const url = defaultGetTheGraphClientUrl('core');
  expect(url).toBe(
    'https://thegraph.coredao.org/subgraphs/name/requestnetwork/request-payments-core'
  );
});
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4597d37 and f413833.

📒 Files selected for processing (2)
  • packages/payment-detection/src/thegraph/client.ts (5 hunks)
  • packages/payment-detection/test/thegraph/client.test.ts (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/payment-detection/test/thegraph/client.test.ts (1)
packages/payment-detection/src/thegraph/client.ts (1)
  • defaultGetTheGraphClientUrl (137-169)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build-and-test
🔇 Additional comments (5)
packages/payment-detection/src/thegraph/client.ts (5)

12-14: Well-structured URL template for TheGraph Explorer

The addition of this constant follows the existing pattern for URL templates and clearly defines the structure for TheGraph Explorer endpoints.


27-38: Explicit Alchemy chains enumeration improves maintainability

Defining supported Alchemy chains as a constant array makes the code more maintainable. If Alchemy adds support for more chains, we'll only need to update this list.


83-84: Clear documentation for new API key option

The added option is well-documented with a helpful comment that explains its purpose.


96-102: Properly excludes API key from client options

The updated destructuring correctly extracts and excludes the API key from client options, with appropriate comments explaining why the variable is unused.


171-179: Simplified client creation logic

The refactored function is more concise and easier to understand by leveraging the new URL resolution function. The early return for unsupported networks is also a good practice.

Copy link
Contributor

@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: 0

♻️ Duplicate comments (1)
packages/payment-detection/src/thegraph/client.ts (1)

153-153: Good implementation of handling missing subgraph IDs

This line properly addresses the previous review comment by ensuring TheGraph Explorer is only used when both an API key and a valid subgraph ID are available.

🧹 Nitpick comments (1)
packages/payment-detection/src/thegraph/client.ts (1)

137-172: Consider adding comments to explain the network normalization

The URL resolution function is well-structured with clear prioritization logic. However, the reasoning behind replacing 'aurora' with 'near' in the network name could use some explanation.

+ // Aurora uses the same subgraph as Near, so we normalize the network name
  const filteredNetwork = network.replace('aurora', 'near');
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Lite

📥 Commits

Reviewing files that changed from the base of the PR and between f413833 and f09c643.

📒 Files selected for processing (1)
  • packages/payment-detection/src/thegraph/client.ts (5 hunks)
🔇 Additional comments (5)
packages/payment-detection/src/thegraph/client.ts (5)

12-14: Great addition of TheGraph Explorer support

Adding support for TheGraph Explorer with API key integration is a valuable feature that gives developers more flexibility in their deployment options.


27-38: Well-structured constants for network configuration

The clear enumeration of Alchemy-supported chains and TheGraph Explorer subgraph IDs improves code readability and maintainability. This approach makes it easy to update supported networks in the future.

Also applies to: 40-58


83-84: Good type extension for the new API key option

The type definition is properly extended with clear documentation. This ensures type safety when users provide the new optional parameter.


96-102: Proper handling of the API key in options extraction

The code correctly extracts and excludes the API key from client options, which is a good practice since it's only needed for URL construction.


178-181: Good refactoring of client creation

The client creation function is now more concise and follows a clear separation of concerns by delegating URL resolution to a dedicated function.

Copy link
Member

@MantisClone MantisClone left a comment

Choose a reason for hiding this comment

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

Well done 👍

@alexandre-abrioux alexandre-abrioux enabled auto-merge (squash) April 23, 2025 22:28
@alexandre-abrioux alexandre-abrioux merged commit 0cb5dfe into master Apr 23, 2025
10 checks passed
@alexandre-abrioux alexandre-abrioux deleted the thegraph-api-key branch April 23, 2025 23:30
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.

3 participants