use std::pin::Pin;
use async_trait::async_trait;
use futures::Stream;
use lightning_invoice::{Bolt11Invoice, ParseOrSemanticError};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use crate::nuts::{CurrencyUnit, MeltQuoteBolt11Request, MeltQuoteState, MintQuoteState};
use crate::{mint, Amount};
#[derive(Debug, Error)]
pub enum Error {
#[error("Invoice already paid")]
InvoiceAlreadyPaid,
#[error("Invoice pay is pending")]
InvoicePaymentPending,
#[error("Unsupported unit")]
UnsupportedUnit,
#[error("Payment state is unknown")]
UnknownPaymentState,
#[error(transparent)]
Lightning(Box<dyn std::error::Error + Send + Sync>),
#[error(transparent)]
Serde(#[from] serde_json::Error),
#[error(transparent)]
Anyhow(#[from] anyhow::Error),
#[error(transparent)]
Parse(#[from] ParseOrSemanticError),
#[error(transparent)]
Amount(#[from] crate::amount::Error),
#[error(transparent)]
NUT05(#[from] crate::nuts::nut05::Error),
}
#[async_trait]
pub trait MintLightning {
type Err: Into<Error> + From<Error>;
fn get_settings(&self) -> Settings;
async fn create_invoice(
&self,
amount: Amount,
unit: &CurrencyUnit,
description: String,
unix_expiry: u64,
) -> Result<CreateInvoiceResponse, Self::Err>;
async fn get_payment_quote(
&self,
melt_quote_request: &MeltQuoteBolt11Request,
) -> Result<PaymentQuoteResponse, Self::Err>;
async fn pay_invoice(
&self,
melt_quote: mint::MeltQuote,
partial_amount: Option<Amount>,
max_fee_amount: Option<Amount>,
) -> Result<PayInvoiceResponse, Self::Err>;
async fn wait_any_invoice(
&self,
) -> Result<Pin<Box<dyn Stream<Item = String> + Send>>, Self::Err>;
fn is_wait_invoice_active(&self) -> bool;
fn cancel_wait_invoice(&self);
async fn check_incoming_invoice_status(
&self,
request_lookup_id: &str,
) -> Result<MintQuoteState, Self::Err>;
async fn check_outgoing_payment(
&self,
request_lookup_id: &str,
) -> Result<PayInvoiceResponse, Self::Err>;
}
#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct CreateInvoiceResponse {
pub request_lookup_id: String,
pub request: Bolt11Invoice,
pub expiry: Option<u64>,
}
#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct PayInvoiceResponse {
pub payment_lookup_id: String,
pub payment_preimage: Option<String>,
pub status: MeltQuoteState,
pub total_spent: Amount,
pub unit: CurrencyUnit,
}
#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct PaymentQuoteResponse {
pub request_lookup_id: String,
pub amount: Amount,
pub fee: Amount,
pub state: MeltQuoteState,
}
#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct Settings {
pub mpp: bool,
pub unit: CurrencyUnit,
pub invoice_description: bool,
}