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-indexer): eip 4906 update metadata processor #2984

Merged
merged 20 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
94 changes: 94 additions & 0 deletions crates/torii/indexer/src/processors/erc4906_metadata_update.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use std::hash::{DefaultHasher, Hash, Hasher};
use std::sync::Arc;

use anyhow::Error;
use async_trait::async_trait;
use cainome::cairo_serde::{CairoSerde, U256 as U256Cainome};
use dojo_world::contracts::world::WorldContractReader;
use starknet::core::types::{Event, U256};
use starknet::providers::Provider;
use torii_sqlite::Sql;
use tracing::debug;

use super::{EventProcessor, EventProcessorConfig};
use crate::task_manager::{TaskId, TaskPriority};

pub(crate) const LOG_TARGET: &str = "torii_indexer::processors::erc4906_metadata_update";

#[derive(Default, Debug)]
pub struct Erc4906MetadataUpdateProcessor;

#[async_trait]
impl<P> EventProcessor<P> for Erc4906MetadataUpdateProcessor
where
P: Provider + Send + Sync + std::fmt::Debug,
{
fn event_key(&self) -> String {
// We'll handle both event types in validate()
"MetadataUpdate".to_string()
}

fn validate(&self, event: &Event) -> bool {
// Single token metadata update: [hash(MetadataUpdate), token_id.low, token_id.high]
if event.keys.len() == 3 && event.data.is_empty() {
return true;
}

// Batch metadata update: [hash(BatchMetadataUpdate), from_token_id.low, from_token_id.high, to_token_id.low, to_token_id.high]
if event.keys.len() == 5 && event.data.is_empty() {
return true;
}

false
}

fn task_priority(&self) -> TaskPriority {
2 // Lower priority than transfers
}

fn task_identifier(&self, event: &Event) -> TaskId {
let mut hasher = DefaultHasher::new();
event.keys[0].hash(&mut hasher); // Hash the event key

// For single token updates
if event.keys.len() == 3 {
event.keys[1].hash(&mut hasher); // token_id.low
event.keys[2].hash(&mut hasher); // token_id.high
} else {
// For batch updates
event.keys[1].hash(&mut hasher); // from_token_id.low
event.keys[2].hash(&mut hasher); // from_token_id.high
event.keys[3].hash(&mut hasher); // to_token_id.low
event.keys[4].hash(&mut hasher); // to_token_id.high
}

hasher.finish()
}

async fn process(
&self,
world: &WorldContractReader<P>,
db: &mut Sql,
_block_number: u64,
_block_timestamp: u64,
_event_id: &str,
event: &Event,
_config: &EventProcessorConfig,
) -> Result<(), Error> {
let token_address = event.from_address;
let token_id = U256Cainome::cairo_deserialize(&event.keys, 1)?;
let token_id = U256::from_words(token_id.low, token_id.high);

db.update_erc721_metadata(token_address, token_id, Arc::new(world.provider().clone()))
.await?;

debug!(
target: LOG_TARGET,
token_address = ?token_address,
token_id = ?token_id,
"ERC721 metadata updated"
);

Ok(())
}
Larkooo marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 2 additions & 0 deletions crates/torii/indexer/src/processors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub mod store_update_member;
pub mod store_update_record;
pub mod upgrade_event;
pub mod upgrade_model;
pub mod erc4906_metadata_update;

#[derive(Clone, Debug, Default)]
pub struct EventProcessorConfig {
pub historical_events: HashSet<String>,
Expand Down
122 changes: 84 additions & 38 deletions crates/torii/sqlite/src/executor/erc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,21 +180,17 @@ impl<'c, P: Provider + Sync + Send + 'static> Executor<'c, P> {
Ok(())
}

pub async fn process_register_erc721_token_query(
register_erc721_token: RegisterErc721TokenQuery,
provider: Arc<P>,
name: String,
symbol: String,
) -> Result<RegisterErc721TokenMetadata> {
async fn fetch_token_uri(
provider: &P,
contract_address: Felt,
token_id: U256,
) -> Result<String> {
let token_uri = if let Ok(token_uri) = provider
.call(
FunctionCall {
contract_address: register_erc721_token.contract_address,
contract_address,
entry_point_selector: get_selector_from_name("token_uri").unwrap(),
calldata: vec![
register_erc721_token.actual_token_id.low().into(),
register_erc721_token.actual_token_id.high().into(),
],
calldata: vec![token_id.low().into(), token_id.high().into()],
},
BlockId::Tag(BlockTag::Pending),
)
Expand All @@ -204,12 +200,9 @@ impl<'c, P: Provider + Sync + Send + 'static> Executor<'c, P> {
} else if let Ok(token_uri) = provider
.call(
FunctionCall {
contract_address: register_erc721_token.contract_address,
contract_address,
entry_point_selector: get_selector_from_name("tokenURI").unwrap(),
calldata: vec![
register_erc721_token.actual_token_id.low().into(),
register_erc721_token.actual_token_id.high().into(),
],
calldata: vec![token_id.low().into(), token_id.high().into()],
},
BlockId::Tag(BlockTag::Pending),
)
Expand All @@ -218,12 +211,10 @@ impl<'c, P: Provider + Sync + Send + 'static> Executor<'c, P> {
token_uri
} else {
warn!(
contract_address = format!("{:#x}", register_erc721_token.contract_address),
token_id = %register_erc721_token.actual_token_id,
contract_address = format!("{:#x}", contract_address),
token_id = %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())
};

Expand All @@ -234,42 +225,97 @@ impl<'c, P: Provider + Sync + Send + 'static> Executor<'c, P> {
.iter()
.map(parse_cairo_short_string)
.collect::<Result<Vec<String>, _>>()
.map(|strings| strings.join(""))
.map_err(|_| anyhow::anyhow!("Failed parsing Array<Felt> to String"))?
.map(|strings| strings.join(""))?
} else {
return Err(anyhow::anyhow!("token_uri is neither ByteArray nor Array<Felt>"));
};

let metadata = if token_uri.is_empty() {
"".to_string()
Ok(token_uri)
}

async fn fetch_token_metadata(
contract_address: Felt,
token_id: U256,
token_uri: &str,
) -> Result<String> {
if token_uri.is_empty() {
Ok("".to_string())
} else {
let metadata = Self::fetch_metadata(&token_uri).await.with_context(|| {
let metadata = Self::fetch_metadata(token_uri).await.with_context(|| {
format!(
"Failed to fetch metadata for token_id: {}",
register_erc721_token.actual_token_id
token_id
)
});

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()
match metadata {
Ok(metadata) => serde_json::to_string(&metadata)
.context("Failed to serialize metadata"),
Err(e) => {
warn!(
contract_address = format!("{:#x}", contract_address),
token_id = %token_id,
error = ?e,
"Error fetching metadata, empty metadata will be used instead.",
);
Ok("".to_string())
}
}
};
}
}

Ok(RegisterErc721TokenMetadata { query: register_erc721_token, metadata, name, symbol })
pub async fn update_erc721_metadata(
&mut self,
contract_address: Felt,
token_id: U256,
provider: Arc<P>,
) -> Result<()> {
let token_uri = Self::fetch_token_uri(&provider, contract_address, token_id).await?;
let metadata = Self::fetch_token_metadata(contract_address, token_id, &token_uri).await?;

// Update metadata in database
sqlx::query(
"UPDATE tokens SET metadata = ? WHERE contract_address = ? AND id LIKE ?",
)
.bind(&metadata)
.bind(felt_to_sql_string(&contract_address))
.bind(format!("%{}", u256_to_sql_string(&token_id)))
.execute(&mut *self.transaction)
.await?;

Ok(())
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Ohayo sensei, consider using an exact match instead of LIKE

Using LIKE '%... may inadvertently update multiple tokens if IDs share a suffix. Switching to an exact match:

- "UPDATE tokens SET metadata = ? WHERE contract_address = ? AND id LIKE ?"
+ "UPDATE tokens SET metadata = ? WHERE contract_address = ? AND id = ?"

...avoids accidental collisions.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
pub async fn update_erc721_metadata(
&mut self,
contract_address: Felt,
token_id: U256,
provider: Arc<P>,
) -> Result<()> {
let token_uri = Self::fetch_token_uri(&provider, contract_address, token_id).await?;
let metadata = Self::fetch_token_metadata(contract_address, token_id, &token_uri).await?;
// Update metadata in database
sqlx::query(
"UPDATE tokens SET metadata = ? WHERE contract_address = ? AND id LIKE ?",
)
.bind(&metadata)
.bind(felt_to_sql_string(&contract_address))
.bind(format!("%{}", u256_to_sql_string(&token_id)))
.execute(&mut *self.transaction)
.await?;
Ok(())
}
pub async fn update_erc721_metadata(
&mut self,
contract_address: Felt,
token_id: U256,
provider: Arc<P>,
) -> Result<()> {
let token_uri = Self::fetch_token_uri(&provider, contract_address, token_id).await?;
let metadata = Self::fetch_token_metadata(contract_address, token_id, &token_uri).await?;
// Update metadata in database
sqlx::query(
"UPDATE tokens SET metadata = ? WHERE contract_address = ? AND id = ?",
)
.bind(&metadata)
.bind(felt_to_sql_string(&contract_address))
.bind(format!("%{}", u256_to_sql_string(&token_id)))
.execute(&mut *self.transaction)
.await?;
Ok(())
}
🧰 Tools
🪛 GitHub Actions: ci

[warning] 280-280: Line exceeds maximum length. Consider breaking the line for better readability.


pub async fn process_register_erc721_token_query(
register_erc721_token: RegisterErc721TokenQuery,
provider: Arc<P>,
name: String,
symbol: String,
) -> Result<RegisterErc721TokenMetadata> {
let token_uri = Self::fetch_token_uri(
&provider,
register_erc721_token.contract_address,
register_erc721_token.actual_token_id,
).await?;

let metadata = Self::fetch_token_metadata(
register_erc721_token.contract_address,
register_erc721_token.actual_token_id,
&token_uri,
).await?;

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

// 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
Expand Down
Loading