Skip to content
Snippets Groups Projects
Commit ecbfc6cf authored by Hugo Trentesaux's avatar Hugo Trentesaux Committed by Hugo Trentesaux
Browse files

WIP add predefined network for gdev

parent 0f5721ee
No related branches found
No related tags found
No related merge requests found
...@@ -24,10 +24,9 @@ subxt = { git = "https://github.com/duniter/subxt.git", branch = "duniter-subst ...@@ -24,10 +24,9 @@ subxt = { git = "https://github.com/duniter/subxt.git", branch = "duniter-subst
tokio = { version = "1.26.0", features = ["macros"] } tokio = { version = "1.26.0", features = ["macros"] }
confy = "0.5.1" 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] [features]
default = ["dev"] # default feature is "dev" default = ["gdev"] # default runtime is "gdev", gdev network is available
dev = []
gdev = [] gdev = []
gtest = [] gtest = []
g1 = [] g1 = []
...@@ -12,8 +12,8 @@ pub struct Config { ...@@ -12,8 +12,8 @@ pub struct Config {
impl std::default::Default for Config { impl std::default::Default for Config {
fn default() -> Self { fn default() -> Self {
Self { Self {
duniter_endpoint: String::from("ws://localhost:9944"), duniter_endpoint: String::from(data::LOCAL_DUNITER_ENDPOINT),
indexer_endpoint: String::from("http://localhost:8080/v1/graphql"), indexer_endpoint: String::from(data::LOCAL_INDEXER_ENDPOINT),
} }
} }
} }
......
use crate::*; use crate::*;
use indexer::Indexer; 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 derived from command arguments
/// Data of current command /// Data of current command
...@@ -81,6 +99,38 @@ impl Data { ...@@ -81,6 +99,38 @@ impl Data {
// --- mutators --- // --- mutators ---
/// use arguments to overwrite config /// use arguments to overwrite config
pub fn overwrite_from_args(mut self) -> Self { 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() { if let Some(duniter_endpoint) = self.args.url.clone() {
self.cfg.duniter_endpoint = duniter_endpoint; self.cfg.duniter_endpoint = duniter_endpoint;
} }
...@@ -124,10 +174,10 @@ impl Data { ...@@ -124,10 +174,10 @@ impl Data {
self.client = Some( self.client = Some(
Client::from_url(&duniter_endpoint) Client::from_url(&duniter_endpoint)
.await .await
.unwrap_or_else(|_| { .unwrap_or_else(|e| {
panic!( panic!(
"could not establish connection with the server {}", "could not establish connection with the server {}, due to error {}",
duniter_endpoint duniter_endpoint, dbg!(e)
) )
}), }),
); );
......
...@@ -129,7 +129,8 @@ pub async fn handle_command(data: Data, command: Subcommand) -> anyhow::Result<( ...@@ -129,7 +129,8 @@ pub async fn handle_command(data: Data, command: Subcommand) -> anyhow::Result<(
} }
Subcommand::LatestBlock => { Subcommand::LatestBlock => {
println!( println!(
"latest indexed block is: {}", "latest block indexed by {} is: {}",
data.cfg.indexer_endpoint,
data.indexer().fetch_latest_block().await? data.indexer().fetch_latest_block().await?
); );
} }
......
...@@ -12,7 +12,7 @@ use keys::*; ...@@ -12,7 +12,7 @@ use keys::*;
use serde::Deserialize; use serde::Deserialize;
use sp_core::{sr25519::Pair, Pair as _, H256}; use sp_core::{sr25519::Pair, Pair as _, H256};
#[cfg(feature = "dev")] #[cfg(feature = "gdev")]
#[subxt::subxt( #[subxt::subxt(
runtime_metadata_path = "res/metadata.scale", runtime_metadata_path = "res/metadata.scale",
derive_for_all_types = "Debug" derive_for_all_types = "Debug"
...@@ -93,6 +93,10 @@ pub struct Args { ...@@ -93,6 +93,10 @@ pub struct Args {
/// Overwrite duniter websocket RPC endpoint /// Overwrite duniter websocket RPC endpoint
#[clap(short, long)] #[clap(short, long)]
url: Option<String>, url: Option<String>,
/// Chose target network
#[clap(short, long)]
network: Option<String>,
} }
/// track progress of transaction on the network /// track progress of transaction on the network
...@@ -579,7 +583,8 @@ async fn main() -> Result<(), GcliError> { ...@@ -579,7 +583,8 @@ async fn main() -> Result<(), GcliError> {
Subcommand::CurrentBlock => { Subcommand::CurrentBlock => {
data = data.build_client().await; data = data.build_client().await;
println!( println!(
"current block: {}", "current block on {}: {}",
data.cfg.duniter_endpoint,
data.client() data.client()
.storage() .storage()
.fetch(&runtime::storage().system().number(), None) .fetch(&runtime::storage().system().number(), None)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment