sim-cli 0.21.0

CLI tool for running and comparing Solana simulator backtests
Documentation
mod commands;
mod fetch;
mod output;
mod subscription;

use clap::{Parser, Subcommand};
use commands::{
    bundle::{self, BundleBuildArgs, BundleDeleteArgs, BundleListArgs, BundleStatusArgs},
    compare::{self, CompareArgs},
    datasets::{self, DatasetCredentialsArgs},
    depth::{self, DepthCoverageArgs, DepthMeasureArgs, DepthStatusArgs},
    ranges::{self, RangesArgs},
    run::{self, RunArgs},
    summarize::{self, SummarizeArgs},
    update,
    usage::{self, UsageArgs},
};
use console::style;
use sim_cli::signals::spawn_os_signal_handler;
use tokio_util::sync::CancellationToken;

/// Simulator CLI
/// Run and compare simulations against historical Solana slots
#[derive(Parser)]
struct Cli {
    /// Simulator endpoint hostname (e.g. staging.simulator.termina.technology)
    #[arg(
        long,
        global = true,
        env = "SIMULATOR_URL",
        default_value = "simulator.termina.technology"
    )]
    url: String,

    #[command(subcommand)]
    command: Command,
}

#[derive(Subcommand)]
enum Command {
    /// Run a backtest simulation over a slot range
    Run(Box<RunArgs>),
    /// Compare two simulation output files (baseline vs experiment)
    Compare(CompareArgs),
    /// Extract and print a summary of a simulation output file
    Summarize(SummarizeArgs),
    /// Update the sim binary to the latest published version
    Update,
    /// List available slot ranges on the simulator server
    Ranges(RangesArgs),
    /// Show API key usage metrics for a time window
    Usage(UsageArgs),
    /// Build, inspect and delete this API key's bundles
    #[command(subcommand)]
    Bundle(BundleCommand),
    /// Request and track venue-depth datasets
    #[command(subcommand)]
    Depth(DepthCommand),
    /// Access the published datasets
    #[command(subcommand)]
    Datasets(DatasetsCommand),
}

#[derive(Subcommand)]
enum BundleCommand {
    /// Issue a bundle build over a slot range on the manager
    Build(BundleBuildArgs),
    /// Check the status of a previously-issued bundle build
    Status(BundleStatusArgs),
    /// List this API key's bundle builds and the bundles it owns
    List(BundleListArgs),
    /// Delete one of this API key's own bundles
    Delete(BundleDeleteArgs),
}

#[derive(Subcommand)]
enum DepthCommand {
    /// Measure venue depth over a slot range and publish it as a dataset
    Measure(DepthMeasureArgs),
    /// Check the status of a previously-issued depth run
    Status(DepthStatusArgs),
    /// Show which markets, venues and sizes a depth request would sample
    Coverage(DepthCoverageArgs),
}

#[derive(Subcommand)]
enum DatasetsCommand {
    /// Print temporary, read-only S3 credentials for the shared datasets and
    /// this API key's own
    Credentials(DatasetCredentialsArgs),
}

#[tokio::main]
async fn main() {
    let _ = rustls::crypto::aws_lc_rs::default_provider().install_default();

    let cli = Cli::parse();

    let cancellation = CancellationToken::new();
    // Only `run` observes the token; installing the tokio signal handler for
    // the other commands would swallow Ctrl+C instead of terminating them
    // (tokio replaces the OS SIGINT disposition process-wide).
    if matches!(cli.command, Command::Run(_)) {
        spawn_os_signal_handler(cancellation.clone());
    }

    let result = match cli.command {
        Command::Compare(args) => compare::compare(args).await,
        Command::Summarize(args) => summarize::summarize(args),
        Command::Update => update::update().await,

        // With API key usage
        Command::Bundle(BundleCommand::Build(args)) => bundle::build_bundle(args, cli.url).await,
        Command::Bundle(BundleCommand::Status(args)) => bundle::get_status(args, cli.url).await,
        Command::Bundle(BundleCommand::List(args)) => bundle::list_builds(args, cli.url).await,
        Command::Bundle(BundleCommand::Delete(args)) => bundle::delete_bundle(args, cli.url).await,
        Command::Depth(DepthCommand::Measure(args)) => depth::measure(args, cli.url).await,
        Command::Depth(DepthCommand::Status(args)) => depth::status(args, cli.url).await,
        Command::Depth(DepthCommand::Coverage(args)) => depth::coverage(args, cli.url).await,
        Command::Datasets(DatasetsCommand::Credentials(args)) => {
            datasets::credentials(args, cli.url).await
        }
        Command::Ranges(args) => ranges::ranges(args, cli.url).await,
        Command::Run(args) => run::run(*args, cli.url, cancellation).await,
        Command::Usage(args) => usage::usage(args, cli.url).await,
    };

    // Render failures as a single clean line (the error chain joined with `: `)
    // and exit non-zero. Avoids eyre's default `{:?}` dump with its `Location:`
    // and backtrace, which is noise for an operator reading run output.
    if let Err(e) = result {
        eprintln!("{} {:#}", style("✖").red().bold(), e);
        std::process::exit(1);
    }
}