Skip to content

Commit

Permalink
feat(cli): support query transaction (#2556)
Browse files Browse the repository at this point in the history
[CLI] support query transaction
  • Loading branch information
newraina authored Sep 2, 2024
1 parent 4b23b70 commit a5fb397
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 3 deletions.
19 changes: 19 additions & 0 deletions crates/rooch-rpc-client/src/rooch_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use rooch_rpc_api::api::btc_api::BtcAPIClient;
use rooch_rpc_api::api::rooch_api::RoochAPIClient;
use rooch_rpc_api::jsonrpc_types::btc::ord::InscriptionFilterView;
use rooch_rpc_api::jsonrpc_types::btc::utxo::UTXOFilterView;
use rooch_rpc_api::jsonrpc_types::transaction_view::TransactionFilterView;
use rooch_rpc_api::jsonrpc_types::{
account_view::BalanceInfoView, transaction_view::TransactionWithInfoView,
DryRunTransactionResponseView, InscriptionPageView, UTXOPageView,
Expand Down Expand Up @@ -127,6 +128,24 @@ impl RoochRpcClient {
.await?)
}

pub async fn query_transactions(
&self,
filter: TransactionFilterView,
cursor: Option<u64>,
limit: Option<u64>,
query_options: Option<QueryOptions>,
) -> Result<TransactionWithInfoPageView> {
Ok(self
.http
.query_transactions(
filter,
cursor.map(Into::into),
limit.map(Into::into),
query_options,
)
.await?)
}

pub async fn get_transactions_by_hash(
&self,
tx_hashes: Vec<H256>,
Expand Down
29 changes: 28 additions & 1 deletion crates/rooch/src/cli_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// SPDX-License-Identifier: Apache-2.0

use async_trait::async_trait;
use clap::Parser;
use clap::{ArgGroup, Parser};
use moveos_types::h256::H256;
use rooch_key::key_derive::verify_password;
use rooch_key::keystore::account_keystore::AccountKeystore;
use rooch_rpc_client::wallet_context::WalletContext;
Expand Down Expand Up @@ -99,6 +100,32 @@ pub struct TransactionOptions {
pub(crate) session_key: Option<AuthenticationKey>,
}

#[derive(Debug, Parser)]
#[clap(group = ArgGroup::new("filter").required(true).multiple(true))]
pub struct TransactionFilterOptions {
/// Sender address
#[clap(long, value_parser=ParsedAddress::parse, group = "filter")]
pub(crate) sender: Option<ParsedAddress>,

/// Transaction's hashes
#[clap(long, value_delimiter = ',', group = "filter")]
pub(crate) tx_hashes: Option<Vec<H256>>,

/// [start-time, end-time) interval, unit: millisecond
#[clap(long, requires = "end_time", group = "filter")]
pub(crate) start_time: Option<u64>,
/// [start-time, end-time) interval, unit: millisecond
#[clap(long, requires = "start_time", group = "filter")]
pub(crate) end_time: Option<u64>,

/// [from-order, to-order) interval
#[clap(long, requires = "to_order", group = "filter")]
pub(crate) from_order: Option<u64>,
/// [from-order, to-order) interval
#[clap(long, requires = "from_order", group = "filter")]
pub(crate) to_order: Option<u64>,
}

#[derive(Debug, Parser)]
pub struct WalletContextOptions {
/// The key store password
Expand Down
1 change: 1 addition & 0 deletions crates/rooch/src/commands/transaction/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::{env, fs::File, io::Write, path::PathBuf};
pub mod build;
pub mod get_transactions_by_hash;
pub mod get_transactions_by_order;
pub mod query;
pub mod sign;
pub mod submit;

Expand Down
95 changes: 95 additions & 0 deletions crates/rooch/src/commands/transaction/commands/query.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use crate::cli_types::{CommandAction, TransactionFilterOptions, WalletContextOptions};
use async_trait::async_trait;
use clap::Parser;
use rooch_rpc_api::jsonrpc_types::transaction_view::TransactionFilterView;
use rooch_rpc_api::jsonrpc_types::{
H256View, QueryOptions, RoochAddressView, TransactionWithInfoPageView,
};
use rooch_rpc_client::wallet_context::WalletContext;
use rooch_types::error::{RoochError, RoochResult};

/// Query transactions
#[derive(Debug, Parser)]
pub struct QueryCommand {
#[clap(flatten)]
pub filter: TransactionFilterOptions,

#[clap(long)]
pub cursor: Option<u64>,

#[clap(long)]
pub limit: Option<u64>,

/// Descending order
#[clap(short = 'd', long, default_value = "false")]
pub descending_order: bool,

/// Render and return display fields
#[clap(long, default_value = "false")]
pub show_display: bool,

/// If true, filter out all match items
#[clap(long, default_value = "false")]
pub filter_out: bool,

#[clap(flatten)]
pub(crate) context_options: WalletContextOptions,
}

#[async_trait]
impl CommandAction<TransactionWithInfoPageView> for QueryCommand {
async fn execute(self) -> RoochResult<TransactionWithInfoPageView> {
let context = self.context_options.build()?;
let client = context.get_client().await?;

let filter_view = convert_to_filter_view(self.filter, &context)?;
let query_options = QueryOptions {
decode: true,
filter_out: self.filter_out,
descending: self.descending_order,
show_display: self.show_display,
};

let resp = client
.rooch
.query_transactions(filter_view, self.cursor, self.limit, Some(query_options))
.await?;

Ok(resp)
}
}

fn convert_to_filter_view(
options: TransactionFilterOptions,
context: &WalletContext,
) -> RoochResult<TransactionFilterView> {
if let Some(sender) = options.sender {
let rooch_address_view: RoochAddressView = sender
.into_rooch_address(&context.address_mapping())
.unwrap()
.into();

Ok(TransactionFilterView::Sender(rooch_address_view.into()))
} else if let Some(tx_hashes) = options.tx_hashes {
Ok(TransactionFilterView::TxHashes(
tx_hashes.into_iter().map(H256View::from).collect(),
))
} else if options.start_time.is_some() && options.end_time.is_some() {
Ok(TransactionFilterView::TimeRange {
start_time: options.start_time.unwrap().into(),
end_time: options.end_time.unwrap().into(),
})
} else if options.from_order.is_some() && options.to_order.is_some() {
Ok(TransactionFilterView::TxOrderRange {
from_order: options.from_order.unwrap().into(),
to_order: options.to_order.unwrap().into(),
})
} else {
Err(RoochError::CommandArgumentError(
"Invalid transaction filter options".to_string(),
))
}
}
6 changes: 4 additions & 2 deletions crates/rooch/src/commands/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
use crate::cli_types::CommandAction;
use crate::commands::transaction::commands::{
build::BuildCommand, get_transactions_by_hash::GetTransactionsByHashCommand,
get_transactions_by_order::GetTransactionsByOrderCommand, sign::SignCommand,
submit::SubmitCommand,
get_transactions_by_order::GetTransactionsByOrderCommand, query::QueryCommand,
sign::SignCommand, submit::SubmitCommand,
};
use async_trait::async_trait;
use clap::{Parser, Subcommand};
Expand All @@ -26,6 +26,7 @@ impl CommandAction<String> for Transaction {
match self.cmd {
TransactionCommand::GetTransactionsByOrder(cmd) => cmd.execute_serialized().await,
TransactionCommand::GetTransactionsByHash(cmd) => cmd.execute_serialized().await,
TransactionCommand::Query(cmd) => cmd.execute_serialized().await,
TransactionCommand::Build(cmd) => cmd.execute_serialized().await,
TransactionCommand::Sign(cmd) => cmd.execute_serialized().await,
TransactionCommand::Submit(cmd) => cmd.execute_serialized().await,
Expand All @@ -38,6 +39,7 @@ pub enum TransactionCommand {
Build(BuildCommand),
GetTransactionsByOrder(GetTransactionsByOrderCommand),
GetTransactionsByHash(GetTransactionsByHashCommand),
Query(QueryCommand),
Sign(SignCommand),
Submit(SubmitCommand),
}

0 comments on commit a5fb397

Please sign in to comment.