Skip to content

Commit

Permalink
Merge pull request #538 from getlipa/feature/expose-amount-user-currency
Browse files Browse the repository at this point in the history
Expose user currency amount in `OfferKind::Pocket`
  • Loading branch information
danielgranhao authored Jul 31, 2023
2 parents 44127ab + f4554db commit 08c17dc
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 24 deletions.
41 changes: 23 additions & 18 deletions examples/3l-node/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::hinter::{CommandHint, CommandHinter};

use uniffi_lipalightninglib::{Amount, MaxRoutingFeeMode, OfferKind, TopupCurrency, TzConfig};
use uniffi_lipalightninglib::{
Amount, FiatValue, MaxRoutingFeeMode, OfferKind, TopupCurrency, TzConfig,
};

use bitcoin::secp256k1::PublicKey;
use chrono::offset::FixedOffset;
Expand Down Expand Up @@ -547,21 +549,22 @@ fn list_offers(node: &LightningNode) -> Result<(), String> {
println!(" LNURL-w: {:?}", offer.lnurlw);
match offer.offer_kind {
OfferKind::Pocket {
topup_value,
exchange_fee,
exchange_fee_rate_permyriad,
} => {
println!(
" Value exchanged: {}",
fiat_value_to_string(topup_value)
);
println!(
" Exchange fee rate: {}%",
exchange_fee_rate_permyriad as f64 / 100_f64
);
let converted_at: DateTime<Utc> = exchange_fee.converted_at.into();
let fiat = format!(
"{:.2} {} as of {}",
exchange_fee.minor_units as f64 / 100f64,
exchange_fee.currency_code,
converted_at.format("%d/%m/%Y %T UTC"),
println!(
" Exchange fee value: {}",
fiat_value_to_string(exchange_fee)
);
println!(" Exchange fee value: {}", fiat);
}
}
println!();
Expand Down Expand Up @@ -619,18 +622,20 @@ fn list_payments(node: &LightningNode) -> Result<(), String> {
Ok(())
}

fn fiat_value_to_string(value: FiatValue) -> String {
let converted_at: DateTime<Utc> = value.converted_at.into();
format!(
"{:.2} {} as of {}",
value.minor_units as f64 / 100f64,
value.currency_code,
converted_at.format("%d/%m/%Y %T UTC"),
)
}

fn amount_to_string(amount: Amount) -> String {
let fiat = match amount.fiat {
Some(fiat) => {
let converted_at: DateTime<Utc> = fiat.converted_at.into();
format!(
"{:.2} {} as of {}",
fiat.minor_units as f64 / 100f64,
fiat.currency_code,
converted_at.format("%d/%m/%Y %T UTC"),
)
}
None => "exchange rate uknown".to_string(),
Some(fiat) => fiat_value_to_string(fiat),
None => "exchange rate unknown".to_string(),
};
format!("{} SAT ({fiat})", amount.sats)
}
20 changes: 15 additions & 5 deletions src/backend_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ fn topup_to_offer_info(topup: &ListAvailableTopupsTopup) -> graphql::Result<Offe
)?,
);
let exchange_rate = (100_000_000_f64 / topup.exchange_rate).round() as u32;
let fiat_value_minor_units = (topup.exchange_fee_user_currency * 100_f64).round() as u64;
let topup_fiat_value_minor_units = (topup.amount_user_currency * 100_f64).round() as u64;
let exchange_fee_fiat_value_minor_units =
(topup.exchange_fee_user_currency * 100_f64).round() as u64;
let currency_code = topup.user_currency.to_string().to_uppercase();
let lnurlw = topup
.lnurl
.as_ref()
Expand All @@ -77,16 +80,22 @@ fn topup_to_offer_info(topup: &ListAvailableTopupsTopup) -> graphql::Result<Offe

Ok(OfferInfo {
offer_kind: OfferKind::Pocket {
topup_value: FiatValue {
minor_units: topup_fiat_value_minor_units,
currency_code: currency_code.clone(),
rate: exchange_rate,
converted_at: created_at,
},
exchange_fee: FiatValue {
minor_units: fiat_value_minor_units,
currency_code: topup.user_currency.to_string().to_uppercase(),
minor_units: exchange_fee_fiat_value_minor_units,
currency_code: currency_code.clone(),
rate: exchange_rate,
converted_at: created_at,
},
exchange_fee_rate_permyriad: (topup.exchange_fee_rate * 10000_f64).round() as u16,
},
amount: (topup.amount_sat * 1000).to_amount_down(&Some(ExchangeRate {
currency_code: topup.user_currency.to_string().to_uppercase(),
currency_code,
rate: exchange_rate,
updated_at: created_at,
})),
Expand Down Expand Up @@ -131,6 +140,7 @@ mod tests {
let offer_info = topup_to_offer_info(&topup).unwrap();

let OfferKind::Pocket {
topup_value,
exchange_fee,
exchange_fee_rate_permyriad,
} = offer_info.offer_kind;
Expand All @@ -142,7 +152,7 @@ mod tests {
assert_eq!(offer_info.lnurlw, String::from(LNURL));
assert_eq!(
offer_info.amount.fiat.unwrap().minor_units + exchange_fee.minor_units,
(amount_user_currency * 100.0).round() as u64
topup_value.minor_units
);

topup.lnurl = None;
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ pub struct OfferInfo {

pub enum OfferKind {
Pocket {
topup_value: FiatValue,
exchange_fee: FiatValue,
exchange_fee_rate_permyriad: u16,
},
Expand Down
3 changes: 2 additions & 1 deletion src/lipalightninglib.udl
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ dictionary FiatTopupInfo {
// Information on a funds offer that can be claimed using LNURL-w.
dictionary OfferInfo {
OfferKind offer_kind;
Amount amount;
Amount amount; // Amount available for withdrawal
string lnurlw;
timestamp created_at;
timestamp expires_at;
Expand All @@ -398,6 +398,7 @@ dictionary OfferInfo {
[Enum]
interface OfferKind {
Pocket(
FiatValue topup_value, // The original fiat amount sent to the exchange
FiatValue exchange_fee, // The fee paid to perform the exchange from fiat to sats
u16 exchange_fee_rate_permyriad // The rate of the fee expressed in permyriad (e.g. 1.5% would be 150)
);
Expand Down

0 comments on commit 08c17dc

Please sign in to comment.