frank 38308d48f2
feat_: log on panic (#5849)
* feat_: log error and stacktrace when panic in goroutine

* test_: add test TestSafeGo

* chore_: rename logAndCall to call

* chore_: rename SafeGo to Go

* chore_: make lint-fix

* chore_: use t.Cleanup

* chore_: Revert "chore_: use t.Cleanup"

This reverts commit 4eb420d179cc0e208e84c13cb941e6b3d1ed9819.

* chore_: Revert "chore_: make lint-fix"

This reverts commit fcc995f157e671a4229b47419c3a0e4004b5fdab.

* chore_: Revert "chore_: rename SafeGo to Go"

This reverts commit a6d73d6df583f313032d79aac62f66328039cb55.

* chore_: Revert "chore_: rename logAndCall to call"

This reverts commit 8fbe993bedb9fbba67349a44f151e2dd5e3bc4cc.

* chore_: Revert "test_: add test TestSafeGo"

This reverts commit a1fa91839f3960398980c6bf456e6462ec944819.

* chore_: Revert "feat_: log error and stacktrace when panic in goroutine"

This reverts commit f612dd828fa2ce410d0e806fe773ecbe3e86a68a.

* feat_: log error and stacktrace when panic in goroutine

* chore_: make lint-fix

* chore_: rename logAndCall to call

* chore_: renaming LogOnPanic

* chore_: update rest goroutine function calls

* chore_: make lint-fix
2024-09-27 06:37:32 +08:00

126 lines
2.8 KiB
Go

package currency
import (
"context"
"database/sql"
"time"
"github.com/ethereum/go-ethereum/event"
gocommon "github.com/status-im/status-go/common"
"github.com/status-im/status-go/services/wallet/market"
"github.com/status-im/status-go/services/wallet/token"
"github.com/status-im/status-go/services/wallet/walletevent"
)
const (
EventCurrencyTickUpdateFormat walletevent.EventType = "wallet-currency-tick-update-format"
currencyFormatUpdateInterval = 1 * time.Hour
)
type Service struct {
currency *Currency
db *DB
tokenManager *token.Manager
walletFeed *event.Feed
cancelFn context.CancelFunc
}
func NewService(db *sql.DB, walletFeed *event.Feed, tokenManager *token.Manager, marketManager *market.Manager) *Service {
return &Service{
currency: NewCurrency(marketManager),
db: NewCurrencyDB(db),
tokenManager: tokenManager,
walletFeed: walletFeed,
}
}
func (s *Service) Start() {
// Update all fiat currency formats in cache
fiatFormats, err := s.getAllFiatCurrencyFormats()
if err == nil {
_ = s.db.UpdateCachedFormats(fiatFormats)
}
ctx, cancel := context.WithCancel(context.Background())
s.cancelFn = cancel
go func() {
defer gocommon.LogOnPanic()
ticker := time.NewTicker(currencyFormatUpdateInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
s.walletFeed.Send(walletevent.Event{
Type: EventCurrencyTickUpdateFormat,
})
}
}
}()
}
func (s *Service) Stop() {
if s.cancelFn != nil {
s.cancelFn()
}
}
func (s *Service) GetCachedCurrencyFormats() (FormatPerSymbol, error) {
return s.db.GetCachedFormats()
}
func (s *Service) FetchAllCurrencyFormats() (FormatPerSymbol, error) {
// Only token prices can change, so we fetch those
tokenFormats, err := s.fetchAllTokenCurrencyFormats()
if err != nil {
return nil, err
}
err = s.db.UpdateCachedFormats(tokenFormats)
if err != nil {
return nil, err
}
return s.GetCachedCurrencyFormats()
}
func (s *Service) getAllFiatCurrencyFormats() (FormatPerSymbol, error) {
return GetFiatCurrencyFormats(GetAllFiatCurrencySymbols())
}
func (s *Service) fetchAllTokenCurrencyFormats() (FormatPerSymbol, error) {
tokens, err := s.tokenManager.GetAllTokens()
if err != nil {
return nil, err
}
tokenPerSymbolMap := make(map[string]bool)
tokenSymbols := make([]string, 0)
for _, t := range tokens {
symbol := t.Symbol
if !tokenPerSymbolMap[symbol] {
tokenPerSymbolMap[symbol] = true
tokenSymbols = append(tokenSymbols, symbol)
}
}
tokenFormats, err := s.currency.FetchTokenCurrencyFormats(tokenSymbols)
if err != nil {
return nil, err
}
gweiSymbol := "Gwei"
tokenFormats[gweiSymbol] = Format{
Symbol: gweiSymbol,
DisplayDecimals: 9,
StripTrailingZeroes: true,
}
return tokenFormats, err
}