-
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
+479
−133
Merged
Changes from all 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
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
74 changes: 74 additions & 0 deletions
74
crates/torii/indexer/src/processors/erc4906_batch_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,74 @@ | ||
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::{self, TaskId, TaskPriority}; | ||
|
||
pub(crate) const LOG_TARGET: &str = "torii_indexer::processors::erc4906_metadata_update_batch"; | ||
|
||
#[derive(Default, Debug)] | ||
pub struct Erc4906BatchMetadataUpdateProcessor; | ||
|
||
#[async_trait] | ||
impl<P> EventProcessor<P> for Erc4906BatchMetadataUpdateProcessor | ||
where | ||
P: Provider + Send + Sync + std::fmt::Debug, | ||
{ | ||
fn event_key(&self) -> String { | ||
"BatchMetadataUpdate".to_string() | ||
} | ||
|
||
fn validate(&self, event: &Event) -> bool { | ||
// Batch metadata update: [hash(BatchMetadataUpdate), from_token_id.low, from_token_id.high, | ||
// to_token_id.low, to_token_id.high] | ||
event.keys.len() == 5 && event.data.is_empty() | ||
} | ||
|
||
fn task_priority(&self) -> TaskPriority { | ||
2 | ||
} | ||
|
||
fn task_identifier(&self, _event: &Event) -> TaskId { | ||
task_manager::TASK_ID_SEQUENTIAL | ||
} | ||
|
||
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 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_nft_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, | ||
"NFT metadata updated for token range" | ||
); | ||
|
||
Ok(()) | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
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,64 @@ | ||
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::{self, 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 { | ||
"MetadataUpdate".to_string() | ||
} | ||
|
||
fn validate(&self, event: &Event) -> bool { | ||
// Single token metadata update: [hash(MetadataUpdate), token_id.low, token_id.high] | ||
event.keys.len() == 3 && event.data.is_empty() | ||
} | ||
|
||
fn task_priority(&self) -> TaskPriority { | ||
2 | ||
} | ||
|
||
fn task_identifier(&self, _event: &Event) -> TaskId { | ||
task_manager::TASK_ID_SEQUENTIAL | ||
} | ||
|
||
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_nft_metadata(token_address, token_id).await?; | ||
|
||
debug!( | ||
target: LOG_TARGET, | ||
token_address = ?token_address, | ||
token_id = ?token_id, | ||
"NFT metadata updated for single token" | ||
); | ||
|
||
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
Oops, something went wrong.
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.
🛠️ Refactor suggestion
Ohayo sensei! Consider adding range size limits.
The batch update processor doesn't limit the token ID range size. Large ranges could:
Consider adding a reasonable maximum range size (e.g., 1000 tokens).
📝 Committable suggestion