decred.org/dcrdex@v1.0.5/dex/fiatrates/types.go (about) 1 package fiatrates 2 3 import ( 4 "context" 5 "strings" 6 "sync" 7 "time" 8 9 "decred.org/dcrdex/dex" 10 ) 11 12 // reactivateDuration is how long it'll take before an auto disabled rate source 13 // is auto re-enabled. 14 var reactivateDuration = 24*time.Hour + FiatRateDataExpiry 15 16 type Config struct { 17 CryptoCompareAPIKey string `long:"ccdataapikey" description:"This is your free API Key from cryptocompare.com."` 18 EnableBinanceUS bool `long:"enablebinanceus" description:"Set to true, if running the tatanka mesh from a US based server."` 19 DisabledFiatSources string `long:"disabledfiatsources" description:"A list of disabled sources separated by comma. See fiatrate/sources.go."` 20 } 21 22 // AllFiatSourceDisabled checks if all currently supported fiat rate sources 23 // are disabled. 24 func (cfg Config) AllFiatSourceDisabled() bool { 25 disabledSources := strings.ToLower(cfg.DisabledFiatSources) 26 return strings.Contains(disabledSources, strings.ToLower(cryptoCompare)) && strings.Contains(disabledSources, strings.ToLower(binance)) && 27 strings.Contains(disabledSources, strings.ToLower(coinpaprika)) && strings.Contains(disabledSources, strings.ToLower(messari)) && 28 strings.Contains(disabledSources, strings.ToLower(kuCoin)) 29 } 30 31 type source struct { 32 name string 33 mtx sync.RWMutex 34 // rates is a map of ticker to their fiat value. It will be empty if this 35 // source is auto-deactivated. 36 rates map[string]float64 37 // disabled is set to true if this source is deactivated automatically or by 38 // admin. Rates from this source would no longer be requested once it 39 // disabled. 40 disabled bool 41 // canReactivate is set to false if source was disabled by admin. 42 canReactivate bool 43 // lastRefresh is the last time this source made a successful request for 44 // ticker rates. 45 lastRefresh time.Time 46 requestInterval time.Duration 47 getRates func(ctx context.Context, tickers []string, log dex.Logger) (map[string]float64, error) 48 } 49 50 func (s *source) isDisabled() bool { 51 s.mtx.RLock() 52 defer s.mtx.RUnlock() 53 return s.disabled 54 } 55 56 func (s *source) hasTicker() bool { 57 s.mtx.RLock() 58 defer s.mtx.RUnlock() 59 return len(s.rates) != 0 60 } 61 62 func (s *source) isExpired() bool { 63 s.mtx.RLock() 64 defer s.mtx.RUnlock() 65 return time.Since(s.lastRefresh) > FiatRateDataExpiry 66 } 67 68 func (s *source) deactivate() { 69 s.mtx.Lock() 70 defer s.mtx.Unlock() 71 s.rates = nil // empty previous rates 72 s.disabled = true 73 } 74 75 // checkIfSourceCanReactivate reactivates a disabled source 76 func (s *source) checkIfSourceCanReactivate() bool { 77 s.mtx.Lock() 78 defer s.mtx.Unlock() 79 if !s.disabled { 80 return false 81 } 82 83 reactivate := s.canReactivate && !s.lastRefresh.IsZero() && time.Since(s.lastRefresh) > reactivateDuration 84 s.disabled = reactivate 85 return reactivate 86 } 87 88 type fiatRateAndSourceCount struct { 89 sources int 90 totalFiatRate float64 91 } 92 93 // FiatRateInfo holds the fiat rate and the last update time for an 94 // asset. 95 type FiatRateInfo struct { 96 Value float64 97 LastUpdate time.Time 98 }