Skip to content

Commit

Permalink
fix(torii): avoid panic on fetch token URI fail (#2750)
Browse files Browse the repository at this point in the history
fix: ensure torii doesn't stop on fetch URI fail

In this occasions, the token URI may not be fetched correctly.
Since it's mostly use for metadata, a default metadata is
used in this scenario.
  • Loading branch information
glihm authored Dec 2, 2024
1 parent a0a173e commit 6644564
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions crates/torii/core/src/executor/erc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use starknet::core::types::{BlockId, BlockTag, FunctionCall, U256};
use starknet::core::utils::{get_selector_from_name, parse_cairo_short_string};
use starknet::providers::Provider;
use starknet_crypto::Felt;
use tracing::{debug, trace};
use tracing::{debug, trace, warn};

use super::{ApplyBalanceDiffQuery, Executor};
use crate::constants::{IPFS_CLIENT_MAX_RETRY, SQL_FELT_DELIMITER, TOKEN_BALANCE_TABLE};
Expand Down Expand Up @@ -176,7 +176,14 @@ impl<'c, P: Provider + Sync + Send + 'static> Executor<'c, P> {
{
token_uri
} else {
return Err(anyhow::anyhow!("Failed to fetch token_uri"));
warn!(
contract_address = format!("{:#x}", register_erc721_token.contract_address),
token_id = %register_erc721_token.actual_token_id,
"Error fetching token URI, empty metadata will be used instead.",
);

// Ignoring the token URI if the contract can't return it.
ByteArray::cairo_serialize(&"".try_into().unwrap())
};

let token_uri = if let Ok(byte_array) = ByteArray::cairo_deserialize(&token_uri, 0) {
Expand All @@ -192,13 +199,19 @@ impl<'c, P: Provider + Sync + Send + 'static> Executor<'c, P> {
return Err(anyhow::anyhow!("token_uri is neither ByteArray nor Array<Felt>"));
};

let metadata = Self::fetch_metadata(&token_uri).await.with_context(|| {
format!(
"Failed to fetch metadata for token_id: {}",
register_erc721_token.actual_token_id
)
})?;
let metadata = serde_json::to_string(&metadata).context("Failed to serialize metadata")?;
let metadata = if token_uri.is_empty() {
"".to_string()
} else {
let metadata = Self::fetch_metadata(&token_uri).await.with_context(|| {
format!(
"Failed to fetch metadata for token_id: {}",
register_erc721_token.actual_token_id
)
})?;

serde_json::to_string(&metadata).context("Failed to serialize metadata")?
};

Ok(RegisterErc721TokenMetadata { query: register_erc721_token, metadata, name, symbol })
}

Expand Down

0 comments on commit 6644564

Please sign in to comment.