Skip to main content

fiber_json_types/
channel.rs

1//! Channel management types for the Fiber Network JSON-RPC API.
2
3use crate::define_rpc_flags;
4use crate::schema_helpers::*;
5use crate::serde_utils::{EntityHex, Hash256, Pubkey, U128Hex, U64Hex};
6use ckb_jsonrpc_types::{CellDep, EpochNumberWithFraction, Script, Transaction};
7use ckb_types::packed::OutPoint;
8use ckb_types::H256;
9use schemars::JsonSchema;
10use serde::{Deserialize, Serialize};
11use serde_with::serde_as;
12
13/// Serde default function returning `Some(true)`.
14/// Used for `Option<bool>` fields whose server-side default is `true`.
15fn default_true() -> Option<bool> {
16    Some(true)
17}
18
19define_rpc_flags! {
20    #[derive(Copy, Clone, Debug, PartialEq, Eq)]
21    pub struct NegotiatingFundingFlags(u32) {
22        const OUR_INIT_SENT = 1;
23        const THEIR_INIT_SENT = 1 << 1;
24        const INIT_SENT = 1 | (1 << 1);
25        const AWAITING_EXTERNAL_FUNDING = 1 << 2;
26    }
27}
28
29define_rpc_flags! {
30    #[derive(Copy, Clone, Debug, PartialEq, Eq)]
31    pub struct CollaboratingFundingTxFlags(u32) {
32        const AWAITING_REMOTE_TX_COLLABORATION_MSG = 1;
33        const PREPARING_LOCAL_TX_COLLABORATION_MSG = 1 << 1;
34        const OUR_TX_COMPLETE_SENT = 1 << 2;
35        const THEIR_TX_COMPLETE_SENT = 1 << 3;
36        const COLLABORATION_COMPLETED = (1 << 2) | (1 << 3);
37    }
38}
39
40define_rpc_flags! {
41    #[derive(Copy, Clone, Debug, PartialEq, Eq)]
42    pub struct SigningCommitmentFlags(u32) {
43        const OUR_COMMITMENT_SIGNED_SENT = 1;
44        const THEIR_COMMITMENT_SIGNED_SENT = 1 << 1;
45        const COMMITMENT_SIGNED_SENT = 1 | (1 << 1);
46    }
47}
48
49define_rpc_flags! {
50    #[derive(Copy, Clone, Debug, PartialEq, Eq)]
51    pub struct AwaitingTxSignaturesFlags(u32) {
52        const OUR_TX_SIGNATURES_SENT = 1;
53        const THEIR_TX_SIGNATURES_SENT = 1 << 1;
54        const TX_SIGNATURES_SENT = 1 | (1 << 1);
55    }
56}
57
58define_rpc_flags! {
59    #[derive(Copy, Clone, Debug, PartialEq, Eq)]
60    pub struct AwaitingChannelReadyFlags(u32) {
61        const OUR_CHANNEL_READY = 1;
62        const THEIR_CHANNEL_READY = 1 << 1;
63        const CHANNEL_READY = 1 | (1 << 1);
64    }
65}
66
67define_rpc_flags! {
68    #[derive(Copy, Clone, Debug, PartialEq, Eq)]
69    pub struct ShuttingDownFlags(u32) {
70        const OUR_SHUTDOWN_SENT = 1;
71        const THEIR_SHUTDOWN_SENT = 1 << 1;
72        const AWAITING_PENDING_TLCS = 1 | (1 << 1);
73        const DROPPING_PENDING = 1 << 2;
74        const WAITING_COMMITMENT_CONFIRMATION = 1 << 3;
75    }
76}
77
78define_rpc_flags! {
79    #[derive(Copy, Clone, Debug, PartialEq, Eq)]
80    pub struct CloseFlags(u32) {
81        const COOPERATIVE = 1;
82        const UNCOOPERATIVE_LOCAL = 1 << 1;
83        const ABANDONED = 1 << 2;
84        const FUNDING_ABORTED = 1 << 3;
85        const UNCOOPERATIVE_REMOTE = 1 << 4;
86        const WAITING_ONCHAIN_SETTLEMENT = 1 << 5;
87    }
88}
89
90/// Parameters for opening a channel.
91#[serde_as]
92#[derive(Serialize, Deserialize, Debug, JsonSchema)]
93pub struct OpenChannelParams {
94    /// The public key of the peer to open a channel with.
95    /// The peer must be connected through the [connect_peer](#peer-connect_peer) rpc first.
96    pub pubkey: Pubkey,
97
98    /// The amount of CKB or UDT to fund the channel with.
99    #[serde_as(as = "U128Hex")]
100    #[schemars(schema_with = "schema_as_uint_hex")]
101    pub funding_amount: u128,
102
103    /// Whether this is a public channel (will be broadcasted to network, and can be used to forward TLCs),
104    /// an optional parameter, default value is true.
105    #[serde(default = "default_true")]
106    pub public: Option<bool>,
107
108    /// Whether this is a one-way channel (will not be broadcasted to network, and can only be used to send payment one way),
109    /// an optional parameter, default value is false.
110    pub one_way: Option<bool>,
111
112    /// The type script of the UDT to fund the channel with, an optional parameter.
113    pub funding_udt_type_script: Option<Script>,
114
115    /// The script used to receive the channel balance, an optional parameter,
116    /// default value is the secp256k1_blake160_sighash_all script corresponding to the configured private key.
117    pub shutdown_script: Option<Script>,
118
119    /// The delay time for the commitment transaction, must be an
120    /// [EpochNumberWithFraction](https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0017-tx-valid-since/e-i-l-encoding.png)
121    /// in u64 format, an optional parameter, default value is 1 epoch, which is 4 hours.
122    pub commitment_delay_epoch: Option<EpochNumberWithFraction>,
123
124    /// The fee rate for the commitment transaction, an optional parameter.
125    #[serde_as(as = "Option<U64Hex>")]
126    #[schemars(schema_with = "schema_as_uint_hex_optional")]
127    pub commitment_fee_rate: Option<u64>,
128
129    /// The fee rate for the funding transaction, an optional parameter.
130    #[serde_as(as = "Option<U64Hex>")]
131    #[schemars(schema_with = "schema_as_uint_hex_optional")]
132    pub funding_fee_rate: Option<u64>,
133
134    /// The expiry delta to forward a tlc, in milliseconds, default to 4 hours, which is 4 * 60 * 60 * 1000 milliseconds
135    /// Expect it >= 2/3 commitment_delay_epoch.
136    /// This parameter can be updated with rpc `update_channel` later.
137    #[serde_as(as = "Option<U64Hex>")]
138    #[schemars(schema_with = "schema_as_uint_hex_optional")]
139    pub tlc_expiry_delta: Option<u64>,
140
141    /// The minimum value for a TLC our side can send,
142    /// an optional parameter, default is 0, which means we can send any TLC is larger than 0.
143    /// This parameter can be updated with rpc `update_channel` later.
144    #[serde_as(as = "Option<U128Hex>")]
145    #[schemars(schema_with = "schema_as_uint_hex_optional")]
146    pub tlc_min_value: Option<u128>,
147
148    /// The fee proportional millionths for a TLC, proportional to the amount of the forwarded tlc.
149    /// The unit is millionths of the amount. default is 1000 which means 0.1%.
150    /// This parameter can be updated with rpc `update_channel` later.
151    /// Not that, we use outbound channel to calculate the fee for TLC forwarding. For example,
152    /// if we have a path A -> B -> C, then the fee B requires for TLC forwarding, is calculated
153    /// the channel configuration of B and C, not A and B.
154    #[serde_as(as = "Option<U128Hex>")]
155    #[schemars(schema_with = "schema_as_uint_hex_optional")]
156    pub tlc_fee_proportional_millionths: Option<u128>,
157
158    /// The maximum total value of in-flight TLCs our side will accept from the peer, an optional parameter.
159    /// This parameter can not be updated after channel is opened.
160    #[serde_as(as = "Option<U128Hex>")]
161    #[schemars(schema_with = "schema_as_uint_hex_optional")]
162    pub max_tlc_value_in_flight: Option<u128>,
163
164    /// The maximum number of in-flight TLCs our side will accept from the peer, an optional parameter, default is 125
165    /// This parameter can not be updated after channel is opened.
166    #[serde_as(as = "Option<U64Hex>")]
167    #[schemars(schema_with = "schema_as_uint_hex_optional")]
168    pub max_tlc_number_in_flight: Option<u64>,
169}
170
171/// Result of opening a channel.
172#[derive(Clone, Serialize, Deserialize, JsonSchema)]
173pub struct OpenChannelResult {
174    /// The temporary channel ID of the channel being opened
175    pub temporary_channel_id: Hash256,
176}
177
178/// Parameters for opening a channel with external funding.
179#[serde_as]
180#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
181pub struct OpenChannelWithExternalFundingParams {
182    /// The identity public key of the peer to open a channel with.
183    /// The peer must already be connected through the [connect_peer](#peer-connect_peer) rpc first.
184    pub pubkey: Pubkey,
185
186    /// The amount of CKB or UDT to fund the channel with.
187    #[serde_as(as = "U128Hex")]
188    #[schemars(schema_with = "schema_as_uint_hex")]
189    pub funding_amount: u128,
190
191    /// Whether this is a public channel (will be broadcasted to network, and can be used to forward TLCs), an optional parameter, default value is true.
192    pub public: Option<bool>,
193
194    /// The type script of the UDT to fund the channel with, an optional parameter.
195    pub funding_udt_type_script: Option<Script>,
196
197    /// The script used to receive the channel balance when the channel is closed. This is REQUIRED for external funding.
198    pub shutdown_script: Script,
199
200    /// The lock script that controls the funding cells. The node will collect cells with this lock script
201    /// to build the funding transaction. The user must be able to sign for this lock script.
202    pub funding_lock_script: Script,
203
204    /// Optional extra cell deps required by `funding_lock_script`.
205    /// This is useful for custom wallet lock scripts whose deps are not part of the genesis defaults.
206    pub funding_lock_script_cell_deps: Option<Vec<CellDep>>,
207
208    /// The delay time for the commitment transaction, must be an
209    /// [EpochNumberWithFraction](https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0017-tx-valid-since/e-i-l-encoding.png)
210    /// in u64 format, an optional parameter, default value is 1 epoch, which is 4 hours.
211    pub commitment_delay_epoch: Option<EpochNumberWithFraction>,
212
213    /// The fee rate for the commitment transaction, an optional parameter.
214    #[serde_as(as = "Option<U64Hex>")]
215    #[schemars(schema_with = "schema_as_uint_hex_optional")]
216    pub commitment_fee_rate: Option<u64>,
217
218    /// The fee rate for the funding transaction, an optional parameter.
219    #[serde_as(as = "Option<U64Hex>")]
220    #[schemars(schema_with = "schema_as_uint_hex_optional")]
221    pub funding_fee_rate: Option<u64>,
222
223    /// The expiry delta to forward a tlc, in milliseconds, default to 4 hours, which is 4 * 60 * 60 * 1000 milliseconds
224    /// Expect it >= 2/3 commitment_delay_epoch.
225    /// This parameter can be updated with rpc `update_channel` later.
226    #[serde_as(as = "Option<U64Hex>")]
227    #[schemars(schema_with = "schema_as_uint_hex_optional")]
228    pub tlc_expiry_delta: Option<u64>,
229
230    /// The minimum value for a TLC our side can send,
231    /// an optional parameter, default is 0, which means we can send any TLC is larger than 0.
232    /// This parameter can be updated with rpc `update_channel` later.
233    #[serde_as(as = "Option<U128Hex>")]
234    #[schemars(schema_with = "schema_as_uint_hex_optional")]
235    pub tlc_min_value: Option<u128>,
236
237    /// The fee proportional millionths for a TLC, proportional to the amount of the forwarded tlc.
238    /// The unit is millionths of the amount. default is 1000 which means 0.1%.
239    /// This parameter can be updated with rpc `update_channel` later.
240    #[serde_as(as = "Option<U128Hex>")]
241    #[schemars(schema_with = "schema_as_uint_hex_optional")]
242    pub tlc_fee_proportional_millionths: Option<u128>,
243
244    /// The maximum total value of in-flight TLCs our side will accept from the peer, an optional parameter.
245    /// This parameter can not be updated after channel is opened.
246    #[serde_as(as = "Option<U128Hex>")]
247    #[schemars(schema_with = "schema_as_uint_hex_optional")]
248    pub max_tlc_value_in_flight: Option<u128>,
249
250    /// The maximum number of in-flight TLCs our side will accept from the peer, an optional parameter, default is 125
251    /// This parameter can not be updated after channel is opened.
252    #[serde_as(as = "Option<U64Hex>")]
253    #[schemars(schema_with = "schema_as_uint_hex_optional")]
254    pub max_tlc_number_in_flight: Option<u64>,
255}
256
257/// Result of opening a channel with external funding.
258#[derive(Clone, Serialize, Deserialize, Debug, JsonSchema)]
259pub struct OpenChannelWithExternalFundingResult {
260    /// The channel ID of the channel being opened.
261    pub channel_id: Hash256,
262
263    /// The final unsigned funding transaction that needs to be signed.
264    pub unsigned_funding_tx: Transaction,
265}
266
267/// Parameters for submitting a signed funding transaction.
268#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
269pub struct SubmitSignedFundingTxParams {
270    /// The channel ID returned from `open_channel_with_external_funding`.
271    pub channel_id: Hash256,
272
273    /// The signed funding transaction.
274    pub signed_funding_tx: Transaction,
275}
276
277/// Result of submitting a signed funding transaction.
278#[derive(Clone, Serialize, Deserialize, Debug, JsonSchema)]
279pub struct SubmitSignedFundingTxResult {
280    /// The channel ID.
281    pub channel_id: Hash256,
282
283    /// The hash of the funding transaction that was submitted.
284    pub funding_tx_hash: Hash256,
285}
286
287/// Parameters for abandoning a channel.
288#[derive(Serialize, Deserialize, Debug, JsonSchema)]
289pub struct AbandonChannelParams {
290    /// The temporary channel ID or real channel ID of the channel being abandoned
291    pub channel_id: Hash256,
292}
293
294/// Parameters for accepting a channel.
295#[serde_as]
296#[derive(Serialize, Deserialize, Debug, JsonSchema)]
297pub struct AcceptChannelParams {
298    /// The temporary channel ID of the channel to accept
299    pub temporary_channel_id: Hash256,
300
301    /// The amount of CKB or UDT to fund the channel with
302    #[serde_as(as = "U128Hex")]
303    #[schemars(schema_with = "schema_as_uint_hex")]
304    pub funding_amount: u128,
305
306    /// The script used to receive the channel balance, an optional parameter,
307    /// default value is the secp256k1_blake160_sighash_all script corresponding to the configured private key
308    pub shutdown_script: Option<Script>,
309
310    /// The maximum total value of in-flight TLCs our side will accept from the peer, default is u128::MAX
311    /// This parameter can not be updated after channel is opened.
312    #[serde_as(as = "Option<U128Hex>")]
313    #[schemars(schema_with = "schema_as_uint_hex_optional")]
314    pub max_tlc_value_in_flight: Option<u128>,
315
316    /// The maximum number of in-flight TLCs our side will accept from the peer, default is 125
317    /// This parameter can not be updated after channel is opened.
318    #[serde_as(as = "Option<U64Hex>")]
319    #[schemars(schema_with = "schema_as_uint_hex_optional")]
320    pub max_tlc_number_in_flight: Option<u64>,
321
322    /// The minimum value for a TLC our side can send,
323    /// an optional parameter, default is 0, which means we can send any TLC is larger than 0.
324    /// This parameter can be updated with rpc `update_channel` later.
325    #[serde_as(as = "Option<U128Hex>")]
326    #[schemars(schema_with = "schema_as_uint_hex_optional")]
327    pub tlc_min_value: Option<u128>,
328
329    /// The fee proportional millionths for a TLC, proportional to the amount of the forwarded tlc.
330    /// The unit is millionths of the amount. default is 1000 which means 0.1%.
331    /// This parameter can be updated with rpc `update_channel` later.
332    /// Not that, we use outbound channel to calculate the fee for TLC forwarding. For example,
333    /// if we have a path A -> B -> C, then the fee B requires for TLC forwarding, is calculated
334    /// the channel configuration of B and C, not A and B.
335    #[serde_as(as = "Option<U128Hex>")]
336    #[schemars(schema_with = "schema_as_uint_hex_optional")]
337    pub tlc_fee_proportional_millionths: Option<u128>,
338
339    /// The expiry delta to forward a tlc, in milliseconds, default to 1 day, which is 24 * 60 * 60 * 1000 milliseconds
340    /// This parameter can be updated with rpc `update_channel` later.
341    #[serde_as(as = "Option<U64Hex>")]
342    #[schemars(schema_with = "schema_as_uint_hex_optional")]
343    pub tlc_expiry_delta: Option<u64>,
344}
345
346/// Result of accepting a channel.
347#[derive(Clone, Serialize, Deserialize, JsonSchema)]
348pub struct AcceptChannelResult {
349    /// The final ID of the channel that was accepted, it's different from the temporary channel ID
350    pub channel_id: Hash256,
351}
352
353/// Parameters for listing channels.
354#[serde_as]
355#[derive(Serialize, Deserialize, JsonSchema)]
356pub struct ListChannelsParams {
357    /// The public key to list channels for.
358    /// An optional parameter, if not provided, all channels will be listed.
359    pub pubkey: Option<Pubkey>,
360    /// Whether to include closed channels in the list, an optional parameter, default value is false
361    pub include_closed: Option<bool>,
362    /// When set to true, only return channels that are still being opened (non-final states:
363    /// negotiating, collaborating on funding tx, signing, awaiting tx signatures, awaiting channel
364    /// ready) as well as channels whose opening attempt failed. Default is false.
365    /// Mutually exclusive with `include_closed`.
366    pub only_pending: Option<bool>,
367}
368
369/// Result of listing channels.
370#[derive(Clone, Serialize, Deserialize, Debug, JsonSchema)]
371pub struct ListChannelsResult {
372    /// The list of channels
373    pub channels: Vec<Channel>,
374}
375
376/// The state of a channel.
377///
378/// Serialized with adjacently-tagged representation using PascalCase variant names and flags.
379/// This is different from the internal `ChannelState` in fiber-types which uses
380/// default serde for bincode compatibility.
381#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
382#[serde(tag = "state_name", content = "state_flags")]
383pub enum ChannelState {
384    /// We are negotiating the parameters required for the channel prior to funding it.
385    /// For channels opened with external funding, this state is also used together with
386    /// `NegotiatingFundingFlags::AWAITING_EXTERNAL_FUNDING` to indicate that we are waiting
387    /// for the user to sign and submit the funding transaction externally.
388    NegotiatingFunding(#[schemars(schema_with = "schema_as_string")] NegotiatingFundingFlags),
389    /// We're collaborating with the other party on the funding transaction.
390    CollaboratingFundingTx(
391        #[schemars(schema_with = "schema_as_string")] CollaboratingFundingTxFlags,
392    ),
393    /// We have collaborated over the funding and are now waiting for CommitmentSigned messages.
394    SigningCommitment(#[schemars(schema_with = "schema_as_string")] SigningCommitmentFlags),
395    /// We've received and sent `commitment_signed` and are now waiting for both
396    /// party to collaborate on creating a valid funding transaction.
397    AwaitingTxSignatures(#[schemars(schema_with = "schema_as_string")] AwaitingTxSignaturesFlags),
398    /// We've received/sent `funding_created` and `funding_signed` and are thus now waiting on the
399    /// funding transaction to confirm.
400    AwaitingChannelReady(#[schemars(schema_with = "schema_as_string")] AwaitingChannelReadyFlags),
401    /// Both we and our counterparty consider the funding transaction confirmed and the channel is
402    /// now operational.
403    ChannelReady,
404    /// We've successfully negotiated a `closing_signed` dance. At this point, the `ChannelManager`
405    ShuttingDown(#[schemars(schema_with = "schema_as_string")] ShuttingDownFlags),
406    /// This channel is closed.
407    Closed(#[schemars(schema_with = "schema_as_string")] CloseFlags),
408    /// The channel state is potentially outdated (e.g., after a database restore).
409    /// We must perform a passive audit with the peer before resuming operations.
410    Stale,
411}
412
413/// The channel data structure.
414#[serde_as]
415#[derive(Clone, Serialize, Deserialize, Debug, JsonSchema)]
416pub struct Channel {
417    /// The channel ID
418    pub channel_id: Hash256,
419    /// Whether the channel is public
420    pub is_public: bool,
421    /// Is this channel initially inbound?
422    /// An inbound channel is one where the counterparty is the funder of the channel.
423    pub is_acceptor: bool,
424    /// Is this channel one-way?
425    /// Combines with is_acceptor to determine if the channel able to send payment to the counterparty or not.
426    pub is_one_way: bool,
427    /// The outpoint of the channel
428    #[serde_as(as = "Option<EntityHex>")]
429    #[schemars(schema_with = "schema_as_hex_bytes_optional")]
430    pub channel_outpoint: Option<OutPoint>,
431    /// The public key of the channel counterparty.
432    pub pubkey: Pubkey,
433    /// The UDT type script of the channel
434    pub funding_udt_type_script: Option<Script>,
435    /// The state of the channel
436    pub state: ChannelState,
437    /// The local balance of the channel
438    #[serde_as(as = "U128Hex")]
439    #[schemars(schema_with = "schema_as_uint_hex")]
440    pub local_balance: u128,
441    /// The offered balance of the channel
442    #[serde_as(as = "U128Hex")]
443    #[schemars(schema_with = "schema_as_uint_hex")]
444    pub offered_tlc_balance: u128,
445    /// The remote balance of the channel
446    #[serde_as(as = "U128Hex")]
447    #[schemars(schema_with = "schema_as_uint_hex")]
448    pub remote_balance: u128,
449    /// The received balance of the channel
450    #[serde_as(as = "U128Hex")]
451    #[schemars(schema_with = "schema_as_uint_hex")]
452    pub received_tlc_balance: u128,
453    /// The list of pending tlcs
454    pub pending_tlcs: Vec<Htlc>,
455    /// The hash of the latest commitment transaction
456    pub latest_commitment_transaction_hash: Option<H256>,
457    /// The time the channel was created at, in milliseconds from UNIX epoch
458    #[serde_as(as = "U64Hex")]
459    #[schemars(schema_with = "schema_as_uint_hex")]
460    pub created_at: u64,
461    /// Whether the channel is enabled
462    pub enabled: bool,
463    /// The expiry delta to forward a tlc, in milliseconds, default to 1 day, which is 24 * 60 * 60 * 1000 milliseconds
464    /// This parameter can be updated with rpc `update_channel` later.
465    #[serde_as(as = "U64Hex")]
466    #[schemars(schema_with = "schema_as_uint_hex")]
467    pub tlc_expiry_delta: u64,
468    /// The fee proportional millionths for a TLC, proportional to the amount of the forwarded tlc.
469    /// The unit is millionths of the amount. default is 1000 which means 0.1%.
470    /// This parameter can be updated with rpc `update_channel` later.
471    /// Not that, we use outbound channel to calculate the fee for TLC forwarding. For example,
472    /// if we have a path A -> B -> C, then the fee B requires for TLC forwarding, is calculated
473    /// the channel configuration of B and C, not A and B.
474    #[serde_as(as = "U128Hex")]
475    #[schemars(schema_with = "schema_as_uint_hex")]
476    pub tlc_fee_proportional_millionths: u128,
477    /// The hash of the shutdown transaction
478    pub shutdown_transaction_hash: Option<H256>,
479    /// Human-readable reason why the channel opening failed.
480    /// Only present when the channel is in a failed state (e.g. abandoned or funding aborted).
481    pub failure_detail: Option<String>,
482}
483
484/// The status of a tlc.
485#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, JsonSchema)]
486pub enum TlcStatus {
487    /// Outbound tlc
488    Outbound(OutboundTlcStatus),
489    /// Inbound tlc
490    Inbound(InboundTlcStatus),
491}
492
493/// The status of an outbound tlc.
494#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, JsonSchema)]
495pub enum OutboundTlcStatus {
496    /// Offered tlc created and sent to remote party
497    LocalAnnounced,
498    /// Received ACK from remote party for this offered tlc
499    Committed,
500    /// Remote party removed this tlc
501    RemoteRemoved,
502    /// We received another RemoveTlc message from peer when we are waiting for the ack of the last one.
503    RemoveWaitPrevAck,
504    /// We have sent commitment signed to peer and waiting ACK for confirming this RemoveTlc
505    RemoveWaitAck,
506    /// We have received the ACK for the RemoveTlc, it's safe to remove this tlc
507    RemoveAckConfirmed,
508}
509
510/// The status of an inbound tlc.
511#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, JsonSchema)]
512pub enum InboundTlcStatus {
513    /// Received tlc from remote party, but not committed yet
514    RemoteAnnounced,
515    /// We received another AddTlc peer message when we are waiting for the ack of the last one.
516    AnnounceWaitPrevAck,
517    /// We have sent commitment signed to peer and waiting ACK for confirming this AddTlc
518    AnnounceWaitAck,
519    /// We have received ACK from peer and Committed this tlc
520    Committed,
521    /// We have removed this tlc, but haven't received ACK from peer
522    LocalRemoved,
523    /// We have received the ACK for the RemoveTlc, it's safe to remove this tlc
524    RemoveAckConfirmed,
525}
526
527/// The htlc data structure.
528#[serde_as]
529#[derive(Clone, Serialize, Deserialize, Debug, JsonSchema)]
530pub struct Htlc {
531    /// The id of the htlc
532    #[serde_as(as = "U64Hex")]
533    #[schemars(schema_with = "schema_as_uint_hex")]
534    pub id: u64,
535    /// The amount of the htlc
536    #[serde_as(as = "U128Hex")]
537    #[schemars(schema_with = "schema_as_uint_hex")]
538    pub amount: u128,
539    /// The payment hash of the htlc
540    pub payment_hash: Hash256,
541    /// The expiry of the htlc
542    #[serde_as(as = "U64Hex")]
543    #[schemars(schema_with = "schema_as_uint_hex")]
544    pub expiry: u64,
545    /// If this HTLC is involved in a forwarding operation, this field indicates the forwarding channel.
546    /// For an outbound htlc, it is the inbound channel. For an inbound htlc, it is the outbound channel.
547    pub forwarding_channel_id: Option<Hash256>,
548    /// If this HTLC is involved in a forwarding operation, this field indicates the forwarding tlc id.
549    #[serde_as(as = "Option<U64Hex>")]
550    #[schemars(schema_with = "schema_as_uint_hex_optional")]
551    pub forwarding_tlc_id: Option<u64>,
552    /// The status of the htlc
553    pub status: TlcStatus,
554}
555
556/// Parameters for shutting down a channel.
557#[serde_as]
558#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
559pub struct ShutdownChannelParams {
560    /// The channel ID of the channel to shut down
561    pub channel_id: Hash256,
562    /// The script used to receive the channel balance, only support secp256k1_blake160_sighash_all script for now
563    /// default is `default_funding_lock_script` in `CkbConfig`
564    pub close_script: Option<Script>,
565    /// The fee rate for the closing transaction, the fee will be deducted from the closing initiator's channel balance
566    /// default is 1000 shannons/KW
567    #[serde_as(as = "Option<U64Hex>")]
568    #[schemars(schema_with = "schema_as_uint_hex_optional")]
569    pub fee_rate: Option<u64>,
570    /// Whether to force the channel to close, when set to false, `close_script` and `fee_rate` should be set, default is false.
571    /// When set to true, `close_script` and `fee_rate` will be ignored and will use the default value when opening the channel.
572    pub force: Option<bool>,
573}
574
575/// Parameters for updating a channel.
576#[serde_as]
577#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
578pub struct UpdateChannelParams {
579    /// The channel ID of the channel to update
580    pub channel_id: Hash256,
581    /// Whether the channel is enabled, default value is true
582    #[serde(default = "default_true")]
583    pub enabled: Option<bool>,
584    /// The expiry delta for the TLC locktime
585    #[serde_as(as = "Option<U64Hex>")]
586    #[schemars(schema_with = "schema_as_uint_hex_optional")]
587    pub tlc_expiry_delta: Option<u64>,
588    /// The minimum value for a TLC
589    #[serde_as(as = "Option<U128Hex>")]
590    #[schemars(schema_with = "schema_as_uint_hex_optional")]
591    pub tlc_minimum_value: Option<u128>,
592    /// The fee proportional millionths for a TLC
593    #[serde_as(as = "Option<U128Hex>")]
594    #[schemars(schema_with = "schema_as_uint_hex_optional")]
595    pub tlc_fee_proportional_millionths: Option<u128>,
596}