ckb_cli_plugin_protocol/
jsonrpc.rs

1use ckb_types::{
2    packed::{CellInput, OutPoint},
3    prelude::Pack,
4    H256,
5};
6use serde_derive::{Deserialize, Serialize};
7
8pub const JSONRPC_VERSION: &str = "2.0";
9
10/// A JSONRPC error object
11#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
12#[serde(deny_unknown_fields)]
13pub struct JsonrpcError {
14    /// The integer identifier of the error
15    pub code: i32,
16    /// A string describing the error
17    pub message: String,
18    /// Additional data specific to the error
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub data: Option<serde_json::Value>,
21}
22
23/// A JSONRPC request object
24#[derive(Serialize, Deserialize, Debug, Clone)]
25#[serde(deny_unknown_fields)]
26pub struct JsonrpcRequest {
27    /// jsonrpc field, MUST be "2.0"
28    pub jsonrpc: String,
29    /// Identifier for this Request, which should appear in the response
30    pub id: serde_json::Value,
31    /// The name of the RPC call
32    pub method: String,
33    /// Parameters to the RPC call
34    pub params: Vec<serde_json::Value>,
35}
36
37/// A JSONRPC response object
38#[derive(Serialize, Deserialize, Debug, Clone)]
39#[serde(deny_unknown_fields)]
40pub struct JsonrpcResponse {
41    /// jsonrpc field, MUST be "2.0"
42    pub jsonrpc: String,
43    /// Identifier for this Request, which should match that of the request
44    pub id: serde_json::Value,
45    /// A result if there is one, or null
46    #[serde(skip_serializing_if = "Option::is_none")]
47    pub result: Option<serde_json::Value>,
48    /// An error if there is one, or null
49    #[serde(skip_serializing_if = "Option::is_none")]
50    pub error: Option<JsonrpcError>,
51}
52
53#[derive(Hash, Eq, PartialEq, Debug, Clone, Serialize, Deserialize)]
54pub struct LiveCellInfo {
55    pub tx_hash: H256,
56    pub output_index: u32,
57    pub data_bytes: u64,
58    pub lock_hash: H256,
59    // Type script's code_hash and script_hash
60    pub type_hashes: Option<(H256, H256)>,
61    // Capacity
62    pub capacity: u64,
63    // Block number
64    pub number: u64,
65    // Location in the block
66    pub index: CellIndex,
67}
68
69impl LiveCellInfo {
70    pub fn out_point(&self) -> OutPoint {
71        OutPoint::new(self.tx_hash.pack(), self.output_index)
72    }
73    pub fn input(&self) -> CellInput {
74        CellInput::new(self.out_point(), 0)
75    }
76}
77
78// LiveCell index in a block
79#[derive(Debug, Hash, Eq, PartialEq, Clone, Copy, Serialize, Deserialize)]
80pub struct CellIndex {
81    // The transaction index in the block
82    pub tx_index: u32,
83    // The output index in the transaction
84    pub output_index: u32,
85}