|
| 1 | +# Fiat Exchange Rate |
| 2 | + |
| 3 | +This example demonstrates how to retrieve and convert fiat currency exchange rates using the Go Wallet Toolbox SDK. It supports converting one fiat currency to another based on the latest rates (e.g., EUR to USD, GBP to EUR). |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The process involves: |
| 8 | +1. Setting up the service with a mock configuration including fiat exchange rates. |
| 9 | +2. Calling the `FiatExchangeRate()` method to retrieve the exchange rate of one currency relative to another. |
| 10 | +3. Processing the returned `float64` result representing the conversion rate. |
| 11 | +4. Handling invalid currencies or missing data. |
| 12 | + |
| 13 | +This showcases a simplified currency conversion mechanism used internally within the wallet toolbox. |
| 14 | + |
| 15 | +## Code Walkthrough |
| 16 | + |
| 17 | +### Configuration |
| 18 | + |
| 19 | +The fiat exchange rates are mocked for testing or example purposes using a `map[defs.Currency]float64`: |
| 20 | + |
| 21 | +```go |
| 22 | +{ |
| 23 | + USD: 1.0, |
| 24 | + EUR: 0.85, |
| 25 | + GBP: 0.65, |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +You can replace or update this map with actual rates from a provider or service. |
| 30 | + |
| 31 | +### Method Signature |
| 32 | + |
| 33 | +```go |
| 34 | +func (s *WalletServices) FiatExchangeRate(currency defs.Currency, base *defs.Currency) float64 |
| 35 | +``` |
| 36 | + |
| 37 | +- **`currency`**: Target fiat currency (e.g., EUR). |
| 38 | +- **`base`**: Base fiat currency to convert against (e.g., USD). Defaults to USD if `nil`. |
| 39 | +- **Returns**: The conversion rate from `currency` to `base`. |
| 40 | + |
| 41 | +### Example Scenarios |
| 42 | + |
| 43 | +- `FiatExchangeRate(EUR, USD)` → 0.85 (EUR to USD) |
| 44 | +- `FiatExchangeRate(GBP, EUR)` → 0.65 / 0.85 ≈ 0.7647 |
| 45 | +- `FiatExchangeRate(GBP, nil)` → 0.65 (GBP to default USD) |
| 46 | +- `FiatExchangeRate(ABC, USD)` → Error (unknown currency) |
| 47 | + |
| 48 | +## Running the Example |
| 49 | + |
| 50 | +```bash |
| 51 | +go run ./examples/services_examples/fiat_exchange_rate/fiat_exchange_rate.go |
| 52 | +``` |
| 53 | + |
| 54 | +## Expected Output |
| 55 | + |
| 56 | +```text |
| 57 | +🚀 STARTING: Fiat Exchange Rate Conversion |
| 58 | +============================================================ |
| 59 | + |
| 60 | +=== STEP === |
| 61 | +FiatExchangeRate is performing: Getting fiat rate for EUR per USD |
| 62 | +-------------------------------------------------- |
| 63 | + |
| 64 | + WALLET CALL: FiatExchangeRate |
| 65 | +Args: EUR/USD |
| 66 | +✅ Result: 0.85 |
| 67 | + |
| 68 | +=== STEP === |
| 69 | +FiatExchangeRate is performing: Getting fiat rate for GBP per EUR |
| 70 | +-------------------------------------------------- |
| 71 | + |
| 72 | + WALLET CALL: FiatExchangeRate |
| 73 | +Args: GBP/EUR |
| 74 | +✅ Result: 0.7647058823529412 |
| 75 | + |
| 76 | +=== STEP === |
| 77 | +FiatExchangeRate is performing: Getting fiat rate for GBP per <nil> |
| 78 | +-------------------------------------------------- |
| 79 | + |
| 80 | + WALLET CALL: FiatExchangeRate |
| 81 | +Args: GBP/<nil> |
| 82 | +✅ Result: 0.65 |
| 83 | + |
| 84 | +=== STEP === |
| 85 | +FiatExchangeRate is performing: Getting fiat rate for ABC per USD |
| 86 | +-------------------------------------------------- |
| 87 | + |
| 88 | + WALLET CALL: FiatExchangeRate |
| 89 | +Args: ABC/USD |
| 90 | +❌ Error: rate not found |
| 91 | +============================================================ |
| 92 | +🎉 COMPLETED: Fiat Exchange Rate Conversion |
| 93 | +``` |
| 94 | + |
| 95 | +## Integration Steps |
| 96 | + |
| 97 | +1. **Add fiat exchange rates** to your service configuration or implement a live provider. |
| 98 | +2. **Invoke `FiatExchangeRate()`** with the desired currency and optional base. |
| 99 | +3. **Use the returned value** to display or convert currency values in your app. |
| 100 | +4. **Handle errors gracefully** if a currency is missing or unknown. |
| 101 | +5. **Cache frequently-used rates** to avoid redundant calls in performance-critical paths. |
| 102 | + |
| 103 | +## Additional Resources |
| 104 | + |
| 105 | +- [FiatExchangeRate Code](./fiat_exchange_rate.go) - Main example implementation |
| 106 | +- [BSVExchangeRate](../bsv_exchange_rate/bsv_exchange_rate.go) - Check BSV/USD rate |
| 107 | +- [Currency Definitions](../../../pkg/defs/currency.go) - Enum of supported fiat currencies |
0 commit comments