2023-02-21 09:05:16 +00:00
|
|
|
package market
|
2023-02-17 14:11:07 +00:00
|
|
|
|
|
|
|
import (
|
2024-07-02 17:58:55 +00:00
|
|
|
"context"
|
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
|
|
|
|
2024-10-28 20:54:17 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2023-03-20 13:02:09 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2023-02-28 08:14:37 +00:00
|
|
|
|
2024-06-21 09:53:31 +00:00
|
|
|
"github.com/status-im/status-go/circuitbreaker"
|
2024-10-28 20:54:17 +00:00
|
|
|
"github.com/status-im/status-go/logutils"
|
2023-02-21 09:05:16 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/thirdparty"
|
2023-03-20 13:02:09 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/walletevent"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
EventMarketStatusChanged walletevent.EventType = "wallet-market-status-changed"
|
2023-02-17 14:11:07 +00:00
|
|
|
)
|
|
|
|
|
2024-09-20 11:24:43 +00:00
|
|
|
const (
|
|
|
|
MaxAgeInSecondsForFresh int64 = -1
|
|
|
|
MaxAgeInSecondsForBalances int64 = 60
|
|
|
|
)
|
|
|
|
|
2023-02-17 14:11:07 +00:00
|
|
|
type DataPoint struct {
|
|
|
|
Price float64
|
|
|
|
UpdatedAt int64
|
|
|
|
}
|
|
|
|
|
2024-09-20 11:24:43 +00:00
|
|
|
type MarketValuesSnapshot struct {
|
|
|
|
MarketValues thirdparty.TokenMarketValues
|
|
|
|
UpdatedAt int64
|
|
|
|
}
|
|
|
|
|
2023-02-17 14:11:07 +00:00
|
|
|
type DataPerTokenAndCurrency = map[string]map[string]DataPoint
|
2024-09-20 11:24:43 +00:00
|
|
|
type MarketValuesPerCurrencyAndToken = map[string]map[string]MarketValuesSnapshot
|
|
|
|
type TokenMarketCache MarketValuesPerCurrencyAndToken
|
|
|
|
type TokenPriceCache DataPerTokenAndCurrency
|
2023-02-17 14:11:07 +00:00
|
|
|
|
|
|
|
type Manager struct {
|
2023-03-20 13:02:09 +00:00
|
|
|
feed *event.Feed
|
2024-09-20 11:24:43 +00:00
|
|
|
priceCache MarketCache[TokenPriceCache]
|
|
|
|
marketCache MarketCache[TokenMarketCache]
|
2023-03-20 13:02:09 +00:00
|
|
|
IsConnected bool
|
|
|
|
LastCheckedAt int64
|
|
|
|
IsConnectedLock sync.RWMutex
|
2024-06-21 09:53:31 +00:00
|
|
|
circuitbreaker *circuitbreaker.CircuitBreaker
|
|
|
|
providers []thirdparty.MarketDataProvider
|
2023-02-17 14:11:07 +00:00
|
|
|
}
|
|
|
|
|
2024-06-21 09:53:31 +00:00
|
|
|
func NewManager(providers []thirdparty.MarketDataProvider, feed *event.Feed) *Manager {
|
|
|
|
cb := circuitbreaker.NewCircuitBreaker(circuitbreaker.Config{
|
2023-02-28 08:14:37 +00:00
|
|
|
Timeout: 10000,
|
|
|
|
MaxConcurrentRequests: 100,
|
|
|
|
SleepWindow: 300000,
|
|
|
|
ErrorPercentThreshold: 25,
|
|
|
|
})
|
|
|
|
|
2023-02-17 14:11:07 +00:00
|
|
|
return &Manager{
|
2024-06-21 09:53:31 +00:00
|
|
|
feed: feed,
|
2024-09-20 11:24:43 +00:00
|
|
|
priceCache: *NewCache(make(TokenPriceCache)),
|
|
|
|
marketCache: *NewCache(make(TokenMarketCache)),
|
2024-06-21 09:53:31 +00:00
|
|
|
IsConnected: true,
|
|
|
|
LastCheckedAt: time.Now().Unix(),
|
|
|
|
circuitbreaker: cb,
|
|
|
|
providers: providers,
|
2023-02-17 14:11:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-20 13:02:09 +00:00
|
|
|
func (pm *Manager) setIsConnected(value bool) {
|
|
|
|
pm.IsConnectedLock.Lock()
|
|
|
|
defer pm.IsConnectedLock.Unlock()
|
|
|
|
pm.LastCheckedAt = time.Now().Unix()
|
|
|
|
if value != pm.IsConnected {
|
|
|
|
message := "down"
|
|
|
|
if value {
|
|
|
|
message = "up"
|
|
|
|
}
|
|
|
|
pm.feed.Send(walletevent.Event{
|
|
|
|
Type: EventMarketStatusChanged,
|
|
|
|
Accounts: []common.Address{},
|
|
|
|
Message: message,
|
|
|
|
At: time.Now().Unix(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
pm.IsConnected = value
|
|
|
|
}
|
|
|
|
|
2024-06-21 09:53:31 +00:00
|
|
|
func (pm *Manager) makeCall(providers []thirdparty.MarketDataProvider, f func(provider thirdparty.MarketDataProvider) (interface{}, error)) (interface{}, error) {
|
2024-07-02 17:58:55 +00:00
|
|
|
cmd := circuitbreaker.NewCommand(context.Background(), nil)
|
2024-06-21 09:53:31 +00:00
|
|
|
for _, provider := range providers {
|
|
|
|
provider := provider
|
|
|
|
cmd.Add(circuitbreaker.NewFunctor(func() ([]interface{}, error) {
|
|
|
|
result, err := f(provider)
|
|
|
|
return []interface{}{result}, err
|
2024-07-02 17:58:55 +00:00
|
|
|
}, provider.ID()))
|
2024-06-21 09:53:31 +00:00
|
|
|
}
|
2023-02-28 08:14:37 +00:00
|
|
|
|
2024-06-21 09:53:31 +00:00
|
|
|
result := pm.circuitbreaker.Execute(cmd)
|
|
|
|
pm.setIsConnected(result.Error() == nil)
|
2023-02-28 08:14:37 +00:00
|
|
|
|
2024-06-21 09:53:31 +00:00
|
|
|
if result.Error() != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Error("Error fetching prices", zap.Error(result.Error()))
|
2024-06-21 09:53:31 +00:00
|
|
|
return nil, result.Error()
|
2023-02-28 08:14:37 +00:00
|
|
|
}
|
2024-06-21 09:53:31 +00:00
|
|
|
|
|
|
|
return result.Result()[0], nil
|
2023-02-28 08:14:37 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2024-06-21 09:53:31 +00:00
|
|
|
result, err := pm.makeCall(pm.providers, func(provider thirdparty.MarketDataProvider) (interface{}, error) {
|
|
|
|
return provider.FetchHistoricalDailyPrices(symbol, currency, limit, allData, aggregate)
|
|
|
|
})
|
|
|
|
|
2023-02-28 08:14:37 +00:00
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Error("Error fetching prices", zap.Error(err))
|
2023-02-28 08:14:37 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-06-21 09:53:31 +00:00
|
|
|
prices := result.([]thirdparty.HistoricalPrice)
|
|
|
|
return prices, nil
|
2023-02-21 09:05:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *Manager) FetchHistoricalHourlyPrices(symbol string, currency string, limit int, aggregate int) ([]thirdparty.HistoricalPrice, error) {
|
2024-06-21 09:53:31 +00:00
|
|
|
result, err := pm.makeCall(pm.providers, func(provider thirdparty.MarketDataProvider) (interface{}, error) {
|
|
|
|
return provider.FetchHistoricalHourlyPrices(symbol, currency, limit, aggregate)
|
|
|
|
})
|
|
|
|
|
2023-02-28 08:14:37 +00:00
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Error("Error fetching prices", zap.Error(err))
|
2023-02-28 08:14:37 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-06-21 09:53:31 +00:00
|
|
|
prices := result.([]thirdparty.HistoricalPrice)
|
|
|
|
return prices, nil
|
2023-02-21 09:05:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *Manager) FetchTokenMarketValues(symbols []string, currency string) (map[string]thirdparty.TokenMarketValues, error) {
|
2024-06-21 09:53:31 +00:00
|
|
|
result, err := pm.makeCall(pm.providers, func(provider thirdparty.MarketDataProvider) (interface{}, error) {
|
|
|
|
return provider.FetchTokenMarketValues(symbols, currency)
|
|
|
|
})
|
|
|
|
|
2023-02-28 08:14:37 +00:00
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Error("Error fetching prices", zap.Error(err))
|
2023-02-28 08:14:37 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-06-21 09:53:31 +00:00
|
|
|
marketValues := result.(map[string]thirdparty.TokenMarketValues)
|
|
|
|
return marketValues, nil
|
2023-02-21 09:05:16 +00:00
|
|
|
}
|
|
|
|
|
2024-09-20 11:24:43 +00:00
|
|
|
func (pm *Manager) updateMarketCache(currency string, marketValues map[string]thirdparty.TokenMarketValues) {
|
|
|
|
Write(&pm.marketCache, func(tokenMarketCache TokenMarketCache) TokenMarketCache {
|
|
|
|
for token, tokenMarketValues := range marketValues {
|
|
|
|
if _, present := tokenMarketCache[currency]; !present {
|
|
|
|
tokenMarketCache[currency] = make(map[string]MarketValuesSnapshot)
|
|
|
|
}
|
|
|
|
|
|
|
|
tokenMarketCache[currency][token] = MarketValuesSnapshot{
|
|
|
|
UpdatedAt: time.Now().Unix(),
|
|
|
|
MarketValues: tokenMarketValues,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tokenMarketCache
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *Manager) GetOrFetchTokenMarketValues(symbols []string, currency string, maxAgeInSeconds int64) (map[string]thirdparty.TokenMarketValues, error) {
|
|
|
|
// docs: Determine which token market data to fetch based on what's inside the cache and the last time the cache was updated
|
|
|
|
symbolsToFetch := Read(&pm.marketCache, func(marketCache TokenMarketCache) []string {
|
|
|
|
tokenMarketValuesCache, ok := marketCache[currency]
|
|
|
|
if !ok {
|
|
|
|
return symbols
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now().Unix()
|
|
|
|
symbolsToFetchMap := make(map[string]bool)
|
|
|
|
symbolsToFetch := make([]string, 0, len(symbols))
|
|
|
|
|
|
|
|
for _, symbol := range symbols {
|
|
|
|
marketValueSnapshot, found := tokenMarketValuesCache[symbol]
|
|
|
|
if !found {
|
|
|
|
if !symbolsToFetchMap[symbol] {
|
|
|
|
symbolsToFetchMap[symbol] = true
|
|
|
|
symbolsToFetch = append(symbolsToFetch, symbol)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if now-marketValueSnapshot.UpdatedAt > maxAgeInSeconds {
|
|
|
|
if !symbolsToFetchMap[symbol] {
|
|
|
|
symbolsToFetchMap[symbol] = true
|
|
|
|
symbolsToFetch = append(symbolsToFetch, symbol)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return symbolsToFetch
|
|
|
|
})
|
|
|
|
|
|
|
|
// docs: Fetch and cache the token market data for missing or stale token market data
|
|
|
|
if len(symbolsToFetch) > 0 {
|
|
|
|
marketValues, err := pm.FetchTokenMarketValues(symbolsToFetch, currency)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pm.updateMarketCache(currency, marketValues)
|
|
|
|
}
|
|
|
|
|
|
|
|
// docs: Extract token market data from populated cache
|
|
|
|
tokenMarketValues := Read(&pm.marketCache, func(tokenMarketCache TokenMarketCache) map[string]thirdparty.TokenMarketValues {
|
|
|
|
tokenMarketValuesPerSymbol := make(map[string]thirdparty.TokenMarketValues)
|
|
|
|
if cachedTokenMarketValues, ok := tokenMarketCache[currency]; ok {
|
|
|
|
for _, symbol := range symbols {
|
|
|
|
if marketValuesSnapshot, found := cachedTokenMarketValues[symbol]; found {
|
|
|
|
tokenMarketValuesPerSymbol[symbol] = marketValuesSnapshot.MarketValues
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tokenMarketValuesPerSymbol
|
|
|
|
})
|
|
|
|
|
|
|
|
return tokenMarketValues, nil
|
|
|
|
}
|
|
|
|
|
2023-02-21 09:05:16 +00:00
|
|
|
func (pm *Manager) FetchTokenDetails(symbols []string) (map[string]thirdparty.TokenDetails, error) {
|
2024-06-21 09:53:31 +00:00
|
|
|
result, err := pm.makeCall(pm.providers, func(provider thirdparty.MarketDataProvider) (interface{}, error) {
|
|
|
|
return provider.FetchTokenDetails(symbols)
|
|
|
|
})
|
|
|
|
|
2023-02-28 08:14:37 +00:00
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Error("Error fetching prices", zap.Error(err))
|
2023-02-28 08:14:37 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-06-21 09:53:31 +00:00
|
|
|
tokenDetails := result.(map[string]thirdparty.TokenDetails)
|
|
|
|
return 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) {
|
2024-06-21 09:53:31 +00:00
|
|
|
response, err := pm.makeCall(pm.providers, func(provider thirdparty.MarketDataProvider) (interface{}, error) {
|
|
|
|
return provider.FetchPrices(symbols, currencies)
|
|
|
|
})
|
2023-02-28 08:14:37 +00:00
|
|
|
|
2023-02-17 14:11:07 +00:00
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Error("Error fetching prices", zap.Error(err))
|
2023-02-17 14:11:07 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-06-21 09:53:31 +00:00
|
|
|
prices := response.(map[string]map[string]float64)
|
2023-02-28 08:14:37 +00:00
|
|
|
pm.updatePriceCache(prices)
|
|
|
|
return prices, nil
|
2023-02-17 14:11:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *Manager) getCachedPricesFor(symbols []string, currencies []string) DataPerTokenAndCurrency {
|
2024-09-20 11:24:43 +00:00
|
|
|
return Read(&pm.priceCache, func(tokenPriceCache TokenPriceCache) DataPerTokenAndCurrency {
|
|
|
|
prices := make(DataPerTokenAndCurrency)
|
|
|
|
for _, symbol := range symbols {
|
|
|
|
prices[symbol] = make(map[string]DataPoint)
|
|
|
|
for _, currency := range currencies {
|
|
|
|
prices[symbol][currency] = tokenPriceCache[symbol][currency]
|
|
|
|
}
|
2023-02-17 14:11:07 +00:00
|
|
|
}
|
2024-09-20 11:24:43 +00:00
|
|
|
return prices
|
|
|
|
})
|
2023-02-17 14:11:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *Manager) updatePriceCache(prices map[string]map[string]float64) {
|
2024-09-20 11:24:43 +00:00
|
|
|
Write(&pm.priceCache, func(tokenPriceCache TokenPriceCache) TokenPriceCache {
|
|
|
|
for token, pricesPerCurrency := range prices {
|
|
|
|
_, present := tokenPriceCache[token]
|
|
|
|
if !present {
|
|
|
|
tokenPriceCache[token] = make(map[string]DataPoint)
|
|
|
|
}
|
|
|
|
for currency, price := range pricesPerCurrency {
|
|
|
|
tokenPriceCache[token][currency] = DataPoint{
|
|
|
|
Price: price,
|
|
|
|
UpdatedAt: time.Now().Unix(),
|
|
|
|
}
|
2023-02-17 14:11:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-20 11:24:43 +00:00
|
|
|
return tokenPriceCache
|
|
|
|
})
|
2023-02-17 14:11:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2024-09-20 11:24:43 +00:00
|
|
|
symbolsToFetch := Read(&pm.priceCache, func(tokenPriceCache TokenPriceCache) []string {
|
|
|
|
symbolsToFetchMap := make(map[string]bool)
|
|
|
|
symbolsToFetch := make([]string, 0, len(symbols))
|
2023-02-17 14:11:07 +00:00
|
|
|
|
2024-09-20 11:24:43 +00:00
|
|
|
now := time.Now().Unix()
|
2023-02-17 14:11:07 +00:00
|
|
|
|
2024-09-20 11:24:43 +00:00
|
|
|
for _, symbol := range symbols {
|
|
|
|
tokenPriceCache, ok := tokenPriceCache[symbol]
|
|
|
|
if !ok {
|
2023-02-17 14:11:07 +00:00
|
|
|
if !symbolsToFetchMap[symbol] {
|
|
|
|
symbolsToFetchMap[symbol] = true
|
|
|
|
symbolsToFetch = append(symbolsToFetch, symbol)
|
|
|
|
}
|
2024-09-20 11:24:43 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, currency := range currencies {
|
|
|
|
if now-tokenPriceCache[currency].UpdatedAt > maxAgeInSeconds {
|
|
|
|
if !symbolsToFetchMap[symbol] {
|
|
|
|
symbolsToFetchMap[symbol] = true
|
|
|
|
symbolsToFetch = append(symbolsToFetch, symbol)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2023-02-17 14:11:07 +00:00
|
|
|
}
|
|
|
|
}
|
2024-09-20 11:24:43 +00:00
|
|
|
|
|
|
|
return symbolsToFetch
|
|
|
|
})
|
2023-02-17 14:11:07 +00:00
|
|
|
|
|
|
|
if len(symbolsToFetch) > 0 {
|
|
|
|
_, err := pm.FetchPrices(symbolsToFetch, currencies)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
prices := pm.getCachedPricesFor(symbols, currencies)
|
|
|
|
|
|
|
|
return prices, nil
|
|
|
|
}
|