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 0d7338e
Show file tree
Hide file tree
Showing 4 changed files with 27 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
17 changes: 8 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,11 @@ 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 NOT NULL".to_string(),
"t.metadata != ''".to_string(),
];

Check warning on line 110 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-L110

Added lines #L106 - L110 were not covered by tests

let mut cursor_param = &connection.after;
if let Some(after_cursor) = &connection.after {
Expand Down Expand Up @@ -234,14 +238,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 241 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#L241

Added line #L241 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 243 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#L243

Added line #L243 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 +300,5 @@ struct BalanceQueryResultRaw {
pub token_id: String,
pub balance: String,
pub contract_type: String,
pub metadata: Option<String>,
pub metadata: String,
}
17 changes: 8 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,11 @@ 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 NOT NULL".to_string(),
"t.metadata != ''".to_string(),
];

Check warning on line 126 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-L126

Added lines #L122 - L126 were not covered by tests

let mut cursor_param = &connection.after;
if let Some(after_cursor) = &connection.after {
Expand Down Expand Up @@ -263,14 +267,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 270 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#L270

Added line #L270 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 272 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#L272

Added line #L272 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 +338,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 0d7338e

Please sign in to comment.