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