Skip to content

Commit f489ed8

Browse files
committed
Allow pushing some msats on channel open
1 parent 6aa4a86 commit f489ed8

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn main() {
2626

2727
node.sync_wallets().unwrap();
2828

29-
node.connect_open_channel("NODE_ID@PEER_ADDR:PORT", 10000, false).unwrap();
29+
node.connect_open_channel("NODE_ID@PEER_ADDR:PORT", 10000, None, false).unwrap();
3030

3131
let invoice = Invoice::from_str("INVOICE_STR").unwrap();
3232
node.send_payment(invoice).unwrap();

src/lib.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
//!
4545
//! node.sync_wallets().unwrap();
4646
//!
47-
//! node.connect_open_channel("NODE_ID@PEER_ADDR:PORT", 10000, false).unwrap();
47+
//! node.connect_open_channel("NODE_ID@PEER_ADDR:PORT", 10000, None, false).unwrap();
4848
//!
4949
//! let invoice = Invoice::from_str("INVOICE_STR").unwrap();
5050
//! node.send_payment(invoice).unwrap();
@@ -797,11 +797,18 @@ impl Node {
797797
self.channel_manager.list_channels()
798798
}
799799

800-
/// Connect to a node and open a new channel. Disconnects and re-connects are handled automatically
800+
/// Connect to a node and opens a new channel.
801801
///
802-
/// Returns a temporary channel id
802+
/// Disconnects and reconnects are handled automatically.
803+
///
804+
/// If `push_to_counterparty_msat` is set, the given value will be pushed (read: sent) to the
805+
/// channel counterparty on channel open. This can be useful to start out with the balance not
806+
/// entirely shifted to one side, therefore allowing to receive payments from the getgo.
807+
///
808+
/// Returns a temporary channel id.
803809
pub fn connect_open_channel(
804-
&self, node_pubkey_and_address: &str, channel_amount_sats: u64, announce_channel: bool,
810+
&self, node_pubkey_and_address: &str, channel_amount_sats: u64,
811+
push_to_counterparty_msat: Option<u64>, announce_channel: bool,
805812
) -> Result<(), Error> {
806813
let runtime_lock = self.running.read().unwrap();
807814
if runtime_lock.is_none() {
@@ -851,12 +858,13 @@ impl Node {
851858
..Default::default()
852859
};
853860

861+
let push_msat = push_to_counterparty_msat.unwrap_or(0);
854862
let user_channel_id: u128 = rand::thread_rng().gen::<u128>();
855863

856864
match self.channel_manager.create_channel(
857865
peer_info.pubkey,
858866
channel_amount_sats,
859-
0,
867+
push_msat,
860868
user_channel_id,
861869
Some(user_config),
862870
) {

src/tests/functional_tests.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,15 @@ fn channel_full_cycle() {
181181

182182
println!("\nA -- connect_open_channel -> B");
183183
let node_b_addr = format!("{}@{}", node_b.node_id(), node_b.listening_address().unwrap());
184-
node_a.connect_open_channel(&node_b_addr, 50000, true).unwrap();
184+
node_a.connect_open_channel(&node_b_addr, 80_000, Some(40_000 * 1000), true).unwrap();
185185

186186
let funding_txo = loop {
187187
let details = node_a.list_channels();
188188

189189
if details.is_empty() || details[0].funding_txo.is_none() {
190190
std::thread::sleep(Duration::from_secs(1));
191191
} else {
192+
assert_eq!(details[0].inbound_capacity_msat, 39000000);
192193
break details[0].funding_txo.unwrap();
193194
}
194195
};
@@ -201,8 +202,8 @@ fn channel_full_cycle() {
201202
node_b.sync_wallets().unwrap();
202203

203204
let node_a_balance = node_a.on_chain_balance().unwrap();
204-
assert!(node_a_balance.get_spendable() < 50000);
205-
assert!(node_a_balance.get_spendable() > 40000);
205+
assert!(node_a_balance.get_spendable() < 20000);
206+
assert!(node_a_balance.get_spendable() > 15000);
206207
assert_eq!(node_b.on_chain_balance().unwrap().get_spendable(), 100000);
207208

208209
expect_event!(node_a, ChannelReady);
@@ -282,8 +283,8 @@ fn channel_full_cycle() {
282283
node_a.sync_wallets().unwrap();
283284
node_b.sync_wallets().unwrap();
284285

285-
assert!(node_a.on_chain_balance().unwrap().get_spendable() > 90000);
286-
assert_eq!(node_b.on_chain_balance().unwrap().get_spendable(), 103234);
286+
assert!(node_a.on_chain_balance().unwrap().get_spendable() > 50000);
287+
assert_eq!(node_b.on_chain_balance().unwrap().get_spendable(), 143234);
287288

288289
node_a.stop().unwrap();
289290
println!("\nA stopped");
@@ -315,7 +316,7 @@ fn channel_open_fails_when_funds_insufficient() {
315316
let node_b_addr = format!("{}@{}", node_b.node_id(), node_b.listening_address().unwrap());
316317
assert_eq!(
317318
Err(Error::InsufficientFunds),
318-
node_a.connect_open_channel(&node_b_addr, 120000, true)
319+
node_a.connect_open_channel(&node_b_addr, 120000, None, true)
319320
);
320321
}
321322

0 commit comments

Comments
 (0)