Skip to content

Commit c3ecaf8

Browse files
committed
portal-app-demo,portal-cli: fix new Amount type
1 parent 2e88687 commit c3ecaf8

5 files changed

Lines changed: 19 additions & 17 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ react-native/portal-app-lib-*.tgz
1111

1212
result
1313
examples/.env
14+
15+
.cursor/

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/portal-app-demo/src/main.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use portal::protocol::model::auth::AuthResponseStatus;
3434
use portal::protocol::model::bindings::PublicKey;
3535
use portal::utils::fetch_nip05_profile;
3636
use portal::protocol::model::payment::{
37-
Currency, ExchangeRate, InvoiceRequestContentWithKey, PaymentResponseContent, PaymentStatus,
37+
Amount, Currency, ExchangeRate, InvoiceRequestContentWithKey, PaymentResponseContent, PaymentStatus
3838
};
3939
use portal_wallet::{NwcWallet, PortalWallet};
4040
use serde::{Deserialize, Serialize};
@@ -143,7 +143,7 @@ impl From<Profile> for ProfileDto {
143143
struct PaymentRequestDto {
144144
request_id: String,
145145
event_id: String,
146-
amount: u64,
146+
amount: Amount,
147147
amount_formatted: String,
148148
is_fiat: bool,
149149
currency: String,
@@ -167,7 +167,7 @@ struct ExchangeRateDto {
167167
#[derive(Serialize, Clone)]
168168
struct InvoiceRequestDto {
169169
request_id: String,
170-
amount: u64,
170+
amount: Amount,
171171
amount_formatted: String,
172172
is_fiat: bool,
173173
currency: String,
@@ -258,11 +258,11 @@ fn single_request_to_dto(r: &SinglePaymentRequest) -> PaymentRequestDto {
258258
let amount = r.content.amount;
259259
let (currency, amount_formatted, is_fiat, exchange_rate, equivalent_sats) = match &r.content.currency {
260260
Currency::Millisats => {
261-
let sats = amount / 1000;
262-
(String::from("msat"), format!("{} msat ({} sats)", amount, sats), false, None, None)
261+
let sats = amount.as_millisats() / 1000;
262+
(String::from("msat"), format!("{} msat ({} sats)", amount.as_millisats(), sats), false, None, None)
263263
}
264264
Currency::Fiat(code) => {
265-
let major = amount as f64 / 100.0;
265+
let major = amount.as_fiat_major();
266266
let formatted = format!("{:.2} {}", major, code);
267267
let (er, eq) = match &r.content.current_exchange_rate {
268268
Some(ExchangeRate { rate, source, .. }) => {
@@ -296,11 +296,11 @@ fn invoice_request_to_dto(r: &InvoiceRequestContentWithKey) -> InvoiceRequestDto
296296
let amount = c.amount;
297297
let (currency, amount_formatted, is_fiat, exchange_rate, equivalent_sats) = match &c.currency {
298298
Currency::Millisats => {
299-
let sats = amount / 1000;
300-
(String::from("msat"), format!("{} msat ({} sats)", amount, sats), false, None, None)
299+
let sats = amount.as_millisats() / 1000;
300+
(String::from("msat"), format!("{} msat ({} sats)", amount.as_millisats(), sats), false, None, None)
301301
}
302302
Currency::Fiat(code) => {
303-
let major = amount as f64 / 100.0;
303+
let major = amount.as_fiat_major();
304304
let formatted = format!("{:.2} {}", major, code);
305305
let (er, eq) = match &c.current_exchange_rate {
306306
Some(ExchangeRate { rate, source, .. }) => {
@@ -329,9 +329,9 @@ fn invoice_request_to_dto(r: &InvoiceRequestContentWithKey) -> InvoiceRequestDto
329329
fn invoice_request_amount_msat(r: &InvoiceRequestContentWithKey) -> Option<u64> {
330330
let c = &r.inner;
331331
match &c.currency {
332-
Currency::Millisats => Some(c.amount),
332+
Currency::Millisats => Some(c.amount.as_millisats()),
333333
Currency::Fiat(_) => {
334-
let major = c.amount as f64 / 100.0;
334+
let major = c.amount.as_fiat_major();
335335
c.current_exchange_rate.as_ref().map(|er| ((major / er.rate) * 100_000_000_000.0) as u64)
336336
}
337337
}

crates/portal-cli/src/bin/invoices.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::sync::Arc;
22

33
use app::nwc::MakeInvoiceResponse;
44
use portal_cli::{CliError, create_app_instance};
5-
use portal::protocol::model::{Timestamp, payment::InvoiceRequestContent};
5+
use portal::protocol::model::{Timestamp, payment::{Amount, InvoiceRequestContent}};
66

77
#[tokio::main]
88
async fn main() -> Result<(), CliError> {
@@ -62,7 +62,7 @@ async fn main() -> Result<(), CliError> {
6262
receiver_key.public_key(),
6363
InvoiceRequestContent {
6464
request_id: String::from("my_id"),
65-
amount: 5000,
65+
amount: Amount::new(5000),
6666
currency: portal::protocol::model::payment::Currency::Millisats,
6767
current_exchange_rate: None,
6868
expires_at: Timestamp::now_plus_seconds(120),

crates/portal-cli/src/bin/single_payment_request.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use portal_cli::{CliError, create_app_instance};
2-
use portal::protocol::model::payment::{Currency, SinglePaymentRequestContent};
2+
use portal::protocol::model::payment::{Amount, Currency, SinglePaymentRequestContent};
33

44
#[tokio::main]
55
async fn main() -> Result<(), CliError> {
@@ -19,12 +19,12 @@ async fn main() -> Result<(), CliError> {
1919

2020
let npub = "ae35195222dbcc280d85a966204bc0e1ce743e99cbcb76216c922edf90cdd3e5";
2121
let invoice = "lnbc100n1p5nrm73pp5qjsms2tw3mm0jjn8jqex2l9mshnl978d779nstg08fcn2yq3256ssp5rdkyjl7lyewqpfhzyl0w5z6ferjuwxk67p587qv49kmq9zd9vskqxq9z0rgqnp4qvyndeaqzman7h898jxm98dzkm0mlrsx36s93smrur7h0azyyuxc5rzjq25carzepgd4vqsyn44jrk85ezrpju92xyrk9apw4cdjh6yrwt5jgqqqqrt49lmtcqqqqqqqqqqq86qq9qrzjqvx2wstnex2mhar83v3c8gwrrnpek8kfyjznh2ulxt35q03tzrzf3apyqr6zgqqqq8hxk2qqae4jsqyugqcqzpgdqv2pshjmt9de6q9qyyssqcsvavq75ksw6u03nunfyjyeygxk27mph4406vsf9jmdqgcaz59k5emrnh5dctvnzshd6d5x6xrqckrq8rn9a2rc83ywe0sut5829ecqpxrtegn";
22-
let amount = 10;
22+
let amount = Amount::new(10 * 1000);
2323

2424
app.single_payment_request(
2525
npub,
2626
SinglePaymentRequestContent {
27-
amount: amount * 1000,
27+
amount,
2828
invoice: invoice.into(),
2929
auth_token: None,
3030
currency: Currency::Millisats,

0 commit comments

Comments
 (0)