diff --git a/Cargo.toml b/Cargo.toml index f14c55c5cf48dc7e0683c59b1bef32640dc3004d..a3e9876819e2421f43ac7845ff141885999bba31 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,10 +24,9 @@ subxt = { git = "https://github.com/duniter/subxt.git", branch = "duniter-subst tokio = { version = "1.26.0", features = ["macros"] } confy = "0.5.1" -# allows to build gcli with different predefined networks +# allows to build gcli for different runtimes and with different predefined networks [features] -default = ["dev"] # default feature is "dev" -dev = [] +default = ["gdev"] # default runtime is "gdev", gdev network is available gdev = [] gtest = [] g1 = [] diff --git a/src/conf.rs b/src/conf.rs index a1628a322e22e6e0eab680223d3ac1cf22bdf58c..ddedd20411daaf9b23e892a8a0775b6bbb9689ad 100644 --- a/src/conf.rs +++ b/src/conf.rs @@ -12,8 +12,8 @@ pub struct Config { impl std::default::Default for Config { fn default() -> Self { Self { - duniter_endpoint: String::from("ws://localhost:9944"), - indexer_endpoint: String::from("http://localhost:8080/v1/graphql"), + duniter_endpoint: String::from(data::LOCAL_DUNITER_ENDPOINT), + indexer_endpoint: String::from(data::LOCAL_INDEXER_ENDPOINT), } } } diff --git a/src/data.rs b/src/data.rs index 2e2c399f73c5717429422bf174c038aefb8676d6..f09fb7cf7984c5ed09d81d757ce980339f984d6f 100644 --- a/src/data.rs +++ b/src/data.rs @@ -1,6 +1,24 @@ use crate::*; use indexer::Indexer; +// consts +pub const LOCAL_DUNITER_ENDPOINT: &str = "ws://localhost:9944"; +pub const LOCAL_INDEXER_ENDPOINT: &str = "http://localhost:8080/v1/graphql"; + +#[cfg(feature = "gdev")] +pub const GDEV_DUNITER_ENDPOINTS: [&str; 5] = [ + "wss://gdev.p2p.legal:443/ws", + "wss://gdev.coinduf.eu:443/ws", + "wss://vit.fdn.org:443/ws", + "wss://gdev.cgeek.fr:443/ws", + "wss://gdev.pini.fr:443/ws", +]; +#[cfg(feature = "gdev")] +pub const GDEV_INDEXER_ENDPOINTS: [&str; 2] = [ + "https://gdev-indexer.p2p.legal/v1/graphql", + "https://hasura.gdev.coinduf.eu/v1/graphql", +]; + // data derived from command arguments /// Data of current command @@ -81,6 +99,38 @@ impl Data { // --- mutators --- /// use arguments to overwrite config pub fn overwrite_from_args(mut self) -> Self { + if let Some(network) = self.args.network.clone() { + // a network was provided as arugment + match &network[..] { + // force local endpoints (always available) + "local" => { + self.cfg.duniter_endpoint = String::from(LOCAL_DUNITER_ENDPOINT); + self.cfg.indexer_endpoint = String::from(LOCAL_INDEXER_ENDPOINT); + } + // if built with gdev feature, use gdev network + #[cfg(feature = "gdev")] + "gdev" => { + // TODO better strategy than first + self.cfg.duniter_endpoint = + String::from(*GDEV_DUNITER_ENDPOINTS.first().unwrap()); + self.cfg.indexer_endpoint = + String::from(*GDEV_INDEXER_ENDPOINTS.first().unwrap()); + } + // if built with gtest feature, use gtest network + #[cfg(feature = "gtest")] + "gtest" => { + unimplemented!(); + } + // if built with g1 feature, use g1 network + #[cfg(feature = "g1")] + "g1" => { + unimplemented!(); + } + other => { + panic!("unknown network \"{other}\""); + } + } + } if let Some(duniter_endpoint) = self.args.url.clone() { self.cfg.duniter_endpoint = duniter_endpoint; } @@ -124,10 +174,10 @@ impl Data { self.client = Some( Client::from_url(&duniter_endpoint) .await - .unwrap_or_else(|_| { + .unwrap_or_else(|e| { panic!( - "could not establish connection with the server {}", - duniter_endpoint + "could not establish connection with the server {}, due to error {}", + duniter_endpoint, dbg!(e) ) }), ); diff --git a/src/indexer.rs b/src/indexer.rs index e8e8de2f2f48ac22cbf6412b2c1f4125b65e7d4c..fd178157c7b07d3fcde0be88aea53dae77153ad3 100644 --- a/src/indexer.rs +++ b/src/indexer.rs @@ -129,7 +129,8 @@ pub async fn handle_command(data: Data, command: Subcommand) -> anyhow::Result<( } Subcommand::LatestBlock => { println!( - "latest indexed block is: {}", + "latest block indexed by {} is: {}", + data.cfg.indexer_endpoint, data.indexer().fetch_latest_block().await? ); } diff --git a/src/main.rs b/src/main.rs index 81166d86bd7bedc02808bf85de4b6b822d20d058..73ea92bb082211c302c1941d6d612244082224ea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ use keys::*; use serde::Deserialize; use sp_core::{sr25519::Pair, Pair as _, H256}; -#[cfg(feature = "dev")] +#[cfg(feature = "gdev")] #[subxt::subxt( runtime_metadata_path = "res/metadata.scale", derive_for_all_types = "Debug" @@ -93,6 +93,10 @@ pub struct Args { /// Overwrite duniter websocket RPC endpoint #[clap(short, long)] url: Option<String>, + /// Chose target network + #[clap(short, long)] + network: Option<String>, + } /// track progress of transaction on the network @@ -579,7 +583,8 @@ async fn main() -> Result<(), GcliError> { Subcommand::CurrentBlock => { data = data.build_client().await; println!( - "current block: {}", + "current block on {}: {}", + data.cfg.duniter_endpoint, data.client() .storage() .fetch(&runtime::storage().system().number(), None)