diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index a742c457d3..46bb8e0c8c 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -21,16 +21,17 @@ 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 First release of the `cli-wallet`. 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..fddb6dc806 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().nth(index) + } + }; + + if let Some(tx) = transaction { println_log_info!("{:#?}", tx); } else { println_log_info!("No transaction found");