breez_sdk_spark/common/
fiat.rs

1use std::sync::Arc;
2
3use serde::{Deserialize, Serialize};
4
5use crate::ServiceConnectivityError;
6
7/// Trait covering fiat-related functionality
8#[cfg_attr(feature = "uniffi", uniffi::export(with_foreign))]
9#[macros::async_trait]
10pub trait FiatService: Send + Sync {
11    /// List all supported fiat currencies for which there is a known exchange rate.
12    async fn fetch_fiat_currencies(&self) -> Result<Vec<FiatCurrency>, ServiceConnectivityError>;
13
14    /// Get the live rates from the server.
15    async fn fetch_fiat_rates(&self) -> Result<Vec<Rate>, ServiceConnectivityError>;
16}
17
18pub(crate) struct FiatServiceWrapper {
19    inner: Arc<dyn FiatService>,
20}
21
22impl FiatServiceWrapper {
23    pub fn new(inner: Arc<dyn FiatService>) -> Self {
24        FiatServiceWrapper { inner }
25    }
26}
27
28#[macros::async_trait]
29impl breez_sdk_common::fiat::FiatService for FiatServiceWrapper {
30    async fn fetch_fiat_currencies(
31        &self,
32    ) -> Result<
33        Vec<breez_sdk_common::fiat::FiatCurrency>,
34        breez_sdk_common::error::ServiceConnectivityError,
35    > {
36        Ok(self
37            .inner
38            .fetch_fiat_currencies()
39            .await?
40            .into_iter()
41            .map(From::from)
42            .collect())
43    }
44
45    async fn fetch_fiat_rates(
46        &self,
47    ) -> Result<Vec<breez_sdk_common::fiat::Rate>, breez_sdk_common::error::ServiceConnectivityError>
48    {
49        Ok(self
50            .inner
51            .fetch_fiat_rates()
52            .await?
53            .into_iter()
54            .map(From::from)
55            .collect())
56    }
57}
58
59/// Wrapper around the [`CurrencyInfo`] of a fiat currency
60#[derive(Clone, Debug, Serialize, Deserialize)]
61#[macros::derive_from(breez_sdk_common::fiat::FiatCurrency)]
62#[macros::derive_into(breez_sdk_common::fiat::FiatCurrency)]
63#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
64pub struct FiatCurrency {
65    pub id: String,
66    pub info: CurrencyInfo,
67}
68
69/// Details about a supported currency in the fiat rate feed
70#[derive(Clone, Debug, Deserialize, Serialize)]
71#[macros::derive_from(breez_sdk_common::fiat::CurrencyInfo)]
72#[macros::derive_into(breez_sdk_common::fiat::CurrencyInfo)]
73#[serde(rename_all = "camelCase")]
74#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
75pub struct CurrencyInfo {
76    pub name: String,
77    pub fraction_size: u32,
78    pub spacing: Option<u32>,
79    pub symbol: Option<Symbol>,
80    pub uniq_symbol: Option<Symbol>,
81    #[serde(default)]
82    pub localized_name: Vec<LocalizedName>,
83    #[serde(default)]
84    pub locale_overrides: Vec<LocaleOverrides>,
85}
86
87/// Localized name of a currency
88#[derive(Clone, Debug, Deserialize, Serialize)]
89#[macros::derive_from(breez_sdk_common::fiat::LocalizedName)]
90#[macros::derive_into(breez_sdk_common::fiat::LocalizedName)]
91#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
92pub struct LocalizedName {
93    pub locale: String,
94    pub name: String,
95}
96
97/// Locale-specific settings for the representation of a currency
98#[derive(Clone, Debug, Deserialize, Serialize)]
99#[macros::derive_from(breez_sdk_common::fiat::LocaleOverrides)]
100#[macros::derive_into(breez_sdk_common::fiat::LocaleOverrides)]
101#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
102pub struct LocaleOverrides {
103    pub locale: String,
104    pub spacing: Option<u32>,
105    pub symbol: Symbol,
106}
107
108/// Denominator in an exchange rate
109#[derive(Clone, Debug, Deserialize, Serialize)]
110#[macros::derive_from(breez_sdk_common::fiat::Rate)]
111#[macros::derive_into(breez_sdk_common::fiat::Rate)]
112#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
113pub struct Rate {
114    pub coin: String,
115    pub value: f64,
116}
117
118/// Settings for the symbol representation of a currency
119#[derive(Clone, Debug, Deserialize, Serialize)]
120#[macros::derive_from(breez_sdk_common::fiat::Symbol)]
121#[macros::derive_into(breez_sdk_common::fiat::Symbol)]
122#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
123pub struct Symbol {
124    pub grapheme: Option<String>,
125    pub template: Option<String>,
126    pub rtl: Option<bool>,
127    pub position: Option<u32>,
128}