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

fix(torii/graphql): filter out tokens with zero balance #2769

Merged
merged 2 commits into from
Dec 5, 2024

Conversation

lambda-0x
Copy link
Contributor

@lambda-0x lambda-0x commented Dec 5, 2024

Summary by CodeRabbit

  • New Features

    • Enhanced token balance retrieval by filtering out zero balances, ensuring users only see relevant data.
  • Bug Fixes

    • Improved the accuracy of token balance data displayed to users.

Copy link

coderabbitai bot commented Dec 5, 2024

Walkthrough

Ohayo, sensei! This pull request modifies the fetch_token_balances function within the ErcBalanceObject implementation. A new condition has been added to the SQL query to filter out zero token balances, ensuring that only non-zero balances are retrieved. The overall structure of the query remains intact, with adjustments made to accommodate this filtering logic. Pagination and cursor-based navigation mechanisms are unchanged, and there are no alterations to the signatures of exported or public entities.

Changes

File Path Change Summary
crates/torii/graphql/src/object/erc/token_balance.rs Modified fetch_token_balances to include a condition that filters out zero token balances. Adjusted SQL query logic while maintaining overall structure.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant ErcBalanceObject
    participant Database

    Client->>ErcBalanceObject: Request token balances
    ErcBalanceObject->>Database: Execute SQL query with non-zero balance filter
    Database-->>ErcBalanceObject: Return non-zero token balances
    ErcBalanceObject-->>Client: Send token balances
Loading

Possibly related PRs


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 683d381 and 72018da.

📒 Files selected for processing (1)
  • crates/torii/graphql/src/object/erc/token_balance.rs (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • crates/torii/graphql/src/object/erc/token_balance.rs

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

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

🧹 Outside diff range and nitpick comments (1)
crates/torii/graphql/src/object/erc/token_balance.rs (1)

109-111: Ohayo sensei! Consider extracting the zero balance constant.

The implementation correctly filters out zero balances, but the hardcoded hex string could be moved to a constant for better maintainability and reusability.

Consider applying this change:

+ const ZERO_BALANCE: &str = "0x0000000000000000000000000000000000000000000000000000000000000000";

 // In fetch_token_balances:
     let mut conditions = vec![
         "(b.account_address = ?)".to_string(),
         "(t.metadata IS NULL OR length(t.metadata) > 0)".to_string(),
-        "b.balance != '0x0000000000000000000000000000000000000000000000000000000000000000'"
+        format!("b.balance != '{}'", ZERO_BALANCE)
             .to_string(),
     ];
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between ffc9259 and f4073bc.

📒 Files selected for processing (1)
  • crates/torii/graphql/src/object/erc/token_balance.rs (1 hunks)
🔇 Additional comments (1)
crates/torii/graphql/src/object/erc/token_balance.rs (1)

109-111: LGTM! Pagination handling is correct.

The zero balance filter is properly integrated with the existing WHERE clause and pagination mechanism. The implementation correctly:

  • Maintains cursor-based navigation
  • Updates total_count to exclude zero balances
  • Preserves the query structure

Let's verify the query construction:

✅ Verification successful

Ohayo sensei! The pagination implementation is correctly handling the zero balance filter! ✨

The code shows that:

  • The zero balance filter is properly added to the conditions vector along with other base conditions
  • All conditions are joined with AND in the WHERE clause
  • The cursor-based pagination logic (after/before) correctly adds its conditions after the zero balance filter
  • The query construction maintains proper order of operations

This implementation ensures that:

  1. Zero balances are filtered out consistently
  2. Pagination cursors work correctly with the filter
  3. The WHERE clause is properly constructed with all conditions
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the SQL query construction and pagination
# Test: Check if the zero balance filter is properly combined with other conditions

rg -A 5 "WHERE.*AND.*balance.*!=" crates/torii/graphql/src/object/erc/token_balance.rs

Length of output: 88


Script:

#!/bin/bash
# Let's check the query construction more broadly
rg -B 5 -A 10 "WHERE" crates/torii/graphql/src/object/erc/token_balance.rs

# Also check how conditions are built
rg -B 3 -A 3 "conditions" crates/torii/graphql/src/object/erc/token_balance.rs

Length of output: 1702


Script:

#!/bin/bash
# Let's check the full token balance query construction
rg -C 20 "let mut conditions = vec!" crates/torii/graphql/src/object/erc/token_balance.rs

Length of output: 1526

Copy link

codecov bot commented Dec 5, 2024

Codecov Report

Attention: Patch coverage is 0% with 7 lines in your changes missing coverage. Please review.

Project coverage is 55.48%. Comparing base (90e2edd) to head (72018da).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...ates/torii/graphql/src/object/erc/token_balance.rs 0.00% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2769      +/-   ##
==========================================
- Coverage   55.49%   55.48%   -0.01%     
==========================================
  Files         434      434              
  Lines       54312    54318       +6     
==========================================
  Hits        30138    30138              
- Misses      24174    24180       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@glihm
Copy link
Collaborator

glihm commented Dec 5, 2024

@lambda-0x we're sure the value is always padded this way, correct?

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.

2 participants