Cache currency conversion

pull/243/head
Simon Roberts 3 years ago
parent 83a4acc172
commit 30c853cb07
No known key found for this signature in database
GPG Key ID: 0F30F99E6B771FD4

@ -309,11 +309,8 @@ func (ct *Cointop) Convert(convertFrom string, convertTo string, amount float64)
var rate float64
if convertFrom == convertTo {
rate = 1.0
} else if convertFrom == "usd" && convertTo == "aud" {
rate = 1.33 // TODO: cache exchange rates and remove this dodgy hack
log.Debugf("XXX DODGY HACK Convert(%s, %s, %f) = %f", convertFrom, convertTo, amount, rate*amount)
} else {
crate, err := ct.api.GetExchangeRate(convertFrom, convertTo)
crate, err := ct.api.GetExchangeRate(convertFrom, convertTo, true)
if err != nil {
return 0, err
}

@ -236,7 +236,7 @@ func (ct *Cointop) UpdateTableHeader() error {
}
leftAlign := ct.GetTableColumnAlignLeft(col)
switch col {
case "price", "balance", "profit":
case "price", "balance", "profit", "cost":
label = fmt.Sprintf("%s%s", ct.CurrencySymbol(), label)
}
if leftAlign {

@ -12,6 +12,7 @@ import (
apitypes "github.com/cointop-sh/cointop/pkg/api/types"
"github.com/cointop-sh/cointop/pkg/api/util"
gecko "github.com/cointop-sh/cointop/pkg/api/vendors/coingecko/v3"
"github.com/cointop-sh/cointop/pkg/api/vendors/coingecko/v3/types"
geckoTypes "github.com/cointop-sh/cointop/pkg/api/vendors/coingecko/v3/types"
)
@ -33,6 +34,7 @@ type Service struct {
maxResultsPerPage uint
maxPages uint
cacheMap sync.Map
cachedRates *types.ExchangeRatesItem
}
// NewCoinGecko new service
@ -146,14 +148,26 @@ func (s *Service) GetCoinGraphData(convert, symbol, name string, start, end int6
return ret, nil
}
// GetCachedExchangeRates returns an indefinitely cached set of exchange rates
func (s *Service) GetExchangeRates(cached bool) (*types.ExchangeRatesItem, error) {
if s.cachedRates == nil || !cached {
rates, err := s.client.ExchangeRates()
if err != nil {
return nil, err
}
s.cachedRates = rates
}
return s.cachedRates, nil
}
// GetExchangeRate gets the current excange rate between two currencies
func (s *Service) GetExchangeRate(convertFrom string, convertTo string) (float64, error) {
func (s *Service) GetExchangeRate(convertFrom string, convertTo string, cached bool) (float64, error) {
convertFrom = strings.ToLower(convertFrom)
convertTo = strings.ToLower(convertTo)
if convertFrom == convertTo {
return 1.0, nil
}
rates, err := s.client.ExchangeRates()
rates, err := s.GetExchangeRates(cached)
if err != nil {
return 0, err
}
@ -188,7 +202,7 @@ func (s *Service) GetGlobalMarketGraphData(convert string, start int64, end int6
// This API does not appear to support vs_currency and only returns USD, so use ExchangeRates to convert
// TODO: watch out - this is not cached, so we hit the backend every time!
rate, err := s.GetExchangeRate("usd", convertTo)
rate, err := s.GetExchangeRate("usd", convertTo, true)
if err != nil {
return ret, err
}

@ -432,7 +432,7 @@ func getChartInterval(start, end int64) string {
}
// GetExchangeRate gets the current excange rate between two currencies
func (s *Service) GetExchangeRate(convertFrom string, convertTo string) (float64, error) {
func (s *Service) GetExchangeRate(convertFrom string, convertTo string, cached bool) (float64, error) {
if convertFrom == convertTo {
return 1.0, nil
}

@ -16,5 +16,5 @@ type Interface interface {
CoinLink(name string) string
SupportedCurrencies() []string
Price(name string, convert string) (float64, error)
GetExchangeRate(convertFrom string, convertTo string) (float64, error)
GetExchangeRate(convertFrom string, convertTo string, cached bool) (float64, error) // I don't love this caching
}

Loading…
Cancel
Save