// Consolidated chain-switch handler for the popup. // // Every state change required when the active network changes is // performed here so that callers (settings UI, future chain additions) all go // through a single code path. // // Adding a new chain (e.g. ETC) requires only a new entry in // networks.js — no per-caller wiring is needed. // // The background does NOT come through here: this function mutates the // module-level `state` singleton, which the MV3 service worker never // populates, and a background switch performed on it wrote DEFAULT_STATE over // the user's whole profile // (https://git.eeqj.de/sneak/AutistMask/issues/316). The field mutations // themselves live in chainSwitchFields.js, which takes the record to mutate as // an argument; src/background/state.js applies them inside a read-modify-write // against storage, and the singleton is not reachable from the background // bundle at all (enforced by the ESLint rule in eslint.config.js). const { applyChainSwitchFields } = require("./chainSwitchFields"); const { clearPrices } = require("./prices"); // Switch the active chain and reset all chain-specific cached state. // Returns the network configuration object for the new chain. async function onChainSwitch(newNetworkId) { const { state, saveState } = require("./state"); const net = applyChainSwitchFields(state, newNetworkId); // --- price cache --- // Prices are chain-specific (testnet tokens are worthless, // ETC has different pricing, etc.). In-memory and per bundle, so this is // the popup's own cache — the only context that ever fills it. clearPrices(); await saveState(); return net; } module.exports = { onChainSwitch };