Skip to content

Commit

Permalink
fix(torii): handle failed metadata fetch and empty metadata in db
Browse files Browse the repository at this point in the history
commit-id:5474e77e
  • Loading branch information
lambda-0x committed Dec 3, 2024
1 parent 859c9d9 commit 266c8cc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
13 changes: 11 additions & 2 deletions crates/torii/core/src/executor/erc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,18 @@ impl<'c, P: Provider + Sync + Send + 'static> Executor<'c, P> {
"Failed to fetch metadata for token_id: {}",
register_erc721_token.actual_token_id
)
})?;
});

Check warning on line 245 in crates/torii/core/src/executor/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/erc.rs#L245

Added line #L245 was not covered by tests

serde_json::to_string(&metadata).context("Failed to serialize metadata")?
if let Ok(metadata) = metadata {
serde_json::to_string(&metadata).context("Failed to serialize metadata")?

Check warning on line 248 in crates/torii/core/src/executor/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/erc.rs#L247-L248

Added lines #L247 - L248 were not covered by tests
} else {
warn!(
contract_address = format!("{:#x}", register_erc721_token.contract_address),
token_id = %register_erc721_token.actual_token_id,
"Error fetching metadata, empty metadata will be used instead.",

Check warning on line 253 in crates/torii/core/src/executor/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/erc.rs#L250-L253

Added lines #L250 - L253 were not covered by tests
);
"".to_string()

Check warning on line 255 in crates/torii/core/src/executor/erc.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/executor/erc.rs#L255

Added line #L255 was not covered by tests
}
};

Ok(RegisterErc721TokenMetadata { query: register_erc721_token, metadata, name, symbol })
Expand Down
2 changes: 0 additions & 2 deletions crates/torii/core/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,8 +679,6 @@ impl<'c, P: Provider + Sync + Send + 'static> Executor<'c, P> {

self.register_tasks.spawn(async move {
let permit = semaphore.acquire().await.unwrap();
let span = tracing::span!(tracing::Level::INFO, "contract_address_span", contract_address = %register_erc721_token.contract_address);
let _enter = span.enter();

let result = Self::process_register_erc721_token_query(
register_erc721_token,
Expand Down
16 changes: 7 additions & 9 deletions crates/torii/graphql/src/object/erc/token_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ 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()];
let mut conditions = vec![
"(b.account_address = ?)".to_string(),
"(t.metadata IS NULL OR length(t.metadata) > 0)".to_string(),
];

Check warning on line 109 in crates/torii/graphql/src/object/erc/token_balance.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/graphql/src/object/erc/token_balance.rs#L106-L109

Added lines #L106 - L109 were not covered by tests

let mut cursor_param = &connection.after;
if let Some(after_cursor) = &connection.after {
Expand Down Expand Up @@ -234,14 +237,9 @@ fn token_balances_connection_output<'a>(
let token_id = row.token_id.split(':').collect::<Vec<&str>>();
assert!(token_id.len() == 2);

// skip the token if metadata is null
if row.metadata.is_none() {
continue;
}
let metadata_str = row.metadata.as_ref().unwrap();

let metadata_str = row.metadata;

Check warning on line 240 in crates/torii/graphql/src/object/erc/token_balance.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/graphql/src/object/erc/token_balance.rs#L240

Added line #L240 was not covered by tests
let metadata: serde_json::Value =
serde_json::from_str(metadata_str).expect("metadata is always json");
serde_json::from_str(&metadata_str).expect("metadata is always json");

Check warning on line 242 in crates/torii/graphql/src/object/erc/token_balance.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/graphql/src/object/erc/token_balance.rs#L242

Added line #L242 was not covered by tests
let metadata_name =
metadata.get("name").map(|v| v.to_string().trim_matches('"').to_string());
let metadata_description = metadata
Expand Down Expand Up @@ -301,5 +299,5 @@ struct BalanceQueryResultRaw {
pub token_id: String,
pub balance: String,
pub contract_type: String,
pub metadata: Option<String>,
pub metadata: String,
}
16 changes: 7 additions & 9 deletions crates/torii/graphql/src/object/erc/token_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ JOIN
"#,
);

let mut conditions = vec!["et.from_address = ? OR et.to_address = ?".to_string()];
let mut conditions = vec![
"(et.from_address = ? OR et.to_address = ?)".to_string(),
"(t.metadata IS NULL OR length(t.metadata) > 0)".to_string(),
];

Check warning on line 125 in crates/torii/graphql/src/object/erc/token_transfer.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/graphql/src/object/erc/token_transfer.rs#L122-L125

Added lines #L122 - L125 were not covered by tests

let mut cursor_param = &connection.after;
if let Some(after_cursor) = &connection.after {
Expand Down Expand Up @@ -263,14 +266,9 @@ fn token_transfers_connection_output<'a>(
let token_id = row.token_id.split(':').collect::<Vec<&str>>();
assert!(token_id.len() == 2);

// skip the token if metadata is null
if row.metadata.is_none() {
continue;
}

let metadata_str = row.metadata.as_ref().unwrap();
let metadata_str = row.metadata;

Check warning on line 269 in crates/torii/graphql/src/object/erc/token_transfer.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/graphql/src/object/erc/token_transfer.rs#L269

Added line #L269 was not covered by tests
let metadata: serde_json::Value =
serde_json::from_str(metadata_str).expect("metadata is always json");
serde_json::from_str(&metadata_str).expect("metadata is always json");

Check warning on line 271 in crates/torii/graphql/src/object/erc/token_transfer.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/graphql/src/object/erc/token_transfer.rs#L271

Added line #L271 was not covered by tests
let metadata_name =
metadata.get("name").map(|v| v.to_string().trim_matches('"').to_string());
let metadata_description = metadata
Expand Down Expand Up @@ -339,7 +337,7 @@ struct TransferQueryResultRaw {
pub symbol: String,
pub decimals: u8,
pub contract_type: String,
pub metadata: Option<String>,
pub metadata: String,
}

#[derive(Debug, Clone)]
Expand Down

0 comments on commit 266c8cc

Please sign in to comment.