Skip to content
Snippets Groups Projects
main.rs 3.57 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,
	events::StaticEvent,
	tx::{BaseExtrinsicParamsBuilder, PairSigner, StaticTxPayload, TxPayload, 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>,
	/// Target network (local, gdev, gtest...)
	#[clap(short, long)]
	network: Option<String>,
	/// prevent waiting for extrinsic completion
	#[clap(long)]
	no_wait: bool,
É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)]
	DoNothing,
	/// Account (balance, transfer...)
	#[clap(subcommand)]
	Account(commands::account::Subcommand),
	/// Identity (get, create, confirm, revoke...)
	#[clap(subcommand)]
	Identity(commands::identity::Subcommand),
	/// Smith (certify, go-online, go-offline...)
	#[clap(subcommand)]
	Smith(commands::smith::Subcommand),
	/// Tech (list members, proposals, vote...)
	#[clap(subcommand)]
	Tech(commands::collective::Subcommand),
	/// Universal Dividend (claim...)
	#[clap(subcommand)]
	Ud(commands::ud::Subcommand),
	/// Oneshot account (balance, create, consume...)
	#[clap(subcommand)]
	Oneshot(commands::oneshot::Subcommand),
	/// Blockchain (current block, runtime info...)
	#[clap(subcommand)]
	Blockchain(commands::blockchain::Subcommand),
	/// Indexer (check, latest block)
Hugo Trentesaux's avatar
Hugo Trentesaux committed
	#[clap(subcommand)]
	Indexer(indexer::Subcommand),
	/// Config (show, save...)
Hugo Trentesaux's avatar
Hugo Trentesaux committed
	#[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 data = Data::new(Args::parse());
Éloïs's avatar
Éloïs committed

	// match subcommands
	match data.args.subcommand.clone() {
		Subcommand::DoNothing => Ok(()),
		Subcommand::Account(subcommand) => {
Hugo Trentesaux's avatar
Hugo Trentesaux committed
			commands::account::handle_command(data, subcommand).await
		}
		Subcommand::Identity(subcommand) => {
Hugo Trentesaux's avatar
Hugo Trentesaux committed
			commands::identity::handle_command(data, subcommand).await
Hugo Trentesaux's avatar
Hugo Trentesaux committed
		Subcommand::Smith(subcommand) => commands::smith::handle_command(data, subcommand).await,
		Subcommand::Tech(subcommand) => {
Hugo Trentesaux's avatar
Hugo Trentesaux committed
			commands::collective::handle_command(data, subcommand).await
Hugo Trentesaux's avatar
Hugo Trentesaux committed
		Subcommand::Ud(subcommand) => commands::ud::handle_command(data, subcommand).await,
		Subcommand::Oneshot(subcommand) => {
Hugo Trentesaux's avatar
Hugo Trentesaux committed
			commands::oneshot::handle_command(data, subcommand).await
		Subcommand::Blockchain(subcommand) => {
Hugo Trentesaux's avatar
Hugo Trentesaux committed
			commands::blockchain::handle_command(data, subcommand).await
Hugo Trentesaux's avatar
Hugo Trentesaux committed
		Subcommand::Indexer(subcommand) => indexer::handle_command(data, subcommand).await,
		Subcommand::Config(subcommand) => conf::handle_command(data, subcommand),
	}
	.map_err(|e| dbg!(e).into())
Éloïs's avatar
Éloïs committed
}