From 4030148546d5b683916d45889668f58fc6a5abe0 Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Tue, 9 Jan 2024 16:13:10 +0100 Subject: [PATCH] CLI: add congestion command (#1819) * CLI: add congestion command * Update cli/src/wallet_cli/mod.rs Co-authored-by: DaughterOfMars * fmt --------- Co-authored-by: DaughterOfMars --- cli/src/wallet_cli/completer.rs | 1 + cli/src/wallet_cli/mod.rs | 34 +++++++++++++++++++++++++++++++-- sdk/src/types/api/core.rs | 2 +- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/cli/src/wallet_cli/completer.rs b/cli/src/wallet_cli/completer.rs index af3bc3a843..b3f316db4f 100644 --- a/cli/src/wallet_cli/completer.rs +++ b/cli/src/wallet_cli/completer.rs @@ -17,6 +17,7 @@ const WALLET_COMMANDS: &[&str] = &[ "claim", "claimable-outputs", "clear", + "congestion", "consolidate", "create-alias-output", "create-native-token", diff --git a/cli/src/wallet_cli/mod.rs b/cli/src/wallet_cli/mod.rs index 73fdfae5bc..96d6386e7c 100644 --- a/cli/src/wallet_cli/mod.rs +++ b/cli/src/wallet_cli/mod.rs @@ -24,8 +24,8 @@ use iota_sdk::{ }, utils::ConvertTo, wallet::{ - types::OutputData, ConsolidationParams, CreateNativeTokenParams, MintNftParams, OutputsToClaim, - SendNativeTokenParams, SendNftParams, SendParams, SyncOptions, TransactionOptions, Wallet, + types::OutputData, ConsolidationParams, CreateNativeTokenParams, Error as WalletError, MintNftParams, + OutputsToClaim, SendNativeTokenParams, SendNftParams, SendParams, SyncOptions, TransactionOptions, Wallet, }, U256, }; @@ -81,6 +81,8 @@ pub enum WalletCommand { }, /// Print details about claimable outputs - if there are any. ClaimableOutputs, + /// Checks if an account is ready to issue a block. + Congestion { account_id: Option }, /// Consolidate all basic outputs into one address. Consolidate, /// Create a new account output. @@ -478,6 +480,33 @@ pub async fn claimable_outputs_command(wallet: &Wallet) -> Result<(), Error> { Ok(()) } +// `congestion` command +pub async fn congestion_command(wallet: &Wallet, account_id: Option) -> Result<(), Error> { + let account_id = { + let wallet_data = wallet.data().await; + account_id + .or_else(|| { + wallet_data + .accounts() + .next() + .map(|o| o.output.as_account().account_id_non_null(&o.output_id)) + }) + .or_else(|| { + wallet_data + .implicit_accounts() + .next() + .map(|o| AccountId::from(&o.output_id)) + }) + .ok_or(WalletError::NoAccountToIssueBlock)? + }; + + let congestion = wallet.client().get_account_congestion(&account_id).await?; + + println_log_info!("{congestion:#?}"); + + Ok(()) +} + // `consolidate` command pub async fn consolidate_command(wallet: &Wallet) -> Result<(), Error> { println_log_info!("Consolidating outputs."); @@ -1151,6 +1180,7 @@ pub async fn prompt_internal( WalletCommand::BurnNft { nft_id } => burn_nft_command(wallet, nft_id).await, WalletCommand::Claim { output_id } => claim_command(wallet, output_id).await, WalletCommand::ClaimableOutputs => claimable_outputs_command(wallet).await, + WalletCommand::Congestion { account_id } => congestion_command(wallet, account_id).await, WalletCommand::Consolidate => consolidate_command(wallet).await, WalletCommand::CreateAccountOutput => create_account_output_command(wallet).await, WalletCommand::CreateNativeToken { diff --git a/sdk/src/types/api/core.rs b/sdk/src/types/api/core.rs index 83f6391c68..804f5f7ca7 100644 --- a/sdk/src/types/api/core.rs +++ b/sdk/src/types/api/core.rs @@ -325,7 +325,7 @@ pub struct CongestionResponse { pub reference_mana_cost: u64, /// The Block Issuance Credits of the requested account. #[serde(with = "string")] - pub block_issuance_credits: u64, + pub block_issuance_credits: i128, } /// Response of POST /api/core/v3/blocks.