ckb_jsonrpc_types/
cell.rs

1use crate::{CellOutput, JsonBytes};
2use ckb_types::{
3    H256,
4    core::cell::{CellMeta, CellStatus},
5};
6use schemars::JsonSchema;
7use serde::{Deserialize, Serialize};
8
9/// The JSON view of a cell with its status information.
10///
11/// ## Examples
12///
13/// ```
14/// # serde_json::from_str::<ckb_jsonrpc_types::CellWithStatus>(r#"
15/// {
16///   "cell": {
17///     "data": {
18///       "content": "0x7f454c460201010000000000000000000200f3000100000078000100000000004000000000000000980000000000000005000000400038000100400003000200010000000500000000000000000000000000010000000000000001000000000082000000000000008200000000000000001000000000000001459308d00573000000002e7368737472746162002e74657874000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b000000010000000600000000000000780001000000000078000000000000000a0000000000000000000000000000000200000000000000000000000000000001000000030000000000000000000000000000000000000082000000000000001100000000000000000000000000000001000000000000000000000000000000",
19///       "hash": "0x28e83a1277d48add8e72fadaa9248559e1b632bab2bd60b27955ebc4c03800a5"
20///     },
21///     "output": {
22///       "capacity": "0x802665800",
23///       "lock": {
24///         "args": "0x",
25///         "code_hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
26///         "hash_type": "data"
27///       },
28///       "type": null
29///     }
30///   },
31///   "status": "live"
32/// }
33/// # "#).unwrap();
34/// ```
35///
36/// ```
37/// # serde_json::from_str::<ckb_jsonrpc_types::CellWithStatus>(r#"
38/// {
39///   "cell": null,
40///   "status": "unknown"
41/// }
42/// # "#).unwrap();
43/// ```
44#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
45pub struct CellWithStatus {
46    /// The cell information.
47    ///
48    /// For performance issues, CKB only keeps the information for live cells.
49    pub cell: Option<CellInfo>,
50    /// Status of the cell.
51    ///
52    /// Allowed values: "live", "dead", "unknown".
53    ///
54    /// * `live` - The transaction creating this cell is in the chain, and there are no
55    ///   transactions found in the chain that uses this cell as an input.
56    /// * `dead` - (**Deprecated**: the dead status will be removed since 0.36.0, please do not
57    ///   rely on the logic that differentiates dead and unknown cells.) The transaction creating
58    ///   this cell is in the chain, and a transaction is found in the chain which uses this cell as
59    ///   an input.
60    /// * `unknown` - CKB does not know the status of the cell. Either the transaction creating
61    ///   this cell is not in the chain yet, or it is no longer live.
62    pub status: String,
63}
64
65/// The JSON view of a cell combining the fields in cell output and cell data.
66///
67/// ## Examples
68///
69/// ```
70/// # serde_json::from_str::<ckb_jsonrpc_types::CellInfo>(r#"
71/// {
72///   "data": {
73///     "content": "0x7f454c460201010000000000000000000200f3000100000078000100000000004000000000000000980000000000000005000000400038000100400003000200010000000500000000000000000000000000010000000000000001000000000082000000000000008200000000000000001000000000000001459308d00573000000002e7368737472746162002e74657874000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b000000010000000600000000000000780001000000000078000000000000000a0000000000000000000000000000000200000000000000000000000000000001000000030000000000000000000000000000000000000082000000000000001100000000000000000000000000000001000000000000000000000000000000",
74///     "hash": "0x28e83a1277d48add8e72fadaa9248559e1b632bab2bd60b27955ebc4c03800a5"
75///   },
76///   "output": {
77///     "capacity": "0x802665800",
78///     "lock": {
79///       "args": "0x",
80///       "code_hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
81///       "hash_type": "data"
82///     },
83///     "type": null
84///   }
85/// }
86/// # "#).unwrap();
87/// ```
88#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
89pub struct CellInfo {
90    /// Cell fields appears in the transaction `outputs` array.
91    pub output: CellOutput,
92    /// Cell data.
93    ///
94    /// This is `null` when the data is not requested, which does not mean the cell data is empty.
95    pub data: Option<CellData>,
96}
97
98/// The cell data content and hash.
99///
100/// ## Examples
101///
102/// ```
103/// # serde_json::from_str::<ckb_jsonrpc_types::CellData>(r#"
104/// {
105///   "content": "0x7f454c460201010000000000000000000200f3000100000078000100000000004000000000000000980000000000000005000000400038000100400003000200010000000500000000000000000000000000010000000000000001000000000082000000000000008200000000000000001000000000000001459308d00573000000002e7368737472746162002e74657874000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b000000010000000600000000000000780001000000000078000000000000000a0000000000000000000000000000000200000000000000000000000000000001000000030000000000000000000000000000000000000082000000000000001100000000000000000000000000000001000000000000000000000000000000",
106///   "hash": "0x28e83a1277d48add8e72fadaa9248559e1b632bab2bd60b27955ebc4c03800a5"
107/// }
108/// # "#).unwrap();
109/// ```
110#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
111pub struct CellData {
112    /// Cell content.
113    pub content: JsonBytes,
114    /// Cell content hash.
115    pub hash: H256,
116}
117
118impl From<CellMeta> for CellInfo {
119    fn from(cell_meta: CellMeta) -> Self {
120        let data = cell_meta.mem_cell_data;
121        let data_hash = cell_meta.mem_cell_data_hash;
122        let output = cell_meta.cell_output.into();
123        CellInfo {
124            output,
125            data: data.and_then(move |data| {
126                data_hash.map(|hash| CellData {
127                    content: JsonBytes::from_bytes(data),
128                    hash: hash.into(),
129                })
130            }),
131        }
132    }
133}
134
135impl From<CellStatus> for CellWithStatus {
136    fn from(status: CellStatus) -> Self {
137        let (cell, status) = match status {
138            CellStatus::Live(cell_meta) => (Some(cell_meta), "live"),
139            CellStatus::Dead => (None, "dead"),
140            CellStatus::Unknown => (None, "unknown"),
141        };
142        Self {
143            cell: cell.map(Into::into),
144            status: status.to_string(),
145        }
146    }
147}