feat: refresh collectibles when testnet mode setting changes

This commit is contained in:
Dario Gabriel Lipicar 2023-10-10 09:18:47 -03:00 committed by dlipicar
parent 6d19e165d9
commit 28cfeb4bc8
5 changed files with 46 additions and 6 deletions

View File

@ -98,7 +98,7 @@ func (b *StatusNode) initServices(config *params.NodeConfig, mediaServer *server
// Wallet Service is used by wakuExtSrvc/wakuV2ExtSrvc
// Keep this initialization before the other two
if config.WalletConfig.Enabled {
walletService := b.walletService(accDB, accountsFeed, &b.walletFeed)
walletService := b.walletService(accDB, accountsFeed, settingsFeed, &b.walletFeed)
services = append(services, walletService)
}
@ -526,10 +526,10 @@ func (b *StatusNode) SetWalletCollectibleCommunityInfoProvider(provider thirdpar
}
}
func (b *StatusNode) walletService(accountsDB *accounts.Database, accountsFeed *event.Feed, walletFeed *event.Feed) *wallet.Service {
func (b *StatusNode) walletService(accountsDB *accounts.Database, accountsFeed *event.Feed, settingsFeed *event.Feed, walletFeed *event.Feed) *wallet.Service {
if b.walletSrvc == nil {
b.walletSrvc = wallet.NewService(
b.walletDB, accountsDB, b.rpcClient, accountsFeed, b.gethAccountManager, b.transactor, b.config,
b.walletDB, accountsDB, b.rpcClient, accountsFeed, settingsFeed, b.gethAccountManager, b.transactor, b.config,
b.ensService(b.timeSourceNow()),
b.stickersService(accountsDB),
b.pendingTracker,

View File

@ -11,8 +11,10 @@ import (
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log"
"github.com/status-im/status-go/multiaccounts/accounts"
"github.com/status-im/status-go/multiaccounts/settings"
"github.com/status-im/status-go/rpc/network"
"github.com/status-im/status-go/services/accounts/accountsevent"
"github.com/status-im/status-go/services/accounts/settingsevent"
"github.com/status-im/status-go/services/wallet/async"
walletCommon "github.com/status-im/status-go/services/wallet/common"
"github.com/status-im/status-go/services/wallet/transfer"
@ -35,6 +37,7 @@ type Controller struct {
walletFeed *event.Feed
accountsDB *accounts.Database
accountsFeed *event.Feed
settingsFeed *event.Feed
networkManager *network.Manager
cancelFn context.CancelFunc
@ -44,6 +47,7 @@ type Controller struct {
group *async.Group
accountsWatcher *accountsevent.Watcher
walletEventsWatcher *walletevent.Watcher
settingsWatcher *settingsevent.Watcher
commandsLock sync.RWMutex
}
@ -53,6 +57,7 @@ func NewController(
walletFeed *event.Feed,
accountsDB *accounts.Database,
accountsFeed *event.Feed,
settingsFeed *event.Feed,
networkManager *network.Manager,
manager *Manager) *Controller {
return &Controller{
@ -61,6 +66,7 @@ func NewController(
walletFeed: walletFeed,
accountsDB: accountsDB,
accountsFeed: accountsFeed,
settingsFeed: settingsFeed,
networkManager: networkManager,
commands: make(commandPerAddressAndChainID),
timers: make(timerPerAddressAndChainID),
@ -76,9 +82,14 @@ func (c *Controller) Start() {
// Setup collectibles fetch when relevant activity is detected
c.startWalletEventsWatcher()
// Setup collectibles fetch when chain-related settings change
c.startSettingsWatcher()
}
func (c *Controller) Stop() {
c.stopSettingsWatcher()
c.stopWalletEventsWatcher()
c.stopAccountsWatcher()
@ -355,3 +366,30 @@ func (c *Controller) stopWalletEventsWatcher() {
c.walletEventsWatcher = nil
}
}
func (c *Controller) startSettingsWatcher() {
if c.settingsWatcher != nil {
return
}
settingChangeCb := func(setting settings.SettingField, value interface{}) {
if setting.Equals(settings.TestNetworksEnabled) || setting.Equals(settings.IsSepoliaEnabled) {
c.stopPeriodicalOwnershipFetch()
err := c.startPeriodicalOwnershipFetch()
if err != nil {
log.Error("Error starting periodical collectibles fetch", "error", err)
}
}
}
c.settingsWatcher = settingsevent.NewWatcher(c.settingsFeed, settingChangeCb)
c.settingsWatcher.Start()
}
func (c *Controller) stopSettingsWatcher() {
if c.settingsWatcher != nil {
c.settingsWatcher.Stop()
c.settingsWatcher = nil
}
}

View File

@ -55,11 +55,12 @@ func NewService(
walletFeed *event.Feed,
accountsDB *accounts.Database,
accountsFeed *event.Feed,
settingsFeed *event.Feed,
networkManager *network.Manager,
manager *Manager) *Service {
return &Service{
manager: manager,
controller: NewController(db, walletFeed, accountsDB, accountsFeed, networkManager, manager),
controller: NewController(db, walletFeed, accountsDB, accountsFeed, settingsFeed, networkManager, manager),
ownershipDB: NewOwnershipDB(db),
walletFeed: walletFeed,
scheduler: async.NewMultiClientScheduler(),

View File

@ -16,7 +16,7 @@ func TestKeycardPairingsFile(t *testing.T) {
db, err := helpers.SetupTestMemorySQLDB(walletdatabase.DbInitializer{})
require.NoError(t, err)
service := NewService(db, nil, &rpc.Client{}, nil, nil, nil, &params.NodeConfig{}, nil, nil, nil, nil)
service := NewService(db, nil, &rpc.Client{}, nil, nil, nil, nil, &params.NodeConfig{}, nil, nil, nil, nil)
data, err := service.KeycardPairings().GetPairingsJSONFileContent()
require.NoError(t, err)

View File

@ -45,6 +45,7 @@ func NewService(
accountsDB *accounts.Database,
rpcClient *rpc.Client,
accountFeed *event.Feed,
settingsFeed *event.Feed,
gethManager *account.GethManager,
transactor *transactions.Transactor,
config *params.NodeConfig,
@ -131,7 +132,7 @@ func NewService(
}
collectiblesManager := collectibles.NewManager(db, rpcClient, contractOwnershipProviders, accountOwnershipProviders, collectibleDataProviders, collectionDataProviders, feed)
collectibles := collectibles.NewService(db, feed, accountsDB, accountFeed, rpcClient.NetworkManager, collectiblesManager)
collectibles := collectibles.NewService(db, feed, accountsDB, accountFeed, settingsFeed, rpcClient.NetworkManager, collectiblesManager)
activity := activity.NewService(db, tokenManager, collectiblesManager, feed)