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

feat(torii-erc): add avif support #3011

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
80 changes: 80 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ hashlink = "0.9.1"
hex = "0.4.3"
hex-literal = "0.4.1"
http = "0.2.9"
image = "0.25.2"
image = { version = "0.25.2", features = [ "avif-native" ] }
indexmap = "2.2.5"
indoc = "1.0.7"
itertools = "0.12.1"
Expand Down
20 changes: 8 additions & 12 deletions crates/torii/server/src/artifacts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ use camino::Utf8PathBuf;
use data_url::mime::Mime;
use data_url::DataUrl;
use image::{DynamicImage, ImageFormat};
use reqwest::Client;
use serde::{Deserialize, Serialize};
use sqlx::{Pool, Sqlite};
use tokio::fs;
use tokio::fs::File;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::sync::broadcast::Receiver;
use torii_sqlite::constants::TOKENS_TABLE;
use torii_sqlite::utils::fetch_content_from_ipfs;
use torii_sqlite::utils::{fetch_content_from_http, fetch_content_from_ipfs};
use tracing::{debug, error, trace};
use warp::http::Response;
use warp::path::Tail;
Expand Down Expand Up @@ -61,7 +60,6 @@ async fn serve_static_file(
if !token_image_dir.exists() {
match fetch_and_process_image(&artifacts_dir, &token_id, pool)
.await
.context(format!("Failed to fetch and process image for token_id: {}", token_id))
{
Ok(path) => path,
Err(e) => {
Expand Down Expand Up @@ -173,19 +171,15 @@ async fn fetch_and_process_image(
.with_context(|| format!("Image field not a string for token_id: {}", token_id))?
.to_string();

println!("image_uri: {}", image_uri);
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Remove debug print statements.

Ohayo sensei! These debug print statements should be replaced with proper logging using the tracing crate that's already imported.

-    println!("image_uri: {}", image_uri);
+    debug!(image_uri = %image_uri, "Processing image URI");

-    println!("image_type: {:?}", image_type);
+    debug!(image_type = ?image_type, "Determined image type");

Also applies to: 247-247


let image_type = match image_uri {
uri if uri.starts_with("http") || uri.starts_with("https") => {
debug!(image_uri = %uri, "Fetching image from http/https URL");
// Fetch image from HTTP/HTTPS URL
let client = Client::new();
let response = client
.get(uri)
.send()
.await
.context("Failed to fetch image from URL")?
.bytes()
let response = fetch_content_from_http(&uri)
.await
.context("Failed to read image bytes from response")?;
.context("Failed to fetch image from URL")?;

// svg files typically start with <svg or <?xml
if response.starts_with(b"<svg") || response.starts_with(b"<?xml") {
Expand Down Expand Up @@ -218,7 +212,7 @@ async fn fetch_and_process_image(
)
})?;
ErcImageType::DynamicImage((
image::load_from_memory(&response)
image::load_from_memory_with_format(&response, format)
.context("Failed to load image from bytes")?,
format,
))
Expand Down Expand Up @@ -250,6 +244,8 @@ async fn fetch_and_process_image(
}
};

println!("image_type: {:?}", image_type);

// Extract contract_address and token_id from token_id
let parts: Vec<&str> = token_id.split(':').collect();
if parts.len() != 2 {
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/sqlite/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ pub const TOKEN_TRANSFER_TABLE: &str = "token_transfers";
pub const TOKENS_TABLE: &str = "tokens";
pub const WORLD_CONTRACT_TYPE: &str = "WORLD";
pub const SQL_FELT_DELIMITER: &str = "/";
pub const REQ_MAX_RETRIES: u8 = 3;

pub const IPFS_URL: &str = "https://ipfs.io/ipfs/";
pub const IPFS_CLIENT_MAX_RETRY: u8 = 3;

pub const IPFS_CLIENT_URL: &str = "https://ipfs.infura.io:5001";
pub const IPFS_CLIENT_USERNAME: &str = "2EBrzr7ZASQZKH32sl2xWauXPSA";
Expand Down
48 changes: 17 additions & 31 deletions crates/torii/sqlite/src/executor/erc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use anyhow::{Context, Result};
use cainome::cairo_serde::{ByteArray, CairoSerde};
use data_url::mime::Mime;
use data_url::DataUrl;
use reqwest::Client;
use starknet::core::types::{BlockId, BlockTag, FunctionCall, U256};
use starknet::core::utils::{get_selector_from_name, parse_cairo_short_string};
use starknet::providers::Provider;
Expand All @@ -18,8 +17,7 @@ use crate::executor::LOG_TARGET;
use crate::simple_broker::SimpleBroker;
use crate::types::{ContractType, Token, TokenBalance};
use crate::utils::{
felt_to_sql_string, fetch_content_from_ipfs, sanitize_json_string, sql_string_to_u256,
u256_to_sql_string, I256,
felt_to_sql_string, fetch_content_from_http, fetch_content_from_ipfs, sanitize_json_string, sql_string_to_u256, u256_to_sql_string, I256
};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -76,7 +74,7 @@ impl<'c, P: Provider + Sync + Send + 'static> Executor<'c, P> {
apply_balance_diff.block_id,
)
.await
.with_context(|| "Failed to apply balance diff in apply_cache_diff")?;
.context("Failed to apply balance diff in apply_cache_diff")?;
}
ContractType::ERC20 => {
// account_address/contract_address/ => ERC20
Expand All @@ -95,7 +93,7 @@ impl<'c, P: Provider + Sync + Send + 'static> Executor<'c, P> {
apply_balance_diff.block_id,
)
.await
.with_context(|| "Failed to apply balance diff in apply_cache_diff")?;
.context("Failed to apply balance diff in apply_cache_diff")?;
}
}
}
Expand Down Expand Up @@ -143,7 +141,7 @@ impl<'c, P: Provider + Sync + Send + 'static> Executor<'c, P> {
block_id,
)
.await
.with_context(|| format!("Failed to fetch balance for id: {}", id))?;
.context(format!("Failed to fetch balance for id: {}", id))?;

let current_balance =
cainome::cairo_serde::U256::cairo_deserialize(&current_balance, 0).unwrap();
Expand Down Expand Up @@ -255,6 +253,7 @@ impl<'c, P: Provider + Sync + Send + 'static> Executor<'c, P> {
warn!(
contract_address = format!("{:#x}", register_erc721_token.contract_address),
token_id = %register_erc721_token.actual_token_id,
token_uri = %token_uri,
"Error fetching metadata, empty metadata will be used instead.",
);
"".to_string()
Expand All @@ -268,39 +267,25 @@ impl<'c, P: Provider + Sync + Send + 'static> Executor<'c, P> {
// given a uri which can be either http/https url or data uri, fetch the metadata erc721
// metadata json schema
pub async fn fetch_metadata(token_uri: &str) -> Result<serde_json::Value> {
// Parse the token_uri

match token_uri {
uri if uri.starts_with("http") || uri.starts_with("https") => {
// Fetch metadata from HTTP/HTTPS URL
debug!(token_uri = %token_uri, "Fetching metadata from http/https URL");
let client = Client::new();
let response = client
.get(token_uri)
.send()
let bytes = fetch_content_from_http(token_uri)
.await
.context("Failed to fetch metadata from URL")?;

let bytes = response.bytes().await.context("Failed to read response bytes")?;
let json: serde_json::Value = serde_json::from_slice(&bytes)
.context(format!("Failed to parse metadata JSON from response: {:?}", bytes))?;
.context(format!("Failed to fetch metadata from URL: {}", token_uri))?;

Ok(json)
serde_json::from_slice(&bytes)
.context(format!("Failed to parse metadata JSON from response: {:?}", bytes))
}
uri if uri.starts_with("ipfs") => {
let cid = uri.strip_prefix("ipfs://").unwrap();
debug!(cid = %cid, "Fetching metadata from IPFS");
let response = fetch_content_from_ipfs(cid)
let bytes = fetch_content_from_ipfs(cid)
.await
.context("Failed to fetch metadata from IPFS")?;

let json: serde_json::Value =
serde_json::from_slice(&response).context(format!(
"Failed to parse metadata JSON from IPFS: {:?}, data: {:?}",
cid, &response
))?;

Ok(json)
serde_json::from_slice(&bytes)
.context(format!("Failed to parse metadata JSON from IPFS: {:?}, data: {:?}", cid, bytes))
}
uri if uri.starts_with("data") => {
// Parse and decode data URI
Expand All @@ -326,9 +311,10 @@ impl<'c, P: Provider + Sync + Send + 'static> Executor<'c, P> {
let sanitized_json = sanitize_json_string(&decoded_str);

let json: serde_json::Value =
serde_json::from_str(&sanitized_json).with_context(|| {
format!("Failed to parse metadata JSON from data URI: {}", &uri)
})?;
serde_json::from_str(&sanitized_json).context(format!(
"Failed to parse metadata JSON from data URI: {}",
&uri
))?;

Ok(json)
}
Expand All @@ -354,7 +340,7 @@ impl<'c, P: Provider + Sync + Send + 'static> Executor<'c, P> {
let token = query
.fetch_optional(&mut *self.transaction)
.await
.with_context(|| format!("Failed to execute721Token query: {:?}", result))?;
.context(format!("Failed to execute721Token query: {:?}", result))?;

if let Some(token) = token {
self.publish_queue.push(BrokerMessage::TokenRegistered(token));
Expand Down
Loading
Loading