Skip to content

Commit

Permalink
chore_: empty active networks change event
Browse files Browse the repository at this point in the history
  • Loading branch information
dlipicar committed Feb 13, 2025
1 parent 86fa895 commit 21ad7f5
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 74 deletions.
54 changes: 4 additions & 50 deletions rpc/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 1 addition & 8 deletions rpc/network/networksevent/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
13 changes: 4 additions & 9 deletions rpc/network/networksevent/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
}
}
Expand Down
12 changes: 5 additions & 7 deletions services/wallet/collectibles/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down

0 comments on commit 21ad7f5

Please sign in to comment.