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 value in flight for TLCs, 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 TLCs that can be accepted, 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 value in flight for TLCs, 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 TLCs that can be accepted, 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 max tlc sum value in flight for the channel, 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 max tlc number in flight send from our side, 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}
409
410/// The channel data structure.
411#[serde_as]
412#[derive(Clone, Serialize, Deserialize, Debug, JsonSchema)]
413pub struct Channel {
414    /// The channel ID
415    pub channel_id: Hash256,
416    /// Whether the channel is public
417    pub is_public: bool,
418    /// Is this channel initially inbound?
419    /// An inbound channel is one where the counterparty is the funder of the channel.
420    pub is_acceptor: bool,
421    /// Is this channel one-way?
422    /// Combines with is_acceptor to determine if the channel able to send payment to the counterparty or not.
423    pub is_one_way: bool,
424    /// The outpoint of the channel
425    #[serde_as(as = "Option<EntityHex>")]
426    #[schemars(schema_with = "schema_as_hex_bytes_optional")]
427    pub channel_outpoint: Option<OutPoint>,
428    /// The public key of the channel counterparty.
429    pub pubkey: Pubkey,
430    /// The UDT type script of the channel
431    pub funding_udt_type_script: Option<Script>,
432    /// The state of the channel
433    pub state: ChannelState,
434    /// The local balance of the channel
435    #[serde_as(as = "U128Hex")]
436    #[schemars(schema_with = "schema_as_uint_hex")]
437    pub local_balance: u128,
438    /// The offered balance of the channel
439    #[serde_as(as = "U128Hex")]
440    #[schemars(schema_with = "schema_as_uint_hex")]
441    pub offered_tlc_balance: u128,
442    /// The remote balance of the channel
443    #[serde_as(as = "U128Hex")]
444    #[schemars(schema_with = "schema_as_uint_hex")]
445    pub remote_balance: u128,
446    /// The received balance of the channel
447    #[serde_as(as = "U128Hex")]
448    #[schemars(schema_with = "schema_as_uint_hex")]
449    pub received_tlc_balance: u128,
450    /// The list of pending tlcs
451    pub pending_tlcs: Vec<Htlc>,
452    /// The hash of the latest commitment transaction
453    pub latest_commitment_transaction_hash: Option<H256>,
454    /// The time the channel was created at, in milliseconds from UNIX epoch
455    #[serde_as(as = "U64Hex")]
456    #[schemars(schema_with = "schema_as_uint_hex")]
457    pub created_at: u64,
458    /// Whether the channel is enabled
459    pub enabled: bool,
460    /// The expiry delta to forward a tlc, in milliseconds, default to 1 day, which is 24 * 60 * 60 * 1000 milliseconds
461    /// This parameter can be updated with rpc `update_channel` later.
462    #[serde_as(as = "U64Hex")]
463    #[schemars(schema_with = "schema_as_uint_hex")]
464    pub tlc_expiry_delta: u64,
465    /// The fee proportional millionths for a TLC, proportional to the amount of the forwarded tlc.
466    /// The unit is millionths of the amount. default is 1000 which means 0.1%.
467    /// This parameter can be updated with rpc `update_channel` later.
468    /// Not that, we use outbound channel to calculate the fee for TLC forwarding. For example,
469    /// if we have a path A -> B -> C, then the fee B requires for TLC forwarding, is calculated
470    /// the channel configuration of B and C, not A and B.
471    #[serde_as(as = "U128Hex")]
472    #[schemars(schema_with = "schema_as_uint_hex")]
473    pub tlc_fee_proportional_millionths: u128,
474    /// The hash of the shutdown transaction
475    pub shutdown_transaction_hash: Option<H256>,
476    /// Human-readable reason why the channel opening failed.
477    /// Only present when the channel is in a failed state (e.g. abandoned or funding aborted).
478    pub failure_detail: Option<String>,
479}
480
481/// The status of a tlc.
482#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, JsonSchema)]
483pub enum TlcStatus {
484    /// Outbound tlc
485    Outbound(OutboundTlcStatus),
486    /// Inbound tlc
487    Inbound(InboundTlcStatus),
488}
489
490/// The status of an outbound tlc.
491#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, JsonSchema)]
492pub enum OutboundTlcStatus {
493    /// Offered tlc created and sent to remote party
494    LocalAnnounced,
495    /// Received ACK from remote party for this offered tlc
496    Committed,
497    /// Remote party removed this tlc
498    RemoteRemoved,
499    /// We received another RemoveTlc message from peer when we are waiting for the ack of the last one.
500    RemoveWaitPrevAck,
501    /// We have sent commitment signed to peer and waiting ACK for confirming this RemoveTlc
502    RemoveWaitAck,
503    /// We have received the ACK for the RemoveTlc, it's safe to remove this tlc
504    RemoveAckConfirmed,
505}
506
507/// The status of an inbound tlc.
508#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, JsonSchema)]
509pub enum InboundTlcStatus {
510    /// Received tlc from remote party, but not committed yet
511    RemoteAnnounced,
512    /// We received another AddTlc peer message when we are waiting for the ack of the last one.
513    AnnounceWaitPrevAck,
514    /// We have sent commitment signed to peer and waiting ACK for confirming this AddTlc
515    AnnounceWaitAck,
516    /// We have received ACK from peer and Committed this tlc
517    Committed,
518    /// We have removed this tlc, but haven't received ACK from peer
519    LocalRemoved,
520    /// We have received the ACK for the RemoveTlc, it's safe to remove this tlc
521    RemoveAckConfirmed,
522}
523
524/// The htlc data structure.
525#[serde_as]
526#[derive(Clone, Serialize, Deserialize, Debug, JsonSchema)]
527pub struct Htlc {
528    /// The id of the htlc
529    #[serde_as(as = "U64Hex")]
530    #[schemars(schema_with = "schema_as_uint_hex")]
531    pub id: u64,
532    /// The amount of the htlc
533    #[serde_as(as = "U128Hex")]
534    #[schemars(schema_with = "schema_as_uint_hex")]
535    pub amount: u128,
536    /// The payment hash of the htlc
537    pub payment_hash: Hash256,
538    /// The expiry of the htlc
539    #[serde_as(as = "U64Hex")]
540    #[schemars(schema_with = "schema_as_uint_hex")]
541    pub expiry: u64,
542    /// If this HTLC is involved in a forwarding operation, this field indicates the forwarding channel.
543    /// For an outbound htlc, it is the inbound channel. For an inbound htlc, it is the outbound channel.
544    pub forwarding_channel_id: Option<Hash256>,
545    /// If this HTLC is involved in a forwarding operation, this field indicates the forwarding tlc id.
546    #[serde_as(as = "Option<U64Hex>")]
547    #[schemars(schema_with = "schema_as_uint_hex_optional")]
548    pub forwarding_tlc_id: Option<u64>,
549    /// The status of the htlc
550    pub status: TlcStatus,
551}
552
553/// Parameters for shutting down a channel.
554#[serde_as]
555#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
556pub struct ShutdownChannelParams {
557    /// The channel ID of the channel to shut down
558    pub channel_id: Hash256,
559    /// The script used to receive the channel balance, only support secp256k1_blake160_sighash_all script for now
560    /// default is `default_funding_lock_script` in `CkbConfig`
561    pub close_script: Option<Script>,
562    /// The fee rate for the closing transaction, the fee will be deducted from the closing initiator's channel balance
563    /// default is 1000 shannons/KW
564    #[serde_as(as = "Option<U64Hex>")]
565    #[schemars(schema_with = "schema_as_uint_hex_optional")]
566    pub fee_rate: Option<u64>,
567    /// Whether to force the channel to close, when set to false, `close_script` and `fee_rate` should be set, default is false.
568    /// When set to true, `close_script` and `fee_rate` will be ignored and will use the default value when opening the channel.
569    pub force: Option<bool>,
570}
571
572/// Parameters for updating a channel.
573#[serde_as]
574#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
575pub struct UpdateChannelParams {
576    /// The channel ID of the channel to update
577    pub channel_id: Hash256,
578    /// Whether the channel is enabled, default value is true
579    #[serde(default = "default_true")]
580    pub enabled: Option<bool>,
581    /// The expiry delta for the TLC locktime
582    #[serde_as(as = "Option<U64Hex>")]
583    #[schemars(schema_with = "schema_as_uint_hex_optional")]
584    pub tlc_expiry_delta: Option<u64>,
585    /// The minimum value for a TLC
586    #[serde_as(as = "Option<U128Hex>")]
587    #[schemars(schema_with = "schema_as_uint_hex_optional")]
588    pub tlc_minimum_value: Option<u128>,
589    /// The fee proportional millionths for a TLC
590    #[serde_as(as = "Option<U128Hex>")]
591    #[schemars(schema_with = "schema_as_uint_hex_optional")]
592    pub tlc_fee_proportional_millionths: Option<u128>,
593}