Skip to content
Snippets Groups Projects
main.rs 4.41 KiB
Newer Older
mod cache;
mod commands;
Hugo Trentesaux's avatar
Hugo Trentesaux committed
mod conf;
Hugo Trentesaux's avatar
Hugo Trentesaux committed
mod data;
mod indexer;
mod runtime_config;
mod utils;
use anyhow::anyhow;
Éloïs's avatar
Éloïs committed
use clap::Parser;
use codec::Encode;
Hugo Trentesaux's avatar
Hugo Trentesaux committed
use data::*;
Hugo Trentesaux's avatar
Hugo Trentesaux committed
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::*;
Éloïs's avatar
Éloïs committed

/// define command line arguments
Hugo Trentesaux's avatar
Hugo Trentesaux committed
#[derive(Clone, clap::Parser, Debug, Default)]
Éloïs's avatar
Éloïs committed
#[clap(author, version, about, long_about = None)]
pub struct Args {
Hugo Trentesaux's avatar
Hugo Trentesaux committed
	/// Subcommands
	#[clap(subcommand)]
	pub subcommand: Subcommand,
	/// Overwrite indexer endpoint
	#[clap(short, long)]
	indexer: Option<String>,
	/// Do not use indexer
	#[clap(long)]
	no_indexer: bool,
	/// Secret key or BIP39 mnemonic
	/// (eventually followed by derivation path)
	#[clap(short, long)]
	secret: Option<String>,
	/// Secret key format (seed, substrate)
	#[clap(short = 'S', long, default_value = SecretFormat::Substrate)]
	secret_format: SecretFormat,
	/// Address
	#[clap(short, long)]
	address: Option<String>,
	/// Overwrite duniter websocket RPC endpoint
	#[clap(short, long)]
	url: Option<String>,
	/// Chose target network
	#[clap(short, long)]
	network: Option<String>,
Éloïs's avatar
Éloïs committed
}

/// define subcommands
Hugo Trentesaux's avatar
Hugo Trentesaux committed
#[derive(Clone, Debug, clap::Subcommand, Default)]
Éloïs's avatar
Éloïs committed
pub enum Subcommand {
	#[clap(hide = true)]
	Repart {
		// Number of transactions per block to target
		target: u32,
		#[clap(short = 'o', long = "old-repart")]
		// Old/actual repartition
		actual_repart: Option<u32>,
	},
	#[clap(hide = true)]
	SpamRoll { actual_repart: usize },
Hugo Trentesaux's avatar
Hugo Trentesaux committed
	/// Get information about runtime
	RuntimeInfo,
	/// Check current block
	CurrentBlock,
	/// Account subcommands
	#[clap(subcommand)]
	Account(commands::account::Subcommand),
	/// Identity subcommands
	#[clap(subcommand)]
	Identity(commands::identity::Subcommand),
	/// Smith subcommands
	#[clap(subcommand)]
	Smith(commands::smith::Subcommand),
	/// Tech subcommands
	#[clap(subcommand)]
	Tech(commands::collective::Subcommand),
	/// Universal Dividend subcommands
	#[clap(subcommand)]
	Ud(commands::ud::Subcommand),
	/// Oneshot account subcommands
	#[clap(subcommand)]
	Oneshot(commands::oneshot::Subcommand),
Hugo Trentesaux's avatar
Hugo Trentesaux committed
	/// Indexer subcommands
	#[clap(subcommand)]
	Indexer(indexer::Subcommand),
Hugo Trentesaux's avatar
Hugo Trentesaux committed
	/// Config subcommands
	#[clap(subcommand)]
	Config(conf::Subcommand),
Éloïs's avatar
Éloïs committed
}

/// maint function
Éloïs's avatar
Éloïs committed
#[tokio::main(flavor = "current_thread")]
Hugo Trentesaux's avatar
Hugo Trentesaux committed
async fn main() -> Result<(), GcliError> {
Hugo Trentesaux's avatar
Hugo Trentesaux committed
	// init logger
	env_logger::init();
Éloïs's avatar
Éloïs committed

Hugo Trentesaux's avatar
Hugo Trentesaux committed
	// parse argument and initialize data
	let args = Args::parse();
Hugo Trentesaux's avatar
Hugo Trentesaux committed
	let mut data = Data::new(args.clone());
Éloïs's avatar
Éloïs committed

	// match subcommands
	match args.subcommand {
		Subcommand::Repart {
			target,
			actual_repart,
		} => {
			data = data.build_client().await?;
			commands::net_test::repart(
				get_keys(
					args.secret_format,
					&args.address,
					&args.secret,
					NeededKeys::Secret,
				)?
				.1
				.unwrap(),
				data.client(),
				target,
				actual_repart,
			)
			.await?
		}
		Subcommand::SpamRoll { actual_repart } => {
			data = data.build_client().await?;
			commands::net_test::spam_roll(
				get_keys(
					args.secret_format,
					&args.address,
					&args.secret,
					NeededKeys::Secret,
				)?
				.1
				.unwrap(),
				data.client(),
				actual_repart,
			)
			.await?
		}
		Subcommand::RuntimeInfo => {
			data = data.build_client().await?.fetch_system_properties().await?;
			commands::runtime::runtime_info(data).await;
		}
		Subcommand::CurrentBlock => {
			data = data.build_client().await?;
				"current block on {}: {}",
				data.cfg.duniter_endpoint,
				data.client()
					.storage()
					.fetch(&runtime::storage().system().number(), None)
					.await?
					.unwrap()
			);
		}
		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::Ud(subcommand) => commands::ud::handle_command(data, subcommand).await?,
		Subcommand::Oneshot(subcommand) => {
			commands::oneshot::handle_command(data, subcommand).await?
		}
		Subcommand::Indexer(subcommand) => indexer::handle_command(data, subcommand).await?,
Hugo Trentesaux's avatar
Hugo Trentesaux committed
		Subcommand::Config(subcommand) => conf::handle_command(data, subcommand)?,
Éloïs's avatar
Éloïs committed

	Ok(())
Éloïs's avatar
Éloïs committed
}