Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow selecting transactions by index #1033

Merged
merged 5 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
},
/// 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
DaughterOfMars marked this conversation as resolved.
Show resolved Hide resolved
selector: TransactionSelector,
},
/// List the account transactions.
#[clap(visible_alias = "txs")]
Expand Down Expand Up @@ -246,6 +247,25 @@
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 @@
}

/// `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`
DaughterOfMars marked this conversation as resolved.
Show resolved Hide resolved
}
};

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