From dec793cc1ba43050081f2d6187d07e98dbfe959e Mon Sep 17 00:00:00 2001 From: sslivkoff Date: Wed, 27 Dec 2023 09:27:39 -0800 Subject: [PATCH 1/2] fix typo's --- SPECIFICATION.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/SPECIFICATION.md b/SPECIFICATION.md index 72edb65..5c65018 100644 --- a/SPECIFICATION.md +++ b/SPECIFICATION.md @@ -275,8 +275,8 @@ This is a basic configuration of 5 endpoints across 3 networks. It also contains TODO: Remove this comment before submitting --> -Want to satsfy all of these constraints: -- has an interface that can be expressed naturally in most common programming langauges +Want to satisfy all of these constraints: +- has an interface that can be expressed naturally in most common programming languages - able to manage large numbers of endpoints, including multiple endpoints per network and a default endpoint for each network - able to label each endpoint with metadata - able to express groupings of endpoints @@ -285,9 +285,9 @@ Want to satsfy all of these constraints: - minimize complexity Other notes: -- `global_metadata` and `endpoint_metadata` allow extra information to be stored in the config without breaking the standard. This includes api keys, rate limits, and organizational labels. This information might be specific to idiosyncratic to each application. +- `global_metadata` and `endpoint_metadata` allow extra information to be stored in the config without breaking the standard. This includes api keys, rate limits, and organizational labels. This information might be specific to each application. - `Profile`s allow different defaults to be assigned to each tool or each mode of operation. -- Allowing RPC information to be configured using either a file or an environment variable allows optimal deployment patterns across a wide range of computing environments. Each also have their own advantages, e.g. file can be used with version control whereas environment variables can be changed more dynamically. +- Allowing RPC information to be configured using either a file or an environment variable allows optimal deployment patterns across a wide range of computing environments. Each also has their own advantages, e.g. file can be used with version control whereas environment variables can be changed more dynamically. ## Backwards Compatibility From 60e04ad1218c50ba5c94d1bbeaaa426c7bffd5cb Mon Sep 17 00:00:00 2001 From: sslivkoff Date: Mon, 1 Jan 2024 10:03:21 -0800 Subject: [PATCH 2/2] refine the defaults subcommand --- rust/Cargo.lock | 10 ++- rust/crates/mesc/src/directory.rs | 11 +++ rust/crates/mesc/src/lib.rs | 3 +- rust/crates/mesc/src/types/chain_ids.rs | 14 ++++ .../mesc_cli/src/cli/subcommands/setup.rs | 81 +++++++++++++------ 5 files changed, 92 insertions(+), 27 deletions(-) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 43c8dab..89b5b42 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -1154,7 +1154,7 @@ dependencies = [ [[package]] name = "mesc" -version = "0.1.0" +version = "0.1.1" dependencies = [ "serde", "serde_json", @@ -1162,7 +1162,7 @@ dependencies = [ [[package]] name = "mesc_cli" -version = "0.1.0" +version = "0.1.1" dependencies = [ "clap", "colored_json", @@ -2439,7 +2439,9 @@ dependencies = [ [[package]] name = "toolstr" -version = "0.1.0" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b90a491925fd828fb7dd69207b247042d525257d0932dd59fb1eb41c7fee57a" dependencies = [ "chrono", "polars", @@ -2451,6 +2453,8 @@ dependencies = [ [[package]] name = "toolstr_colored" version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d335176ed9b133b9d9e226656f2f486f84ebac50250c598246ac6d6d512ac05" dependencies = [ "lazy_static", "windows-sys 0.48.0", diff --git a/rust/crates/mesc/src/directory.rs b/rust/crates/mesc/src/directory.rs index 7cf810a..383e055 100644 --- a/rust/crates/mesc/src/directory.rs +++ b/rust/crates/mesc/src/directory.rs @@ -14,6 +14,17 @@ fn known_networks() -> HashMap<&'static str, ChainId> { .collect() } +/// get chain id of given network name pub(crate) fn get_network_chain_id(network: &str) -> Option { known_networks().remove(network) } + +/// get network name of given chain id +pub fn get_network_name(chain_id: &ChainId) -> Option { + for (name, other_chain_id) in known_networks().iter() { + if chain_id == other_chain_id { + return Some(name.to_string()) + } + } + None +} diff --git a/rust/crates/mesc/src/lib.rs b/rust/crates/mesc/src/lib.rs index 493ab9a..42446e5 100644 --- a/rust/crates/mesc/src/lib.rs +++ b/rust/crates/mesc/src/lib.rs @@ -7,7 +7,8 @@ attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_variables)) ))] -mod directory; +/// directory matching chain_id's to network names +pub mod directory; mod types; mod validate; pub use types::*; diff --git a/rust/crates/mesc/src/types/chain_ids.rs b/rust/crates/mesc/src/types/chain_ids.rs index ac4bb83..943f242 100644 --- a/rust/crates/mesc/src/types/chain_ids.rs +++ b/rust/crates/mesc/src/types/chain_ids.rs @@ -20,6 +20,20 @@ impl ChainId { } } + /// convert to hex representation, zero-padded to 256 bits + pub fn to_hex_256(&self) -> Result { + let ChainId(chain_id) = self; + if chain_id.starts_with("0x") { + Ok(chain_id.clone()) + } else { + match chain_id.parse::() { + Ok(number) => Ok(format!("0x{:016x}", number)), + Err(_) => Err(MescError::IntegrityError("bad chain_id".to_string())), + } + } + } + + /// return chain_id as &str pub fn as_str(&self) -> &str { let ChainId(chain_id) = self; diff --git a/rust/crates/mesc_cli/src/cli/subcommands/setup.rs b/rust/crates/mesc_cli/src/cli/subcommands/setup.rs index a05b1c1..aecbf9e 100644 --- a/rust/crates/mesc_cli/src/cli/subcommands/setup.rs +++ b/rust/crates/mesc_cli/src/cli/subcommands/setup.rs @@ -1,5 +1,5 @@ use crate::{print_defaults, MescCliError, SetupArgs}; -use inquire::ui::{Attributes, Color, IndexPrefix, RenderConfig, StyleSheet, Styled}; +use inquire::{InquireError, ui::{Attributes, Color, IndexPrefix, RenderConfig, StyleSheet, Styled}}; use mesc::{ChainId, Endpoint, RpcConfig, TryIntoChainId}; use std::collections::{HashMap, HashSet}; use toolstr::Colorize; @@ -140,10 +140,17 @@ async fn modify_existing_config( // let mut config_write_mode: Option = None; loop { // modify config - match inquire::Select::new("What do you want to do?", options.clone()) + let input = match inquire::Select::new("What do you want to do?", options.clone()) .with_page_size(10) - .prompt()? + .prompt() { + Ok(input) => input, + Err(InquireError::OperationCanceled) => { + std::process::exit(0) + }, + Err(e) => return Err(e.into()), + }; + match input { "Setup environment" => setup_environment(&mut config, &mut config_write_mode)?, "Add new endpoint" => add_endpoint(&mut config).await?, "Modify endpoint" => modify_endpoint(&mut config)?, @@ -203,8 +210,12 @@ fn setup_environment( println!(); } else if let Some(config_write_mode) = config_write_mode { match config_write_mode { - ConfigWriteMode::Path(path) => println!(" MESC not yet enabled, but will write config to {}", path.green().bold()), - ConfigWriteMode::Env(_) => println!(" ENV mode not yet available in the interactive cli"), + ConfigWriteMode::Path(path) => { + println!(" MESC not yet enabled, but will write config to {}", path.green().bold()) + } + ConfigWriteMode::Env(_) => { + println!(" ENV mode not yet available in the interactive cli") + } }; } else { let options = @@ -438,13 +449,17 @@ fn modify_global_metadata(config: &mut RpcConfig) -> Result<(), MescCliError> { } fn modify_defaults(config: &mut RpcConfig) -> Result<(), MescCliError> { + println!(); + print_defaults(config)?; + println!(); + let options = [ "Set the default endpoint", "Set the default endpoint for network", "Add new profile", "Modify existing profile", "Print current defaults", - "Return to main menu", + "Done modifying defaults", ] .to_vec(); @@ -463,7 +478,7 @@ fn modify_defaults(config: &mut RpcConfig) -> Result<(), MescCliError> { let prompt = "Set the default endpoint for which network?"; let chain_id = select_chain_id(config, prompt)?; let prompt = "What should be the default endpoint for this network?"; - let endpoint_name = select_endpoint(config, prompt)?; + let endpoint_name = select_endpoint_of_network(config, &chain_id, prompt)?; config.network_defaults.insert(chain_id, endpoint_name); } "Add new profile" => { @@ -519,7 +534,7 @@ fn modify_defaults(config: &mut RpcConfig) -> Result<(), MescCliError> { let prompt = "Set the profile's default endpoint for which network?"; let chain_id = select_chain_id(config, prompt)?; let prompt = "What should be the default endpoint for this network?"; - let endpoint_name = select_endpoint(config, prompt)?; + let endpoint_name = select_endpoint_of_network(config, &chain_id, prompt)?; if let Some(profile) = config.profiles.get_mut(&profile_name) { profile.network_defaults.insert(chain_id, endpoint_name); } else { @@ -536,7 +551,7 @@ fn modify_defaults(config: &mut RpcConfig) -> Result<(), MescCliError> { print_defaults(config)?; println!(); } - "Return to main menu" => return Ok(()), + "Done modifying defaults" => return Ok(()), _ => return Err(MescCliError::InvalidInput("invalid input".to_string())), } } @@ -548,6 +563,22 @@ fn select_endpoint(config: &RpcConfig, prompt: &str) -> Result Result { + let mut options: Vec = config + .endpoints + .clone() + .values() + .filter(|endpoint| endpoint.chain_id.as_ref() == Some(chain_id)) + .map(|endpoint| endpoint.name.clone()) + .collect(); + options.sort(); + Ok(inquire::Select::new(prompt, options).prompt()?) +} + fn select_profile(config: &RpcConfig, prompt: &str) -> Result { let mut options: Vec<_> = config.profiles.keys().collect(); options.sort(); @@ -555,21 +586,25 @@ fn select_profile(config: &RpcConfig, prompt: &str) -> Result Result { - let mut options: Vec<_> = vec![]; - for chain_id in config.network_defaults.keys() { - options.push(chain_id.as_str()) + let mut chain_ids: HashSet = HashSet::new(); + for endpoint in config.endpoints.values() { + if let Some(chain_id) = endpoint.chain_id.as_ref() { + chain_ids.insert(chain_id.clone()); + } } - options.push("Enter a chain_id"); - options.push("Enter a network name"); - match inquire::Select::new(prompt, options).prompt()? { - "Enter a chain_id" => { - let input = inquire::Text::new("Enter a chain_id").prompt()?; - Ok(input.try_into_chain_id()?) - } - "Enter a network name" => { - todo!() - } - input => Ok(input.try_into_chain_id()?), + let mut sorted_chain_ids: Vec = chain_ids.into_iter().collect(); + sorted_chain_ids.sort_by_key(|chain_id| chain_id.to_hex_256().unwrap_or("".to_string())); + let options: Vec = sorted_chain_ids + .iter() + .map(|chain_id| match mesc::directory::get_network_name(chain_id) { + Some(name) => format!("{} ({})", chain_id.as_str(), name), + None => chain_id.to_string(), + }) + .collect(); + let input = inquire::Select::new(prompt, options.clone()).prompt()?; + match options.iter().position(|x| x == &input) { + Some(index) => Ok(sorted_chain_ids.remove(index)), + None => Err(MescCliError::Error("invalid input".to_string())), } }