@metamask-previews/multichain-network-controller
Version:
Multichain network controller
134 lines • 7.2 kB
JavaScript
"use strict";
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;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MultichainNetworkController = void 0;
const base_controller_1 = require("@metamask/base-controller");
const keyring_api_1 = require("@metamask/keyring-api");
const utils_1 = require("@metamask/utils");
const constants_1 = require("./constants.cjs");
const types_1 = require("./types.cjs");
const utils_2 = require("./utils.cjs");
/**
* The MultichainNetworkController is responsible for fetching and caching account
* balances.
*/
class MultichainNetworkController extends base_controller_1.BaseController {
constructor({ messenger, state, }) {
super({
messenger,
name: types_1.MULTICHAIN_NETWORK_CONTROLLER_NAME,
metadata: constants_1.MULTICHAIN_NETWORK_CONTROLLER_METADATA,
state: {
...(0, constants_1.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 = (0, keyring_api_1.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 = (0, utils_2.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 ((0, utils_1.isCaipChainId)(id)) {
const isSupportedCaipChainId = (0, utils_2.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);
}
}
exports.MultichainNetworkController = MultichainNetworkController;
_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.cjs.map