Skip to content

Commit

Permalink
feat: payment process server
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Feb 13, 2025
1 parent b8a27f8 commit 1a0196f
Show file tree
Hide file tree
Showing 7 changed files with 393 additions and 399 deletions.
67 changes: 66 additions & 1 deletion crates/cdk-common/src/proto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@ use std::str::FromStr;
use cashu::{Bolt11Invoice, CurrencyUnit, MeltQuoteBolt11Request};
use melt_options::Options;

use crate::lightning::{CreateInvoiceResponse, PayInvoiceResponse};
use crate::lightning::{CreateInvoiceResponse, PayInvoiceResponse, Settings};

tonic::include_proto!("cdk_payment_processor");

impl From<Settings> for SettingsResponse {
fn from(value: Settings) -> Self {
Self {
mpp: value.mpp,
unit: value.unit.to_string(),
invoice_description: value.invoice_description,
}
}
}

impl TryFrom<MakePaymentResponse> for PayInvoiceResponse {
type Error = crate::error::Error;
fn try_from(value: MakePaymentResponse) -> Result<Self, Self::Error> {
Expand All @@ -32,6 +42,16 @@ impl From<PayInvoiceResponse> for MakePaymentResponse {
}
}

impl From<CreateInvoiceResponse> for CreatePaymentResponse {
fn from(value: CreateInvoiceResponse) -> Self {
Self {
request_lookup_id: value.request_lookup_id,
request: value.request.to_string(),
expiry: value.expiry,
}
}
}

impl TryFrom<CreatePaymentResponse> for CreateInvoiceResponse {
type Error = crate::error::Error;

Expand All @@ -54,6 +74,17 @@ impl From<&MeltQuoteBolt11Request> for PaymentQuoteRequest {
}
}

impl From<crate::lightning::PaymentQuoteResponse> for PaymentQuoteResponse {
fn from(value: crate::lightning::PaymentQuoteResponse) -> Self {
Self {
request_lookup_id: value.request_lookup_id,
amount: value.amount.into(),
fee: value.fee.into(),
state: QuoteState::from(value.state).into(),
}
}
}

impl From<cashu::nut05::MeltOptions> for MeltOptions {
fn from(value: cashu::nut05::MeltOptions) -> Self {
Self {
Expand Down Expand Up @@ -100,6 +131,7 @@ impl From<QuoteState> for cashu::nut05::QuoteState {
QuoteState::Pending => Self::Pending,
QuoteState::Unknown => Self::Unknown,
QuoteState::Failed => Self::Failed,
QuoteState::Issued => Self::Unknown,
}
}
}
Expand All @@ -116,6 +148,17 @@ impl From<cashu::nut05::QuoteState> for QuoteState {
}
}

impl From<cashu::nut04::QuoteState> for QuoteState {
fn from(value: cashu::nut04::QuoteState) -> Self {
match value {
cashu::MintQuoteState::Unpaid => Self::Unpaid,
cashu::MintQuoteState::Paid => Self::Paid,
cashu::MintQuoteState::Pending => Self::Pending,
cashu::MintQuoteState::Issued => Self::Issued,
}
}
}

impl From<cashu::mint::MeltQuote> for MeltQuote {
fn from(value: cashu::mint::MeltQuote) -> Self {
Self {
Expand All @@ -133,6 +176,28 @@ impl From<cashu::mint::MeltQuote> for MeltQuote {
}
}

impl TryFrom<MeltQuote> for cashu::mint::MeltQuote {
type Error = crate::error::Error;

fn try_from(value: MeltQuote) -> Result<Self, Self::Error> {
Ok(Self {
id: value
.id
.parse()
.map_err(|_| crate::error::Error::Internal)?,
unit: value.unit.parse()?,
amount: value.amount.into(),
request: value.request.clone(),
fee_reserve: value.fee_reserve.into(),
state: cashu::nut05::QuoteState::from(value.state()),
expiry: value.expiry,
payment_preimage: value.payment_preimage,
request_lookup_id: value.request_lookup_id,
msat_to_pay: value.msat_to_pay.map(|a| a.into()),
})
}
}

impl TryFrom<PaymentQuoteRequest> for MeltQuoteBolt11Request {
type Error = crate::error::Error;

Expand Down
1 change: 1 addition & 0 deletions crates/cdk-common/src/proto/payment_processor.proto
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ enum QuoteState {
PENDING = 2;
UNKNOWN = 3;
FAILED = 4;
ISSUED = 5;
}


Expand Down
Loading

0 comments on commit 1a0196f

Please sign in to comment.