From 4136c31fd952ab051cbd13a64a4646187332c892 Mon Sep 17 00:00:00 2001 From: EthanYuan Date: Fri, 2 Feb 2024 13:16:36 +0800 Subject: [PATCH] impl get_transactions. --- src/subcommands/rpc.rs | 64 ++++++++++++++++++++++++++++++++++++++++- src/utils/rpc/client.rs | 16 +++++++++++ src/utils/rpc/types.rs | 16 +++++++++++ 3 files changed, 95 insertions(+), 1 deletion(-) diff --git a/src/subcommands/rpc.rs b/src/subcommands/rpc.rs index 9c5427ea..d7afa993 100644 --- a/src/subcommands/rpc.rs +++ b/src/subcommands/rpc.rs @@ -384,7 +384,39 @@ impl<'a> RpcSubCommand<'a> { .about("Pagination parameter") ) .about("Returns the live cells collection by the lock or type script"), - App::new("get_cells_capacity") + App::new("get_transactions") + .arg( + Arg::with_name("json-path") + .long("json-path") + .takes_value(true) + .validator(|input| FilePathParser::new(true).validate(input)) + .required(true) + .about("Indexer search key")) + .arg( + Arg::with_name("order") + .long("order") + .takes_value(true) + .possible_values(&["asc", "desc"]) + .required(true) + .about("Indexer search order") + ) + .arg( + Arg::with_name("limit") + .long("limit") + .takes_value(true) + .validator(|input| FromStrParser::::default().validate(input)) + .required(true) + .about("Limit the number of results") + ) + .arg( + Arg::with_name("after") + .long("after") + .takes_value(true) + .validator(|input| HexParser.validate(input)) + .about("Pagination parameter") + ) + .about("Returns the transactions collection by the lock or type script"), + App::new("get_cells_capacity") .arg( Arg::with_name("json-path") .long("json-path") @@ -1152,6 +1184,36 @@ impl<'a> CliSubCommand for RpcSubCommand<'a> { Ok(Output::new_output(resp)) } } + ("get_transactions", Some(m)) => { + let json_path: PathBuf = FilePathParser::new(true) + .from_matches_opt(m, "json-path")? + .expect("json-path is required"); + let content = fs::read_to_string(json_path).map_err(|err| err.to_string())?; + let search_key = serde_json::from_str(&content).map_err(|err| err.to_string())?; + let order_str = m.value_of("order").expect("order is required"); + let order = parse_order(order_str)?; + let limit: u32 = FromStrParser::::default().from_matches(m, "limit")?; + let after_opt: Option = HexParser + .from_matches_opt::(m, "after")? + .map(JsonBytes::from_bytes); + + let is_raw_data = is_raw_data || m.is_present("raw-data"); + if is_raw_data { + let resp = self + .raw_rpc_client + .get_transactions(search_key, order, limit.into(), after_opt) + .map_err(|err| err.to_string())?; + Ok(Output::new_output(resp)) + } else { + let resp = self.rpc_client.get_transactions( + search_key, + order, + limit.into(), + after_opt, + )?; + Ok(Output::new_output(resp)) + } + } ("get_cells_capacity", Some(m)) => { let json_path: PathBuf = FilePathParser::new(true) .from_matches_opt(m, "json-path")? diff --git a/src/utils/rpc/client.rs b/src/utils/rpc/client.rs index c9ba8581..30594cad 100644 --- a/src/utils/rpc/client.rs +++ b/src/utils/rpc/client.rs @@ -438,6 +438,22 @@ impl HttpRpcClient { .map_err(|err| err.to_string()) } + pub fn get_transactions( + &mut self, + search_key: SearchKey, + order: Order, + limit: Uint32, + after: Option, + ) -> Result, String> { + self.client + .get_transactions(search_key, order, limit, after) + .map(|p| Pagination { + objects: p.objects.into_iter().map(Into::into).collect(), + last_cursor: p.last_cursor, + }) + .map_err(|err| err.to_string()) + } + pub fn get_cells_capacity( &mut self, search_key: SearchKey, diff --git a/src/utils/rpc/types.rs b/src/utils/rpc/types.rs index 91017f49..4255263b 100644 --- a/src/utils/rpc/types.rs +++ b/src/utils/rpc/types.rs @@ -1741,6 +1741,22 @@ impl From for Cell { } } +#[derive(Serialize, Deserialize, Clone, Debug)] +#[serde(untagged)] +pub enum Tx { + Ungrouped(TxWithCell), + Grouped(TxWithCells), +} + +impl From for Tx { + fn from(tx: ckb_indexer::Tx) -> Tx { + match tx { + ckb_indexer::Tx::Ungrouped(tx_with_cell) => Tx::Ungrouped(tx_with_cell.into()), + ckb_indexer::Tx::Grouped(tx_with_cells) => Tx::Grouped(tx_with_cells.into()), + } + } +} + /// Response type of the RPC method `get_transactions`. #[derive(Serialize, Deserialize, Clone, Debug)] pub struct TxWithCell {