From abbf59207a3c3de6f75ced4ba17c0cf93fb97fda Mon Sep 17 00:00:00 2001 From: mik-tf Date: Mon, 8 Sep 2025 15:15:50 -0400 Subject: [PATCH] feat: implement MC as base currency replacing USD across marketplace and currency services --- src/models/marketplace.rs | 4 +- src/services/currency.rs | 110 +++++++++++++++---------------- src/services/user_persistence.rs | 2 +- 3 files changed, 58 insertions(+), 58 deletions(-) diff --git a/src/models/marketplace.rs b/src/models/marketplace.rs index 5a8e5ec..f6594be 100644 --- a/src/models/marketplace.rs +++ b/src/models/marketplace.rs @@ -40,8 +40,8 @@ impl Default for MarketplaceConfig { marketplace: MarketplaceInfo { name: "Mycelium Marketplace".to_string(), marketplace_type: "infrastructure".to_string(), - base_currency: "USD".to_string(), - default_display_currency: "USD".to_string(), + base_currency: "MC".to_string(), + default_display_currency: "MC".to_string(), supported_languages: vec!["en".to_string()], features: MarketplaceFeatures { reviews_enabled: false, diff --git a/src/services/currency.rs b/src/services/currency.rs index 0229ebd..73922cd 100644 --- a/src/services/currency.rs +++ b/src/services/currency.rs @@ -18,11 +18,11 @@ impl CurrencyService { let mut service = Self { exchange_rates_cache: HashMap::default(), last_update: Utc::now(), - default_display_currency: "USD".to_string(), + default_display_currency: "MC".to_string(), }; - - // USD Credits is now the base currency - no conversion needed - service.exchange_rates_cache.insert("USD".to_string(), dec!(1.0)); + + // MC is now the base currency - no conversion needed + service.exchange_rates_cache.insert("MC".to_string(), dec!(1.0)); service.update_exchange_rates(); service } @@ -36,13 +36,13 @@ impl CurrencyService { let mut service = Self { exchange_rates_cache: fallback_rates, last_update: Utc::now(), - default_display_currency: "USD".to_string(), + default_display_currency: "MC".to_string(), }; - + if auto_update { service.update_exchange_rates(); } - + service } @@ -58,11 +58,11 @@ impl CurrencyService { last_update: Utc::now(), default_display_currency: display_currency, }; - + if auto_update { service.update_exchange_rates(); } - + service } @@ -72,15 +72,27 @@ impl CurrencyService { /// Get all supported currencies pub fn get_supported_currencies(&self) -> Vec { - // Return standard supported currencies without mock data + // Return standard supported currencies with MC as base currency vec![ + Currency { + code: "MC".to_string(), + name: "Mycelium Credit".to_string(), + symbol: "MC".to_string(), + currency_type: crate::models::currency::CurrencyType::Custom("credits".to_string()), + exchange_rate_to_base: dec!(1.0), // MC is the base currency + is_base_currency: true, + decimal_places: 2, + is_active: true, + provider_config: None, + last_updated: chrono::Utc::now(), + }, Currency { code: "USD".to_string(), name: "US Dollar".to_string(), symbol: "$".to_string(), currency_type: crate::models::currency::CurrencyType::Fiat, - exchange_rate_to_base: dec!(1.0), - is_base_currency: true, + exchange_rate_to_base: dec!(1.0), // 1 USD = 1 MC (parity for UX preservation) + is_base_currency: false, decimal_places: 2, is_active: true, provider_config: None, @@ -91,19 +103,7 @@ impl CurrencyService { name: "Euro".to_string(), symbol: "€".to_string(), currency_type: crate::models::currency::CurrencyType::Fiat, - exchange_rate_to_base: dec!(0.85), - is_base_currency: false, - decimal_places: 2, - is_active: true, - provider_config: None, - last_updated: chrono::Utc::now(), - }, - Currency { - code: "MC".to_string(), - name: "Mycelium Credit".to_string(), - symbol: "MC".to_string(), - currency_type: crate::models::currency::CurrencyType::Custom("credits".to_string()), - exchange_rate_to_base: dec!(1.0), // 1 MC = 1 USD + exchange_rate_to_base: dec!(0.85), // 1 EUR = 0.85 MC is_base_currency: false, decimal_places: 2, is_active: true, @@ -115,7 +115,7 @@ impl CurrencyService { name: "Canadian Dollar".to_string(), symbol: "C$".to_string(), currency_type: crate::models::currency::CurrencyType::Fiat, - exchange_rate_to_base: dec!(1.35), + exchange_rate_to_base: dec!(1.35), // 1 CAD = 1.35 MC is_base_currency: false, decimal_places: 2, is_active: true, @@ -127,7 +127,7 @@ impl CurrencyService { name: "Mycelium Token".to_string(), symbol: "TFT".to_string(), currency_type: crate::models::currency::CurrencyType::Token, - exchange_rate_to_base: dec!(0.05), + exchange_rate_to_base: dec!(0.05), // 1 TFT = 0.05 MC is_base_currency: false, decimal_places: 3, is_active: true, @@ -139,7 +139,7 @@ impl CurrencyService { name: "UAE Dirham".to_string(), symbol: "د.إ".to_string(), currency_type: crate::models::currency::CurrencyType::Fiat, - exchange_rate_to_base: dec!(3.67), + exchange_rate_to_base: dec!(3.67), // 1 AED = 3.67 MC is_base_currency: false, decimal_places: 2, is_active: true, @@ -159,10 +159,10 @@ impl CurrencyService { /// Get base currency pub fn get_base_currency(&self) -> Currency { Currency { - code: "USD".to_string(), - name: "US Dollar".to_string(), - symbol: "$".to_string(), - currency_type: crate::models::currency::CurrencyType::Fiat, + code: "MC".to_string(), + name: "Mycelium Credit".to_string(), + symbol: "MC".to_string(), + currency_type: crate::models::currency::CurrencyType::Custom("credits".to_string()), exchange_rate_to_base: dec!(1.0), is_base_currency: true, decimal_places: 2, @@ -183,7 +183,7 @@ impl CurrencyService { return Ok(amount); } - let base_currency_code = "USD"; // Use USD as base currency + let base_currency_code = "MC"; // Use MC as base currency // Convert to base currency first if needed let base_amount = if from_currency == base_currency_code { @@ -204,8 +204,8 @@ impl CurrencyService { /// Get exchange rate from base currency to target currency pub fn get_exchange_rate_to_base(&self, currency_code: &str) -> Result { - let base_currency_code = "USD"; // Use USD as base currency - + let base_currency_code = "MC"; // Use MC as base currency + if currency_code == base_currency_code { return Ok(dec!(1.0)); } @@ -300,7 +300,7 @@ impl CurrencyService { if !self.default_display_currency.is_empty() { self.default_display_currency.clone() } else { - "USD".to_string() // Default to USD when no preference is set + "MC".to_string() // Default to MC when no preference is set } } @@ -335,23 +335,23 @@ impl CurrencyService { /// Get all exchange rates relative to base currency pub fn get_all_exchange_rates(&self) -> HashMap { let mut rates = HashMap::default(); - let base_currency_code = "USD".to_string(); // Use USD as base currency - + let base_currency_code = "MC".to_string(); // Use MC as base currency + // Base currency always has rate 1.0 rates.insert(base_currency_code.clone(), dec!(1.0)); - + // Add cached rates for (currency, rate) in &self.exchange_rates_cache { rates.insert(currency.clone(), *rate); } - + // Add any missing currencies from static config for currency in self.get_supported_currencies() { if !rates.contains_key(¤cy.code) && !currency.is_base_currency { rates.insert(currency.code.clone(), currency.exchange_rate_to_base); } } - + rates } @@ -365,19 +365,19 @@ impl CurrencyService { /// Get currency statistics for admin/debug purposes pub fn get_currency_stats(&self) -> HashMap { let mut stats = HashMap::default(); - - stats.insert("total_currencies".to_string(), + + stats.insert("total_currencies".to_string(), serde_json::Value::Number(serde_json::Number::from(self.get_supported_currencies().len()))); - - stats.insert("base_currency".to_string(), - serde_json::Value::String("USD".to_string())); - - stats.insert("last_update".to_string(), + + stats.insert("base_currency".to_string(), + serde_json::Value::String("MC".to_string())); + + stats.insert("last_update".to_string(), serde_json::Value::String(self.last_update.to_rfc3339())); - - stats.insert("cached_rates_count".to_string(), + + stats.insert("cached_rates_count".to_string(), serde_json::Value::Number(serde_json::Number::from(self.exchange_rates_cache.len()))); - + let rate_values: Vec = self.exchange_rates_cache.iter() .map(|(currency, rate)| { serde_json::json!({ @@ -386,9 +386,9 @@ impl CurrencyService { }) }) .collect(); - + stats.insert("current_rates".to_string(), serde_json::Value::Array(rate_values)); - + stats } @@ -436,7 +436,7 @@ impl CurrencyService { session: &Session, ) -> Result<(Decimal, String), String> { let display_currency = self.get_user_preferred_currency(session); - + if display_currency == "USD" { Ok((usd_amount, "USD".to_string())) } else { @@ -452,7 +452,7 @@ impl CurrencyService { session: &Session, ) -> Result { let display_currency = self.get_user_preferred_currency(session); - + if display_currency == "USD" { Ok(amount) } else { diff --git a/src/services/user_persistence.rs b/src/services/user_persistence.rs index 775efee..9d5026a 100644 --- a/src/services/user_persistence.rs +++ b/src/services/user_persistence.rs @@ -155,7 +155,7 @@ impl Default for UserPersistentData { node_groups: Vec::new(), slice_rentals: Vec::new(), slice_assignments: Vec::new(), - display_currency: Some("USD".to_string()), // Default to USD for new users + display_currency: Some("MC".to_string()), // Default to MC for new users quick_topup_amounts: Some(vec![dec!(10), dec!(25), dec!(50), dec!(100)]), // USD amounts auto_topup_settings: None, // User can configure later products: Vec::new(), // Initialize empty products list