chore_: empty active networks change event

This commit is contained in:
Dario Gabriel Lipicar 2025-02-13 11:43:11 -03:00 committed by dlipicar
parent 3dfa6bd3fa
commit aae12af211
4 changed files with 14 additions and 74 deletions

View File

@ -102,58 +102,19 @@ func (nm *Manager) Stop() {
}
func (nm *Manager) onTestNetworksEnabledChanged() {
areTestNetworksEnabled, err := nm.GetTestNetworksEnabled()
if err != nil {
nm.logger.Error("failed to get test networks enabled", zap.Error(err))
return
}
oldActiveNetworks, err := nm.getActiveNetworksForTestMode(!areTestNetworksEnabled)
if err != nil {
nm.logger.Error("failed to get old active networks", zap.Error(err))
return
}
newActiveNetworks, err := nm.getActiveNetworksForTestMode(areTestNetworksEnabled)
if err != nil {
nm.logger.Error("failed to get new active networks", zap.Error(err))
return
}
nm.notifyActiveNetworksChange(newActiveNetworks, oldActiveNetworks)
nm.notifyActiveNetworksChange()
}
func (nm *Manager) notifyActiveNetworksChange(activatedNetworks []*params.Network, deactivatedNetworks []*params.Network) {
func (nm *Manager) notifyActiveNetworksChange() {
if nm.networksFeed == nil {
return
}
currentActiveNetworks, err := nm.GetActiveNetworks()
if err != nil {
nm.logger.Error("failed to get active networks", zap.Error(err))
return
}
params := &networksevent.ActiveNetworksChangedParams{
CurrentActiveChainIDs: networksToChainIDs(currentActiveNetworks),
ActivatedChainIDs: networksToChainIDs(activatedNetworks),
DeactivatedChainIDs: networksToChainIDs(deactivatedNetworks),
}
nm.networksFeed.Send(networksevent.Event{
Type: networksevent.EventTypeActiveNetworksChanged,
ActiveNetworksChangedParams: params,
Type: networksevent.EventTypeActiveNetworksChanged,
})
}
func networksToChainIDs(networks []*params.Network) []uint64 {
chainIDs := make([]uint64, 0, len(networks))
for _, network := range networks {
chainIDs = append(chainIDs, network.ChainID)
}
return chainIDs
}
// Init initializes the nets, merges them with existing ones, and wraps the operation in a transaction.
// We should store the following information in the DB:
// - User's RPC providers
@ -295,14 +256,7 @@ func (nm *Manager) SetActive(chainID uint64, active bool) error {
return errors.CreateErrorResponseFromError(fmt.Errorf("failed to persist active status: %w", err))
}
activatedNetworks := []*params.Network{}
deactivatedNetworks := []*params.Network{}
if active {
activatedNetworks = append(activatedNetworks, network)
} else {
deactivatedNetworks = append(deactivatedNetworks, network)
}
nm.notifyActiveNetworksChange(activatedNetworks, deactivatedNetworks)
nm.notifyActiveNetworksChange()
return nil
}

View File

@ -5,17 +5,10 @@ type EventType string
// Event is a type for networks events.
type Event struct {
Type EventType `json:"type"`
ActiveNetworksChangedParams *ActiveNetworksChangedParams `json:"activeNetworksChangedParams,omitempty"` // Only for EventTypeActiveNetworksChanged
Type EventType `json:"type"`
}
const (
// EventTypeActiveNetworksChanged is emitted when networks are activated/deactivated. This includes Testnet mode change.
EventTypeActiveNetworksChanged EventType = "active-networks-changed"
)
type ActiveNetworksChangedParams struct {
ActivatedChainIDs []uint64 `json:"activatedChainIds"`
DeactivatedChainIDs []uint64 `json:"deactivatedChainIds"`
CurrentActiveChainIDs []uint64 `json:"currentActiveChainIds"`
}

View File

@ -14,7 +14,7 @@ type EventCallbacks struct {
ActiveNetworksChangeCb ActiveNetworksChangeCb
}
type ActiveNetworksChangeCb func(params *ActiveNetworksChangedParams)
type ActiveNetworksChangeCb func()
// Watcher executes a given callback whenever a network event is emitted
type Watcher struct {
@ -49,17 +49,12 @@ func (w *Watcher) Stop() {
}
}
func onActiveNetworksChange(callback ActiveNetworksChangeCb, params *ActiveNetworksChangedParams) {
func onActiveNetworksChange(callback ActiveNetworksChangeCb) {
if callback == nil {
return
}
if params == nil {
logutils.ZapLogger().Error("no params in event EventTypeActiveNetworksChanged")
return
}
callback(params)
callback()
}
func watch(ctx context.Context, accountFeed *event.Feed, callbacks EventCallbacks) error {
@ -78,7 +73,7 @@ func watch(ctx context.Context, accountFeed *event.Feed, callbacks EventCallback
case ev := <-ch:
switch ev.Type {
case EventTypeActiveNetworksChanged:
onActiveNetworksChange(callbacks.ActiveNetworksChangeCb, ev.ActiveNetworksChangedParams)
onActiveNetworksChange(callbacks.ActiveNetworksChangeCb)
}
}
}

View File

@ -365,15 +365,13 @@ func (c *Controller) startNetworkEventsWatcher() {
return
}
activeNetworksChangeCb := func(params *networksevent.ActiveNetworksChangedParams) {
activeNetworksChangeCb := func() {
// Lazy logic for now, just restart everything if there's any network change.
// TODO #17183: Per-network logic
if len(params.ActivatedChainIDs) > 0 || len(params.DeactivatedChainIDs) > 0 {
c.stopPeriodicalOwnershipFetch()
err := c.startPeriodicalOwnershipFetch()
if err != nil {
logutils.ZapLogger().Error("Error starting periodical collectibles fetch", zap.Error(err))
}
c.stopPeriodicalOwnershipFetch()
err := c.startPeriodicalOwnershipFetch()
if err != nil {
logutils.ZapLogger().Error("Error starting periodical collectibles fetch", zap.Error(err))
}
}