fix: suppress USD display on testnet networks
All checks were successful
check / check (push) Successful in 13s

When connected to a testnet (e.g. Sepolia), stale mainnet prices from
the in-memory cache caused USD values to display even though
refreshPrices() correctly skipped fetching. Three fixes applied:

- refreshPrices() now clears the price cache when on a testnet instead
  of silently returning, removing any stale mainnet prices
- getPrice(), getAddressValueUsd(), getWalletValueUsd(), and
  getTotalValueUsd() all return null when the current network is a
  testnet, as defense-in-depth
- The settings network switcher immediately clears prices when
  switching to a testnet, so the UI updates without waiting for the
  next refresh cycle

closes #139
This commit is contained in:
user 2026-03-01 11:14:39 -08:00
parent e53420f2e2
commit 57de01b546
2 changed files with 24 additions and 2 deletions

View File

@ -2,6 +2,7 @@ const { $, showView, showFlash, escapeHtml } = require("./helpers");
const { applyTheme } = require("../theme");
const { state, saveState, currentNetwork } = require("../../shared/state");
const { NETWORKS, SUPPORTED_CHAIN_IDS } = require("../../shared/networks");
const { clearPrices } = require("../../shared/prices");
const { log, debugFetch } = require("../../shared/log");
const deleteWallet = require("./deleteWallet");
@ -227,6 +228,7 @@ function init(ctx) {
state.blockscoutUrl = net.defaultBlockscoutUrl;
$("settings-rpc").value = state.rpcUrl;
$("settings-blockscout").value = state.blockscoutUrl;
if (net.isTestnet) clearPrices();
await saveState();
showFlash("Switched to " + net.name + ".");
});

View File

@ -8,9 +8,13 @@ const prices = {};
let lastFetchedAt = 0;
async function refreshPrices() {
// Testnet tokens have no real market value — skip price fetching.
// Testnet tokens have no real market value — skip price fetching
// and clear any stale mainnet prices so the UI shows no USD values.
const { currentNetwork } = require("./state");
if (currentNetwork().isTestnet) return;
if (currentNetwork().isTestnet) {
clearPrices();
return;
}
const now = Date.now();
if (now - lastFetchedAt < PRICE_CACHE_TTL) return;
try {
@ -22,7 +26,16 @@ async function refreshPrices() {
}
}
function clearPrices() {
for (const key of Object.keys(prices)) {
delete prices[key];
}
lastFetchedAt = 0;
}
function getPrice(symbol) {
const { currentNetwork } = require("./state");
if (currentNetwork().isTestnet) return null;
return prices[symbol] || null;
}
@ -40,6 +53,8 @@ function formatUsd(amount) {
}
function getAddressValueUsd(addr) {
const { currentNetwork } = require("./state");
if (currentNetwork().isTestnet) return null;
if (!prices.ETH) return null;
let total = 0;
const ethBal = parseFloat(addr.balance || "0");
@ -54,6 +69,8 @@ function getAddressValueUsd(addr) {
}
function getWalletValueUsd(wallet) {
const { currentNetwork } = require("./state");
if (currentNetwork().isTestnet) return null;
if (!prices.ETH) return null;
let total = 0;
for (const addr of wallet.addresses) {
@ -63,6 +80,8 @@ function getWalletValueUsd(wallet) {
}
function getTotalValueUsd(wallets) {
const { currentNetwork } = require("./state");
if (currentNetwork().isTestnet) return null;
if (!prices.ETH) return null;
let total = 0;
for (const wallet of wallets) {
@ -74,6 +93,7 @@ function getTotalValueUsd(wallets) {
module.exports = {
prices,
refreshPrices,
clearPrices,
getPrice,
formatUsd,
getAddressValueUsd,