Skip to content
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

fix(torii): ensure correct serde of args in config file #2704

Merged
merged 4 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Ohayo sensei! The clap_complete dependency in torii seems unused!

The dependency is added to torii's Cargo.toml but there's no actual usage of it in the crate. While clap_complete is properly used in katana and sozo for shell completion generation, it appears to be an unnecessary dependency for torii at this time.

  • Consider removing clap_complete from bin/torii/Cargo.toml if there are no immediate plans to implement shell completions for torii
🔗 Analysis chain

Let's verify the usage of clap_complete, sensei!

We should ensure this new dependency is being utilized in the codebase.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if clap_complete is being used in the codebase

# Search for clap_complete usage in Rust files
rg -t rust "use.*clap_complete" 

# Search for shell completion generation code
ast-grep --pattern 'clap_complete::generate'

Length of output: 323


Script:

#!/bin/bash
# Let's check if clap_complete is also used in the torii crate
rg -t rust "use.*clap_complete" bin/torii/

# Check for any shell completion generation in torii
ast-grep --pattern 'clap_complete::generate' bin/torii/

# Let's see if there's a completions command or similar functionality
rg -t rust "completions" bin/torii/

Length of output: 136

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: 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
Loading