Skip to content

Commit

Permalink
impl get_transactions.
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanYuan committed Feb 2, 2024
1 parent f22e0b9 commit 4136c31
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
64 changes: 63 additions & 1 deletion src/subcommands/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u64>::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")
Expand Down Expand Up @@ -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::<u32>::default().from_matches(m, "limit")?;
let after_opt: Option<JsonBytes> = HexParser
.from_matches_opt::<Bytes>(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")?
Expand Down
16 changes: 16 additions & 0 deletions src/utils/rpc/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonBytes>,
) -> Result<Pagination<types::Tx>, 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,
Expand Down
16 changes: 16 additions & 0 deletions src/utils/rpc/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1741,6 +1741,22 @@ impl From<ckb_indexer::Cell> for Cell {
}
}

#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(untagged)]
pub enum Tx {
Ungrouped(TxWithCell),
Grouped(TxWithCells),
}

impl From<ckb_indexer::Tx> 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 {
Expand Down

0 comments on commit 4136c31

Please sign in to comment.