Skip to content

Commit

Permalink
refactor(hermes): move to clap_derive
Browse files Browse the repository at this point in the history
  • Loading branch information
Reisen committed Sep 25, 2023
1 parent 7ee97ce commit 26b8dcd
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 115 deletions.
138 changes: 110 additions & 28 deletions hermes/Cargo.lock

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

9 changes: 5 additions & 4 deletions hermes/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[package]
name = "hermes"
version = "0.1.20"
edition = "2021"
name = "hermes"
version = "0.1.21"
description = "Hermes is an agent that provides Verified Prices from the Pythnet Pyth Oracle."
edition = "2021"

[dependencies]
async-trait = { version = "0.1.73" }
Expand Down Expand Up @@ -33,7 +34,7 @@ serde_json = { version = "1.0.93" }
serde_qs = { version = "0.12.0", features = ["axum"] }
serde_wormhole = { git = "https://github.com/wormhole-foundation/wormhole", tag = "v2.17.1" }
sha3 = { version = "0.10.4" }
structopt = { version = "0.3.26" }
clap = { version = "4.4.4", features = ["derive", "env", "cargo"] }
strum = { version = "0.24.1", features = ["derive"] }
tokio = { version = "1.26.0", features = ["full"] }
tower-http = { version = "0.4.0", features = ["cors"] }
Expand Down
122 changes: 41 additions & 81 deletions hermes/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,98 +1,58 @@
use {
libp2p::Multiaddr,
reqwest::Url,
solana_sdk::pubkey::Pubkey,
std::net::SocketAddr,
structopt::StructOpt,
use clap::{
crate_authors,
crate_description,
crate_name,
crate_version,
Args,
Parser,
};

const DEFAULT_NETWORK_ID: &str = "/wormhole/mainnet/2";
const DEFAULT_WORMHOLE_BOOTSTRAP_ADDRS: &str = "/dns4/wormhole-mainnet-v2-bootstrap.certus.one/udp/8999/quic/p2p/12D3KooWQp644DK27fd3d4Km3jr7gHiuJJ5ZGmy8hH4py7fP4FP7,/dns4/wormhole-v2-mainnet-bootstrap.xlabs.xyz/udp/8999/quic/p2p/12D3KooWNQ9tVrcb64tw6bNs2CaNrUGPM7yRrKvBBheQ5yCyPHKC";
const DEFAULT_WORMHOLE_LISTEN_ADDRS: &str = "/ip4/0.0.0.0/udp/30910/quic,/ip6/::/udp/30910/quic";
const DEFAULT_API_ADDR: &str = "127.0.0.1:33999";

/// `Options` is a structup definition to provide clean command-line args for Hermes.
#[derive(StructOpt, Debug)]
#[structopt(name = "hermes", about = "Hermes")]
mod benchmarks;
mod pythnet;
mod rpc;
mod wormhole;

// `Options` is a structup definition to provide clean command-line args for Hermes.
#[derive(Parser, Debug)]
#[command(name = crate_name!())]
#[command(author = crate_authors!())]
#[command(about = crate_description!())]
#[command(version = crate_version!())]
#[allow(clippy::large_enum_variant)]
pub enum Options {
/// Run the hermes service.
/// Run the Hermes Price Service.
Run(RunOptions),

/// Show Overridden Environment Variables.
ShowEnv(ShowEnvOptions),
}

#[derive(Clone, Debug, StructOpt)]
#[derive(Args, Clone, Debug)]
pub struct RunOptions {
/// Wormhole Options.
#[structopt(flatten)]
pub wormhole: WormholeOptions,
#[command(flatten)]
pub wormhole: wormhole::Options,

/// PythNet Options
#[structopt(flatten)]
pub pythnet: PythNetOptions,
#[command(flatten)]
pub pythnet: pythnet::Options,

/// RPC Options
#[structopt(flatten)]
pub rpc: RpcOptions,
#[command(flatten)]
pub rpc: rpc::Options,

/// Benchmarks Options
#[structopt(flatten)]
pub benchmarks: BenchmarksOptions,
}

#[derive(Clone, Debug, StructOpt)]
pub struct WormholeOptions {
/// Multiaddresses for Wormhole bootstrap peers (separated by comma).
#[structopt(long)]
#[structopt(use_delimiter = true)]
#[structopt(default_value = DEFAULT_WORMHOLE_BOOTSTRAP_ADDRS)]
#[structopt(env = "WORMHOLE_BOOTSTRAP_ADDRS")]
pub bootstrap_addrs: Vec<Multiaddr>,

/// Address of the Wormhole contract on the target PythNet cluster.
#[structopt(long)]
#[structopt(default_value = "H3fxXJ86ADW2PNuDDmZJg6mzTtPxkYCpNuQUTgmJ7AjU")]
#[structopt(env = "WORMHOLE_CONTRACT_ADDR")]
pub contract_addr: Pubkey,

/// Multiaddresses to bind Wormhole P2P to (separated by comma)
#[structopt(long)]
#[structopt(use_delimiter = true)]
#[structopt(default_value = DEFAULT_WORMHOLE_LISTEN_ADDRS)]
#[structopt(env = "WORMHOLE_LISTEN_ADDRS")]
pub listen_addrs: Vec<Multiaddr>,

/// Network ID for Wormhole
#[structopt(long)]
#[structopt(default_value = DEFAULT_NETWORK_ID)]
#[structopt(env = "WORMHOLE_NETWORK_ID")]
pub network_id: String,
}

#[derive(Clone, Debug, StructOpt)]
pub struct PythNetOptions {
/// Address of a PythNet compatible websocket RPC endpoint.
#[structopt(long)]
#[structopt(env = "PYTHNET_WS_ENDPOINT")]
pub ws_endpoint: String,

/// Addres of a PythNet compatible HTP RPC endpoint.
#[structopt(long)]
#[structopt(env = "PYTHNET_HTTP_ENDPOINT")]
pub http_endpoint: String,
}

#[derive(Clone, Debug, StructOpt)]
pub struct RpcOptions {
/// Address to bind the API server to.
#[structopt(long)]
#[structopt(default_value = DEFAULT_API_ADDR)]
#[structopt(env = "API_ADDR")]
pub addr: SocketAddr,
#[command(flatten)]
pub benchmarks: benchmarks::Options,
}

#[derive(Clone, Debug, StructOpt)]
pub struct BenchmarksOptions {
/// Benchmarks endpoint to retrieve historical update data from.
#[structopt(long)]
#[structopt(env = "BENCHMARKS_ENDPOINT")]
pub endpoint: Option<Url>,
#[derive(Args, Clone, Debug)]
pub struct ShowEnvOptions {
/// Show Hermes environment variables.
///
/// By default this command will attempt to read the variable from the environment and fall
/// back to the argument default if not present. Set this flag if you want only the defaults
/// and to ignore the current environment.
#[arg(long = "defaults")]
pub defaults: bool,
}
14 changes: 14 additions & 0 deletions hermes/src/config/benchmarks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use {
clap::Args,
reqwest::Url,
};

#[derive(Args, Clone, Debug)]
#[command(next_help_heading = "Benchmark Options")]
#[group(id = "Benchmarks")]
pub struct Options {
/// Benchmarks endpoint to retrieve historical update data from.
#[arg(long = "benchmarks-endpoint")]
#[arg(env = "BENCHMARKS_ENDPOINT")]
pub endpoint: Option<Url>,
}
Loading

0 comments on commit 26b8dcd

Please sign in to comment.