2023-02-21 09:05:16 +00:00
|
|
|
package market
|
2023-02-17 14:11:07 +00:00
|
|
|
|
|
|
|
import (
|
2023-02-21 09:05:16 +00:00
|
|
|
"sync"
|
2023-02-17 14:11:07 +00:00
|
|
|
"time"
|
2023-02-21 09:05:16 +00:00
|
|
|
|
2023-02-28 08:14:37 +00:00
|
|
|
"github.com/afex/hystrix-go/hystrix"
|
|
|
|
|
2023-02-21 09:05:16 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/thirdparty"
|
2023-02-17 14:11:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type DataPoint struct {
|
|
|
|
Price float64
|
|
|
|
UpdatedAt int64
|
|
|
|
}
|
|
|
|
|
|
|
|
type DataPerTokenAndCurrency = map[string]map[string]DataPoint
|
|
|
|
|
|
|
|
type Manager struct {
|
2023-03-17 06:33:20 +00:00
|
|
|
main thirdparty.MarketDataProvider
|
|
|
|
fallback thirdparty.MarketDataProvider
|
|
|
|
priceCache DataPerTokenAndCurrency
|
|
|
|
IsConnected bool
|
|
|
|
LastCheckedAt int64
|
|
|
|
priceCacheLock sync.RWMutex
|
2023-02-17 14:11:07 +00:00
|
|
|
}
|
|
|
|
|
2023-02-28 08:14:37 +00:00
|
|
|
func NewManager(main thirdparty.MarketDataProvider, fallback thirdparty.MarketDataProvider) *Manager {
|
|
|
|
hystrix.ConfigureCommand("marketClient", hystrix.CommandConfig{
|
|
|
|
Timeout: 10000,
|
|
|
|
MaxConcurrentRequests: 100,
|
|
|
|
SleepWindow: 300000,
|
|
|
|
ErrorPercentThreshold: 25,
|
|
|
|
})
|
|
|
|
|
2023-02-17 14:11:07 +00:00
|
|
|
return &Manager{
|
2023-02-28 08:14:37 +00:00
|
|
|
main: main,
|
|
|
|
fallback: fallback,
|
2023-02-17 14:11:07 +00:00
|
|
|
priceCache: make(DataPerTokenAndCurrency),
|
2023-02-21 09:05:16 +00:00
|
|
|
IsConnected: true,
|
|
|
|
LastCheckedAt: time.Now().Unix(),
|
2023-02-17 14:11:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-28 08:14:37 +00:00
|
|
|
func (pm *Manager) makeCall(main func() (any, error), fallback func() (any, error)) (any, error) {
|
|
|
|
resultChan := make(chan any, 1)
|
|
|
|
pm.LastCheckedAt = time.Now().Unix()
|
|
|
|
errChan := hystrix.Go("marketClient", func() error {
|
|
|
|
res, err := main()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pm.IsConnected = true
|
|
|
|
resultChan <- res
|
|
|
|
return nil
|
|
|
|
}, func(err error) error {
|
|
|
|
if pm.fallback == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := fallback()
|
|
|
|
if err != nil {
|
|
|
|
pm.IsConnected = false
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pm.IsConnected = true
|
|
|
|
resultChan <- res
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
select {
|
|
|
|
case result := <-resultChan:
|
|
|
|
return result, nil
|
|
|
|
case err := <-errChan:
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-21 09:05:16 +00:00
|
|
|
func (pm *Manager) FetchHistoricalDailyPrices(symbol string, currency string, limit int, allData bool, aggregate int) ([]thirdparty.HistoricalPrice, error) {
|
2023-02-28 08:14:37 +00:00
|
|
|
prices, err := pm.makeCall(
|
|
|
|
func() (any, error) {
|
|
|
|
return pm.main.FetchHistoricalDailyPrices(symbol, currency, limit, allData, aggregate)
|
|
|
|
},
|
|
|
|
func() (any, error) {
|
|
|
|
return pm.fallback.FetchHistoricalDailyPrices(symbol, currency, limit, allData, aggregate)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return prices.([]thirdparty.HistoricalPrice), nil
|
2023-02-21 09:05:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *Manager) FetchHistoricalHourlyPrices(symbol string, currency string, limit int, aggregate int) ([]thirdparty.HistoricalPrice, error) {
|
2023-02-28 08:14:37 +00:00
|
|
|
prices, err := pm.makeCall(
|
|
|
|
func() (any, error) {
|
|
|
|
return pm.main.FetchHistoricalHourlyPrices(symbol, currency, limit, aggregate)
|
|
|
|
},
|
|
|
|
func() (any, error) {
|
|
|
|
return pm.fallback.FetchHistoricalHourlyPrices(symbol, currency, limit, aggregate)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return prices.([]thirdparty.HistoricalPrice), nil
|
2023-02-21 09:05:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *Manager) FetchTokenMarketValues(symbols []string, currency string) (map[string]thirdparty.TokenMarketValues, error) {
|
2023-02-28 08:14:37 +00:00
|
|
|
marketValues, err := pm.makeCall(
|
|
|
|
func() (any, error) {
|
|
|
|
return pm.main.FetchTokenMarketValues(symbols, currency)
|
|
|
|
},
|
|
|
|
func() (any, error) {
|
|
|
|
return pm.fallback.FetchTokenMarketValues(symbols, currency)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return marketValues.(map[string]thirdparty.TokenMarketValues), nil
|
2023-02-21 09:05:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *Manager) FetchTokenDetails(symbols []string) (map[string]thirdparty.TokenDetails, error) {
|
2023-02-28 08:14:37 +00:00
|
|
|
tokenDetails, err := pm.makeCall(
|
|
|
|
func() (any, error) {
|
|
|
|
return pm.main.FetchTokenDetails(symbols)
|
|
|
|
},
|
|
|
|
func() (any, error) {
|
|
|
|
return pm.fallback.FetchTokenDetails(symbols)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tokenDetails.(map[string]thirdparty.TokenDetails), nil
|
2023-02-21 09:05:16 +00:00
|
|
|
}
|
|
|
|
|
2023-02-17 14:11:07 +00:00
|
|
|
func (pm *Manager) FetchPrice(symbol string, currency string) (float64, error) {
|
|
|
|
symbols := [1]string{symbol}
|
|
|
|
currencies := [1]string{currency}
|
|
|
|
|
|
|
|
prices, err := pm.FetchPrices(symbols[:], currencies[:])
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return prices[symbol][currency], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *Manager) FetchPrices(symbols []string, currencies []string) (map[string]map[string]float64, error) {
|
2023-02-28 08:14:37 +00:00
|
|
|
result, err := pm.makeCall(
|
|
|
|
func() (any, error) {
|
|
|
|
return pm.main.FetchPrices(symbols, currencies)
|
|
|
|
},
|
|
|
|
func() (any, error) {
|
|
|
|
return pm.fallback.FetchPrices(symbols, currencies)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2023-02-17 14:11:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-02-28 08:14:37 +00:00
|
|
|
prices := result.(map[string]map[string]float64)
|
|
|
|
pm.updatePriceCache(prices)
|
|
|
|
return prices, nil
|
2023-02-17 14:11:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *Manager) getCachedPricesFor(symbols []string, currencies []string) DataPerTokenAndCurrency {
|
|
|
|
prices := make(DataPerTokenAndCurrency)
|
|
|
|
|
|
|
|
for _, symbol := range symbols {
|
|
|
|
prices[symbol] = make(map[string]DataPoint)
|
|
|
|
for _, currency := range currencies {
|
|
|
|
prices[symbol][currency] = pm.priceCache[symbol][currency]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return prices
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *Manager) updatePriceCache(prices map[string]map[string]float64) {
|
2023-03-17 06:33:20 +00:00
|
|
|
pm.priceCacheLock.Lock()
|
|
|
|
defer pm.priceCacheLock.Unlock()
|
|
|
|
|
2023-02-17 14:11:07 +00:00
|
|
|
for token, pricesPerCurrency := range prices {
|
|
|
|
_, present := pm.priceCache[token]
|
|
|
|
if !present {
|
|
|
|
pm.priceCache[token] = make(map[string]DataPoint)
|
|
|
|
}
|
|
|
|
for currency, price := range pricesPerCurrency {
|
|
|
|
pm.priceCache[token][currency] = DataPoint{
|
|
|
|
Price: price,
|
|
|
|
UpdatedAt: time.Now().Unix(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *Manager) GetCachedPrices() DataPerTokenAndCurrency {
|
2023-03-17 06:33:20 +00:00
|
|
|
pm.priceCacheLock.RLock()
|
|
|
|
defer pm.priceCacheLock.RUnlock()
|
|
|
|
|
2023-02-17 14:11:07 +00:00
|
|
|
return pm.priceCache
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return cached price if present in cache and age is less than maxAgeInSeconds. Fetch otherwise.
|
|
|
|
func (pm *Manager) GetOrFetchPrices(symbols []string, currencies []string, maxAgeInSeconds int64) (DataPerTokenAndCurrency, error) {
|
|
|
|
symbolsToFetchMap := make(map[string]bool)
|
|
|
|
symbolsToFetch := make([]string, 0, len(symbols))
|
|
|
|
|
|
|
|
now := time.Now().Unix()
|
|
|
|
|
|
|
|
for _, symbol := range symbols {
|
2023-03-17 06:33:20 +00:00
|
|
|
tokenPriceCache, ok := pm.GetCachedPrices()[symbol]
|
2023-02-17 14:11:07 +00:00
|
|
|
if !ok {
|
|
|
|
if !symbolsToFetchMap[symbol] {
|
|
|
|
symbolsToFetchMap[symbol] = true
|
|
|
|
symbolsToFetch = append(symbolsToFetch, symbol)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, currency := range currencies {
|
|
|
|
if now-tokenPriceCache[currency].UpdatedAt > maxAgeInSeconds {
|
|
|
|
if !symbolsToFetchMap[symbol] {
|
|
|
|
symbolsToFetchMap[symbol] = true
|
|
|
|
symbolsToFetch = append(symbolsToFetch, symbol)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(symbolsToFetch) > 0 {
|
|
|
|
_, err := pm.FetchPrices(symbolsToFetch, currencies)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
prices := pm.getCachedPricesFor(symbols, currencies)
|
|
|
|
|
|
|
|
return prices, nil
|
|
|
|
}
|