Skip to main content

quantus_cli/cli/
treasury.rs

1//! `quantus treasury` subcommand – Treasury account info
2//!
3//! The chain Treasury is a single account that receives a configurable portion of mining rewards.
4//! This command shows the treasury account and its balance.
5use crate::{chain::quantus_subxt, cli::address_format::QuantusSS58, log_print};
6use clap::Subcommand;
7use colored::Colorize;
8
9/// Treasury commands
10#[derive(Subcommand, Debug)]
11pub enum TreasuryCommands {
12	/// Show Treasury account and balance
13	Info,
14}
15
16/// Handle treasury commands
17pub async fn handle_treasury_command(
18	command: TreasuryCommands,
19	node_url: &str,
20	_execution_mode: crate::cli::common::ExecutionMode,
21) -> crate::error::Result<()> {
22	let quantus_client = crate::chain::client::QuantusClient::new(node_url).await?;
23
24	match command {
25		TreasuryCommands::Info => show_treasury_info(&quantus_client).await,
26	}
27}
28
29/// Show Treasury account, portion and balance
30async fn show_treasury_info(
31	quantus_client: &crate::chain::client::QuantusClient,
32) -> crate::error::Result<()> {
33	log_print!("💰 Treasury");
34	log_print!("");
35
36	let latest_block_hash = quantus_client.get_latest_block().await?;
37	let storage_at = quantus_client.client().storage().at(latest_block_hash);
38
39	// Treasury account from pallet storage (receives mining rewards)
40	let treasury_account_addr = quantus_subxt::api::storage().treasury_pallet().treasury_account();
41	let treasury_account = storage_at.fetch(&treasury_account_addr).await?.ok_or_else(|| {
42		crate::error::QuantusError::Generic("Treasury account not set in storage".to_string())
43	})?;
44
45	// Portion of mining rewards that goes to treasury (Permill: parts per million)
46	let portion_addr = quantus_subxt::api::storage().treasury_pallet().treasury_portion();
47	let portion = storage_at.fetch(&portion_addr).await?.map(|p| p.0).unwrap_or(0);
48
49	// Account balance
50	let account_storage = quantus_subxt::api::storage().system().account(treasury_account.clone());
51	let account_info = storage_at.fetch(&account_storage).await?.ok_or_else(|| {
52		crate::error::QuantusError::Generic("Treasury account not found in system".to_string())
53	})?;
54
55	let free = account_info.data.free;
56	let reserved = account_info.data.reserved;
57
58	let formatted_free = crate::cli::send::format_balance_with_symbol(quantus_client, free).await?;
59	let formatted_reserved =
60		crate::cli::send::format_balance_with_symbol(quantus_client, reserved).await?;
61
62	let account_ss58 = treasury_account.to_quantus_ss58();
63	log_print!("📍 Account: {}", account_ss58.bright_yellow());
64	// Permill is parts per million, so divide by 10000 to get percentage
65	let portion_percent = portion as f64 / 10000.0;
66	log_print!("📊 Reward portion: {:.2}%", portion_percent.to_string().bright_cyan());
67	log_print!("💰 Free: {}", formatted_free);
68	log_print!("💰 Reserved: {}", formatted_reserved);
69
70	Ok(())
71}