From 47148fb5699690805fd6d5c0ffed2630cfca74c0 Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Wed, 10 Jan 2024 17:47:21 +0100 Subject: [PATCH 1/6] Added fn --- cli/src/wallet_cli/mod.rs | 11 +---------- sdk/src/wallet/core/mod.rs | 8 ++++++++ sdk/src/wallet/operations/block.rs | 18 +++--------------- 3 files changed, 12 insertions(+), 25 deletions(-) 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() From 9e85efbcd4a4d9e4defbc0d161f5ab770416ec56 Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Wed, 10 Jan 2024 18:11:33 +0100 Subject: [PATCH 2/6] whyd that comment not appear grr --- sdk/src/wallet/core/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sdk/src/wallet/core/mod.rs b/sdk/src/wallet/core/mod.rs index a4bd843fa6..ae4d146c60 100644 --- a/sdk/src/wallet/core/mod.rs +++ b/sdk/src/wallet/core/mod.rs @@ -313,6 +313,8 @@ impl WalletData { .filter(|output_data| output_data.output.is_account()) } + // Returns the first possible Account id, which can be an implicit account. + // If none are found, returns AccountNotFound. pub fn first_account_id(&self) -> Result { self.accounts() .next() From 51fceb1a3b2daea2af1633b1e5b2d06483f41266 Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Wed, 17 Jan 2024 17:25:53 +0100 Subject: [PATCH 3/6] changed to option --- cli/src/wallet_cli/mod.rs | 7 ++++--- sdk/src/wallet/core/mod.rs | 5 ++--- sdk/src/wallet/operations/block.rs | 5 ++++- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/cli/src/wallet_cli/mod.rs b/cli/src/wallet_cli/mod.rs index 3a9fb899cb..6bf5635f9e 100644 --- a/cli/src/wallet_cli/mod.rs +++ b/cli/src/wallet_cli/mod.rs @@ -366,7 +366,9 @@ 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.unwrap_or(wallet_data.first_account_id()?); + let account_id = account_id + .or_else(|| wallet_data.first_account_id()) + .ok_or(WalletError::AccountNotFound)?; drop(wallet_data); let transaction = wallet.allot_mana([ManaAllotment::new(account_id, mana)?], None).await?; @@ -1033,8 +1035,7 @@ pub async fn unspent_outputs_command(wallet: &Wallet) -> Result<(), Error> { // pub async fn participation_overview_command( // wallet: &Wallet, // event_ids: Option>, -// ) -> Result<(), Error> { -// let participation_overview = wallet.get_participation_overview(event_ids).await?; +// ) -> Result<(), Error> { let participation_overview = wallet.get_participation_overview(event_ids).await?; // println_log_info!("Participation overview: {participation_overview:?}"); diff --git a/sdk/src/wallet/core/mod.rs b/sdk/src/wallet/core/mod.rs index ae4d146c60..d5458f3bba 100644 --- a/sdk/src/wallet/core/mod.rs +++ b/sdk/src/wallet/core/mod.rs @@ -315,12 +315,11 @@ impl WalletData { // Returns the first possible Account id, which can be an implicit account. // If none are found, returns AccountNotFound. - pub fn first_account_id(&self) -> Result { + pub fn first_account_id(&self) -> Option { 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) + .or_else(|| self.implicit_accounts().next().map(|o| AccountId::from(&o.output_id))) } /// Get the [`OutputData`] of an output stored in the wallet. diff --git a/sdk/src/wallet/operations/block.rs b/sdk/src/wallet/operations/block.rs index 71c3db6d9a..772a7aee1c 100644 --- a/sdk/src/wallet/operations/block.rs +++ b/sdk/src/wallet/operations/block.rs @@ -21,7 +21,10 @@ where 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().unwrap_or(wallet_data.first_account_id()?); + let issuer_id = issuer_id + .into() + .or_else(|| wallet_data.first_account_id()) + .ok_or(Error::AccountNotFound)?; drop(wallet_data); let block = self From 2abd4c9556a2015d10ccf60b3f7b10bd828f8e52 Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Wed, 17 Jan 2024 17:33:33 +0100 Subject: [PATCH 4/6] revert comment? --- cli/src/wallet_cli/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cli/src/wallet_cli/mod.rs b/cli/src/wallet_cli/mod.rs index 6bf5635f9e..c7f6a347f4 100644 --- a/cli/src/wallet_cli/mod.rs +++ b/cli/src/wallet_cli/mod.rs @@ -1035,7 +1035,8 @@ pub async fn unspent_outputs_command(wallet: &Wallet) -> Result<(), Error> { // pub async fn participation_overview_command( // wallet: &Wallet, // event_ids: Option>, -// ) -> Result<(), Error> { let participation_overview = wallet.get_participation_overview(event_ids).await?; +// ) -> Result<(), Error> { +// let participation_overview = wallet.get_participation_overview(event_ids).await?; // println_log_info!("Participation overview: {participation_overview:?}"); From e65146e3f9151d1a4e9aad2e1feaffa608264d08 Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Wed, 17 Jan 2024 17:34:23 +0100 Subject: [PATCH 5/6] revert comment2 --- cli/src/wallet_cli/mod.rs | 2 +- sdk/src/wallet/core/mod.rs | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/cli/src/wallet_cli/mod.rs b/cli/src/wallet_cli/mod.rs index c7f6a347f4..67af582d9b 100644 --- a/cli/src/wallet_cli/mod.rs +++ b/cli/src/wallet_cli/mod.rs @@ -1035,7 +1035,7 @@ pub async fn unspent_outputs_command(wallet: &Wallet) -> Result<(), Error> { // pub async fn participation_overview_command( // wallet: &Wallet, // event_ids: Option>, -// ) -> Result<(), Error> { +// ) -> Result<(), Error> { // let participation_overview = wallet.get_participation_overview(event_ids).await?; // println_log_info!("Participation overview: {participation_overview:?}"); diff --git a/sdk/src/wallet/core/mod.rs b/sdk/src/wallet/core/mod.rs index d5458f3bba..9397a9c7de 100644 --- a/sdk/src/wallet/core/mod.rs +++ b/sdk/src/wallet/core/mod.rs @@ -314,7 +314,6 @@ impl WalletData { } // Returns the first possible Account id, which can be an implicit account. - // If none are found, returns AccountNotFound. pub fn first_account_id(&self) -> Option { self.accounts() .next() From 3cacea13f3953211d3020a3fa18acf0de8fc1ba6 Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Wed, 17 Jan 2024 17:57:42 +0100 Subject: [PATCH 6/6] added to another fn --- cli/src/wallet_cli/mod.rs | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/cli/src/wallet_cli/mod.rs b/cli/src/wallet_cli/mod.rs index 67af582d9b..97c5fefa14 100644 --- a/cli/src/wallet_cli/mod.rs +++ b/cli/src/wallet_cli/mod.rs @@ -523,18 +523,7 @@ pub async fn congestion_command(wallet: &Wallet, account_id: Option) 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)) - }) + .or_else(|| wallet_data.first_account_id()) .ok_or(WalletError::AccountNotFound)? };