diff --git a/cli/src/wallet_cli/mod.rs b/cli/src/wallet_cli/mod.rs index 88616d9bb4..156ba0a56b 100644 --- a/cli/src/wallet_cli/mod.rs +++ b/cli/src/wallet_cli/mod.rs @@ -356,16 +356,7 @@ pub async fn address_command(wallet: &Wallet) -> Result<(), Error> { // `allot-mana` command pub async fn allot_mana_command(wallet: &Wallet, mana: u64, 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::AccountNotFound)?; + let account_id = account_id.unwrap_or(wallet_data.first_account_id()?); drop(wallet_data); let transaction = wallet.allot_mana([ManaAllotment::new(account_id, mana)?], None).await?; diff --git a/sdk/src/wallet/core/mod.rs b/sdk/src/wallet/core/mod.rs index 16db6cdf27..a4bd843fa6 100644 --- a/sdk/src/wallet/core/mod.rs +++ b/sdk/src/wallet/core/mod.rs @@ -313,6 +313,14 @@ impl WalletData { .filter(|output_data| output_data.output.is_account()) } + pub fn first_account_id(&self) -> Result { + self.accounts() + .next() + .map(|o| o.output.as_account().account_id_non_null(&o.output_id)) + .or(self.implicit_accounts().next().map(|o| AccountId::from(&o.output_id))) + .ok_or(Error::AccountNotFound) + } + /// Get the [`OutputData`] of an output stored in the wallet. pub fn get_output(&self, output_id: &OutputId) -> Option<&OutputData> { self.outputs.get(output_id) diff --git a/sdk/src/wallet/operations/block.rs b/sdk/src/wallet/operations/block.rs index 4c3d269202..71c3db6d9a 100644 --- a/sdk/src/wallet/operations/block.rs +++ b/sdk/src/wallet/operations/block.rs @@ -18,23 +18,11 @@ where issuer_id: impl Into> + Send, ) -> Result { log::debug!("submit_basic_block"); + let wallet_data = self.data().await; // If an issuer ID is provided, use it; otherwise, use the first available account or implicit account. - let issuer_id = issuer_id - .into() - .or(self - .data() - .await - .accounts() - .next() - .map(|o| o.output.as_account().account_id_non_null(&o.output_id))) - .or(self - .data() - .await - .implicit_accounts() - .next() - .map(|o| AccountId::from(&o.output_id))) - .ok_or(Error::AccountNotFound)?; + let issuer_id = issuer_id.into().unwrap_or(wallet_data.first_account_id()?); + drop(wallet_data); let block = self .client()