|
1 | 1 | import type {
|
2 | 2 | ControllerGetStateAction,
|
3 | 3 | ControllerStateChangeEvent,
|
| 4 | + MessengerMethodAction, |
4 | 5 | RestrictedMessenger,
|
5 | 6 | } from '@metamask/base-controller';
|
6 |
| -import { BaseController } from '@metamask/base-controller'; |
| 7 | +import { |
| 8 | + BaseController, |
| 9 | + registerMethodActionHandlers, |
| 10 | +} from '@metamask/base-controller'; |
7 | 11 | import type { Partialize } from '@metamask/controller-utils';
|
8 | 12 | import {
|
9 | 13 | InfuraNetworkType,
|
@@ -515,87 +519,30 @@ export type NetworkControllerGetEthQueryAction = {
|
515 | 519 | handler: () => EthQuery | undefined;
|
516 | 520 | };
|
517 | 521 |
|
518 |
| -export type NetworkControllerGetNetworkClientByIdAction = { |
519 |
| - type: `NetworkController:getNetworkClientById`; |
520 |
| - handler: NetworkController['getNetworkClientById']; |
521 |
| -}; |
522 |
| - |
523 |
| -export type NetworkControllerGetSelectedNetworkClientAction = { |
524 |
| - type: `NetworkController:getSelectedNetworkClient`; |
525 |
| - handler: NetworkController['getSelectedNetworkClient']; |
526 |
| -}; |
527 |
| - |
528 |
| -export type NetworkControllerGetSelectedChainIdAction = { |
529 |
| - type: 'NetworkController:getSelectedChainId'; |
530 |
| - handler: NetworkController['getSelectedChainId']; |
531 |
| -}; |
532 |
| - |
533 |
| -export type NetworkControllerGetEIP1559CompatibilityAction = { |
534 |
| - type: `NetworkController:getEIP1559Compatibility`; |
535 |
| - handler: NetworkController['getEIP1559Compatibility']; |
536 |
| -}; |
537 |
| - |
538 |
| -export type NetworkControllerFindNetworkClientIdByChainIdAction = { |
539 |
| - type: `NetworkController:findNetworkClientIdByChainId`; |
540 |
| - handler: NetworkController['findNetworkClientIdByChainId']; |
541 |
| -}; |
542 |
| - |
543 |
| -/** |
544 |
| - * Change the currently selected network to the given built-in network type. |
545 |
| - * |
546 |
| - * @deprecated This action has been replaced by `setActiveNetwork`, and will be |
547 |
| - * removed in a future release. |
548 |
| - */ |
549 |
| -export type NetworkControllerSetProviderTypeAction = { |
550 |
| - type: `NetworkController:setProviderType`; |
551 |
| - handler: NetworkController['setProviderType']; |
552 |
| -}; |
553 |
| - |
554 |
| -export type NetworkControllerSetActiveNetworkAction = { |
555 |
| - type: `NetworkController:setActiveNetwork`; |
556 |
| - handler: NetworkController['setActiveNetwork']; |
557 |
| -}; |
558 |
| - |
559 |
| -export type NetworkControllerGetNetworkConfigurationByChainId = { |
560 |
| - type: `NetworkController:getNetworkConfigurationByChainId`; |
561 |
| - handler: NetworkController['getNetworkConfigurationByChainId']; |
562 |
| -}; |
563 |
| - |
564 |
| -export type NetworkControllerGetNetworkConfigurationByNetworkClientId = { |
565 |
| - type: `NetworkController:getNetworkConfigurationByNetworkClientId`; |
566 |
| - handler: NetworkController['getNetworkConfigurationByNetworkClientId']; |
567 |
| -}; |
568 |
| - |
569 |
| -export type NetworkControllerAddNetworkAction = { |
570 |
| - type: 'NetworkController:addNetwork'; |
571 |
| - handler: NetworkController['addNetwork']; |
572 |
| -}; |
573 |
| - |
574 |
| -export type NetworkControllerRemoveNetworkAction = { |
575 |
| - type: 'NetworkController:removeNetwork'; |
576 |
| - handler: NetworkController['removeNetwork']; |
577 |
| -}; |
578 |
| - |
579 |
| -export type NetworkControllerUpdateNetworkAction = { |
580 |
| - type: 'NetworkController:updateNetwork'; |
581 |
| - handler: NetworkController['updateNetwork']; |
582 |
| -}; |
| 522 | +const MESSENGER_EXPOSED_METHODS = [ |
| 523 | + 'getNetworkClientById', |
| 524 | + 'getSelectedNetworkClient', |
| 525 | + 'getSelectedChainId', |
| 526 | + 'getEIP1559Compatibility', |
| 527 | + 'findNetworkClientIdByChainId', |
| 528 | + 'setProviderType', |
| 529 | + 'setActiveNetwork', |
| 530 | + 'getNetworkConfigurationByChainId', |
| 531 | + 'getNetworkConfigurationByNetworkClientId', |
| 532 | + 'addNetwork', |
| 533 | + 'removeNetwork', |
| 534 | + 'updateNetwork', |
| 535 | +] as const; |
| 536 | + |
| 537 | +export type NetworkControllerMethodAction = MessengerMethodAction< |
| 538 | + NetworkController, |
| 539 | + (typeof MESSENGER_EXPOSED_METHODS)[number] |
| 540 | +>; |
583 | 541 |
|
584 | 542 | export type NetworkControllerActions =
|
585 | 543 | | NetworkControllerGetStateAction
|
586 | 544 | | NetworkControllerGetEthQueryAction
|
587 |
| - | NetworkControllerGetNetworkClientByIdAction |
588 |
| - | NetworkControllerGetSelectedNetworkClientAction |
589 |
| - | NetworkControllerGetSelectedChainIdAction |
590 |
| - | NetworkControllerGetEIP1559CompatibilityAction |
591 |
| - | NetworkControllerFindNetworkClientIdByChainIdAction |
592 |
| - | NetworkControllerSetActiveNetworkAction |
593 |
| - | NetworkControllerSetProviderTypeAction |
594 |
| - | NetworkControllerGetNetworkConfigurationByChainId |
595 |
| - | NetworkControllerGetNetworkConfigurationByNetworkClientId |
596 |
| - | NetworkControllerAddNetworkAction |
597 |
| - | NetworkControllerRemoveNetworkAction |
598 |
| - | NetworkControllerUpdateNetworkAction; |
| 545 | + | NetworkControllerMethodAction; |
599 | 546 |
|
600 | 547 | /**
|
601 | 548 | * All actions that {@link NetworkController} calls internally.
|
@@ -1228,93 +1175,10 @@ export class NetworkController extends BaseController<
|
1228 | 1175 | this.state.networkConfigurationsByChainId,
|
1229 | 1176 | );
|
1230 | 1177 |
|
1231 |
| - this.messagingSystem.registerActionHandler( |
1232 |
| - // TODO: Either fix this lint violation or explain why it's necessary to ignore. |
1233 |
| - // eslint-disable-next-line @typescript-eslint/restrict-template-expressions |
1234 |
| - `${this.name}:getEthQuery`, |
1235 |
| - () => { |
1236 |
| - return this.#ethQuery; |
1237 |
| - }, |
1238 |
| - ); |
1239 |
| - |
1240 |
| - this.messagingSystem.registerActionHandler( |
1241 |
| - // TODO: Either fix this lint violation or explain why it's necessary to ignore. |
1242 |
| - // eslint-disable-next-line @typescript-eslint/restrict-template-expressions |
1243 |
| - `${this.name}:getNetworkClientById`, |
1244 |
| - this.getNetworkClientById.bind(this), |
1245 |
| - ); |
1246 |
| - |
1247 |
| - this.messagingSystem.registerActionHandler( |
1248 |
| - // TODO: Either fix this lint violation or explain why it's necessary to ignore. |
1249 |
| - // eslint-disable-next-line @typescript-eslint/restrict-template-expressions |
1250 |
| - `${this.name}:getEIP1559Compatibility`, |
1251 |
| - this.getEIP1559Compatibility.bind(this), |
1252 |
| - ); |
1253 |
| - |
1254 |
| - this.messagingSystem.registerActionHandler( |
1255 |
| - // TODO: Either fix this lint violation or explain why it's necessary to ignore. |
1256 |
| - // eslint-disable-next-line @typescript-eslint/restrict-template-expressions |
1257 |
| - `${this.name}:setActiveNetwork`, |
1258 |
| - this.setActiveNetwork.bind(this), |
1259 |
| - ); |
1260 |
| - |
1261 |
| - this.messagingSystem.registerActionHandler( |
1262 |
| - // TODO: Either fix this lint violation or explain why it's necessary to ignore. |
1263 |
| - // eslint-disable-next-line @typescript-eslint/restrict-template-expressions |
1264 |
| - `${this.name}:setProviderType`, |
1265 |
| - this.setProviderType.bind(this), |
1266 |
| - ); |
1267 |
| - |
1268 |
| - this.messagingSystem.registerActionHandler( |
1269 |
| - // TODO: Either fix this lint violation or explain why it's necessary to ignore. |
1270 |
| - // eslint-disable-next-line @typescript-eslint/restrict-template-expressions |
1271 |
| - `${this.name}:findNetworkClientIdByChainId`, |
1272 |
| - this.findNetworkClientIdByChainId.bind(this), |
1273 |
| - ); |
1274 |
| - |
1275 |
| - this.messagingSystem.registerActionHandler( |
1276 |
| - // TODO: Either fix this lint violation or explain why it's necessary to ignore. |
1277 |
| - // eslint-disable-next-line @typescript-eslint/restrict-template-expressions |
1278 |
| - `${this.name}:getNetworkConfigurationByChainId`, |
1279 |
| - this.getNetworkConfigurationByChainId.bind(this), |
1280 |
| - ); |
1281 |
| - |
1282 |
| - this.messagingSystem.registerActionHandler( |
1283 |
| - // ESLint is mistaken here; `name` is a string. |
1284 |
| - // eslint-disable-next-line @typescript-eslint/restrict-template-expressions |
1285 |
| - `${this.name}:getNetworkConfigurationByNetworkClientId`, |
1286 |
| - this.getNetworkConfigurationByNetworkClientId.bind(this), |
1287 |
| - ); |
1288 |
| - |
1289 |
| - this.messagingSystem.registerActionHandler( |
1290 |
| - `${this.name}:getSelectedNetworkClient`, |
1291 |
| - this.getSelectedNetworkClient.bind(this), |
1292 |
| - ); |
1293 |
| - |
1294 |
| - this.messagingSystem.registerActionHandler( |
1295 |
| - `${this.name}:getSelectedChainId`, |
1296 |
| - this.getSelectedChainId.bind(this), |
1297 |
| - ); |
1298 |
| - |
1299 |
| - this.messagingSystem.registerActionHandler( |
1300 |
| - // ESLint is mistaken here; `name` is a string. |
1301 |
| - // eslint-disable-next-line @typescript-eslint/restrict-template-expressions |
1302 |
| - `${this.name}:addNetwork`, |
1303 |
| - this.addNetwork.bind(this), |
1304 |
| - ); |
1305 |
| - |
1306 |
| - this.messagingSystem.registerActionHandler( |
1307 |
| - // ESLint is mistaken here; `name` is a string. |
1308 |
| - // eslint-disable-next-line @typescript-eslint/restrict-template-expressions |
1309 |
| - `${this.name}:removeNetwork`, |
1310 |
| - this.removeNetwork.bind(this), |
1311 |
| - ); |
1312 |
| - |
1313 |
| - this.messagingSystem.registerActionHandler( |
1314 |
| - // ESLint is mistaken here; `name` is a string. |
1315 |
| - // eslint-disable-next-line @typescript-eslint/restrict-template-expressions |
1316 |
| - `${this.name}:updateNetwork`, |
1317 |
| - this.updateNetwork.bind(this), |
| 1178 | + registerMethodActionHandlers( |
| 1179 | + this, |
| 1180 | + this.messagingSystem, |
| 1181 | + MESSENGER_EXPOSED_METHODS, |
1318 | 1182 | );
|
1319 | 1183 | }
|
1320 | 1184 |
|
|
0 commit comments