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-sqlite): erc721 tokens upsert do nothing #2986

Merged
merged 3 commits into from
Feb 5, 2025

Conversation

Larkooo
Copy link
Collaborator

@Larkooo Larkooo commented Feb 3, 2025

Summary by CodeRabbit

Release Notes:

  • New Features
    • Improved token registration process by fetching token name and symbol based on contract address.
  • Bug Fixes
    • Prevented duplicate asset registrations by adding a new ON CONFLICT DO NOTHING clause to the token insertion process.
    • Enhanced error handling to provide clearer user feedback during registrations.

Copy link

coderabbitai bot commented Feb 3, 2025

Ohayo, sensei!

Walkthrough

This change modifies the SQL queries related to ERC721 token operations in the database. The token details query now fetches the name and symbol based on the contract address instead of requiring a separate fetch operation. Additionally, the insert operation for token metadata has been updated to include an ON CONFLICT DO NOTHING clause, allowing for graceful handling of duplicate entries without raising errors. The overall structure and error handling of the token registration process remain consistent with prior implementations.

Changes

File Change Summary
crates/torii/.../executor/mod.rs - Updated RegisterErc721Token query to fetch token name and symbol using contract address.
crates/torii/.../executor/erc.rs - Modified insert query in handle_erc721_token_metadata to include ON CONFLICT DO NOTHING for handling duplicate token entries.

Suggested reviewers

  • glihm

Tip

🌐 Web search-backed reviews and chat
  • We have enabled web search-based reviews and chat for all users. This feature allows CodeRabbit to access the latest documentation and information on the web.
  • You can disable this feature by setting web_search: false in the knowledge_base settings.
  • Please share any feedback in the Discord discussion.

📜 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 4ee95eb and da558c3.

📒 Files selected for processing (1)
  • crates/torii/sqlite/src/executor/mod.rs (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • crates/torii/sqlite/src/executor/mod.rs
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: ensure-wasm
  • GitHub Check: clippy
  • GitHub Check: build
  • GitHub Check: docs

🪧 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. (Beta)
  • @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

🧹 Nitpick comments (1)
crates/torii/sqlite/src/executor/mod.rs (1)

634-639: Consider adding transaction isolation level, sensei!

While the query modification to use token ID is good, there's a potential race condition between the existence check and token registration. Consider using SERIALIZABLE isolation level or adding a unique constraint on the token ID.

-- Add unique constraint to prevent duplicate tokens
ALTER TABLE tokens ADD CONSTRAINT unique_token_id UNIQUE (id);
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between e823d42 and b7ac26d.

📒 Files selected for processing (1)
  • crates/torii/sqlite/src/executor/mod.rs (1 hunks)
🔇 Additional comments (1)
crates/torii/sqlite/src/executor/mod.rs (1)

624-632: Ohayo! Nice addition of token existence check, sensei!

The token existence check efficiently prevents duplicate registrations using COUNT(*) query with early return pattern.

@Larkooo Larkooo changed the title fix(torii-sqlite): erc721 tokens check both cache + db for write fix(torii-sqlite): erc721 tokens upsert do nothing Feb 4, 2025
@Larkooo Larkooo enabled auto-merge (squash) February 4, 2025 03:47
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

🧹 Nitpick comments (2)
crates/torii/sqlite/src/executor/erc.rs (2)

342-344: Ohayo! The ON CONFLICT DO NOTHING addition looks good sensei!

The change makes the token registration process more resilient by gracefully handling duplicate entries.

Consider adding debug logging when a conflict occurs to help with troubleshooting:

 let query = sqlx::query(
     "INSERT INTO tokens (id, contract_address, name, symbol, decimals, metadata) VALUES \
-     (?, ?, ?, ?, ?, ?) ON CONFLICT DO NOTHING",
+     (?, ?, ?, ?, ?, ?) ON CONFLICT DO NOTHING RETURNING (
+         CASE 
+             WHEN xmax::text::int > 0 THEN 1
+             ELSE 0
+         END
+     ) as skipped",
 )

Then check the result:

let result = query.fetch_one(&mut *self.transaction).await?;
let skipped: i32 = result.get("skipped");
if skipped > 0 {
    debug!(
        target: LOG_TARGET,
        token_id = %result.query.token_id,
        "Skipped duplicate token registration"
    );
}

342-344: Consider future-proofing with UPSERT sensei!

If there's a future requirement to update existing token metadata, you might want to consider using ON CONFLICT UPDATE instead. This would allow updating fields like name, symbol, or metadata when they change.

Here's an example of how it could look:

 let query = sqlx::query(
     "INSERT INTO tokens (id, contract_address, name, symbol, decimals, metadata) VALUES \
-     (?, ?, ?, ?, ?, ?) ON CONFLICT DO NOTHING",
+     (?, ?, ?, ?, ?, ?) ON CONFLICT (id) DO UPDATE SET
+         name = EXCLUDED.name,
+         symbol = EXCLUDED.symbol,
+         metadata = EXCLUDED.metadata
+     WHERE tokens.metadata IS NULL OR tokens.metadata = ''",
 )

This would update existing records only if they have empty metadata, preventing overwrites of valid data.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between b7ac26d and 4ee95eb.

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

Copy link

codecov bot commented Feb 4, 2025

Codecov Report

Attention: Patch coverage is 0% with 1 line in your changes missing coverage. Please review.

Project coverage is 56.97%. Comparing base (e823d42) to head (da558c3).
Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
crates/torii/sqlite/src/executor/erc.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2986      +/-   ##
==========================================
- Coverage   56.99%   56.97%   -0.02%     
==========================================
  Files         425      426       +1     
  Lines       56303    56376      +73     
==========================================
+ Hits        32088    32119      +31     
- Misses      24215    24257      +42     

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

@Larkooo Larkooo merged commit 378b782 into dojoengine:main Feb 5, 2025
13 of 15 checks passed
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