Skip to content

Commit 5733618

Browse files
asmogothesimplekid
authored andcommitted
fix: add optional amount
1 parent ece6969 commit 5733618

16 files changed

Lines changed: 57 additions & 19 deletions

File tree

crates/cashu/src/nuts/nut04.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,11 @@ impl Settings {
381381
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
382382
pub struct MintQuoteCustomRequest {
383383
/// Amount to mint
384-
pub amount: Amount,
384+
///
385+
/// Optional common field. Method-specific NUTs make it required or ignore
386+
/// it as needed (e.g. NUT-23 requires `amount`).
387+
#[serde(default, skip_serializing_if = "Option::is_none")]
388+
pub amount: Option<Amount>,
385389
/// Currency unit
386390
pub unit: CurrencyUnit,
387391
/// Optional description

crates/cashu/src/nuts/nut05.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,12 @@ pub struct MeltQuoteCustomRequest {
493493
pub request: String,
494494
/// Currency unit
495495
pub unit: CurrencyUnit,
496+
/// Amount the wallet would like to pay
497+
///
498+
/// Optional common field. Method-specific NUTs make it required or ignore
499+
/// it as needed (e.g. NUT-30 requires `amount` for onchain melts).
500+
#[serde(default, skip_serializing_if = "Option::is_none")]
501+
pub amount: Option<Amount>,
496502
/// Extra payment-method-specific fields
497503
///
498504
/// These fields are flattened into the JSON representation, allowing

crates/cdk-axum/src/custom_handlers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,7 @@ mod tests {
873873
.get_mint_quote(cdk::mint::MintQuoteRequest::Custom {
874874
method: PaymentMethod::Custom(method.to_string()),
875875
request: MintQuoteCustomRequest {
876-
amount: Amount::from(amount),
876+
amount: Some(Amount::from(amount)),
877877
unit: CurrencyUnit::Sat,
878878
description: None,
879879
pubkey: None,

crates/cdk-common/src/melt.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ mod tests {
497497
method: "cashapp".to_string(),
498498
unit: CurrencyUnit::Sat,
499499
request: "$tag".to_string(),
500+
amount: None,
500501
extra: serde_json::Value::Null,
501502
};
502503
let req: MeltQuoteRequest = custom_req.into();

crates/cdk-common/src/mint_quote.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl MintQuoteRequest {
6363
Self::Bolt11(request) => Some(request.amount),
6464
Self::Bolt12(request) => request.amount,
6565
Self::Onchain(_) => None,
66-
Self::Custom { request, .. } => Some(request.amount),
66+
Self::Custom { request, .. } => request.amount,
6767
}
6868
}
6969

crates/cdk-common/src/payment.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ pub struct CustomIncomingPaymentOptions {
222222
pub method: String,
223223
/// Optional description for the payment request
224224
pub description: Option<String>,
225-
/// Amount for the payment request
226-
pub amount: Amount<CurrencyUnit>,
225+
/// Optional amount for the payment request
226+
pub amount: Option<Amount<CurrencyUnit>>,
227227
/// Optional expiry time as Unix timestamp in seconds
228228
pub unix_expiry: Option<u64>,
229229
/// Extra payment-method-specific fields as JSON string
@@ -293,6 +293,8 @@ pub struct CustomOutgoingPaymentOptions {
293293
pub method: String,
294294
/// Payment request string (method-specific format)
295295
pub request: String,
296+
/// Optional amount the wallet would like to pay (from the melt quote request)
297+
pub amount: Option<Amount<CurrencyUnit>>,
296298
/// Maximum fee amount allowed for the payment
297299
pub max_fee_amount: Option<Amount<CurrencyUnit>>,
298300
/// Optional timeout in seconds
@@ -391,6 +393,8 @@ impl OutgoingPaymentOptions {
391393
Box::new(CustomOutgoingPaymentOptions {
392394
method: method.to_string(),
393395
request: request.to_string(),
396+
// Payment is already quoted; correlation is via quote_id.
397+
amount: None,
394398
max_fee_amount: Some(fee_reserve),
395399
timeout_secs: None,
396400
melt_options: melt_quote.options,

crates/cdk-fake-wallet/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,9 @@ impl MintPayment for FakeWallet {
883883
(
884884
PaymentIdentifier::CustomId(custom_id),
885885
request,
886-
custom_options.amount,
886+
custom_options
887+
.amount
888+
.unwrap_or_else(|| Amount::new(0, self.unit.clone())),
887889
custom_options.unix_expiry,
888890
)
889891
}
@@ -1120,7 +1122,7 @@ mod tests {
11201122
CustomIncomingPaymentOptions {
11211123
method: "venmo".to_string(),
11221124
description: None,
1123-
amount: Amount::new(10, CurrencyUnit::Sat),
1125+
amount: Some(Amount::new(10, CurrencyUnit::Sat)),
11241126
unix_expiry: None,
11251127
extra_json: None,
11261128
},

crates/cdk-integration-tests/tests/integration_tests_pure.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,6 +1758,7 @@ async fn test_direct_connector_custom_melt_enum_roundtrip() {
17581758
method: "paypal".to_string(),
17591759
request: "invoice-123".to_string(),
17601760
unit: CurrencyUnit::Sat,
1761+
amount: None,
17611762
extra: serde_json::Value::Null,
17621763
});
17631764

@@ -2481,6 +2482,7 @@ async fn test_custom_melt_quote_status_preserves_extra_json() {
24812482
method: "test-custom".to_string(),
24822483
request: "custom-request".to_string(),
24832484
unit: CurrencyUnit::Sat,
2485+
amount: None,
24842486
extra: serde_json::json!({ "request_metadata": true }),
24852487
},
24862488
))
@@ -2542,6 +2544,7 @@ async fn test_custom_melt_quote_id_propagates_to_payment_processor() {
25422544
method: "test-custom".to_string(),
25432545
request: "custom-request".to_string(),
25442546
unit: CurrencyUnit::Sat,
2547+
amount: None,
25452548
extra: serde_json::json!({ "request_metadata": true }),
25462549
},
25472550
))

crates/cdk-payment-processor/src/proto/client.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl MintPayment for PaymentProcessorClient {
183183
options: Some(super::incoming_payment_options::Options::Custom(
184184
super::CustomIncomingPaymentOptions {
185185
description: opts.description,
186-
amount: Some(opts.amount.into()),
186+
amount: opts.amount.map(Into::into),
187187
unix_expiry: opts.unix_expiry,
188188
extra_json: opts.extra_json.clone(),
189189
},
@@ -332,6 +332,7 @@ impl MintPayment for PaymentProcessorClient {
332332
options: Some(super::outgoing_payment_variant::Options::Custom(
333333
super::CustomOutgoingPaymentOptions {
334334
offer: opts.request.to_string(),
335+
amount: opts.amount.map(Into::into),
335336
max_fee_amount: opts.max_fee_amount.into_proto(),
336337
timeout_secs: opts.timeout_secs,
337338
melt_options: opts.melt_options.map(Into::into),

crates/cdk-payment-processor/src/proto/payment_processor.proto

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ syntax = "proto3";
22

33
package cdk_payment_processor;
44

5-
service CdkPaymentProcessor {
5+
service CdkPaymentProcessor {
66
rpc GetSettings(EmptyRequest) returns (SettingsResponse) {}
77
rpc CreatePayment(CreatePaymentRequest) returns (CreatePaymentResponse) {}
88
rpc GetPaymentQuote(PaymentQuoteRequest) returns (PaymentQuoteResponse) {}
@@ -232,6 +232,8 @@ message CustomOutgoingPaymentOptions {
232232
string offer = 1;
233233
optional AmountMessage max_fee_amount = 2;
234234
optional uint64 timeout_secs = 3;
235+
// Optional amount the wallet would like to pay (from the melt quote request)
236+
optional AmountMessage amount = 4;
235237
optional MeltOptions melt_options = 5;
236238
// Extra payment-method-specific fields as JSON string
237239
// These fields are flattened into the JSON representation on the client side

0 commit comments

Comments
 (0)