fix: cdk melt quote track payment method (#1021)

Add payment_method field to MeltQuote struct to track whether quotes use BOLT11 or BOLT12 payment methods. Include database migrations for both SQLite and PostgreSQL to support the new field. Update melt operations to set and use the payment method for proper routing between BOLT11 and BOLT12 endpoints.
This commit is contained in:
thesimplekid
2025-09-01 08:39:51 +01:00
committed by GitHub
parent 7246ea2e10
commit 6067242793
8 changed files with 38 additions and 7 deletions

View File

@@ -8,6 +8,8 @@
### Added
- cdk-common: New `Event` enum for payment event handling with `PaymentReceived` variant ([thesimplekid]).
- cdk-common: Added `payment_method` field to `MeltQuote` struct for tracking payment method type ([thesimplekid]).
- cdk-sql-common: Database migration to add `payment_method` column to melt_quote table for SQLite and PostgreSQL ([thesimplekid]).
### Changed
- cdk-common: Refactored `MintPayment` trait method `wait_any_incoming_payment` to `wait_payment_event` with event-driven architecture ([thesimplekid]).
@@ -16,6 +18,9 @@
- cashu: Updated BOLT12 payment method specification from NUT-24 to NUT-25 ([thesimplekid]).
- cdk: Updated BOLT12 import references from nut24 to nut25 module ([thesimplekid]).
### Fixied
- cdk: Wallet melt track and use payment method from quote for BOLT11/BOLT12 routing ([thesimplekid]).
## [0.12.0](https://github.com/cashubtc/cdk/releases/tag/v0.12.0)
### Summary

View File

@@ -84,6 +84,9 @@ pub struct MeltQuote {
pub expiry: u64,
/// Payment preimage
pub payment_preimage: Option<String>,
/// Payment method
#[serde(default)]
pub payment_method: PaymentMethod,
}
impl MintQuote {

View File

@@ -2,6 +2,7 @@
/// Auto-generated by build.rs
pub static MIGRATIONS: &[(&str, &str, &str)] = &[
("postgres", "1_initial.sql", include_str!(r#"./migrations/postgres/1_initial.sql"#)),
("postgres", "20250831215438_melt_quote_method.sql", include_str!(r#"./migrations/postgres/20250831215438_melt_quote_method.sql"#)),
("sqlite", "1_fix_sqlx_migration.sql", include_str!(r#"./migrations/sqlite/1_fix_sqlx_migration.sql"#)),
("sqlite", "20240612132920_init.sql", include_str!(r#"./migrations/sqlite/20240612132920_init.sql"#)),
("sqlite", "20240618200350_quote_state.sql", include_str!(r#"./migrations/sqlite/20240618200350_quote_state.sql"#)),
@@ -22,4 +23,5 @@ pub static MIGRATIONS: &[(&str, &str, &str)] = &[
("sqlite", "20250707093445_bolt12.sql", include_str!(r#"./migrations/sqlite/20250707093445_bolt12.sql"#)),
("sqlite", "20250729111701_keyset_v2_u32.sql", include_str!(r#"./migrations/sqlite/20250729111701_keyset_v2_u32.sql"#)),
("sqlite", "20250812084621_keyset_plus_one.sql", include_str!(r#"./migrations/sqlite/20250812084621_keyset_plus_one.sql"#)),
("sqlite", "20250831215438_melt_quote_method.sql", include_str!(r#"./migrations/sqlite/20250831215438_melt_quote_method.sql"#)),
];

View File

@@ -0,0 +1 @@
ALTER TABLE melt_quote ADD COLUMN payment_method TEXT NOT NULL DEFAULT 'bolt11';

View File

@@ -0,0 +1 @@
ALTER TABLE melt_quote ADD COLUMN payment_method TEXT NOT NULL DEFAULT 'bolt11';

View File

@@ -156,7 +156,8 @@ where
fee_reserve,
state,
expiry,
payment_preimage
payment_preimage,
payment_method
FROM
melt_quote
"#,
@@ -579,16 +580,17 @@ ON CONFLICT(id) DO UPDATE SET
query(
r#"
INSERT INTO melt_quote
(id, unit, amount, request, fee_reserve, state, expiry)
(id, unit, amount, request, fee_reserve, state, expiry, payment_method)
VALUES
(:id, :unit, :amount, :request, :fee_reserve, :state, :expiry)
(:id, :unit, :amount, :request, :fee_reserve, :state, :expiry, :payment_method)
ON CONFLICT(id) DO UPDATE SET
unit = excluded.unit,
amount = excluded.amount,
request = excluded.request,
fee_reserve = excluded.fee_reserve,
state = excluded.state,
expiry = excluded.expiry
expiry = excluded.expiry,
payment_method = excluded.payment_method
;
"#,
)?
@@ -599,6 +601,7 @@ ON CONFLICT(id) DO UPDATE SET
.bind("fee_reserve", u64::from(quote.fee_reserve) as i64)
.bind("state", quote.state.to_string())
.bind("expiry", quote.expiry as i64)
.bind("payment_method", quote.payment_method.to_string())
.execute(&*conn)
.await?;
@@ -618,7 +621,8 @@ ON CONFLICT(id) DO UPDATE SET
fee_reserve,
state,
expiry,
payment_preimage
payment_preimage,
payment_method
FROM
melt_quote
WHERE
@@ -1124,13 +1128,17 @@ fn sql_row_to_melt_quote(row: Vec<Column>) -> Result<wallet::MeltQuote, Error> {
fee_reserve,
state,
expiry,
payment_preimage
payment_preimage,
row_method
) = row
);
let amount: u64 = column_as_number!(amount);
let fee_reserve: u64 = column_as_number!(fee_reserve);
let payment_method =
PaymentMethod::from_str(&column_as_string!(row_method)).map_err(Error::from)?;
Ok(wallet::MeltQuote {
id: column_as_string!(id),
amount: Amount::from(amount),
@@ -1140,6 +1148,7 @@ fn sql_row_to_melt_quote(row: Vec<Column>) -> Result<wallet::MeltQuote, Error> {
state: column_as_string!(state, MeltQuoteState::from_str),
expiry: column_as_number!(expiry),
payment_preimage: column_as_nullable_string!(payment_preimage),
payment_method,
})
}

View File

@@ -3,6 +3,7 @@ use std::str::FromStr;
use cdk_common::amount::SplitTarget;
use cdk_common::wallet::{Transaction, TransactionDirection};
use cdk_common::PaymentMethod;
use lightning_invoice::Bolt11Invoice;
use tracing::instrument;
@@ -87,6 +88,7 @@ impl Wallet {
state: quote_res.state,
expiry: quote_res.expiry,
payment_preimage: quote_res.payment_preimage,
payment_method: PaymentMethod::Bolt11,
};
self.localstore.add_melt_quote(quote.clone()).await?;
@@ -183,7 +185,13 @@ impl Wallet {
Some(premint_secrets.blinded_messages()),
);
let melt_response = self.client.post_melt(request).await;
let melt_response = match quote_info.payment_method {
cdk_common::PaymentMethod::Bolt11 => self.client.post_melt(request).await,
cdk_common::PaymentMethod::Bolt12 => self.client.post_melt_bolt12(request).await,
cdk_common::PaymentMethod::Custom(_) => {
return Err(Error::UnsupportedPaymentMethod);
}
};
let melt_response = match melt_response {
Ok(melt_response) => melt_response,

View File

@@ -6,6 +6,7 @@ use std::str::FromStr;
use cdk_common::amount::amount_for_offer;
use cdk_common::wallet::MeltQuote;
use cdk_common::PaymentMethod;
use lightning::offers::offer::Offer;
use tracing::instrument;
@@ -57,6 +58,7 @@ impl Wallet {
state: quote_res.state,
expiry: quote_res.expiry,
payment_preimage: quote_res.payment_preimage,
payment_method: PaymentMethod::Bolt12,
};
self.localstore.add_melt_quote(quote.clone()).await?;