UNPKG

@metamask-previews/multichain-network-controller

Version:
130 lines 7.05 kB
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _MultichainNetworkController_instances, _MultichainNetworkController_setActiveEvmNetwork, _MultichainNetworkController_setActiveNonEvmNetwork, _MultichainNetworkController_handleSelectedAccountChange, _MultichainNetworkController_subscribeToMessageEvents, _MultichainNetworkController_registerMessageHandlers; import { BaseController } from "@metamask/base-controller"; import { isEvmAccountType } from "@metamask/keyring-api"; import { isCaipChainId } from "@metamask/utils"; import { MULTICHAIN_NETWORK_CONTROLLER_METADATA, getDefaultMultichainNetworkControllerState } from "./constants.mjs"; import { MULTICHAIN_NETWORK_CONTROLLER_NAME } from "./types.mjs"; import { checkIfSupportedCaipChainId, getChainIdForNonEvmAddress } from "./utils.mjs"; /** * The MultichainNetworkController is responsible for fetching and caching account * balances. */ export class MultichainNetworkController extends BaseController { constructor({ messenger, state, }) { super({ messenger, name: MULTICHAIN_NETWORK_CONTROLLER_NAME, metadata: MULTICHAIN_NETWORK_CONTROLLER_METADATA, state: { ...getDefaultMultichainNetworkControllerState(), ...state, }, }); _MultichainNetworkController_instances.add(this); /** * Handles switching between EVM and non-EVM networks when an account is changed * * @param account - The account that was changed */ _MultichainNetworkController_handleSelectedAccountChange.set(this, (account) => { const { type: accountType, address: accountAddress } = account; const isEvmAccount = isEvmAccountType(accountType); // Handle switching to EVM network if (isEvmAccount) { if (this.state.isEvmSelected) { // No need to update if already on evm network return; } // Make EVM network active this.update((state) => { state.isEvmSelected = true; }); return; } // Handle switching to non-EVM network const nonEvmChainId = getChainIdForNonEvmAddress(accountAddress); const isSameNonEvmNetwork = nonEvmChainId === this.state.selectedMultichainNetworkChainId; if (isSameNonEvmNetwork) { // No need to update if already on the same non-EVM network this.update((state) => { state.isEvmSelected = false; }); return; } this.update((state) => { state.selectedMultichainNetworkChainId = nonEvmChainId; state.isEvmSelected = false; }); }); __classPrivateFieldGet(this, _MultichainNetworkController_instances, "m", _MultichainNetworkController_subscribeToMessageEvents).call(this); __classPrivateFieldGet(this, _MultichainNetworkController_instances, "m", _MultichainNetworkController_registerMessageHandlers).call(this); } /** * Sets the active network. * * @param id - The non-EVM Caip chain ID or EVM client ID of the network to set active. * @returns - A promise that resolves when the network is set active. */ async setActiveNetwork(id) { if (isCaipChainId(id)) { const isSupportedCaipChainId = checkIfSupportedCaipChainId(id); if (!isSupportedCaipChainId) { throw new Error(`Unsupported Caip chain ID: ${String(id)}`); } return __classPrivateFieldGet(this, _MultichainNetworkController_instances, "m", _MultichainNetworkController_setActiveNonEvmNetwork).call(this, id); } return await __classPrivateFieldGet(this, _MultichainNetworkController_instances, "m", _MultichainNetworkController_setActiveEvmNetwork).call(this, id); } } _MultichainNetworkController_handleSelectedAccountChange = new WeakMap(), _MultichainNetworkController_instances = new WeakSet(), _MultichainNetworkController_setActiveEvmNetwork = /** * Sets the active EVM network. * * @param id - The client ID of the EVM network to set active. */ async function _MultichainNetworkController_setActiveEvmNetwork(id) { // Notify listeners that setActiveNetwork was called this.messagingSystem.publish('MultichainNetworkController:networkDidChange', id); // Indicate that the non-EVM network is not selected this.update((state) => { state.isEvmSelected = true; }); // Prevent setting same network const { selectedNetworkClientId } = this.messagingSystem.call('NetworkController:getState'); if (id === selectedNetworkClientId) { // EVM network is already selected, no need to update NetworkController return; } // Update evm active network await this.messagingSystem.call('NetworkController:setActiveNetwork', id); }, _MultichainNetworkController_setActiveNonEvmNetwork = function _MultichainNetworkController_setActiveNonEvmNetwork(id) { if (id === this.state.selectedMultichainNetworkChainId) { if (!this.state.isEvmSelected) { // Same non-EVM network is already selected, no need to update return; } // Indicate that the non-EVM network is selected this.update((state) => { state.isEvmSelected = false; }); // Notify listeners that setActiveNetwork was called this.messagingSystem.publish('MultichainNetworkController:networkDidChange', id); } // Notify listeners that setActiveNetwork was called this.messagingSystem.publish('MultichainNetworkController:networkDidChange', id); this.update((state) => { state.selectedMultichainNetworkChainId = id; state.isEvmSelected = false; }); }, _MultichainNetworkController_subscribeToMessageEvents = function _MultichainNetworkController_subscribeToMessageEvents() { // Handle network switch when account is changed this.messagingSystem.subscribe('AccountsController:selectedAccountChange', __classPrivateFieldGet(this, _MultichainNetworkController_handleSelectedAccountChange, "f")); }, _MultichainNetworkController_registerMessageHandlers = function _MultichainNetworkController_registerMessageHandlers() { this.messagingSystem.registerActionHandler('MultichainNetworkController:setActiveNetwork', this.setActiveNetwork.bind(this)); }; //# sourceMappingURL=MultichainNetworkController.mjs.map