From 9359064303034618af157e4cd820f58aede22023 Mon Sep 17 00:00:00 2001
From: Hugo Trentesaux <hugo@trentesaux.fr>
Date: Wed, 7 Jun 2023 15:16:57 +0200
Subject: [PATCH] split main in runtime and utils

---
 src/main.rs           | 134 ++++++------------------------------------
 src/runtime_config.rs |  53 +++++++++++++++++
 src/utils.rs          |  55 +++++++++++++++++
 3 files changed, 126 insertions(+), 116 deletions(-)
 create mode 100644 src/runtime_config.rs
 create mode 100644 src/utils.rs

diff --git a/src/main.rs b/src/main.rs
index 6074dc4..ce62d6e 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -4,73 +4,22 @@ mod conf;
 mod data;
 mod indexer;
 mod keys;
+mod runtime_config;
+mod utils;
 
 use anyhow::anyhow;
 use clap::Parser;
 use codec::Encode;
 use data::*;
 use keys::*;
+use runtime_config::*;
 use serde::Deserialize;
 use sp_core::{sr25519::Pair, Pair as _};
 use subxt::blocks::ExtrinsicEvents;
 use subxt::tx::{BaseExtrinsicParamsBuilder, PairSigner, TxStatus};
+use utils::*;
 
-#[cfg(feature = "gdev")]
-#[subxt::subxt(
-	runtime_metadata_path = "res/metadata.scale",
-	derive_for_all_types = "Debug"
-)]
-pub mod runtime {
-	// IF NEEDED
-	// #[subxt(substitute_type = "spcore::sr25519::Signature")]
-	// use crate::gdev::runtime_types::sp_core::sr25519::Signature;
-}
-
-// declare custom types
-pub type Client = subxt::OnlineClient<Runtime>;
-pub type AccountId = subxt::ext::sp_runtime::AccountId32;
-pub type TxInBlock = subxt::tx::TxInBlock<Runtime, Client>;
-pub type TxProgress = subxt::tx::TxProgress<Runtime, Client>;
-pub type Balance = u64;
-pub type AccountData = runtime::runtime_types::pallet_duniter_account::types::AccountData<Balance>;
-pub type AccountInfo = runtime::runtime_types::frame_system::AccountInfo<u32, AccountData>;
-pub type Hash = sp_core::H256;
-
-// declare runtime types
-pub enum Runtime {}
-impl subxt::config::Config for Runtime {
-	type Index = u32;
-	type BlockNumber = u32;
-	type Hash = Hash;
-	type Hashing = subxt::ext::sp_runtime::traits::BlakeTwo256;
-	type AccountId = AccountId;
-	type Address = subxt::ext::sp_runtime::MultiAddress<Self::AccountId, u32>;
-	type Header = subxt::ext::sp_runtime::generic::Header<
-		Self::BlockNumber,
-		subxt::ext::sp_runtime::traits::BlakeTwo256,
-	>;
-	type Signature = subxt::ext::sp_runtime::MultiSignature;
-	type ExtrinsicParams = subxt::tx::BaseExtrinsicParams<Self, Tip>;
-}
-
-// Tip for transaction fee
-#[derive(Copy, Clone, Debug, Default, codec::Encode)]
-pub struct Tip {
-	#[codec(compact)]
-	tip: u64,
-}
-impl Tip {
-	pub fn new(amount: u64) -> Self {
-		Tip { tip: amount }
-	}
-}
-impl From<u64> for Tip {
-	fn from(n: u64) -> Self {
-		Self::new(n)
-	}
-}
-
-// define command line arguments
+/// define command line arguments
 #[derive(Clone, clap::Parser, Debug, Default)]
 #[clap(author, version, about, long_about = None)]
 pub struct Args {
@@ -101,60 +50,7 @@ pub struct Args {
 	network: Option<String>,
 }
 
-/// track progress of transaction on the network
-/// until it is in block with success or failure
-pub async fn track_progress(
-	mut progress: TxProgress,
-) -> Result<ExtrinsicEvents<Runtime>, subxt::Error> {
-	loop {
-		if let Some(status) = progress.next_item().await {
-			match status? {
-				TxStatus::Ready => {
-					println!("transaction submitted to the network, waiting 6 seconds...");
-				}
-				TxStatus::InBlock(in_block) => break in_block,
-				TxStatus::Invalid => {
-					println!("Invalid");
-				}
-				_ => continue,
-			}
-		}
-	}
-	.wait_for_success()
-	.await
-}
-
-/// custom error type intended to provide more convenient error message to user
-#[derive(Debug)]
-pub enum GcliError {
-	/// error coming from subxt
-	Subxt(subxt::Error),
-	/// error coming from duniter
-	Duniter(String),
-	/// error coming from indexer
-	Indexer(String),
-	/// logic error (illegal operation or security)
-	Logic(String),
-	/// error coming from anyhow
-	Anyhow(anyhow::Error),
-}
-impl std::fmt::Display for GcliError {
-	fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
-		write!(f, "{:?}", self)
-	}
-}
-impl std::error::Error for GcliError {}
-impl From<subxt::Error> for GcliError {
-	fn from(e: subxt::Error) -> GcliError {
-		GcliError::Subxt(e)
-	}
-}
-impl From<anyhow::Error> for GcliError {
-	fn from(e: anyhow::Error) -> GcliError {
-		GcliError::Anyhow(e)
-	}
-}
-
+/// define subcommands
 #[derive(Clone, Debug, clap::Subcommand, Default)]
 pub enum Subcommand {
 	#[clap(hide = true)]
@@ -166,9 +62,7 @@ pub enum Subcommand {
 		actual_repart: Option<u32>,
 	},
 	#[clap(hide = true)]
-	SpamRoll {
-		actual_repart: usize,
-	},
+	SpamRoll { actual_repart: usize },
 	/// Get information about runtime
 	RuntimeInfo,
 	#[default]
@@ -200,6 +94,7 @@ pub enum Subcommand {
 	Config(conf::Subcommand),
 }
 
+/// maint function
 #[tokio::main(flavor = "current_thread")]
 async fn main() -> Result<(), GcliError> {
 	// init logger
@@ -209,6 +104,7 @@ async fn main() -> Result<(), GcliError> {
 	let args = Args::parse();
 	let mut data = Data::new(args.clone());
 
+	// match subcommands
 	match args.subcommand {
 		Subcommand::Repart {
 			target,
@@ -262,10 +158,16 @@ async fn main() -> Result<(), GcliError> {
 					.unwrap()
 			);
 		}
-		Subcommand::Account(subcommand) => commands::account::handle_command(data, subcommand).await?,
-		Subcommand::Identity(subcommand) => commands::identity::handle_command(data, subcommand).await?,
+		Subcommand::Account(subcommand) => {
+			commands::account::handle_command(data, subcommand).await?
+		}
+		Subcommand::Identity(subcommand) => {
+			commands::identity::handle_command(data, subcommand).await?
+		}
 		Subcommand::Smith(subcommand) => commands::smith::handle_command(data, subcommand).await?,
-		Subcommand::Tech(subcommand) => commands::collective::handle_command(data, subcommand).await?,
+		Subcommand::Tech(subcommand) => {
+			commands::collective::handle_command(data, subcommand).await?
+		}
 		Subcommand::Ud(subcommand) => commands::ud::handle_command(data, subcommand).await?,
 		Subcommand::Oneshot(subcommand) => {
 			commands::oneshot::handle_command(data, subcommand).await?
diff --git a/src/runtime_config.rs b/src/runtime_config.rs
new file mode 100644
index 0000000..e1c2a19
--- /dev/null
+++ b/src/runtime_config.rs
@@ -0,0 +1,53 @@
+#[cfg(feature = "gdev")]
+#[subxt::subxt(
+	runtime_metadata_path = "res/metadata.scale",
+	derive_for_all_types = "Debug"
+)]
+pub mod runtime {
+	// IF NEEDED
+	// #[subxt(substitute_type = "spcore::sr25519::Signature")]
+	// use crate::gdev::runtime_types::sp_core::sr25519::Signature;
+}
+
+// declare custom types
+pub type Client = subxt::OnlineClient<Runtime>;
+pub type AccountId = subxt::ext::sp_runtime::AccountId32;
+pub type TxProgress = subxt::tx::TxProgress<Runtime, Client>;
+pub type Balance = u64;
+pub type AccountData = runtime::runtime_types::pallet_duniter_account::types::AccountData<Balance>;
+pub type AccountInfo = runtime::runtime_types::frame_system::AccountInfo<u32, AccountData>;
+pub type Hash = sp_core::H256;
+
+// declare runtime types
+pub enum Runtime {}
+impl subxt::config::Config for Runtime {
+	type Index = u32;
+	type BlockNumber = u32;
+	type Hash = Hash;
+	type Hashing = subxt::ext::sp_runtime::traits::BlakeTwo256;
+	type AccountId = AccountId;
+	type Address = subxt::ext::sp_runtime::MultiAddress<Self::AccountId, u32>;
+	type Header = subxt::ext::sp_runtime::generic::Header<
+		Self::BlockNumber,
+		subxt::ext::sp_runtime::traits::BlakeTwo256,
+	>;
+	type Signature = subxt::ext::sp_runtime::MultiSignature;
+	type ExtrinsicParams = subxt::tx::BaseExtrinsicParams<Self, Tip>;
+}
+
+// Tip for transaction fee
+#[derive(Copy, Clone, Debug, Default, codec::Encode)]
+pub struct Tip {
+	#[codec(compact)]
+	tip: u64,
+}
+impl Tip {
+	pub fn new(amount: u64) -> Self {
+		Tip { tip: amount }
+	}
+}
+impl From<u64> for Tip {
+	fn from(n: u64) -> Self {
+		Self::new(n)
+	}
+}
diff --git a/src/utils.rs b/src/utils.rs
new file mode 100644
index 0000000..ca86c5a
--- /dev/null
+++ b/src/utils.rs
@@ -0,0 +1,55 @@
+use crate::*;
+
+/// track progress of transaction on the network
+/// until it is in block with success or failure
+pub async fn track_progress(
+	mut progress: TxProgress,
+) -> Result<ExtrinsicEvents<Runtime>, subxt::Error> {
+	loop {
+		if let Some(status) = progress.next_item().await {
+			match status? {
+				TxStatus::Ready => {
+					println!("transaction submitted to the network, waiting 6 seconds...");
+				}
+				TxStatus::InBlock(in_block) => break in_block,
+				TxStatus::Invalid => {
+					println!("Invalid");
+				}
+				_ => continue,
+			}
+		}
+	}
+	.wait_for_success()
+	.await
+}
+
+/// custom error type intended to provide more convenient error message to user
+#[derive(Debug)]
+pub enum GcliError {
+	/// error coming from subxt
+	Subxt(subxt::Error),
+	/// error coming from duniter
+	Duniter(String),
+	/// error coming from indexer
+	Indexer(String),
+	/// logic error (illegal operation or security)
+	Logic(String),
+	/// error coming from anyhow
+	Anyhow(anyhow::Error),
+}
+impl std::fmt::Display for GcliError {
+	fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
+		write!(f, "{:?}", self)
+	}
+}
+impl std::error::Error for GcliError {}
+impl From<subxt::Error> for GcliError {
+	fn from(e: subxt::Error) -> GcliError {
+		GcliError::Subxt(e)
+	}
+}
+impl From<anyhow::Error> for GcliError {
+	fn from(e: anyhow::Error) -> GcliError {
+		GcliError::Anyhow(e)
+	}
+}
-- 
GitLab