Skip to content

Commit e678b1f

Browse files
committed
Allow pushing some msats on channel open
1 parent 65d14dc commit e678b1f

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();
@@ -841,11 +841,18 @@ impl Node {
841841
self.channel_manager.list_channels()
842842
}
843843

844-
/// Connect to a node and open a new channel. Disconnects and re-connects are handled automatically
844+
/// Connect to a node and opens a new channel.
845845
///
846-
/// Returns a temporary channel id
846+
/// Disconnects and reconnects are handled automatically.
847+
///
848+
/// If `push_to_counterparty_msat` is set, the given value will be pushed (read: sent) to the
849+
/// channel counterparty on channel open. This can be useful to start out with the balance not
850+
/// entirely shifted to one side, therefore allowing to receive payments from the getgo.
851+
///
852+
/// Returns a temporary channel id.
847853
pub fn connect_open_channel(
848-
&self, node_pubkey_and_address: &str, channel_amount_sats: u64, announce_channel: bool,
854+
&self, node_pubkey_and_address: &str, channel_amount_sats: u64,
855+
push_to_counterparty_msat: Option<u64>, announce_channel: bool,
849856
) -> Result<(), Error> {
850857
let runtime_lock = self.running.read().unwrap();
851858
if runtime_lock.is_none() {
@@ -895,12 +902,13 @@ impl Node {
895902
..Default::default()
896903
};
897904

905+
let push_msat = push_to_counterparty_msat.unwrap_or(0);
898906
let user_channel_id: u128 = rand::thread_rng().gen::<u128>();
899907

900908
match self.channel_manager.create_channel(
901909
peer_info.pubkey,
902910
channel_amount_sats,
903-
0,
911+
push_msat,
904912
user_channel_id,
905913
Some(user_config),
906914
) {

src/test/functional_tests.rs

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

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

3939
let funding_txo = loop {
4040
let details = node_a.list_channels();
4141

4242
if details.is_empty() || details[0].funding_txo.is_none() {
4343
std::thread::sleep(Duration::from_secs(1));
4444
} else {
45+
assert_eq!(details[0].inbound_capacity_msat, 39000000);
4546
break details[0].funding_txo.unwrap();
4647
}
4748
};
@@ -54,8 +55,8 @@ fn channel_full_cycle() {
5455
node_b.sync_wallets().unwrap();
5556

5657
let node_a_balance = node_a.on_chain_balance().unwrap();
57-
assert!(node_a_balance.get_spendable() < 50000);
58-
assert!(node_a_balance.get_spendable() > 40000);
58+
assert!(node_a_balance.get_spendable() < 20000);
59+
assert!(node_a_balance.get_spendable() > 15000);
5960
assert_eq!(node_b.on_chain_balance().unwrap().get_spendable(), 100000);
6061

6162
expect_event!(node_a, ChannelReady);
@@ -174,8 +175,8 @@ fn channel_full_cycle() {
174175
node_a.sync_wallets().unwrap();
175176
node_b.sync_wallets().unwrap();
176177

177-
assert!(node_a.on_chain_balance().unwrap().get_spendable() > 90000);
178-
assert_eq!(node_b.on_chain_balance().unwrap().get_spendable(), 103234);
178+
assert!(node_a.on_chain_balance().unwrap().get_spendable() > 50000);
179+
assert_eq!(node_b.on_chain_balance().unwrap().get_spendable(), 143234);
179180

180181
node_a.stop().unwrap();
181182
println!("\nA stopped");
@@ -214,7 +215,7 @@ fn channel_open_fails_when_funds_insufficient() {
214215
let node_b_addr = format!("{}@{}", node_b.node_id(), node_b.listening_address().unwrap());
215216
assert_eq!(
216217
Err(Error::InsufficientFunds),
217-
node_a.connect_open_channel(&node_b_addr, 120000, true)
218+
node_a.connect_open_channel(&node_b_addr, 120000, None, true)
218219
);
219220
}
220221

0 commit comments

Comments
 (0)