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-tokens-graphql): returns tokens balances with no metadata #2764

Merged
merged 5 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ jobs:
name: Move cache
run: |
rm -rf /tmp/.buildx-cache/prebuild
mv /tmp/.buildx-cache-new/prebuild /tmp/.buildx-cache/prebuild
mv /tmp/.buildx-cache-new/prebuild /tmp/.buildx-cache/prebuild || true
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Ohayo sensei! Consider improving error handling for cache operations.

While adding || true prevents workflow failures, it silently ignores potential cache issues. Consider adding error logging:

- mv /tmp/.buildx-cache-new/prebuild /tmp/.buildx-cache/prebuild || true
- mv /tmp/.buildx-cache-new/build /tmp/.buildx-cache/build || true
+ mv /tmp/.buildx-cache-new/prebuild /tmp/.buildx-cache/prebuild || echo "Warning: Failed to move prebuild cache"
+ mv /tmp/.buildx-cache-new/build /tmp/.buildx-cache/build || echo "Warning: Failed to move build cache"

Also applies to: 86-86

rm -rf /tmp/.buildx-cache/build
mv /tmp/.buildx-cache-new/build /tmp/.buildx-cache/build
mv /tmp/.buildx-cache-new/build /tmp/.buildx-cache/build || true
46 changes: 31 additions & 15 deletions crates/torii/graphql/src/object/erc/token_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,7 @@ async fn fetch_token_balances(
JOIN tokens t ON b.token_id = t.id
JOIN contracts c ON t.contract_address = c.contract_address"
);
let mut conditions = vec![
"(b.account_address = ?)".to_string(),
"(t.metadata IS NULL OR length(t.metadata) > 0)".to_string(),
];
let mut conditions = vec!["(b.account_address = ?)".to_string()];

let mut cursor_param = &connection.after;
if let Some(after_cursor) = &connection.after {
Expand Down Expand Up @@ -238,17 +235,36 @@ fn token_balances_connection_output<'a>(
assert!(token_id.len() == 2);

let metadata_str = row.metadata;
let metadata: serde_json::Value =
serde_json::from_str(&metadata_str).expect("metadata is always json");
let metadata_name =
metadata.get("name").map(|v| v.to_string().trim_matches('"').to_string());
let metadata_description = metadata
.get("description")
.map(|v| v.to_string().trim_matches('"').to_string());
let metadata_attributes =
metadata.get("attributes").map(|v| v.to_string().trim_matches('"').to_string());

let image_path = format!("{}/{}", token_id.join("/"), "image");
let (
metadata_str,
metadata_name,
metadata_description,
metadata_attributes,
image_path,
) = if metadata_str.is_empty() {
(String::new(), None, None, None, String::new())
} else {
let metadata: serde_json::Value =
serde_json::from_str(&metadata_str).expect("metadata is always json");
let metadata_name =
Comment on lines +247 to +249
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider improving error handling for metadata parsing, sensei!

The .expect("metadata is always json") could lead to a panic if the metadata is malformed. Consider using a more graceful error handling approach.

-    let metadata: serde_json::Value =
-        serde_json::from_str(&metadata_str).expect("metadata is always json");
+    let metadata: serde_json::Value = match serde_json::from_str(&metadata_str) {
+        Ok(value) => value,
+        Err(e) => {
+            warn!("Failed to parse metadata JSON: {}", e);
+            return (String::new(), None, None, None, String::new());
+        }
+    };
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let metadata: serde_json::Value =
serde_json::from_str(&metadata_str).expect("metadata is always json");
let metadata_name =
let metadata: serde_json::Value = match serde_json::from_str(&metadata_str) {
Ok(value) => value,
Err(e) => {
warn!("Failed to parse metadata JSON: {}", e);
return (String::new(), None, None, None, String::new());
}
};
let metadata_name =

metadata.get("name").map(|v| v.to_string().trim_matches('"').to_string());
let metadata_description = metadata
.get("description")
.map(|v| v.to_string().trim_matches('"').to_string());
let metadata_attributes = metadata
.get("attributes")
.map(|v| v.to_string().trim_matches('"').to_string());

let image_path = format!("{}/{}", token_id.join("/"), "image");

(
metadata_str,
metadata_name,
metadata_description,
metadata_attributes,
image_path,
)
};

let token_metadata = Erc721Token {
name: row.name,
Expand Down