lightning.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //! CDK Mint Lightning
  2. use std::pin::Pin;
  3. use async_trait::async_trait;
  4. use futures::Stream;
  5. use lightning_invoice::{Bolt11Invoice, ParseOrSemanticError};
  6. use serde::{Deserialize, Serialize};
  7. use thiserror::Error;
  8. use crate::nuts::{CurrencyUnit, MeltQuoteBolt11Request, MeltQuoteState, MintQuoteState};
  9. use crate::{mint, Amount};
  10. /// CDK Lightning Error
  11. #[derive(Debug, Error)]
  12. pub enum Error {
  13. /// Invoice already paid
  14. #[error("Invoice already paid")]
  15. InvoiceAlreadyPaid,
  16. /// Invoice pay pending
  17. #[error("Invoice pay is pending")]
  18. InvoicePaymentPending,
  19. /// Unsupported unit
  20. #[error("Unsupported unit")]
  21. UnsupportedUnit,
  22. /// Payment state is unknown
  23. #[error("Payment state is unknown")]
  24. UnknownPaymentState,
  25. /// Lightning Error
  26. #[error(transparent)]
  27. Lightning(Box<dyn std::error::Error + Send + Sync>),
  28. /// Serde Error
  29. #[error(transparent)]
  30. Serde(#[from] serde_json::Error),
  31. /// AnyHow Error
  32. #[error(transparent)]
  33. Anyhow(#[from] anyhow::Error),
  34. /// Parse Error
  35. #[error(transparent)]
  36. Parse(#[from] ParseOrSemanticError),
  37. /// Amount Error
  38. #[error(transparent)]
  39. Amount(#[from] crate::amount::Error),
  40. /// NUT05 Error
  41. #[error(transparent)]
  42. NUT05(#[from] crate::nuts::nut05::Error),
  43. }
  44. /// MintLighting Trait
  45. #[async_trait]
  46. pub trait MintLightning {
  47. /// Mint Lightning Error
  48. type Err: Into<Error> + From<Error>;
  49. /// Base Unit
  50. fn get_settings(&self) -> Settings;
  51. /// Create a new invoice
  52. async fn create_invoice(
  53. &self,
  54. amount: Amount,
  55. unit: &CurrencyUnit,
  56. description: String,
  57. unix_expiry: u64,
  58. ) -> Result<CreateInvoiceResponse, Self::Err>;
  59. /// Get payment quote
  60. /// Used to get fee and amount required for a payment request
  61. async fn get_payment_quote(
  62. &self,
  63. melt_quote_request: &MeltQuoteBolt11Request,
  64. ) -> Result<PaymentQuoteResponse, Self::Err>;
  65. /// Pay bolt11 invoice
  66. async fn pay_invoice(
  67. &self,
  68. melt_quote: mint::MeltQuote,
  69. partial_amount: Option<Amount>,
  70. max_fee_amount: Option<Amount>,
  71. ) -> Result<PayInvoiceResponse, Self::Err>;
  72. /// Listen for invoices to be paid to the mint
  73. /// Returns a stream of request_lookup_id once invoices are paid
  74. async fn wait_any_invoice(
  75. &self,
  76. ) -> Result<Pin<Box<dyn Stream<Item = String> + Send>>, Self::Err>;
  77. /// Is wait invoice active
  78. fn is_wait_invoice_active(&self) -> bool;
  79. /// Cancel wait invoice
  80. fn cancel_wait_invoice(&self);
  81. /// Check the status of an incoming payment
  82. async fn check_incoming_invoice_status(
  83. &self,
  84. request_lookup_id: &str,
  85. ) -> Result<MintQuoteState, Self::Err>;
  86. /// Check the status of an outgoing payment
  87. async fn check_outgoing_payment(
  88. &self,
  89. request_lookup_id: &str,
  90. ) -> Result<PayInvoiceResponse, Self::Err>;
  91. }
  92. /// Create invoice response
  93. #[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
  94. pub struct CreateInvoiceResponse {
  95. /// Id that is used to look up the invoice from the ln backend
  96. pub request_lookup_id: String,
  97. /// Bolt11 payment request
  98. pub request: Bolt11Invoice,
  99. /// Unix Expiry of Invoice
  100. pub expiry: Option<u64>,
  101. }
  102. /// Pay invoice response
  103. #[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
  104. pub struct PayInvoiceResponse {
  105. /// Payment hash
  106. pub payment_lookup_id: String,
  107. /// Payment Preimage
  108. pub payment_preimage: Option<String>,
  109. /// Status
  110. pub status: MeltQuoteState,
  111. /// Total Amount Spent
  112. pub total_spent: Amount,
  113. /// Unit of total spent
  114. pub unit: CurrencyUnit,
  115. }
  116. /// Payment quote response
  117. #[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
  118. pub struct PaymentQuoteResponse {
  119. /// Request look up id
  120. pub request_lookup_id: String,
  121. /// Amount
  122. pub amount: Amount,
  123. /// Fee required for melt
  124. pub fee: Amount,
  125. /// Status
  126. pub state: MeltQuoteState,
  127. }
  128. /// Ln backend settings
  129. #[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
  130. pub struct Settings {
  131. /// MPP supported
  132. pub mpp: bool,
  133. /// Base unit of backend
  134. pub unit: CurrencyUnit,
  135. /// Invoice Description supported
  136. pub invoice_description: bool,
  137. }