-
Notifications
You must be signed in to change notification settings - Fork 193
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): indexing cartridge controllers #2959
Merged
Merged
Changes from 14 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
bc1b9bd
feat(torii): indexing cartridge controllers & cartrdige paymaster con…
Larkooo cecc550
processor and migration
Larkooo 4069998
rename
Larkooo 18ae749
use adderss for paralle
Larkooo cc5c921
fmt
Larkooo 35a17dd
use udc to retrieve events
Larkooo 497c05c
read paymaster accounts
Larkooo 15617e4
use cartridge magic instead of deployer address
Larkooo 31833a2
comments
Larkooo ad16026
c
Larkooo 3e5fb4d
logs
Larkooo e3102f2
chore
Larkooo 7788904
format address in hex
Larkooo bf62d26
unused event id
Larkooo bd97e10
rename
Larkooo 68f9904
fix build
Larkooo a0a1cb4
Merge branch 'main' into cartridge-controllers
Larkooo c2ad549
fmt
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
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(); | ||
Larkooo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// 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)?; | ||
Larkooo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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
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
Consider tightening bounds checks in
validate
, sensei!You rely on
event.data[5..]
slicing later inprocess
. Ifevent.data
has fewer than 5 items, slicing will silently panic. It may be safer to validate the minimal number of felts here or handle it gracefully inprocess
.