|
|
|
@ -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
|
|
|
|
|
}
|
|
|
|
|