@@ -1486,6 +1486,103 @@ where
1486
1486
/// # }
1487
1487
/// ```
1488
1488
///
1489
+ /// ## BOLT 12 Offers
1490
+ ///
1491
+ /// The [`offers`] module is useful for creating BOLT 12 offers. An [`Offer`] is a precursor to a
1492
+ /// [`Bolt12Invoice`], which must first be requested by the payer. The interchange of these messages
1493
+ /// as defined in the specification is handled by [`ChannelManager`] and its implementation of
1494
+ /// [`OffersMessageHandler`]. However, this only works with an [`Offer`] created using a builder
1495
+ /// returned by [`create_offer_builder`]. With this approach, offers are stateless just as they are
1496
+ /// with BOLT 11 invoices.
1497
+ ///
1498
+ /// ```
1499
+ /// # use lightning::events::{Event, EventsProvider, PaymentPurpose};
1500
+ /// # use lightning::ln::channelmanager::AChannelManager;
1501
+ /// # use lightning::offers::parse::Bolt12SemanticError;
1502
+ /// #
1503
+ /// # fn example<T: AChannelManager>(channel_manager: T) -> Result<(), Bolt12SemanticError> {
1504
+ /// # let channel_manager = channel_manager.get_cm();
1505
+ /// let offer = channel_manager
1506
+ /// .create_offer_builder("coffee".to_string())?
1507
+ /// .amount_msats(10_000_000)
1508
+ /// .build()?;
1509
+ /// let bech32_offer = offer.to_string();
1510
+ ///
1511
+ /// // On the event processing thread
1512
+ /// channel_manager.process_pending_events(&|event| match event {
1513
+ /// Event::PaymentClaimable { payment_hash, purpose, .. } => match purpose {
1514
+ /// PaymentPurpose::InvoicePayment { payment_preimage: Some(payment_preimage), .. } => {
1515
+ /// println!("Claiming payment {}", payment_hash);
1516
+ /// channel_manager.claim_funds(payment_preimage);
1517
+ /// },
1518
+ /// PaymentPurpose::InvoicePayment { payment_preimage: None, .. } => {
1519
+ /// println!("Unknown payment hash: {}", payment_hash);
1520
+ /// },
1521
+ /// // ...
1522
+ /// # _ => {},
1523
+ /// },
1524
+ /// Event::PaymentClaimed { payment_hash, amount_msat, .. } => {
1525
+ /// println!("Claimed {} msats", amount_msat);
1526
+ /// },
1527
+ /// // ...
1528
+ /// # _ => {},
1529
+ /// });
1530
+ /// # Ok(())
1531
+ /// # }
1532
+ /// ```
1533
+ ///
1534
+ /// Use [`pay_for_offer`] to initiated payment, which sends an [`InvoiceRequest`] for an [`Offer`]
1535
+ /// and pays the [`Bolt12Invoice`] response. In addition to success and failure events,
1536
+ /// [`ChannelManager`] may also generate an [`Event::InvoiceRequestFailed`].
1537
+ ///
1538
+ /// ```
1539
+ /// # use core::time::Duration;
1540
+ /// # use lightning::events::{Event, EventsProvider};
1541
+ /// # use lightning::ln::channelmanager::{AChannelManager, PaymentId, RecentPaymentDetails, Retry};
1542
+ /// # use lightning::offers::offer::Offer;
1543
+ /// #
1544
+ /// # fn example<T: AChannelManager>(
1545
+ /// # channel_manager: T, offer: &Offer, quantity: Option<u64>, amount_msats: Option<u64>,
1546
+ /// # payer_note: Option<String>, max_total_routing_fee_msat: Option<u64>
1547
+ /// # ) {
1548
+ /// # let channel_manager = channel_manager.get_cm();
1549
+ /// let payment_id = PaymentId([42; 32]);
1550
+ /// let retry = Retry::Timeout(Duration::from_secs(60));
1551
+ /// match channel_manager.pay_for_offer(
1552
+ /// offer, quantity, amount_msats, payer_note, payment_id, retry, max_total_routing_fee_msat
1553
+ /// ) {
1554
+ /// Ok(()) => println!("Requesting invoice for offer"),
1555
+ /// Err(e) => println!("Unable to request invoice for offer: {:?}", e),
1556
+ /// }
1557
+ ///
1558
+ /// // First the payment will be waiting on an invoice
1559
+ /// let expected_payment_id = payment_id;
1560
+ /// assert!(
1561
+ /// channel_manager.list_recent_payments().iter().find(|details| matches!(
1562
+ /// details,
1563
+ /// RecentPaymentDetails::AwaitingInvoice { payment_id: expected_payment_id }
1564
+ /// )).is_some()
1565
+ /// );
1566
+ ///
1567
+ /// // Once the invoice is received, a payment will be sent
1568
+ /// assert!(
1569
+ /// channel_manager.list_recent_payments().iter().find(|details| matches!(
1570
+ /// details,
1571
+ /// RecentPaymentDetails::Pending { payment_id: expected_payment_id, .. }
1572
+ /// )).is_some()
1573
+ /// );
1574
+ ///
1575
+ /// // On the event processing thread
1576
+ /// channel_manager.process_pending_events(&|event| match event {
1577
+ /// Event::PaymentSent { payment_id: Some(payment_id), .. } => println!("Paid {}", payment_id),
1578
+ /// Event::PaymentFailed { payment_id, .. } => println!("Failed paying {}", payment_id),
1579
+ /// Event::InvoiceRequestFailed { payment_id, .. } => println!("Failed paying {}", payment_id),
1580
+ /// // ...
1581
+ /// # _ => {},
1582
+ /// });
1583
+ /// # }
1584
+ /// ```
1585
+ ///
1489
1586
/// # Persistence
1490
1587
///
1491
1588
/// Implements [`Writeable`] to write out all channel state to disk. Implies [`peer_disconnected`] for
@@ -1561,6 +1658,10 @@ where
1561
1658
/// [`create_inbound_payment_for_hash`]: Self::create_inbound_payment_for_hash
1562
1659
/// [`claim_funds`]: Self::claim_funds
1563
1660
/// [`send_payment`]: Self::send_payment
1661
+ /// [`offers`]: crate::offers
1662
+ /// [`create_offer_builder`]: Self::create_offer_builder
1663
+ /// [`pay_for_offer`]: Self::pay_for_offer
1664
+ /// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
1564
1665
/// [`peer_disconnected`]: msgs::ChannelMessageHandler::peer_disconnected
1565
1666
/// [`funding_created`]: msgs::FundingCreated
1566
1667
/// [`funding_transaction_generated`]: Self::funding_transaction_generated
0 commit comments