-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(torii): indexing cartridge controllers (#2959)
* feat(torii): indexing cartridge controllers & cartrdige paymaster contracts * processor and migration * rename * use adderss for paralle * fmt * use udc to retrieve events * read paymaster accounts * use cartridge magic instead of deployer address * comments * c * logs * chore * format address in hex * unused event id * rename * fix build
- Loading branch information
Showing
16 changed files
with
208 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
use std::hash::{DefaultHasher, Hash, Hasher}; | ||
|
||
use anyhow::{Error, Result}; | ||
use async_trait::async_trait; | ||
use dojo_world::contracts::world::WorldContractReader; | ||
use lazy_static::lazy_static; | ||
use starknet::core::types::Event; | ||
use starknet::core::utils::parse_cairo_short_string; | ||
use starknet::macros::felt; | ||
use starknet::providers::Provider; | ||
use starknet_crypto::Felt; | ||
use torii_sqlite::Sql; | ||
use tracing::info; | ||
|
||
use super::{EventProcessor, EventProcessorConfig}; | ||
use crate::task_manager::{TaskId, TaskPriority}; | ||
|
||
pub(crate) const LOG_TARGET: &str = "torii_indexer::processors::controller"; | ||
|
||
#[derive(Default, Debug)] | ||
pub struct ControllerProcessor; | ||
|
||
lazy_static! { | ||
// https://x.cartridge.gg/ | ||
pub(crate) static ref CARTRIDGE_MAGIC: [Felt; 22] = [ | ||
felt!("0x68"), | ||
felt!("0x74"), | ||
felt!("0x74"), | ||
felt!("0x70"), | ||
felt!("0x73"), | ||
felt!("0x3a"), | ||
felt!("0x2f"), | ||
felt!("0x2f"), | ||
felt!("0x78"), | ||
felt!("0x2e"), | ||
felt!("0x63"), | ||
felt!("0x61"), | ||
felt!("0x72"), | ||
felt!("0x74"), | ||
felt!("0x72"), | ||
felt!("0x69"), | ||
felt!("0x64"), | ||
felt!("0x67"), | ||
felt!("0x65"), | ||
felt!("0x2e"), | ||
felt!("0x67"), | ||
felt!("0x67"), | ||
]; | ||
} | ||
|
||
#[async_trait] | ||
impl<P> EventProcessor<P> for ControllerProcessor | ||
where | ||
P: Provider + Send + Sync + std::fmt::Debug, | ||
{ | ||
fn event_key(&self) -> String { | ||
"ContractDeployed".to_string() | ||
} | ||
|
||
fn validate(&self, event: &Event) -> bool { | ||
// ContractDeployed event has no keys and contains username in data | ||
event.keys.len() == 1 && !event.data.is_empty() | ||
} | ||
|
||
fn task_priority(&self) -> TaskPriority { | ||
3 | ||
} | ||
|
||
fn task_identifier(&self, event: &Event) -> TaskId { | ||
let mut hasher = DefaultHasher::new(); | ||
// the contract address is the first felt in data | ||
event.data[0].hash(&mut hasher); | ||
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> { | ||
// Address is the first felt in data | ||
let address = event.data[0]; | ||
|
||
let calldata = event.data[5..].to_vec(); | ||
// our calldata has to be more than 25 felts. | ||
if calldata.len() < 25 { | ||
return Ok(()); | ||
} | ||
// check for this sequence of felts | ||
let cartridge_magic_len = calldata[2]; | ||
// length has to be 22 | ||
if cartridge_magic_len != Felt::from(22) { | ||
return Ok(()); | ||
} | ||
|
||
// this should never fail if since our len is 22 | ||
let cartridge_magic: [Felt; 22] = calldata[3..25].try_into().unwrap(); | ||
|
||
// has to match with https://x.cartridge.gg/ | ||
if !CARTRIDGE_MAGIC.eq(&cartridge_magic) { | ||
return Ok(()); | ||
} | ||
|
||
// Last felt in data is the salt which is the username encoded as short string | ||
let username_felt = event.data[event.data.len() - 1]; | ||
let username = parse_cairo_short_string(&username_felt)?; | ||
|
||
info!( | ||
target: LOG_TARGET, | ||
username = %username, | ||
address = %format!("{address:#x}"), | ||
"Controller deployed." | ||
); | ||
|
||
db.add_controller(&username, &format!("{address:#x}"), block_timestamp).await?; | ||
|
||
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
-- Cartridge controllers | ||
CREATE TABLE controllers ( | ||
id TEXT PRIMARY KEY NOT NULL, -- Username as primary key | ||
username TEXT NOT NULL, -- Username | ||
address TEXT NOT NULL, -- Wallet address | ||
deployed_at TIMESTAMP NOT NULL -- Block timestamp of deployment | ||
); | ||
|
||
CREATE INDEX idx_controllers_address ON controllers (address); |
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,7 @@ | ||
use starknet::macros::felt; | ||
use starknet_crypto::Felt; | ||
|
||
pub(crate) const LOG_TARGET: &str = "torii:runner"; | ||
|
||
pub(crate) const UDC_ADDRESS: Felt = | ||
felt!("0x041a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf"); |
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