-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(torii-indexer): eip 4906 update metadata processor (#2984)
* feat(torii-indexer): eip 4906 update metadata processor * batch metadata update * fix race condition batch update * fmt * f * fmt * fix fetch token metadta * clippy * add processor for erc721 and 1155 * fmt * add erc4906 component and implement in 721 and 1155 * update emetadata batch and single * fix token id * fmt * add batch processor * fmt * fmt * update test db
- Loading branch information
Showing
14 changed files
with
479 additions
and
133 deletions.
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.