From d884fa8e0c2d0a968b02d0c2b8aea7de60fb75c9 Mon Sep 17 00:00:00 2001 From: Alex Coats Date: Thu, 17 Aug 2023 09:46:43 -0400 Subject: [PATCH 1/5] Allow selecting transactions by index --- cli/src/account.rs | 2 +- cli/src/command/account.rs | 46 ++++++++++++++++++++++++++++---------- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/cli/src/account.rs b/cli/src/account.rs index f5a63acfa1..8491b370a1 100644 --- a/cli/src/account.rs +++ b/cli/src/account.rs @@ -172,7 +172,7 @@ pub async fn account_prompt_internal( } => send_native_token_command(account, address, token_id, amount, gift_storage_deposit).await, AccountCommand::SendNft { address, nft_id } => send_nft_command(account, address, nft_id).await, AccountCommand::Sync => sync_command(account).await, - AccountCommand::Transaction { transaction_id } => transaction_command(account, &transaction_id).await, + AccountCommand::Transaction { selector } => transaction_command(account, selector).await, AccountCommand::Transactions { show_details } => transactions_command(account, show_details).await, AccountCommand::UnspentOutputs => unspent_outputs_command(account).await, AccountCommand::Vote { event_id, answers } => vote_command(account, event_id, answers).await, diff --git a/cli/src/command/account.rs b/cli/src/command/account.rs index 99473f6c53..2e521c01a1 100644 --- a/cli/src/command/account.rs +++ b/cli/src/command/account.rs @@ -195,11 +195,12 @@ pub enum AccountCommand { }, /// Synchronize the account. Sync, - /// Show the details of the transaction. + /// Show the details of a transaction. #[clap(visible_alias = "tx")] Transaction { - /// Transaction ID to be displayed e.g. 0x84fe6b1796bddc022c9bc40206f0a692f4536b02aa8c13140264e2e01a3b7e4b. - transaction_id: String, + /// Selector for transaction. + /// Either by ID (e.g. 0x84fe6b1796bddc022c9bc40206f0a692f4536b02aa8c13140264e2e01a3b7e4b) or index + selector: TransactionSelector, }, /// List the account transactions. #[clap(visible_alias = "txs")] @@ -246,6 +247,25 @@ pub enum AccountCommand { VotingOutput, } +/// Select by transaction ID or list index +#[derive(Debug, Copy, Clone)] +pub enum TransactionSelector { + Id(TransactionId), + Index(usize), +} + +impl FromStr for TransactionSelector { + type Err = Error; + + fn from_str(s: &str) -> Result { + Ok(if let Ok(index) = s.parse() { + Self::Index(index) + } else { + Self::Id(s.parse()?) + }) + } +} + /// `addresses` command pub async fn addresses_command(account: &Account) -> Result<(), Error> { let addresses = account.addresses().await?; @@ -741,15 +761,17 @@ pub async fn sync_command(account: &Account) -> Result<(), Error> { } /// `transaction` command -pub async fn transaction_command(account: &Account, transaction_id_str: &str) -> Result<(), Error> { - let transaction_id = TransactionId::from_str(transaction_id_str)?; - let maybe_transaction = account - .transactions() - .await - .into_iter() - .find(|tx| tx.transaction_id == transaction_id); - - if let Some(tx) = maybe_transaction { +pub async fn transaction_command(account: &Account, selector: TransactionSelector) -> Result<(), Error> { + let mut transactions = account.transactions().await; + let transaction = match selector { + TransactionSelector::Id(id) => transactions.into_iter().find(|tx| tx.transaction_id == id), + TransactionSelector::Index(index) => { + transactions.sort_by(|a, b| a.timestamp.cmp(&b.timestamp)); + transactions.into_iter().skip(index).next() + } + }; + + if let Some(tx) = transaction { println_log_info!("{:#?}", tx); } else { println_log_info!("No transaction found"); From 665cb3b6c35cad2790bdafb8fc7eedf040a2acc2 Mon Sep 17 00:00:00 2001 From: Alexandcoats Date: Thu, 17 Aug 2023 10:41:21 -0400 Subject: [PATCH 2/5] Update cli/src/command/account.rs Co-authored-by: Thoralf-M <46689931+Thoralf-M@users.noreply.github.com> --- cli/src/command/account.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/src/command/account.rs b/cli/src/command/account.rs index 2e521c01a1..5b0aa1737f 100644 --- a/cli/src/command/account.rs +++ b/cli/src/command/account.rs @@ -767,7 +767,7 @@ pub async fn transaction_command(account: &Account, selector: TransactionSelecto TransactionSelector::Id(id) => transactions.into_iter().find(|tx| tx.transaction_id == id), TransactionSelector::Index(index) => { transactions.sort_by(|a, b| a.timestamp.cmp(&b.timestamp)); - transactions.into_iter().skip(index).next() + transactions.into_iter().nth(index) } }; From 3fba6bccffa5b2eaca2b8bb1a7e41dbd884b350e Mon Sep 17 00:00:00 2001 From: Alex Coats Date: Thu, 17 Aug 2023 10:44:45 -0400 Subject: [PATCH 3/5] changelog --- cli/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index a742c457d3..417e0ca9c4 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `WalletCommand::Accounts` variant to list all available accounts in a wallet; - `addresses` now additionally prints the hex version of the address; - `outputs`, `unspent-outputs` print a list that includes number and type of the output; +- `AccountCommand::Transaction` now accepts either an index or an ID; ## 1.0.0 - 2023-07-27 From 24c84d4b301380a89c79d6d71cc090dcd5dbdea5 Mon Sep 17 00:00:00 2001 From: Alex Coats Date: Thu, 17 Aug 2023 10:51:27 -0400 Subject: [PATCH 4/5] more changelog --- cli/CHANGELOG.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index 417e0ca9c4..46bb8e0c8c 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -21,15 +21,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## 1.1.0 - 2023-MM-DD -### Changed - -- `WalletCommand::Mnemonic` now takes 2 optional arguments to avoid user interaction; - ### Added - `WalletCommand::Accounts` variant to list all available accounts in a wallet; - `addresses` now additionally prints the hex version of the address; - `outputs`, `unspent-outputs` print a list that includes number and type of the output; + +### Changed + +- `WalletCommand::Mnemonic` now takes 2 optional arguments to avoid user interaction; - `AccountCommand::Transaction` now accepts either an index or an ID; ## 1.0.0 - 2023-07-27 From a793ad7b436edd00f1359d7d0c4fafad9d90ecee Mon Sep 17 00:00:00 2001 From: Alexandcoats Date: Thu, 17 Aug 2023 10:55:56 -0400 Subject: [PATCH 5/5] Update cli/src/command/account.rs Co-authored-by: Thibault Martinez --- cli/src/command/account.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/src/command/account.rs b/cli/src/command/account.rs index 5b0aa1737f..fddb6dc806 100644 --- a/cli/src/command/account.rs +++ b/cli/src/command/account.rs @@ -199,7 +199,7 @@ pub enum AccountCommand { #[clap(visible_alias = "tx")] Transaction { /// Selector for transaction. - /// Either by ID (e.g. 0x84fe6b1796bddc022c9bc40206f0a692f4536b02aa8c13140264e2e01a3b7e4b) or index + /// Either by ID (e.g. 0x84fe6b1796bddc022c9bc40206f0a692f4536b02aa8c13140264e2e01a3b7e4b) or index. selector: TransactionSelector, }, /// List the account transactions.