- Event::PaymentReceived { payment_hash, payment_secret, amt: amt_msat } => {
- let mut payments = payment_storage.lock().unwrap();
- if let Some((Some(preimage), _, _, _)) = payments.get(&payment_hash) {
- assert!(loop_channel_manager.claim_funds(
- preimage.clone(),
- &payment_secret,
- amt_msat
- ));
- println!(
- "\nEVENT: received payment from payment_hash {} of {} satoshis",
- hex_utils::hex_str(&payment_hash.0),
- amt_msat / 1000
- );
- print!("> ");
- io::stdout().flush().unwrap();
- let (_, _, ref mut status, _) = payments.get_mut(&payment_hash).unwrap();
- *status = HTLCStatus::Succeeded;
- } else {
- println!("\nERROR: we received a payment but didn't know the preimage");
- print!("> ");
- io::stdout().flush().unwrap();
- loop_channel_manager.fail_htlc_backwards(&payment_hash, &payment_secret);
- payments.insert(
- payment_hash,
- (None, HTLCDirection::Inbound, HTLCStatus::Failed, SatoshiAmount(None)),
- );
+ Event::PaymentReceived {
+ payment_hash,
+ payment_preimage,
+ payment_secret,
+ amt,
+ ..
+ } => {
+ let mut payments = inbound_payments.lock().unwrap();
+ let status = match loop_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
+ );
+ print!("> ");
+ io::stdout().flush().unwrap();
+ HTLCStatus::Succeeded
+ }
+ _ => HTLCStatus::Failed,
+ };
+ match payments.entry(payment_hash) {
+ Entry::Occupied(mut e) => {
+ let payment = e.get_mut();
+ payment.status = status;
+ payment.preimage = Some(payment_preimage.unwrap());
+ payment.secret = Some(payment_secret);
+ }
+ Entry::Vacant(e) => {
+ e.insert(PaymentInfo {
+ preimage: Some(payment_preimage.unwrap()),
+ secret: Some(payment_secret),
+ status,
+ amt_msat: MillisatAmount(Some(amt)),
+ });
+ }