ckb_client/
types.rs

1use ckb_jsonrpc_types::{
2    BlockNumber, Capacity, CellOutput, JsonBytes, OutPoint, Script, Uint32, Uint64
3};
4use ckb_types::{prelude::*, H256};
5use serde::{Deserialize, Serialize};
6
7#[derive(Serialize, Deserialize, Clone, Debug)]
8pub struct IndexerTip {
9    pub block_hash: H256,
10    pub block_number: BlockNumber,
11}
12
13#[derive(Serialize, Deserialize, Clone, Debug)]
14#[serde(rename_all = "snake_case")]
15pub enum Order {
16    Desc,
17    Asc,
18}
19
20#[derive(Serialize, Deserialize)]
21pub struct Pagination<T> {
22    pub objects: Vec<T>,
23    pub last_cursor: JsonBytes,
24}
25
26#[derive(Serialize, Deserialize, Clone, Debug)]
27#[serde(rename_all = "snake_case")]
28pub enum CellType {
29    Input,
30    Output,
31}
32
33#[derive(Serialize, Deserialize, Clone, Debug)]
34pub struct TxWithCell {
35    pub tx_hash: H256,
36    pub block_number: BlockNumber,
37    pub tx_index: Uint32,
38    pub io_index: Uint32,
39    pub io_type: CellType,
40}
41
42#[derive(Serialize, Deserialize, Clone, Debug)]
43pub struct TxWithCells {
44    pub tx_hash: H256,
45    pub block_number: BlockNumber,
46    pub tx_index: Uint32,
47    pub cells: Vec<(CellType, Uint32)>,
48}
49
50#[derive(Serialize, Deserialize, Clone, Debug)]
51#[serde(untagged)]
52pub enum Tx {
53    Ungrouped(TxWithCell),
54    Grouped(TxWithCells),
55}
56
57impl Tx {
58    pub fn tx_hash(&self) -> H256 {
59        match self {
60            Tx::Ungrouped(tx) => tx.tx_hash.clone(),
61            Tx::Grouped(tx) => tx.tx_hash.clone(),
62        }
63    }
64}
65
66#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq, Hash)]
67#[serde(rename_all = "snake_case")]
68pub enum IndexerScriptSearchMode {
69    /// Mode `prefix` search script with prefix
70    Prefix,
71    /// Mode `exact` search script with exact match
72    Exact,
73}
74
75impl Default for IndexerScriptSearchMode {
76    fn default() -> Self {
77        Self::Prefix
78    }
79}
80
81#[derive(Serialize, Deserialize, Clone, Debug)]
82pub struct SearchKey {
83    pub script: Script,
84    pub script_type: ScriptType,
85    pub script_search_mode: Option<IndexerScriptSearchMode>,
86    pub filter: Option<SearchKeyFilter>,
87    pub with_data: Option<bool>,
88    pub group_by_transaction: Option<bool>,
89}
90
91#[derive(Serialize, Deserialize, Default, Clone, Debug)]
92pub struct SearchKeyFilter {
93    pub script: Option<Script>,
94    pub script_len_range: Option<[Uint64; 2]>,
95    pub output_data_len_range: Option<[Uint64; 2]>,
96    pub output_capacity_range: Option<[Uint64; 2]>,
97    pub block_range: Option<[BlockNumber; 2]>,
98}
99
100#[derive(Serialize, Deserialize, Clone, Debug, Hash, PartialEq, Eq)]
101#[serde(rename_all = "snake_case")]
102pub enum ScriptType {
103    Lock,
104    Type,
105}
106
107#[derive(Serialize, Deserialize, Clone, Debug)]
108pub struct CellsCapacity {
109    pub capacity: Capacity,
110    pub block_hash: H256,
111    pub block_number: BlockNumber,
112}
113
114#[derive(Serialize, Deserialize, Clone, Debug)]
115pub struct Cell {
116    pub output: CellOutput,
117    pub output_data: Option<JsonBytes>,
118    pub out_point: OutPoint,
119    pub block_number: BlockNumber,
120    pub tx_index: Uint32,
121}
122
123#[derive(Serialize, Deserialize, Debug, Clone, Hash, PartialEq, Eq)]
124pub struct RpcSearchKey {
125    pub script: Script,
126    pub script_type: ScriptType,
127    pub script_search_mode: Option<IndexerScriptSearchMode>,
128    pub filter: Option<RpcSearchKeyFilter>,
129}
130
131impl RpcSearchKey {
132    pub fn into_key(self, block_range: Option<[Uint64; 2]>) -> SearchKey {
133        SearchKey {
134            script: self.script,
135            script_type: self.script_type,
136            filter: if self.filter.is_some() {
137                self.filter.map(|f| f.into_filter(block_range))
138            } else {
139                Some(RpcSearchKeyFilter::default().into_filter(block_range))
140            },
141            script_search_mode: self.script_search_mode,
142            with_data: None,
143            group_by_transaction: Some(true),
144        }
145    }
146}
147
148#[derive(Serialize, Deserialize, Debug, Clone, Default, Hash, PartialEq, Eq)]
149pub struct RpcSearchKeyFilter {
150    pub script: Option<Script>,
151    pub script_len_range: Option<[Uint64; 2]>,
152    pub output_data_len_range: Option<[Uint64; 2]>,
153    pub output_capacity_range: Option<[Uint64; 2]>,
154}
155
156impl RpcSearchKeyFilter {
157    fn into_filter(self, block_range: Option<[Uint64; 2]>) -> SearchKeyFilter {
158        SearchKeyFilter {
159            script: self.script,
160            script_len_range: self.script_len_range,
161            output_data_len_range: self.output_data_len_range,
162            output_capacity_range: self.output_capacity_range,
163            block_range,
164        }
165    }
166}
167
168#[derive(Serialize, Deserialize, Debug, Clone, Default, Hash, PartialEq, Eq)]
169pub struct HeaderViewWithExtension {
170    pub inner: ckb_jsonrpc_types::HeaderView,
171    #[serde(default, skip_serializing_if = "Option::is_none")]
172    pub extension: Option<ckb_jsonrpc_types::JsonBytes>,
173}
174
175impl From<ckb_jsonrpc_types::BlockView> for HeaderViewWithExtension {
176    fn from(value: ckb_jsonrpc_types::BlockView) -> Self {
177        HeaderViewWithExtension {
178            inner: value.header,
179            extension: value.extension,
180        }
181    }
182}