- loop {
- let loop_channel_manager = channel_manager.clone();
- let mut events = channel_manager.get_and_clear_pending_events();
- events.append(&mut chain_monitor.get_and_clear_pending_events());
- for event in events {
- match event {
- Event::FundingGenerationReady {
- temporary_channel_id,
- channel_value_satoshis,
- output_script,
- ..
- } => {
- // Construct the raw transaction with one output, that is paid the amount of the
- // channel.
- let addr = WitnessProgram::from_scriptpubkey(
- &output_script[..],
- match network {
- Network::Bitcoin => bitcoin_bech32::constants::Network::Bitcoin,
- Network::Testnet => bitcoin_bech32::constants::Network::Testnet,
- Network::Regtest => bitcoin_bech32::constants::Network::Regtest,
- Network::Signet => panic!("Signet unsupported"),
- },
- )
- .expect("Lightning funding tx should always be to a SegWit output")
- .to_address();
- let mut outputs = vec![HashMap::with_capacity(1)];
- outputs[0].insert(addr, channel_value_satoshis as f64 / 100_000_000.0);
- let raw_tx = bitcoind_client.create_raw_transaction(outputs).await;
-
- // Have your wallet put the inputs into the transaction such that the output is
- // satisfied.
- let funded_tx = bitcoind_client.fund_raw_transaction(raw_tx).await;
- let change_output_position = funded_tx.changepos;
- assert!(change_output_position == 0 || change_output_position == 1);
-
- // Sign the final funding transaction and broadcast it.
- let signed_tx =
- bitcoind_client.sign_raw_transaction_with_wallet(funded_tx.hex).await;
- assert_eq!(signed_tx.complete, true);
- let final_tx: Transaction =
- encode::deserialize(&hex_utils::to_vec(&signed_tx.hex).unwrap()).unwrap();
- // Give the funding transaction back to LDK for opening the channel.
- loop_channel_manager
- .funding_transaction_generated(&temporary_channel_id, final_tx)
- .unwrap();
- }
- Event::PaymentReceived { payment_hash, payment_secret, amt } => {
- let mut payments = inbound_payments.lock().unwrap();
- if let Some(payment) = payments.get_mut(&payment_hash) {
- if payment.secret == payment_secret {
- assert!(loop_channel_manager.claim_funds(
- payment.preimage.unwrap().clone(),
- &payment.secret,
- payment.amt_msat.0.unwrap(),
- ));
- println!(
- "\nEVENT: received payment from payment hash {} of {} millisatoshis",
- hex_utils::hex_str(&payment_hash.0),
- payment.amt_msat
- );
- print!("> ");
- io::stdout().flush().unwrap();
- payment.status = HTLCStatus::Succeeded;
- } else {
- loop_channel_manager
- .fail_htlc_backwards(&payment_hash, &payment.secret);
- println!("\nERROR: we received a payment from payment hash {} but the payment secret didn't match", hex_utils::hex_str(&payment_hash.0));
- print!("> ");
- io::stdout().flush().unwrap();
- payment.status = HTLCStatus::Failed;
- }
- } else {
- loop_channel_manager.fail_htlc_backwards(&payment_hash, &payment_secret);
- println!("\nERROR: we received a payment for payment hash {} but didn't know the preimage", hex_utils::hex_str(&payment_hash.0));
- print!("> ");
- io::stdout().flush().unwrap();
- payments.insert(
- payment_hash,
- PaymentInfo {
- preimage: None,
- secret: payment_secret,
- status: HTLCStatus::Failed,
- amt_msat: MillisatAmount(Some(amt)),
- },
- );
- }
- }
- Event::PaymentSent { payment_preimage } => {
- let hashed = PaymentHash(Sha256::hash(&payment_preimage.0).into_inner());
- let mut payments = outbound_payments.lock().unwrap();
- for (payment_hash, payment) in payments.iter_mut() {
- if *payment_hash == hashed {
- payment.preimage = Some(payment_preimage);
- payment.status = HTLCStatus::Succeeded;
- println!(
- "\nEVENT: successfully sent payment of {} millisatoshis from \
- payment hash {:?} with preimage {:?}",
- payment.amt_msat,
- hex_utils::hex_str(&payment_hash.0),
- hex_utils::hex_str(&payment_preimage.0)
- );
- print!("> ");
- io::stdout().flush().unwrap();
- }
- }
- }
- Event::PaymentFailed { payment_hash, rejected_by_dest } => {
- print!(
- "\nEVENT: Failed to send payment to payment hash {:?}: ",
- hex_utils::hex_str(&payment_hash.0)
+ match event {
+ Event::FundingGenerationReady {
+ temporary_channel_id,
+ channel_value_satoshis,
+ output_script,
+ ..
+ } => {
+ // Construct the raw transaction with one output, that is paid the amount of the
+ // channel.
+ let addr = WitnessProgram::from_scriptpubkey(
+ &output_script[..],
+ match network {
+ Network::Bitcoin => bitcoin_bech32::constants::Network::Bitcoin,
+ Network::Testnet => bitcoin_bech32::constants::Network::Testnet,
+ Network::Regtest => bitcoin_bech32::constants::Network::Regtest,
+ Network::Signet => panic!("Signet unsupported"),
+ },
+ )
+ .expect("Lightning funding tx should always be to a SegWit output")
+ .to_address();
+ let mut outputs = vec![HashMap::with_capacity(1)];
+ outputs[0].insert(addr, channel_value_satoshis as f64 / 100_000_000.0);
+ let raw_tx = bitcoind_client.create_raw_transaction(outputs).await;
+
+ // Have your wallet put the inputs into the transaction such that the output is
+ // satisfied.
+ let funded_tx = bitcoind_client.fund_raw_transaction(raw_tx).await;
+ let change_output_position = funded_tx.changepos;
+ assert!(change_output_position == 0 || change_output_position == 1);
+
+ // Sign the final funding transaction and broadcast it.
+ let signed_tx = bitcoind_client.sign_raw_transaction_with_wallet(funded_tx.hex).await;
+ assert_eq!(signed_tx.complete, true);
+ let final_tx: Transaction =
+ encode::deserialize(&hex_utils::to_vec(&signed_tx.hex).unwrap()).unwrap();
+ // Give the funding transaction back to LDK for opening the channel.
+ channel_manager.funding_transaction_generated(&temporary_channel_id, final_tx).unwrap();
+ }
+ Event::PaymentReceived { payment_hash, payment_preimage, payment_secret, amt, .. } => {
+ let mut payments = inbound_payments.lock().unwrap();
+ let status = match channel_manager.claim_funds(payment_preimage.unwrap()) {
+ true => {
+ println!(
+ "\nEVENT: received payment from payment hash {} of {} millisatoshis",
+ hex_utils::hex_str(&payment_hash.0),
+ amt