Skip to content

Commit 63a64e5

Browse files
committed
Add BOLT 12 Refunds section to ChannelManager docs
1 parent f84a69e commit 63a64e5

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,6 +1625,100 @@ where
16251625
/// # }
16261626
/// ```
16271627
///
1628+
/// ## BOLT 12 Refunds
1629+
///
1630+
/// A [`Refund`] is a request for an invoice to be paid. Like *paying* for an [`Offer`], *creating*
1631+
/// a [`Refund`] involves maintaining state since it represents a future outbound payment.
1632+
/// Therefore, use [`create_refund_builder`] when creating one, otherwise [`ChannelManager`] will
1633+
/// refuse to pay any corresponding [`Bolt12Invoice`] that it receives.
1634+
///
1635+
/// ```
1636+
/// # use core::time::Duration;
1637+
/// # use lightning::events::{Event, EventsProvider};
1638+
/// # use lightning::ln::channelmanager::{AChannelManager, PaymentId, RecentPaymentDetails, Retry};
1639+
/// # use lightning::offers::parse::Bolt12SemanticError;
1640+
/// #
1641+
/// # fn example<T: AChannelManager>(
1642+
/// # channel_manager: T, amount_msats: u64, absolute_expiry: Duration,
1643+
/// # max_total_routing_fee_msat: Option<u64>
1644+
/// # ) -> Result<(), Bolt12SemanticError> {
1645+
/// # let channel_manager = channel_manager.get_cm();
1646+
/// let payment_id = PaymentId([42; 32]);
1647+
/// let retry = Retry::Timeout(Duration::from_secs(60));
1648+
/// let refund = channel_manager
1649+
/// .create_refund_builder(
1650+
/// "coffee".to_string(), amount_msats, absolute_expiry, payment_id, retry,
1651+
/// max_total_routing_fee_msat
1652+
/// )?
1653+
/// .payer_note("refund for order 1234".to_string())
1654+
/// .build()?;
1655+
/// let bech32_refund = refund.to_string();
1656+
///
1657+
/// // First the payment will be waiting on an invoice
1658+
/// let expected_payment_id = payment_id;
1659+
/// assert!(
1660+
/// channel_manager.list_recent_payments().iter().find(|details| matches!(
1661+
/// details,
1662+
/// RecentPaymentDetails::AwaitingInvoice { payment_id: expected_payment_id }
1663+
/// )).is_some()
1664+
/// );
1665+
///
1666+
/// // Once the invoice is received, a payment will be sent
1667+
/// assert!(
1668+
/// channel_manager.list_recent_payments().iter().find(|details| matches!(
1669+
/// details,
1670+
/// RecentPaymentDetails::Pending { payment_id: expected_payment_id, .. }
1671+
/// )).is_some()
1672+
/// );
1673+
///
1674+
/// // On the event processing thread
1675+
/// channel_manager.process_pending_events(&|event| match event {
1676+
/// Event::PaymentSent { payment_id: Some(payment_id), .. } => println!("Paid {}", payment_id),
1677+
/// Event::PaymentFailed { payment_id, .. } => println!("Failed paying {}", payment_id),
1678+
/// // ...
1679+
/// # _ => {},
1680+
/// });
1681+
/// # Ok(())
1682+
/// # }
1683+
/// ```
1684+
///
1685+
/// Use [`request_refund_payment`] to send a [`Bolt12Invoice`] for receiving the refund. Similar to
1686+
/// *creating* an [`Offer`], this is stateless as it represents an inbound payment.
1687+
///
1688+
/// ```
1689+
/// # use lightning::events::{Event, EventsProvider, PaymentPurpose};
1690+
/// # use lightning::ln::channelmanager::AChannelManager;
1691+
/// # use lightning::offers::refund::Refund;
1692+
/// #
1693+
/// # fn example<T: AChannelManager>(channel_manager: T, refund: &Refund) {
1694+
/// # let channel_manager = channel_manager.get_cm();
1695+
/// match channel_manager.request_refund_payment(refund) {
1696+
/// Ok(()) => println!("Requesting payment for refund"),
1697+
/// Err(e) => println!("Unable to request payment for refund: {:?}", e),
1698+
/// }
1699+
///
1700+
/// // On the event processing thread
1701+
/// channel_manager.process_pending_events(&|event| match event {
1702+
/// Event::PaymentClaimable { payment_hash, purpose, .. } => match purpose {
1703+
/// PaymentPurpose::InvoicePayment { payment_preimage: Some(payment_preimage), .. } => {
1704+
/// println!("Claiming payment {}", payment_hash);
1705+
/// channel_manager.claim_funds(payment_preimage);
1706+
/// },
1707+
/// PaymentPurpose::InvoicePayment { payment_preimage: None, .. } => {
1708+
/// println!("Unknown payment hash: {}", payment_hash);
1709+
/// },
1710+
/// // ...
1711+
/// # _ => {},
1712+
/// },
1713+
/// Event::PaymentClaimed { payment_hash, amount_msat, .. } => {
1714+
/// println!("Claimed {} msats", amount_msat);
1715+
/// },
1716+
/// // ...
1717+
/// # _ => {},
1718+
/// });
1719+
/// # }
1720+
/// ```
1721+
///
16281722
/// # Persistence
16291723
///
16301724
/// Implements [`Writeable`] to write out all channel state to disk. Implies [`peer_disconnected`] for
@@ -1704,6 +1798,8 @@ where
17041798
/// [`create_offer_builder`]: Self::create_offer_builder
17051799
/// [`pay_for_offer`]: Self::pay_for_offer
17061800
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
1801+
/// [`create_refund_builder`]: Self::create_refund_builder
1802+
/// [`request_refund_payment`]: Self::request_refund_payment
17071803
/// [`peer_disconnected`]: msgs::ChannelMessageHandler::peer_disconnected
17081804
/// [`funding_created`]: msgs::FundingCreated
17091805
/// [`funding_transaction_generated`]: Self::funding_transaction_generated

0 commit comments

Comments
 (0)