Overview
Medusa Wallet provides comprehensive fiat conversion features, allowing users to view balances and transaction amounts in their preferred currency. The system supports both real-time conversion for current balances and historical conversion for past transactions.Architecture
The fiat conversion system uses two separate APIs:- LNbits API: Real-time conversion for current balances
- Medusa API: Historical price data for transaction history
Real-Time Conversion
Converting Satoshis to Fiat
Converting Satoshis to Fiat
The LNbits backend provides real-time conversion rates:Supported Currencies:
The wallet supports major fiat currencies including USD, EUR, GBP, JPY, and more through the
SupportedFiatCurrencies type.See ~/workspace/source/api/lnbits.ts:213-228Fetching Exchange Rates
Fetching Exchange Rates
For displaying current exchange rates without converting a specific amount:This returns the current Bitcoin price in the specified fiat currency.See
~/workspace/source/api/lnbits.ts:230-247Historical Price Data
Medusa Price API
Medusa Price API
The Medusa API provides historical Bitcoin prices for all supported currencies:Response Format:See
~/workspace/source/api/medusa.ts:1-29Converting Historical Amounts
Converting Historical Amounts
For transaction history, convert specific amounts at historical timestamps:Conversion Formula:See
~/workspace/source/api/medusa.ts:31-54Transaction History with Fiat Data
Efficient Historical Price Fetching
Efficient Historical Price Fetching
When fetching payment history, the wallet optimizes API calls by grouping transactions by timestamp:Optimization Strategy:
- Group transactions by timestamp
- Fetch one price snapshot per unique timestamp
- Cache snapshots for subsequent fetches
- Reuse cached data across pagination
~/workspace/source/api/lnbits.ts:318-398Historical Price Mapping Utility
Historical Price Mapping Utility
The Example Output:This groups transactions that occurred at the same time, minimizing API calls.
getHistoricalPricesMap utility groups transactions efficiently:Fiat Snapshot Structure
Transaction Fiat Data
Transaction Fiat Data
Each transaction includes a complete fiat snapshot:Example Transaction:This allows the UI to display any currency without additional API calls.
Caching Strategy
Snapshot Cache
Snapshot Cache
The wallet maintains a cache of price snapshots to reduce API calls:Cache Benefits:
- Reduce redundant API calls for same timestamp
- Persist across pagination
- Instant display for repeated views
- Reduced bandwidth usage
Precision and Rounding
Decimal Handling
Decimal Handling
All fiat amounts are rounded to 2 decimal places for display:Conversion Chain:
- Convert satoshis to Bitcoin:
sats / 100,000,000 - Multiply by BTC price:
btc * price - Round to 2 decimals:
toFixed(2) - Convert back to number:
Number(...)
Error Handling
Graceful Degradation
Graceful Degradation
The wallet handles conversion errors gracefully:Fallback Behavior:
- Always display satoshi amounts
- Show fiat as optional enhancement
- Don’t block transactions if conversion fails
- Cache last known rates for offline use
Currency Configuration
Supported Currencies
Supported Currencies
The wallet configuration defines supported fiat currencies:Users can select their preferred currency in settings, which is used throughout the app for displaying fiat amounts.
Performance Considerations
- Batch API Calls: Group transactions by timestamp
- Cache Aggressively: Store price snapshots
- Lazy Loading: Fetch prices on-demand for visible transactions
- Pagination: Load transaction history in chunks
- Parallel Requests: Fetch multiple timestamps concurrently when needed
- Offline Support: Cache recent rates for offline viewing