Skip to main content

fiber_json_types/
graph.rs

1//! Network graph types for the Fiber Network JSON-RPC API.
2
3use crate::schema_helpers::*;
4use crate::serde_utils::{EntityHex, Hash256, Pubkey, U128Hex, U64Hex};
5use ckb_jsonrpc_types::{DepType, JsonBytes, OutPoint as OutPointWrapper, Script, ScriptHashType};
6use ckb_types::packed::OutPoint;
7use ckb_types::H256;
8use schemars::JsonSchema;
9use serde::{Deserialize, Serialize};
10use serde_with::serde_as;
11
12/// Parameters for querying graph nodes.
13#[serde_as]
14#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema)]
15pub struct GraphNodesParams {
16    #[serde_as(as = "Option<U64Hex>")]
17    #[schemars(schema_with = "schema_as_uint_hex_optional")]
18    /// The maximum number of nodes to return.
19    pub limit: Option<u64>,
20    /// The cursor to start returning nodes from.
21    pub after: Option<JsonBytes>,
22}
23
24/// The UDT script which is used to identify the UDT configuration for a Fiber Node.
25#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)]
26pub struct UdtScript {
27    /// The code hash of the script.
28    #[schemars(schema_with = "schema_as_hex_bytes")]
29    pub code_hash: H256,
30    /// The hash type of the script.
31    pub hash_type: ScriptHashType,
32    /// The arguments of the script.
33    pub args: String,
34}
35
36/// Udt script on-chain dependencies.
37#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)]
38pub struct UdtDep {
39    /// cell dep described by out_point.
40    #[serde(default, skip_serializing_if = "Option::is_none")]
41    pub cell_dep: Option<UdtCellDep>,
42    /// cell dep described by type ID.
43    #[serde(default, skip_serializing_if = "Option::is_none")]
44    pub type_id: Option<Script>,
45}
46
47/// The UDT cell dep which is used to identify the UDT configuration for a Fiber Node.
48#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)]
49pub struct UdtCellDep {
50    /// The out point of the cell dep.
51    pub out_point: OutPointWrapper,
52    /// The type of the cell dep.
53    pub dep_type: DepType,
54}
55
56/// The UDT argument info which is used to identify the UDT configuration.
57#[serde_as]
58#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)]
59pub struct UdtArgInfo {
60    /// The name of the UDT.
61    pub name: String,
62    /// The script of the UDT.
63    pub script: UdtScript,
64    #[serde_as(as = "Option<U128Hex>")]
65    #[schemars(schema_with = "schema_as_uint_hex_optional")]
66    /// The minimum amount of the UDT that can be automatically accepted.
67    pub auto_accept_amount: Option<u128>,
68    /// The cell deps of the UDT.
69    pub cell_deps: Vec<UdtDep>,
70}
71
72/// A list of UDT configuration infos.
73#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)]
74pub struct UdtCfgInfos(
75    /// The list of UDT configuration infos.
76    pub Vec<UdtArgInfo>,
77);
78
79/// The Node information.
80#[serde_as]
81#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema)]
82pub struct NodeInfo {
83    /// The name of the node.
84    pub node_name: String,
85    /// The version of the node.
86    pub version: String,
87    /// The addresses of the node (serialized as strings).
88    #[schemars(schema_with = "schema_as_string_array")]
89    pub addresses: Vec<String>,
90    /// The node features supported by the node.
91    pub features: Vec<String>,
92    /// The identity public key of the node (secp256k1 compressed, hex string), same as `pubkey` in `list_peers`.
93    pub pubkey: Pubkey,
94    #[serde_as(as = "U64Hex")]
95    #[schemars(schema_with = "schema_as_uint_hex")]
96    /// The latest timestamp set by the owner for the node announcement.
97    /// When a Node is online this timestamp will be updated to the latest value.
98    pub timestamp: u64,
99    /// The chain hash of the node.
100    pub chain_hash: Hash256,
101    #[serde_as(as = "U64Hex")]
102    #[schemars(schema_with = "schema_as_uint_hex")]
103    /// The minimum CKB funding amount for automatically accepting open channel requests.
104    pub auto_accept_min_ckb_funding_amount: u64,
105    /// The UDT configuration infos of the node.
106    pub udt_cfg_infos: UdtCfgInfos,
107}
108
109/// Result of querying graph nodes.
110#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema)]
111pub struct GraphNodesResult {
112    /// The list of nodes.
113    pub nodes: Vec<NodeInfo>,
114    /// The last cursor.
115    pub last_cursor: JsonBytes,
116}
117
118/// Parameters for querying graph channels.
119#[serde_as]
120#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema)]
121pub struct GraphChannelsParams {
122    /// The maximum number of channels to return.
123    #[serde_as(as = "Option<U64Hex>")]
124    #[schemars(schema_with = "schema_as_uint_hex_optional")]
125    pub limit: Option<u64>,
126    /// The cursor to start returning channels from.
127    pub after: Option<JsonBytes>,
128}
129
130/// The channel update info with a single direction of channel.
131#[serde_as]
132#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize, Deserialize, JsonSchema)]
133pub struct ChannelUpdateInfo {
134    /// The timestamp is the time when the channel update was received by the node.
135    #[serde_as(as = "U64Hex")]
136    #[schemars(schema_with = "schema_as_uint_hex")]
137    pub timestamp: u64,
138    /// Whether the channel can be currently used for payments (in this one direction).
139    pub enabled: bool,
140    /// The exact amount of balance that we can send to the other party via the channel.
141    #[serde_as(as = "Option<U128Hex>")]
142    #[schemars(schema_with = "schema_as_uint_hex_optional")]
143    pub outbound_liquidity: Option<u128>,
144    /// The difference in htlc expiry values that you must have when routing through this channel (in milliseconds).
145    #[serde_as(as = "U64Hex")]
146    #[schemars(schema_with = "schema_as_uint_hex")]
147    pub tlc_expiry_delta: u64,
148    /// The minimum value, which must be relayed to the next hop via the channel
149    #[serde_as(as = "U128Hex")]
150    #[schemars(schema_with = "schema_as_uint_hex")]
151    pub tlc_minimum_value: u128,
152    /// The forwarding fee rate for the channel.
153    #[serde_as(as = "U64Hex")]
154    #[schemars(schema_with = "schema_as_uint_hex")]
155    pub fee_rate: u64,
156}
157
158/// The Channel information.
159#[serde_as]
160#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema)]
161pub struct ChannelInfo {
162    /// The outpoint of the channel.
163    #[serde_as(as = "EntityHex")]
164    #[schemars(schema_with = "schema_as_hex_bytes")]
165    pub channel_outpoint: OutPoint,
166    /// The identity public key of the first node (secp256k1 compressed, hex string).
167    pub node1: Pubkey,
168    /// The identity public key of the second node (secp256k1 compressed, hex string).
169    pub node2: Pubkey,
170    /// The created timestamp of the channel, which is the block header timestamp of the block
171    /// that contains the channel funding transaction.
172    #[serde_as(as = "U64Hex")]
173    #[schemars(schema_with = "schema_as_uint_hex")]
174    pub created_timestamp: u64,
175
176    /// The update info from node1 to node2, e.g. timestamp, fee_rate, tlc_expiry_delta, tlc_minimum_value
177    pub update_info_of_node1: Option<ChannelUpdateInfo>,
178
179    /// The update info from node2 to node1, e.g. timestamp, fee_rate, tlc_expiry_delta, tlc_minimum_value
180    pub update_info_of_node2: Option<ChannelUpdateInfo>,
181
182    /// The capacity of the channel.
183    #[serde_as(as = "U128Hex")]
184    #[schemars(schema_with = "schema_as_uint_hex")]
185    pub capacity: u128,
186    /// The chain hash of the channel.
187    pub chain_hash: Hash256,
188    /// The UDT type script of the channel.
189    pub udt_type_script: Option<Script>,
190}
191
192/// Result of querying graph channels.
193#[derive(Serialize, Deserialize, Clone, JsonSchema)]
194pub struct GraphChannelsResult {
195    /// A list of channels.
196    pub channels: Vec<ChannelInfo>,
197    /// The last cursor for pagination.
198    pub last_cursor: JsonBytes,
199}