Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add NetworkController NetworkConfiguration actions and events #4698

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 71 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,27 @@ export class NetworkController extends BaseController<
`${this.name}:getSelectedNetworkClient`,
this.getSelectedNetworkClient.bind(this),
);

this.messagingSystem.registerActionHandler(
// ESLint is mistaken here; `name` is a string.
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`${this.name}:addNetwork`,
this.addNetwork.bind(this),
);

this.messagingSystem.registerActionHandler(
// ESLint is mistaken here; `name` is a string.
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`${this.name}:updateNetwork`,
this.updateNetwork.bind(this),
);

this.messagingSystem.registerActionHandler(
// ESLint is mistaken here; `name` is a string.
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`${this.name}:removeNetwork`,
this.removeNetwork.bind(this),
);
}

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

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

return updatedNetworkConfiguration;
}

Expand Down Expand Up @@ -1939,6 +2003,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
Loading