-
Notifications
You must be signed in to change notification settings - Fork 2
/
exchange_rates.go
39 lines (32 loc) · 1.02 KB
/
exchange_rates.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package newop
import (
"github.com/muun/libwallet"
"github.com/shopspring/decimal"
)
// ExchangeRateWindow holds a map of exchange rates from BTC to every currency we handle
type ExchangeRateWindow struct {
WindowId int
rates map[string]float64
}
func (w *ExchangeRateWindow) AddRate(currency string, rate float64) {
if w.rates == nil {
w.rates = make(map[string]float64)
}
w.rates[currency] = rate
}
func (w *ExchangeRateWindow) Rate(currency string) float64 {
return w.rates[currency]
}
func (s *ExchangeRateWindow) Currencies() *libwallet.StringList {
var currencies []string
for key := range s.rates {
currencies = append(currencies, key)
}
return libwallet.NewStringListWithElements(currencies)
}
func (w *ExchangeRateWindow) convert(amount *MonetaryAmount, currency string) *MonetaryAmount {
fromRate := decimal.NewFromFloat(w.Rate(amount.Currency))
toRate := decimal.NewFromFloat(w.Rate(currency))
value := amount.Value.Div(fromRate).Mul(toRate)
return &MonetaryAmount{Value: value, Currency: currency}
}