Skip to content

Commit

Permalink
Allow selecting transactions by index (#1033)
Browse files Browse the repository at this point in the history
* Allow selecting transactions by index

* Update cli/src/command/account.rs

Co-authored-by: Thoralf-M <[email protected]>

* changelog

* more changelog

* Update cli/src/command/account.rs

Co-authored-by: Thibault Martinez <[email protected]>

---------

Co-authored-by: Thoralf-M <[email protected]>
Co-authored-by: Thibault Martinez <[email protected]>
  • Loading branch information
3 people authored Aug 17, 2023
1 parent 7c40a44 commit af19a14
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
9 changes: 5 additions & 4 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
2 changes: 1 addition & 1 deletion cli/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
46 changes: 34 additions & 12 deletions cli/src/command/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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<Self, Self::Err> {
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?;
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit af19a14

Please sign in to comment.