Skip to content

Commit 428c120

Browse files
committed
[FEATURE] implement getFiatExchangeRate on WalletServices
1 parent 3f132b5 commit 428c120

File tree

2 files changed

+168
-0
lines changed

2 files changed

+168
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log/slog"
6+
7+
"github.com/bsv-blockchain/go-wallet-toolbox/examples/internal/show"
8+
"github.com/bsv-blockchain/go-wallet-toolbox/pkg/defs"
9+
"github.com/bsv-blockchain/go-wallet-toolbox/pkg/services"
10+
)
11+
12+
func exampleFiatExchangeRate(srv *services.WalletServices) {
13+
examples := []struct {
14+
currency defs.Currency
15+
base *defs.Currency
16+
}{
17+
{currency: defs.EUR, base: ptr(defs.USD)},
18+
{currency: defs.GBP, base: ptr(defs.EUR)},
19+
{currency: defs.GBP, base: nil}, // defaults to USD
20+
{currency: "ABC", base: ptr(defs.USD)}, // invalid case
21+
}
22+
23+
for _, ex := range examples {
24+
base := "<nil>"
25+
if ex.base != nil {
26+
base = string(*ex.base)
27+
}
28+
step := fmt.Sprintf("Getting fiat rate for %s per %s", ex.currency, base)
29+
show.Step("FiatExchangeRate", step)
30+
31+
rate := srv.FiatExchangeRate(ex.currency, ex.base)
32+
if rate == 0 {
33+
show.WalletError("FiatExchangeRate", fmt.Sprintf("%s/%s", ex.currency, base), fmt.Errorf("rate not found"))
34+
} else {
35+
show.WalletSuccess("FiatExchangeRate", fmt.Sprintf("%s/%s", ex.currency, base), rate)
36+
}
37+
}
38+
}
39+
40+
func ptr(c defs.Currency) *defs.Currency {
41+
return &c
42+
}
43+
44+
func main() {
45+
show.ProcessStart("Fiat Exchange Rate Conversion")
46+
47+
cfg := defs.DefaultServicesConfig(defs.NetworkMainnet)
48+
cfg.FiatExchangeRates = defs.FiatExchangeRates{
49+
Rates: map[defs.Currency]float64{
50+
defs.USD: 1.0,
51+
defs.EUR: 0.85,
52+
defs.GBP: 0.65,
53+
},
54+
}
55+
56+
srv := services.New(slog.Default(), cfg)
57+
58+
exampleFiatExchangeRate(srv)
59+
60+
show.ProcessComplete("Fiat Exchange Rate Conversion")
61+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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

Comments
 (0)