@@ -18,8 +18,9 @@ use portal::nostr_relay_pool::RelayOptions;
1818use portal:: protocol:: calendar:: Calendar ;
1919use portal:: protocol:: jwt:: CustomClaims ;
2020use portal:: protocol:: model:: payment:: {
21- CashuDirectContent , CashuRequestContent , Currency , ExchangeRate , InvoiceRequestContent ,
22- PaymentStatus , RecurringPaymentRequestContent , SinglePaymentRequestContent ,
21+ Amount , CashuDirectContent , CashuRequestContent , Currency , ExchangeRate ,
22+ InvoiceRequestContent , PaymentStatus , RecurringPaymentRequestContent ,
23+ SinglePaymentRequestContent ,
2324} ;
2425use portal:: protocol:: model:: Timestamp ;
2526use portal:: utils:: fetch_nip05_profile as portal_fetch_nip05;
@@ -72,22 +73,22 @@ fn parse_subkeys(subkeys: &[String]) -> Result<Vec<PublicKey>, String> {
7273/// Resolve amount and exchange rate: for Millisats returns (amount, None);
7374/// for Fiat fetches market data and returns (amount_msat, Some(ExchangeRate)).
7475async fn resolve_amount_and_exchange_rate (
75- amount : u64 ,
76+ amount : Amount ,
7677 currency : & Currency ,
7778 market_api : Arc < portal_rates:: MarketAPI > ,
78- ) -> Result < ( u64 , Option < ExchangeRate > ) , portal_rates:: RatesError > {
79+ ) -> Result < ( Amount , Option < ExchangeRate > ) , portal_rates:: RatesError > {
7980 match currency {
8081 Currency :: Millisats => Ok ( ( amount, None ) ) ,
8182 Currency :: Fiat ( currency_code) => {
8283 let market_data = market_api. fetch_market_data ( currency_code) . await ?;
83- let fiat_amount = amount as f64 / 100.0 ;
84+ let fiat_amount = amount. as_fiat_major ( ) ;
8485 let msat = ( market_data. calculate_millisats ( fiat_amount) as i64 ) . max ( 0 ) as u64 ;
8586 let exchange_rate = ExchangeRate {
8687 rate : market_data. rate ,
8788 source : market_data. source ,
8889 time : Timestamp :: now ( ) ,
8990 } ;
90- Ok ( ( msat, Some ( exchange_rate) ) )
91+ Ok ( ( Amount :: new ( msat) , Some ( exchange_rate) ) )
9192 }
9293 }
9394}
@@ -316,7 +317,7 @@ pub async fn request_recurring_payment(
316317 let subkeys = parse_subkeys ( & req. subkeys ) . map_err ( |e| bad_request ( format ! ( "Invalid subkeys: {e}" ) ) ) ?;
317318
318319 let ( _, current_exchange_rate) = resolve_amount_and_exchange_rate (
319- req. payment_request . amount ,
320+ Amount :: new ( req. payment_request . amount ) ,
320321 & req. payment_request . currency ,
321322 state. market_api . clone ( ) ,
322323 )
@@ -325,7 +326,7 @@ pub async fn request_recurring_payment(
325326
326327 let payment_request = RecurringPaymentRequestContent {
327328 description : req. payment_request . description ,
328- amount : req. payment_request . amount ,
329+ amount : Amount :: new ( req. payment_request . amount ) ,
329330 currency : req. payment_request . currency ,
330331 auth_token : req. payment_request . auth_token ,
331332 recurrence : req. payment_request . recurrence ,
@@ -383,22 +384,22 @@ pub async fn request_single_payment(
383384
384385 let amount = req. payment_request . amount ;
385386 let ( msat_amount, current_exchange_rate) = resolve_amount_and_exchange_rate (
386- amount,
387+ Amount :: new ( amount) ,
387388 & req. payment_request . currency ,
388389 state. market_api . clone ( ) ,
389390 )
390391 . await
391392 . map_err ( |e| internal_error ( format ! ( "Failed to fetch market data: {e}" ) ) ) ?;
392393
393394 let invoice = wallet
394- . make_invoice ( msat_amount, Some ( req. payment_request . description . clone ( ) ) )
395+ . make_invoice ( msat_amount. as_millisats ( ) , Some ( req. payment_request . description . clone ( ) ) )
395396 . await
396397 . map_err ( |e| internal_error ( format ! ( "Failed to make invoice: {e}" ) ) ) ?;
397398
398399 let request_id = req. payment_request . request_id . clone ( ) . unwrap_or_else ( || Uuid :: new_v4 ( ) . to_string ( ) ) ;
399400 let expires_at = Timestamp :: now_plus_seconds ( 300 ) ;
400401 let payment_request = SinglePaymentRequestContent {
401- amount,
402+ amount : Amount :: new ( amount ) ,
402403 currency : req. payment_request . currency ,
403404 expires_at,
404405 invoice : invoice. clone ( ) ,
@@ -575,7 +576,7 @@ pub async fn request_invoice(
575576
576577 // Resolve amount/exchange rate synchronously — errors returned as 400
577578 let ( expected_amount_msat, current_exchange_rate) = resolve_amount_and_exchange_rate (
578- req. content . amount ,
579+ Amount :: new ( req. content . amount ) ,
579580 & req. content . currency ,
580581 state. market_api . clone ( ) ,
581582 )
@@ -584,7 +585,7 @@ pub async fn request_invoice(
584585
585586 let sdk_content = InvoiceRequestContent {
586587 request_id,
587- amount : req. content . amount ,
588+ amount : Amount :: new ( req. content . amount ) ,
588589 currency : req. content . currency . clone ( ) ,
589590 current_exchange_rate,
590591 expires_at : req. content . expires_at ,
@@ -630,15 +631,15 @@ pub async fn request_invoice(
630631 }
631632 } ;
632633
633- let amount_diff =
634- ( invoice_amount_msat as i128 - expected_amount_msat as i128 ) . abs ( ) ;
634+ let expected_msat = expected_amount_msat . as_millisats ( ) ;
635+ let amount_diff = ( invoice_amount_msat as i128 - expected_msat as i128 ) . abs ( ) ;
635636 if amount_diff > 1 {
636637 events
637638 . push (
638639 & sid,
639640 NotificationData :: Error {
640641 reason : format ! (
641- "Invoice amount mismatch: got {invoice_amount_msat} msat, expected {expected_amount_msat } msat (diff: {amount_diff} msat)"
642+ "Invoice amount mismatch: got {invoice_amount_msat} msat, expected {expected_msat } msat (diff: {amount_diff} msat)"
642643 ) ,
643644 } ,
644645 )
0 commit comments