ckb_jsonrpc_types/
indexer.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
use crate::{BlockNumber, Capacity, CellOutput, JsonBytes, OutPoint, Script, Uint32, Uint64};
use ckb_types::H256;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

/// Indexer tip information
#[derive(Serialize, JsonSchema)]
pub struct IndexerTip {
    /// indexed tip block hash
    pub block_hash: H256,
    /// indexed tip block number
    pub block_number: BlockNumber,
}

/// Live cell
#[derive(Serialize, JsonSchema)]
pub struct IndexerCell {
    /// the fields of an output cell
    pub output: CellOutput,
    /// the cell data
    pub output_data: Option<JsonBytes>,
    /// reference to a cell via transaction hash and output index
    pub out_point: OutPoint,
    /// the number of the transaction committed in the block
    pub block_number: BlockNumber,
    /// the position index of the transaction committed in the block
    pub tx_index: Uint32,
}

/// IndexerPagination wraps objects array and last_cursor to provide paging
#[derive(Serialize, JsonSchema)]
pub struct IndexerPagination<T> {
    /// objects collection
    pub objects: Vec<T>,
    /// pagination parameter
    pub last_cursor: JsonBytes,
}

impl<T> IndexerPagination<T> {
    /// Construct new IndexerPagination
    pub fn new(objects: Vec<T>, last_cursor: JsonBytes) -> Self {
        IndexerPagination {
            objects,
            last_cursor,
        }
    }
}

/// SearchKey represent indexer support params
#[derive(Deserialize, JsonSchema)]
pub struct IndexerSearchKey {
    /// Script
    pub script: Script,
    /// Script Type
    pub script_type: IndexerScriptType,
    /// Script search mode, optional default is `prefix`, means search script with prefix
    pub script_search_mode: Option<IndexerSearchMode>,
    /// filter cells by following conditions, all conditions are optional
    pub filter: Option<IndexerSearchKeyFilter>,
    /// bool, optional default is `true`, if with_data is set to false, the field of returning cell.output_data is null in the result
    pub with_data: Option<bool>,
    /// bool, optional default is `false`, if group_by_transaction is set to true, the returning objects will be grouped by the tx hash
    pub group_by_transaction: Option<bool>,
}

impl Default for IndexerSearchKey {
    fn default() -> Self {
        Self {
            script: Script::default(),
            script_type: IndexerScriptType::Lock,
            script_search_mode: None,
            filter: None,
            with_data: None,
            group_by_transaction: None,
        }
    }
}

/// IndexerSearchMode represent search mode, default is prefix search
#[derive(Deserialize, PartialEq, Eq, JsonSchema, Clone, Copy)]
#[serde(rename_all = "snake_case")]
pub enum IndexerSearchMode {
    /// Mode `prefix` search with prefix
    Prefix,
    /// Mode `exact` search with exact match
    Exact,
    /// Mode `partial` search with partial match
    Partial,
}

impl Default for IndexerSearchMode {
    fn default() -> Self {
        Self::Prefix
    }
}

/// A array represent (half-open) range bounded inclusively below and exclusively above [start, end).
///
/// ## Examples
///
/// |            JSON          |            range             |
/// | -------------------------| ---------------------------- |
/// | ["0x0", "0x2"]           |          [0, 2)              |
/// | ["0x0", "0x174876e801"]  |          [0, 100000000001)   |
///
#[derive(Deserialize, Default, JsonSchema)]
#[serde(transparent)]
pub struct IndexerRange {
    inner: [Uint64; 2],
}

impl IndexerRange {
    /// Construct new range
    pub fn new<U>(start: U, end: U) -> Self
    where
        U: Into<Uint64>,
    {
        IndexerRange {
            inner: [start.into(), end.into()],
        }
    }

    /// Return the lower bound of the range (inclusive).
    pub fn start(&self) -> Uint64 {
        self.inner[0]
    }

    /// Return the upper bound of the range (exclusive).
    pub fn end(&self) -> Uint64 {
        self.inner[1]
    }
}

/// IndexerSearchKeyFilter represent indexer params `filter`
#[derive(Deserialize, Default, JsonSchema)]
pub struct IndexerSearchKeyFilter {
    /// if search script type is lock, filter cells by type script prefix, and vice versa
    pub script: Option<Script>,
    /// filter cells by script len range
    pub script_len_range: Option<IndexerRange>,
    /// filter cells by output data
    pub output_data: Option<JsonBytes>,
    /// output data filter mode, optional default is `prefix`
    pub output_data_filter_mode: Option<IndexerSearchMode>,
    /// filter cells by output data len range
    pub output_data_len_range: Option<IndexerRange>,
    /// filter cells by output capacity range
    pub output_capacity_range: Option<IndexerRange>,
    /// filter cells by block number range
    pub block_range: Option<IndexerRange>,
}

/// ScriptType `Lock` | `Type`
#[derive(Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum IndexerScriptType {
    /// Lock
    Lock,
    /// Type
    Type,
}

/// Order Desc | Asc
#[derive(Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum IndexerOrder {
    /// Descending order
    Desc,
    /// Ascending order
    Asc,
}

/// Cells capacity
#[derive(Serialize, JsonSchema)]
pub struct IndexerCellsCapacity {
    /// total capacity
    pub capacity: Capacity,
    /// indexed tip block hash
    pub block_hash: H256,
    /// indexed tip block number
    pub block_number: BlockNumber,
}

/// Indexer Transaction Object
#[derive(Serialize, JsonSchema, Debug)]
#[serde(untagged)]
pub enum IndexerTx {
    /// # Ungrouped format represent as `IndexerTxWithCell`
    ///
    /// ## Fields
    ///
    /// `IndexerCellType` is equivalent to `"input" | "output"`.
    ///
    /// `IndexerTxWithCell` is a JSON object with the following fields.
    /// *   `tx_hash`: [`H256`] - transaction hash
    /// *   `block_number`: [`BlockNumber`] - the number of the transaction committed in the block
    /// *   `tx_index`: [`Uint32`] - the position index of the transaction committed in the block
    /// *   `io_index`: [`Uint32`] - the position index of the cell in the transaction inputs or outputs
    /// *   `io_type`: [`IndexerCellType`] - io type
    ///
    Ungrouped(IndexerTxWithCell),
    /// # Grouped format represent as `IndexerTxWithCells`
    ///
    /// ## Fields
    ///
    /// `IndexerCellType` is equivalent to `"input" | "output"`.
    ///
    /// `IndexerTxWithCells` is a JSON object with the following fields.
    /// *   `tx_hash`: [`H256`] - transaction hash
    /// *   `block_number`: [`BlockNumber`] - the number of the transaction committed in the block
    /// *   `tx_index`: [`Uint32`]- the position index of the transaction committed in the block
    /// *   `cells`: Array <(IndexerCellType, Uint32)>
    ///
    Grouped(IndexerTxWithCells),
}

impl IndexerTx {
    /// Return tx hash
    pub fn tx_hash(&self) -> H256 {
        match self {
            IndexerTx::Ungrouped(tx) => tx.tx_hash.clone(),
            IndexerTx::Grouped(tx) => tx.tx_hash.clone(),
        }
    }
}

/// Ungrouped Tx inner type
#[derive(Serialize, JsonSchema, Debug)]
pub struct IndexerTxWithCell {
    /// transaction hash
    pub tx_hash: H256,
    /// the number of the transaction committed in the block
    pub block_number: BlockNumber,
    /// the position index of the transaction committed in the block
    pub tx_index: Uint32,
    /// the position index of the cell in the transaction inputs or outputs
    pub io_index: Uint32,
    /// io type
    pub io_type: IndexerCellType,
}

/// Grouped Tx inner type
#[derive(Serialize, JsonSchema, Debug)]
pub struct IndexerTxWithCells {
    /// transaction hash
    pub tx_hash: H256,
    /// the number of the transaction committed in the block
    pub block_number: BlockNumber,
    /// the position index of the transaction committed in the block
    pub tx_index: Uint32,
    /// Array [(io_type, io_index)]
    pub cells: Vec<(IndexerCellType, Uint32)>,
}

/// Cell type
#[derive(Serialize, Clone, JsonSchema, Debug)]
#[serde(rename_all = "snake_case")]
pub enum IndexerCellType {
    /// Input
    Input,
    /// Output
    Output,
}