feat: change network selection strategy

1) Only fetch history for networks that match test mode enabled
* Trade Off: it will be only refetch in 12 hours so changing test mode won't trigger a refetch until app is restarted or 12 hours.
I think it is ok as change test mode is not a common use case
2) Do not consider networks that are enabled or not as this can be change more often than every 12 hours
This commit is contained in:
Anthony Laibe 2023-08-02 11:13:28 +02:00
parent 1592d2a079
commit 0cd140c9b2
1 changed files with 12 additions and 4 deletions

View File

@ -90,7 +90,7 @@ func (s *Service) Start() {
s.timer = time.NewTimer(balanceHistoryUpdateInterval)
update := func() (exit bool) {
err := s.updateBalanceHistoryForAllEnabledNetworks(s.serviceContext)
err := s.updateBalanceHistory(s.serviceContext)
if s.serviceContext.Err() != nil {
s.triggerEvent(EventBalanceHistoryUpdateFinished, statustypes.Address{}, "Service canceled")
s.timer.Stop()
@ -471,11 +471,11 @@ func sortTimeAsc(data map[chainIdentity][]*DataPoint, pos map[chainIdentity]int)
return res
}
// updateBalanceHistoryForAllEnabledNetworks iterates over all enabled and supported networks for the s.visibleTokenSymbol
// updateBalanceHistory iterates over all networks depending on test/prod for the s.visibleTokenSymbol
// and updates the balance history for the given address
//
// expects ctx to have cancellation support and processing to be cancelled by the caller
func (s *Service) updateBalanceHistoryForAllEnabledNetworks(ctx context.Context) error {
func (s *Service) updateBalanceHistory(ctx context.Context) error {
accountsDB, err := accounts.NewDB(s.db)
if err != nil {
return err
@ -486,7 +486,12 @@ func (s *Service) updateBalanceHistoryForAllEnabledNetworks(ctx context.Context)
return err
}
networks, err := s.networkManager.Get(true)
areTestNetworksEnabled, err := accountsDB.GetTestNetworksEnabled()
if err != nil {
return err
}
networks, err := s.networkManager.Get(false)
if err != nil {
return err
}
@ -495,6 +500,9 @@ func (s *Service) updateBalanceHistoryForAllEnabledNetworks(ctx context.Context)
s.triggerEvent(EventBalanceHistoryUpdateStarted, address, "")
for _, network := range networks {
if network.IsTest != areTestNetworksEnabled {
continue
}
tokensForChain, err := s.tokenManager.GetTokens(network.ChainID)
if err != nil {
tokensForChain = make([]*token.Token, 0)