Skip to content

Commit

Permalink
feat: add NetworkController actions and events
Browse files Browse the repository at this point in the history
actions and events for adding, updating, and removing a network configuration
  • Loading branch information
Prithpal-Sooriya committed Sep 13, 2024
1 parent ccd95c8 commit 5c85bb9
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
67 changes: 65 additions & 2 deletions packages/network-controller/src/NetworkController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,13 +409,33 @@ export type NetworkControllerNetworkAddedEvent = {
payload: [networkConfiguration: NetworkConfiguration];
};

/**
* `networkUpdated` is published after a network configuration is updated in the
* network configuration registry and network clients are created as needed.
*/
export type NetworkControllerNetworkUpdatedEvent = {
type: 'NetworkController:networkUpdated';
payload: [networkConfiguration: NetworkConfiguration];
};

/**
* `networkRemoved` is published after a network configuration is removed from the
* network configuration registry and once the network clients have been removed.
*/
export type NetworkControllerNetworkRemovedEvent = {
type: 'NetworkController:networkRemoved';
payload: [networkConfiguration: NetworkConfiguration];
};

export type NetworkControllerEvents =
| NetworkControllerStateChangeEvent
| NetworkControllerNetworkWillChangeEvent
| NetworkControllerNetworkDidChangeEvent
| NetworkControllerInfuraIsBlockedEvent
| NetworkControllerInfuraIsUnblockedEvent
| NetworkControllerNetworkAddedEvent;
| NetworkControllerNetworkAddedEvent
| NetworkControllerNetworkUpdatedEvent
| NetworkControllerNetworkRemovedEvent;

export type NetworkControllerGetStateAction = ControllerGetStateAction<
typeof controllerName,
Expand Down Expand Up @@ -473,6 +493,21 @@ export type NetworkControllerGetNetworkConfigurationByNetworkClientId = {
handler: NetworkController['getNetworkConfigurationByNetworkClientId'];
};

export type NetworkControllerAddNetworkAction = {
type: 'NetworkController:addNetwork';
handler: NetworkController['addNetwork'];
};

export type NetworkControllerUpdateNetworkAction = {
type: 'NetworkController:updateNetwork';
handler: NetworkController['updateNetwork'];
};

export type NetworkControllerRemoveNetworkAction = {
type: 'NetworkController:removeNetwork';
handler: NetworkController['removeNetwork'];
};

export type NetworkControllerActions =
| NetworkControllerGetStateAction
| NetworkControllerGetEthQueryAction
Expand All @@ -483,7 +518,10 @@ export type NetworkControllerActions =
| NetworkControllerSetActiveNetworkAction
| NetworkControllerSetProviderTypeAction
| NetworkControllerGetNetworkConfigurationByChainId
| NetworkControllerGetNetworkConfigurationByNetworkClientId;
| NetworkControllerGetNetworkConfigurationByNetworkClientId
| NetworkControllerAddNetworkAction
| NetworkControllerUpdateNetworkAction
| NetworkControllerRemoveNetworkAction;

export type NetworkControllerMessenger = RestrictedControllerMessenger<
typeof controllerName,
Expand Down Expand Up @@ -954,6 +992,21 @@ export class NetworkController extends BaseController<
`${this.name}:getSelectedNetworkClient`,
this.getSelectedNetworkClient.bind(this),
);

this.messagingSystem.registerActionHandler(
`${this.name}:addNetwork`,

Check failure on line 997 in packages/network-controller/src/NetworkController.ts

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (20.x)

Invalid type "any" of template literal expression
this.addNetwork.bind(this),
);

this.messagingSystem.registerActionHandler(
`${this.name}:updateNetwork`,

Check failure on line 1002 in packages/network-controller/src/NetworkController.ts

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (20.x)

Invalid type "any" of template literal expression
this.updateNetwork.bind(this),
);

this.messagingSystem.registerActionHandler(
`${this.name}:removeNetwork`,

Check failure on line 1007 in packages/network-controller/src/NetworkController.ts

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (20.x)

Invalid type "any" of template literal expression
this.removeNetwork.bind(this),
);
}

/**
Expand Down Expand Up @@ -1881,6 +1934,11 @@ export class NetworkController extends BaseController<
autoManagedNetworkClientRegistry,
});

this.messagingSystem.publish(
'NetworkController:networkUpdated',
updatedNetworkConfiguration,
);

return updatedNetworkConfiguration;
}

Expand Down Expand Up @@ -1939,6 +1997,11 @@ export class NetworkController extends BaseController<
buildNetworkConfigurationsByNetworkClientId(
this.state.networkConfigurationsByChainId,
);

this.messagingSystem.publish(
'NetworkController:networkRemoved',
existingNetworkConfiguration,
);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions packages/network-controller/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export type {
NetworkControllerNetworkDidChangeEvent,
NetworkControllerInfuraIsBlockedEvent,
NetworkControllerInfuraIsUnblockedEvent,
NetworkControllerNetworkAddedEvent,
NetworkControllerNetworkUpdatedEvent,
NetworkControllerNetworkRemovedEvent,
NetworkControllerEvents,
NetworkControllerGetStateAction,
NetworkControllerGetEthQueryAction,
Expand All @@ -26,6 +29,9 @@ export type {
NetworkControllerFindNetworkClientIdByChainIdAction,
NetworkControllerSetProviderTypeAction,
NetworkControllerSetActiveNetworkAction,
NetworkControllerAddNetworkAction,
NetworkControllerUpdateNetworkAction,
NetworkControllerRemoveNetworkAction,
NetworkControllerGetNetworkConfigurationByNetworkClientId,
NetworkControllerActions,
NetworkControllerMessenger,
Expand Down

0 comments on commit 5c85bb9

Please sign in to comment.