Skip to content

Commit 17a91d3

Browse files
committed
Expose list_channels in bindings
1 parent 3dfb064 commit 17a91d3

File tree

4 files changed

+121
-5
lines changed

4 files changed

+121
-5
lines changed

bindings/ldk_node.udl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ interface Node {
5252
Invoice receive_variable_amount_payment([ByRef]string description, u32 expiry_secs);
5353
PaymentInfo? payment_info([ByRef]PaymentHash payment_hash);
5454
sequence<PublicKey> list_peers();
55+
sequence<ChannelDetails> list_channels();
5556
};
5657

5758
[Error]
@@ -111,6 +112,29 @@ dictionary PaymentInfo {
111112
PaymentStatus status;
112113
};
113114

115+
dictionary OutPoint {
116+
string txid;
117+
u16 index;
118+
};
119+
120+
dictionary ChannelDetails {
121+
ChannelId channel_id;
122+
PublicKey counterparty;
123+
OutPoint? funding_txo;
124+
u64? short_channel_id;
125+
u64 channel_value_satoshis;
126+
u64 balance_msat;
127+
u64 outbound_capacity_msat;
128+
u64 inbound_capacity_msat;
129+
u32? confirmations_required;
130+
u32? confirmations;
131+
boolean is_outbound;
132+
boolean is_channel_ready;
133+
boolean is_usable;
134+
boolean is_public;
135+
u16? cltv_expiry_delta;
136+
};
137+
114138
[Custom]
115139
typedef string SocketAddr;
116140

src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ use payment_store::PaymentInfoStorage;
101101
pub use payment_store::{PaymentDirection, PaymentInfo, PaymentStatus};
102102
use peer_store::{PeerInfo, PeerInfoStorage};
103103
use types::{
104-
ChainMonitor, ChannelManager, GossipSync, KeysManager, Network, NetworkGraph, OnionMessenger,
105-
PeerManager, Scorer,
104+
ChainMonitor, ChannelDetails, ChannelManager, GossipSync, KeysManager, Network, NetworkGraph,
105+
OnionMessenger, OutPoint, PeerManager, Scorer,
106106
};
107107
pub use types::{ChannelId, UserChannelId};
108108
use wallet::Wallet;
@@ -112,7 +112,7 @@ use logger::{log_error, log_info, FilesystemLogger, Logger};
112112
use lightning::chain::keysinterface::EntropySource;
113113
use lightning::chain::{chainmonitor, BestBlock, Confirm, Watch};
114114
use lightning::ln::channelmanager::{
115-
self, ChainParameters, ChannelDetails, ChannelManagerReadArgs, PaymentId, Retry,
115+
self, ChainParameters, ChannelManagerReadArgs, PaymentId, Retry,
116116
};
117117
use lightning::ln::peer_handler::{IgnoringMessageHandler, MessageHandler};
118118
use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
@@ -853,7 +853,7 @@ impl Node {
853853

854854
/// Retrieve a list of known channels.
855855
pub fn list_channels(&self) -> Vec<ChannelDetails> {
856-
self.channel_manager.list_channels()
856+
self.channel_manager.list_channels().iter().map(|c| c.clone().into()).collect()
857857
}
858858

859859
/// Connect to a node on the peer-to-peer network.

src/test/functional_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn channel_full_cycle() {
4040
assert_eq!(node_a.list_peers(), [node_b.node_id()]);
4141

4242
let funding_txo = loop {
43-
let details = node_a.list_channels();
43+
let details = node_a.channel_manager.list_channels();
4444

4545
if details.is_empty() || details[0].funding_txo.is_none() {
4646
std::thread::sleep(Duration::from_secs(1));

src/types.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use crate::UniffiCustomTypeConverter;
77

88
use lightning::chain::chainmonitor;
99
use lightning::chain::keysinterface::InMemorySigner;
10+
use lightning::chain::transaction::OutPoint as LdkOutpoint;
11+
use lightning::ln::channelmanager::ChannelDetails as LdkChannelDetails;
1012
use lightning::ln::peer_handler::IgnoringMessageHandler;
1113
use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
1214
use lightning::routing::gossip;
@@ -339,3 +341,93 @@ impl UniffiCustomTypeConverter for Network {
339341
obj.0.to_string()
340342
}
341343
}
344+
345+
/// Details about the user's channel as returned by [`Node::list_channels`].
346+
///
347+
/// [`Node::list_channels`]: [`crate::Node::list_channels`]
348+
pub struct ChannelDetails {
349+
/// The channel's ID.
350+
pub channel_id: ChannelId,
351+
/// The `node_id` of our channel's counterparty.
352+
pub counterparty: PublicKey,
353+
/// Information about the channel's funding transaction output. `None `unless a funding
354+
/// transaction has been successfully negotiated with the channel's counterparty.
355+
pub funding_txo: Option<OutPoint>,
356+
/// Position of the funding transaction on-chain. `None` unless the funding transaction has been
357+
/// confirmed and fully opened.
358+
pub short_channel_id: Option<u64>,
359+
/// The value, in satoshis, of this channel as appears in the funding output.
360+
pub channel_value_satoshis: u64,
361+
/// Total balance of the channel. It is the amount that will be returned to the user if the
362+
/// channel is closed. The value is not exact, due to potential in-flight and fee-rate changes.
363+
/// Therefore, exactly this amount is likely irrecoverable on close.
364+
pub balance_msat: u64,
365+
/// Available outbound capacity for sending HTLCs to the remote peer. The amount does not
366+
/// include any pending HTLCs which are not yet resolved (and, thus, whose balance is not
367+
/// available for inclusion in new outbound HTLCs). This further does not include any
368+
/// pending outgoing HTLCs which are awaiting some other resolution to be sent.
369+
pub outbound_capacity_msat: u64,
370+
/// Available outbound capacity for sending HTLCs to the remote peer. The amount does not
371+
/// include any pending HTLCs which are not yet resolved (and, thus, whose balance is not
372+
/// available for inclusion in new inbound HTLCs). This further does not include any
373+
/// pending outgoing HTLCs which are awaiting some other resolution to be sent.
374+
pub inbound_capacity_msat: u64,
375+
/// The number of required confirmations on the funding transactions before the funding is
376+
/// considered "locked". The amount is selected by the channel fundee.
377+
///
378+
/// The value will be `None` for outbound channels until the counterparty accepts the channel.
379+
pub confirmations_required: Option<u32>,
380+
/// The current number of confirmations on the funding transaction.
381+
pub confirmations: Option<u32>,
382+
/// Returns `True` if the channel was initiated (and therefore funded) by us.
383+
pub is_outbound: bool,
384+
/// Returns `True` if the channel is confirmed, both parties have exchanged `channel_ready`
385+
/// messages, and the channel is not currently being shut down. Both parties exchange
386+
/// `channel_ready` messages upon independently verifying that the required confirmations count
387+
/// provided by `confirmations_required` has been reached.
388+
pub is_channel_ready: bool,
389+
/// Returns `True` if the channel is (a) confirmed and `channel_ready` has been exchanged,
390+
/// (b) the peer is connected, and (c) the channel is not currently negotiating shutdown.
391+
pub is_usable: bool,
392+
/// Returns `True` if this channel is (or will be) publicly-announced
393+
pub is_public: bool,
394+
/// The difference in the CLTV value between incoming HTLCs and an outbound HTLC forwarded over
395+
/// the channel.
396+
pub cltv_expiry_delta: Option<u16>,
397+
}
398+
399+
impl From<LdkChannelDetails> for ChannelDetails {
400+
fn from(value: LdkChannelDetails) -> Self {
401+
ChannelDetails {
402+
channel_id: ChannelId(value.channel_id),
403+
counterparty: value.counterparty.node_id,
404+
funding_txo: value.funding_txo.and_then(|o| Some(o.into())),
405+
short_channel_id: value.short_channel_id,
406+
channel_value_satoshis: value.channel_value_satoshis,
407+
balance_msat: value.balance_msat,
408+
outbound_capacity_msat: value.outbound_capacity_msat,
409+
inbound_capacity_msat: value.inbound_capacity_msat,
410+
confirmations_required: value.confirmations_required,
411+
confirmations: value.confirmations,
412+
is_outbound: value.is_outbound,
413+
is_channel_ready: value.is_channel_ready,
414+
is_usable: value.is_usable,
415+
is_public: value.is_public,
416+
cltv_expiry_delta: value.config.and_then(|c| Some(c.cltv_expiry_delta)),
417+
}
418+
}
419+
}
420+
421+
/// Data structure that references and transaction output.
422+
pub struct OutPoint {
423+
/// The referenced transaction's txid.
424+
pub txid: String,
425+
/// The index of the referenced output in its transaction's vout.
426+
pub index: u16,
427+
}
428+
429+
impl From<LdkOutpoint> for OutPoint {
430+
fn from(value: LdkOutpoint) -> Self {
431+
OutPoint { txid: value.txid.to_string(), index: value.index }
432+
}
433+
}

0 commit comments

Comments
 (0)