From cda0b9a627fa5105e8d12120ca1f10a176867b0e Mon Sep 17 00:00:00 2001 From: glihm Date: Tue, 19 Nov 2024 12:09:49 -0600 Subject: [PATCH 1/4] fix: ensure correct serialization of the contracts --- crates/torii/cli/src/args.rs | 2 +- crates/torii/cli/src/options.rs | 15 +++++++++++++++ crates/torii/core/src/types.rs | 6 ++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/crates/torii/cli/src/args.rs b/crates/torii/cli/src/args.rs index ce0b1bbf3f..13c43655b0 100644 --- a/crates/torii/cli/src/args.rs +++ b/crates/torii/cli/src/args.rs @@ -14,7 +14,7 @@ pub const DEFAULT_RPC_URL: &str = "http://0.0.0.0:5050"; /// Dojo World Indexer #[derive(Parser, Debug, Clone, serde::Serialize, serde::Deserialize)] -#[command(name = "torii", author, version, about, long_about = None)] +#[command(name = "torii", author, about, long_about = None)] #[command(next_help_heading = "Torii general options")] pub struct ToriiArgs { /// The world to index diff --git a/crates/torii/cli/src/options.rs b/crates/torii/cli/src/options.rs index ca97303177..008478b39a 100644 --- a/crates/torii/cli/src/options.rs +++ b/crates/torii/cli/src/options.rs @@ -3,6 +3,7 @@ use std::str::FromStr; use anyhow::Context; use clap::ArgAction; +use serde::ser::SerializeSeq; use serde::{Deserialize, Serialize}; use starknet::core::types::Felt; use torii_core::types::{Contract, ContractType}; @@ -141,6 +142,7 @@ pub struct IndexingOptions { help = "ERC contract addresses to index. You may only specify ERC20 or ERC721 contracts." )] #[serde(deserialize_with = "deserialize_contracts")] + #[serde(serialize_with = "serialize_contracts")] #[serde(default)] pub contracts: Vec, @@ -327,6 +329,19 @@ where contracts.iter().map(|s| parse_erc_contract(s).map_err(serde::de::Error::custom)).collect() } +fn serialize_contracts(contracts: &Vec, serializer: S) -> Result +where + S: serde::Serializer, +{ + let mut seq = serializer.serialize_seq(Some(contracts.len()))?; + + for contract in contracts { + seq.serialize_element(&contract.to_string())?; + } + + seq.end() +} + // ** Default functions to setup serde of the configuration file ** fn default_http_addr() -> IpAddr { DEFAULT_HTTP_ADDR diff --git a/crates/torii/core/src/types.rs b/crates/torii/core/src/types.rs index 8c57e63cb6..fef378f162 100644 --- a/crates/torii/core/src/types.rs +++ b/crates/torii/core/src/types.rs @@ -156,6 +156,12 @@ pub enum ContractType { ERC721, } +impl std::fmt::Display for Contract { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}:{:#x}", self.r#type, self.address) + } +} + impl FromStr for ContractType { type Err = anyhow::Error; From d83596518f4c57887fc655c4fb1d539316808e8c Mon Sep 17 00:00:00 2001 From: glihm Date: Tue, 19 Nov 2024 12:10:01 -0600 Subject: [PATCH 2/4] fix: split the CLI args to ensure version is used at the binary level --- Cargo.lock | 1 + bin/torii/Cargo.toml | 1 + bin/torii/src/cli.rs | 14 ++++++++++++++ bin/torii/src/main.rs | 7 ++++--- 4 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 bin/torii/src/cli.rs diff --git a/Cargo.lock b/Cargo.lock index c07a009fa9..232195d103 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -15320,6 +15320,7 @@ dependencies = [ "camino", "chrono", "clap", + "clap_complete", "clap_config", "ctrlc", "dojo-metrics", diff --git a/bin/torii/Cargo.toml b/bin/torii/Cargo.toml index 2e652a89ca..efa4339e0a 100644 --- a/bin/torii/Cargo.toml +++ b/bin/torii/Cargo.toml @@ -12,6 +12,7 @@ base64.workspace = true camino.workspace = true chrono.workspace = true clap.workspace = true +clap_complete.workspace = true ctrlc = { version = "3.4", features = [ "termination" ] } dojo-metrics.workspace = true dojo-types.workspace = true diff --git a/bin/torii/src/cli.rs b/bin/torii/src/cli.rs new file mode 100644 index 0000000000..1ebc62d347 --- /dev/null +++ b/bin/torii/src/cli.rs @@ -0,0 +1,14 @@ +//! CLI for Torii. +//! +//! Use a `Cli` struct to parse the CLI arguments +//! and to have flexibility in the future to add more commands +//! that may not start Torii directly. +use clap::Parser; +use torii_cli::ToriiArgs; + +#[derive(Parser)] +#[command(name = "torii", author, version, about, long_about = None)] +pub struct Cli { + #[command(flatten)] + pub args: ToriiArgs, +} diff --git a/bin/torii/src/main.rs b/bin/torii/src/main.rs index ff15a88644..29e222e26e 100644 --- a/bin/torii/src/main.rs +++ b/bin/torii/src/main.rs @@ -18,6 +18,7 @@ use std::time::Duration; use camino::Utf8PathBuf; use clap::Parser; +use cli::Cli; use dojo_metrics::exporters::prometheus::PrometheusRecorder; use dojo_world::contracts::world::WorldContractReader; use sqlx::sqlite::{ @@ -30,7 +31,6 @@ use tempfile::{NamedTempFile, TempDir}; use tokio::sync::broadcast; use tokio::sync::broadcast::Sender; use tokio_stream::StreamExt; -use torii_cli::ToriiArgs; use torii_core::engine::{Engine, EngineConfig, IndexingFlags, Processors}; use torii_core::executor::Executor; use torii_core::processors::store_transaction::StoreTransactionProcessor; @@ -46,9 +46,11 @@ use url::{form_urlencoded, Url}; pub(crate) const LOG_TARGET: &str = "torii::cli"; +mod cli; + #[tokio::main] async fn main() -> anyhow::Result<()> { - let mut args = ToriiArgs::parse().with_config_file()?; + let mut args = Cli::parse().args.with_config_file()?; let world_address = if let Some(world_address) = args.world_address { world_address @@ -56,7 +58,6 @@ async fn main() -> anyhow::Result<()> { return Err(anyhow::anyhow!("Please specify a world address.")); }; - // let mut contracts = parse_erc_contracts(&args.contracts)?; args.indexing.contracts.push(Contract { address: world_address, r#type: ContractType::WORLD }); let filter_layer = EnvFilter::try_from_default_env() From 3c3653413a87c0ed5d71706b2f92dd5bbe1e12be Mon Sep 17 00:00:00 2001 From: glihm Date: Tue, 19 Nov 2024 12:21:38 -0600 Subject: [PATCH 3/4] fix: derive missing feature --- crates/katana/cli/src/options.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/katana/cli/src/options.rs b/crates/katana/cli/src/options.rs index 9e9795207e..812de0db3e 100644 --- a/crates/katana/cli/src/options.rs +++ b/crates/katana/cli/src/options.rs @@ -366,6 +366,7 @@ fn default_max_connections() -> u32 { DEFAULT_RPC_MAX_CONNECTIONS } +#[cfg(feature = "server")] fn default_page_size() -> u64 { DEFAULT_RPC_MAX_EVENT_PAGE_SIZE } From 8f8e643215704d5ae9b17153854618c8258242b2 Mon Sep 17 00:00:00 2001 From: glihm Date: Tue, 19 Nov 2024 12:23:42 -0600 Subject: [PATCH 4/4] fix: add missing derive --- crates/katana/cli/src/options.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/katana/cli/src/options.rs b/crates/katana/cli/src/options.rs index 812de0db3e..64dc2ad8a7 100644 --- a/crates/katana/cli/src/options.rs +++ b/crates/katana/cli/src/options.rs @@ -55,6 +55,7 @@ pub struct MetricsOptions { pub metrics_port: u16, } +#[cfg(feature = "server")] impl Default for MetricsOptions { fn default() -> Self { MetricsOptions {