Skip to content

Commit

Permalink
Allow selecting transactions by index
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Coats committed Aug 17, 2023
1 parent 7c40a44 commit d884fa8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
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().skip(index).next()

Check failure on line 770 in cli/src/command/account.rs

View workflow job for this annotation

GitHub Actions / Clippy Results for the Rust Core

called `skip(..).next()` on an iterator

error: called `skip(..).next()` on an iterator --> cli/src/command/account.rs:770:37 | 770 | transactions.into_iter().skip(index).next() | ^^^^^^^^^^^^^^^^^^^ help: use `nth` instead: `.nth(index)` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#iter_skip_next = note: `-D clippy::iter-skip-next` implied by `-D warnings`
}
};

if let Some(tx) = transaction {
println_log_info!("{:#?}", tx);
} else {
println_log_info!("No transaction found");
Expand Down

0 comments on commit d884fa8

Please sign in to comment.