Skip to content

Commit 47611cd

Browse files
committed
Add BOLT 12 Offers section to ChannelManager docs
1 parent 68d5052 commit 47611cd

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

lightning/src/ln/channelmanager.rs

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

0 commit comments

Comments
 (0)