Skip to content

Commit

Permalink
fix(torii): ensure correct serde of args in config file (#2704)
Browse files Browse the repository at this point in the history
* fix: ensure correct serialization of the contracts

* fix: split the CLI args to ensure version is used at the binary level

* fix: derive missing feature

* fix: add missing derive
  • Loading branch information
glihm authored Nov 19, 2024
1 parent 0aa330d commit 0342464
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bin/torii/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions bin/torii/src/cli.rs
Original file line number Diff line number Diff line change
@@ -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,
}
7 changes: 4 additions & 3 deletions bin/torii/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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;
Expand All @@ -46,17 +46,18 @@ 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
} else {
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()
Expand Down
2 changes: 2 additions & 0 deletions crates/katana/cli/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub struct MetricsOptions {
pub metrics_port: u16,
}

#[cfg(feature = "server")]
impl Default for MetricsOptions {
fn default() -> Self {
MetricsOptions {
Expand Down Expand Up @@ -366,6 +367,7 @@ fn default_max_connections() -> u32 {
DEFAULT_RPC_MAX_CONNECTIONS
}

#[cfg(feature = "server")]
fn default_page_size() -> u64 {
DEFAULT_RPC_MAX_EVENT_PAGE_SIZE
}
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions crates/torii/cli/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<Contract>,

Expand Down Expand Up @@ -327,6 +329,19 @@ where
contracts.iter().map(|s| parse_erc_contract(s).map_err(serde::de::Error::custom)).collect()
}

fn serialize_contracts<S>(contracts: &Vec<Contract>, serializer: S) -> Result<S::Ok, S::Error>
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
Expand Down
6 changes: 6 additions & 0 deletions crates/torii/core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 0342464

Please sign in to comment.