-
Notifications
You must be signed in to change notification settings - Fork 194
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
Changes from 2 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
5a75b26
feat(torii-indexer): eip 4906 update metadata processor
Larkooo 605231f
batch metadata update
Larkooo 54e9fcb
fix race condition batch update
Larkooo 036f04b
fmt
Larkooo 18c0083
f
Larkooo 3ab6b64
Merge branch 'main' into metadata-update
Larkooo fc928bd
fmt
Larkooo 9c16fc6
fix fetch token metadta
Larkooo d9b2762
clippy
Larkooo 2fa1ed7
Merge remote-tracking branch 'upstream/main' into metadata-update
Larkooo 22fd5e9
add processor for erc721 and 1155
Larkooo 09923f1
fmt
Larkooo 9aa1b44
add erc4906 component and implement in 721 and 1155
Larkooo 49215fa
update emetadata batch and single
Larkooo 551ad2a
fix token id
Larkooo 06a5f30
fmt
Larkooo 177df71
add batch processor
Larkooo 588bec1
fmt
Larkooo d1cdd82
fmt
Larkooo 952de8e
update test db
Larkooo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
117 changes: 117 additions & 0 deletions
117
crates/torii/indexer/src/processors/erc4906_metadata_update.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
use std::hash::{DefaultHasher, Hash, Hasher}; | ||
|
||
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; | ||
|
||
if event.keys.len() == 3 { | ||
// Single token metadata update | ||
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).await?; | ||
|
||
debug!( | ||
target: LOG_TARGET, | ||
token_address = ?token_address, | ||
token_id = ?token_id, | ||
"ERC721 metadata updated for single token" | ||
); | ||
} else { | ||
// Batch metadata update | ||
let from_token_id = U256Cainome::cairo_deserialize(&event.keys, 1)?; | ||
let from_token_id = U256::from_words(from_token_id.low, from_token_id.high); | ||
|
||
let to_token_id = U256Cainome::cairo_deserialize(&event.keys, 3)?; | ||
let to_token_id = U256::from_words(to_token_id.low, to_token_id.high); | ||
|
||
let mut token_id = from_token_id; | ||
while token_id <= to_token_id { | ||
db.update_erc721_metadata(token_address, token_id).await?; | ||
token_id += U256::from(1u8); | ||
} | ||
|
||
debug!( | ||
target: LOG_TARGET, | ||
token_address = ?token_address, | ||
from_token_id = ?from_token_id, | ||
to_token_id = ?to_token_id, | ||
"ERC721 metadata updated for token range" | ||
); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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:...avoids accidental collisions.
📝 Committable suggestion
🧰 Tools
🪛 GitHub Actions: ci
[warning] 280-280: Line exceeds maximum length. Consider breaking the line for better readability.