From e3fdd4622f32cc6c812345144c7a960674e92fa1 Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Tue, 9 Jan 2024 15:43:36 +0100 Subject: [PATCH 1/3] CLI: add congestion command --- cli/src/wallet_cli/completer.rs | 1 + cli/src/wallet_cli/mod.rs | 29 +++++++++++++++++++++++++++-- sdk/src/types/api/core.rs | 2 +- 3 files changed, 29 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..2c23dfd9ae 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,28 @@ 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 wallet_data = wallet.data().await; + let account_id = account_id + .or(wallet_data + .accounts() + .next() + .map(|o| o.output.as_account().account_id_non_null(&o.output_id))) + .or(wallet_data + .implicit_accounts() + .next() + .map(|o| AccountId::from(&o.output_id))) + .ok_or(WalletError::NoAccountToIssueBlock)?; + drop(wallet_data); + + 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 +1175,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. From 0aa0e36eba895694951a98add2954ccda1914e20 Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Tue, 9 Jan 2024 15:48:16 +0100 Subject: [PATCH 2/3] Update cli/src/wallet_cli/mod.rs Co-authored-by: DaughterOfMars --- cli/src/wallet_cli/mod.rs | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/cli/src/wallet_cli/mod.rs b/cli/src/wallet_cli/mod.rs index 2c23dfd9ae..2d09e84ec1 100644 --- a/cli/src/wallet_cli/mod.rs +++ b/cli/src/wallet_cli/mod.rs @@ -482,18 +482,19 @@ pub async fn claimable_outputs_command(wallet: &Wallet) -> Result<(), Error> { // `congestion` command pub async fn congestion_command(wallet: &Wallet, account_id: Option) -> Result<(), Error> { - let wallet_data = wallet.data().await; - let account_id = account_id - .or(wallet_data - .accounts() - .next() - .map(|o| o.output.as_account().account_id_non_null(&o.output_id))) - .or(wallet_data - .implicit_accounts() - .next() - .map(|o| AccountId::from(&o.output_id))) - .ok_or(WalletError::NoAccountToIssueBlock)?; - drop(wallet_data); + 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?; From c8fc81da88c940104d86d6a6969a93c6c123cdb4 Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Tue, 9 Jan 2024 15:48:44 +0100 Subject: [PATCH 3/3] fmt --- cli/src/wallet_cli/mod.rs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/cli/src/wallet_cli/mod.rs b/cli/src/wallet_cli/mod.rs index 2d09e84ec1..96d6386e7c 100644 --- a/cli/src/wallet_cli/mod.rs +++ b/cli/src/wallet_cli/mod.rs @@ -483,17 +483,21 @@ pub async fn claimable_outputs_command(wallet: &Wallet) -> Result<(), Error> { // `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 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?;