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 d0722c4 commit ef343bb
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
)
})?;
});

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")?
} 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.",
);
"".to_string()
}
};

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(),
];

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;
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");
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(),
];

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;
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");
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 ef343bb

Please sign in to comment.