fix: the address screen shows a token just added at its fetched balance, not 0.0000 (closes #516)
A balance refresh that returns now draws the balances on AddressDetail or AddressToken again when that screen is up, not only the wallet list. Their transaction lists are left alone. Every add path on AddToken and SettingsAddToken tracks a token through trackToken(), which gives the tracked record `balanceNotFetched: true`. While the field is set, the token's line reads `quantity unknown` instead of 0.0000. The popup's refresh deletes the field once it has read the token's balance at every address. refreshBalances() now resolves with those tokens. The field is part of the tracked record, so removing the token removes it, and each network keeps its own records. Model: opus-5-5
This commit is contained in:
+16
-1
@@ -88,6 +88,14 @@ let refreshTimer = null;
|
||||
// removed meanwhile is left as it is. Blockscout may have answered before the
|
||||
// user turned "Use Blockscout" off. So the save drops every token the user has
|
||||
// not tracked if the stored setting is off by then.
|
||||
//
|
||||
// The save also deletes `balanceNotFetched` from each tracked token whose
|
||||
// balance this refresh read at every address. trackToken() in
|
||||
// src/popup/views/helpers.js sets that field.
|
||||
//
|
||||
// Once the save is done, the wallet list is drawn again. So are the balances
|
||||
// on AddressDetail or AddressToken when that screen is up. No other screen is
|
||||
// drawn again, DeleteAddress and its balance lines included.
|
||||
async function doRefreshAndRender() {
|
||||
if (refreshInFlight) return;
|
||||
refreshInFlight = true;
|
||||
@@ -95,7 +103,7 @@ async function doRefreshAndRender() {
|
||||
await followStoredSettings();
|
||||
const networkId = state.networkId;
|
||||
const wallets = structuredClone(state.wallets);
|
||||
await Promise.all([
|
||||
const [, readTokens] = await Promise.all([
|
||||
refreshPrices(),
|
||||
refreshBalances(
|
||||
wallets,
|
||||
@@ -127,10 +135,17 @@ async function doRefreshAndRender() {
|
||||
if (!stored.useBlockscout) {
|
||||
dropUntrackedTokens(state.wallets, stored.trackedTokens);
|
||||
}
|
||||
for (const token of state.trackedTokens) {
|
||||
if (readTokens.includes(token.address.toLowerCase())) {
|
||||
delete token.balanceNotFetched;
|
||||
}
|
||||
}
|
||||
state.lastBalanceRefresh = Date.now();
|
||||
return true;
|
||||
});
|
||||
renderWalletList();
|
||||
if (state.currentView === "address") addressDetail.renderBalances();
|
||||
if (state.currentView === "address-token") addressToken.renderBalance();
|
||||
} catch (e) {
|
||||
// Every call site fires this and walks away — the boot below, the ten
|
||||
// second interval, and eight views through ctx — so it must never
|
||||
|
||||
@@ -1,4 +1,11 @@
|
||||
const { $, showView, showFlash, escapeHtml, goBack } = require("./helpers");
|
||||
const {
|
||||
$,
|
||||
showView,
|
||||
showFlash,
|
||||
escapeHtml,
|
||||
goBack,
|
||||
trackToken,
|
||||
} = require("./helpers");
|
||||
const { getTopTokens } = require("../../shared/tokenList");
|
||||
const { state, saveState, currentNetwork } = require("../../shared/state");
|
||||
const { lookupTokenInfo } = require("../../shared/balances");
|
||||
@@ -72,7 +79,7 @@ function init(ctx) {
|
||||
return;
|
||||
}
|
||||
log.infof("Adding token", info.symbol, contractAddr);
|
||||
state.trackedTokens.push({
|
||||
trackToken({
|
||||
address: contractAddr,
|
||||
symbol: info.symbol,
|
||||
decimals: info.decimals,
|
||||
|
||||
@@ -66,11 +66,27 @@ function show() {
|
||||
});
|
||||
$("address-line").dataset.full = addr.address;
|
||||
attachCopyHandlers($("address-line"));
|
||||
const usdTotal = formatAddressTotal(getAddressValue(addr));
|
||||
$("address-usd-total").innerHTML = escapeHtml(usdTotal) || " ";
|
||||
const ensEl = $("address-ens");
|
||||
// ENS is now shown inside renderAddressHtml, hide the separate element
|
||||
ensEl.classList.add("hidden");
|
||||
renderBalances();
|
||||
// Only Blockscout reports the tokens the user has not tracked.
|
||||
$("address-tokens-note").classList.toggle("hidden", !!state.useBlockscout);
|
||||
renderSendTokenSelect(addr);
|
||||
$("tx-list").innerHTML =
|
||||
'<div class="text-muted text-xs py-1">Loading...</div>';
|
||||
showView("address");
|
||||
loadTransactions(addr.address);
|
||||
}
|
||||
|
||||
// The USD total and the balance list. Drawn again by each balance refresh
|
||||
// that returns while this screen is up (doRefreshAndRender() in
|
||||
// src/popup/index.js), which leaves the rest of the screen as it is.
|
||||
function renderBalances() {
|
||||
const addr =
|
||||
state.wallets[state.selectedWallet].addresses[state.selectedAddress];
|
||||
const usdTotal = formatAddressTotal(getAddressValue(addr));
|
||||
$("address-usd-total").innerHTML = escapeHtml(usdTotal) || " ";
|
||||
$("address-balances").innerHTML = balanceLinesForAddress(
|
||||
addr,
|
||||
state.trackedTokens,
|
||||
@@ -84,13 +100,6 @@ function show() {
|
||||
ctx.showAddressToken();
|
||||
});
|
||||
});
|
||||
// Only Blockscout reports the tokens the user has not tracked.
|
||||
$("address-tokens-note").classList.toggle("hidden", !!state.useBlockscout);
|
||||
renderSendTokenSelect(addr);
|
||||
$("tx-list").innerHTML =
|
||||
'<div class="text-muted text-xs py-1">Loading...</div>';
|
||||
showView("address");
|
||||
loadTransactions(addr.address);
|
||||
}
|
||||
|
||||
let loadedTxs = [];
|
||||
@@ -287,4 +296,4 @@ function init(_ctx) {
|
||||
exportPrivkey.init();
|
||||
}
|
||||
|
||||
module.exports = { init, show };
|
||||
module.exports = { init, show, renderBalances };
|
||||
|
||||
@@ -49,37 +49,18 @@ function show() {
|
||||
const addr = wallet.addresses[state.selectedAddress];
|
||||
const ai = state.selectedAddress;
|
||||
const tokenId = state.selectedToken;
|
||||
|
||||
// Determine token symbol and balance
|
||||
let symbol, amount, price;
|
||||
const knownToken = TOKEN_BY_ADDRESS.get(tokenId.toLowerCase());
|
||||
if (tokenId === "ETH") {
|
||||
symbol = nativeCurrency();
|
||||
amount = parseFloat(addr.balance || "0");
|
||||
price = getPrice("ETH");
|
||||
} else {
|
||||
const tb = (addr.tokenBalances || []).find(
|
||||
(t) => t.address.toLowerCase() === tokenId.toLowerCase(),
|
||||
);
|
||||
symbol = resolveSymbol(
|
||||
tokenId,
|
||||
addr.tokenBalances,
|
||||
state.trackedTokens,
|
||||
);
|
||||
// null when the scale is unknown: no quantity to show, and none to
|
||||
// price. balanceLine() states that rather than printing 0.0000.
|
||||
amount = tb ? unknownableAmount(tb.balance) : 0;
|
||||
price = getPrice(symbol);
|
||||
}
|
||||
|
||||
currentSymbol = symbol;
|
||||
currentSymbol =
|
||||
tokenId === "ETH"
|
||||
? nativeCurrency()
|
||||
: resolveSymbol(tokenId, addr.tokenBalances, state.trackedTokens);
|
||||
|
||||
$("address-token-title").textContent =
|
||||
wallet.name +
|
||||
" \u2014 Address " +
|
||||
(ai + 1) +
|
||||
" \u2014 " +
|
||||
displaySymbol(symbol);
|
||||
displaySymbol(currentSymbol);
|
||||
|
||||
// Blockie
|
||||
const blockieEl = $("address-token-jazzicon");
|
||||
@@ -101,13 +82,7 @@ function show() {
|
||||
$("address-token-line").dataset.full = addr.address;
|
||||
attachCopyHandlers($("address-token-line"));
|
||||
|
||||
// USD total for this token only
|
||||
const usdVal = price && amount !== null ? amount * price : null;
|
||||
const usdStr = formatUsd(usdVal);
|
||||
$("address-token-usd-total").innerHTML = escapeHtml(usdStr) || " ";
|
||||
|
||||
// Single token balance line (no tokenId — not clickable here)
|
||||
$("address-token-balance").innerHTML = balanceLine(symbol, amount, price);
|
||||
renderBalance();
|
||||
|
||||
// Token contract details (ERC-20 only)
|
||||
const contractInfo = $("address-token-contract-info");
|
||||
@@ -169,6 +144,47 @@ function show() {
|
||||
loadTransactions(addr.address, tokenId);
|
||||
}
|
||||
|
||||
// The USD total and the balance line of the selected token. Drawn again by
|
||||
// each balance refresh that returns while this screen is up
|
||||
// (doRefreshAndRender() in src/popup/index.js), which leaves the rest of the
|
||||
// screen as it is.
|
||||
function renderBalance() {
|
||||
const addr =
|
||||
state.wallets[state.selectedWallet].addresses[state.selectedAddress];
|
||||
const tokenId = state.selectedToken;
|
||||
let amount, price;
|
||||
if (tokenId === "ETH") {
|
||||
amount = parseFloat(addr.balance || "0");
|
||||
price = getPrice("ETH");
|
||||
} else {
|
||||
const id = tokenId.toLowerCase();
|
||||
const tb = (addr.tokenBalances || []).find(
|
||||
(t) => t.address.toLowerCase() === id,
|
||||
);
|
||||
const tracked = state.trackedTokens.find(
|
||||
(t) => t.address.toLowerCase() === id,
|
||||
);
|
||||
// null when the scale is unknown, or when no refresh has read the
|
||||
// balance of a token just added: no quantity to show, and none to
|
||||
// price. balanceLine() states that rather than printing 0.0000.
|
||||
if (tb) amount = unknownableAmount(tb.balance);
|
||||
else amount = tracked && tracked.balanceNotFetched ? null : 0;
|
||||
price = getPrice(currentSymbol);
|
||||
}
|
||||
|
||||
// USD total for this token only
|
||||
const usdVal = price && amount !== null ? amount * price : null;
|
||||
const usdStr = formatUsd(usdVal);
|
||||
$("address-token-usd-total").innerHTML = escapeHtml(usdStr) || " ";
|
||||
|
||||
// Single token balance line (no tokenId — not clickable here)
|
||||
$("address-token-balance").innerHTML = balanceLine(
|
||||
currentSymbol,
|
||||
amount,
|
||||
price,
|
||||
);
|
||||
}
|
||||
|
||||
async function loadTransactions(address, tokenId) {
|
||||
try {
|
||||
const rawTxs = await fetchRecentTransactions(
|
||||
@@ -348,4 +364,4 @@ function init(_ctx) {
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { init, show };
|
||||
module.exports = { init, show, renderBalance };
|
||||
|
||||
@@ -417,6 +417,15 @@ function balanceLine(symbol, amount, price, tokenId) {
|
||||
);
|
||||
}
|
||||
|
||||
// Tracks `token` on the active network, as every add path on AddToken and
|
||||
// SettingsAddToken does. The tracked record carries `balanceNotFetched: true`
|
||||
// until a balance refresh has read the token's balance at every address
|
||||
// (doRefreshAndRender() in src/popup/index.js). Until then, an address with no
|
||||
// balance row for the token shows `quantity unknown` for it, not 0.0000.
|
||||
function trackToken(token) {
|
||||
state.trackedTokens.push({ ...token, balanceNotFetched: true });
|
||||
}
|
||||
|
||||
function balanceLinesForAddress(addr, trackedTokens, showZero) {
|
||||
let html = balanceLine(
|
||||
nativeCurrency(),
|
||||
@@ -449,7 +458,7 @@ function balanceLinesForAddress(addr, trackedTokens, showZero) {
|
||||
if (seen.has(t.address.toLowerCase())) continue;
|
||||
html += balanceLine(
|
||||
t.symbol,
|
||||
0,
|
||||
t.balanceNotFetched ? null : 0,
|
||||
getPrice(t.symbol),
|
||||
t.address.toLowerCase(),
|
||||
);
|
||||
@@ -829,6 +838,7 @@ module.exports = {
|
||||
flashCopyFeedback,
|
||||
balanceLine,
|
||||
balanceLinesForAddress,
|
||||
trackToken,
|
||||
addressHoldsFunds,
|
||||
keyMaterialName,
|
||||
unknownableAmount,
|
||||
|
||||
@@ -1,4 +1,11 @@
|
||||
const { $, showView, showFlash, escapeHtml, goBack } = require("./helpers");
|
||||
const {
|
||||
$,
|
||||
showView,
|
||||
showFlash,
|
||||
escapeHtml,
|
||||
goBack,
|
||||
trackToken,
|
||||
} = require("./helpers");
|
||||
const { getTopTokens } = require("../../shared/tokenList");
|
||||
const { state, saveState, currentNetwork } = require("../../shared/state");
|
||||
const { lookupTokenInfo } = require("../../shared/balances");
|
||||
@@ -43,7 +50,7 @@ function renderTop10() {
|
||||
decimals: parseInt(btn.dataset.decimals, 10),
|
||||
name: btn.dataset.name || btn.dataset.symbol,
|
||||
};
|
||||
state.trackedTokens.push(token);
|
||||
trackToken(token);
|
||||
await saveState();
|
||||
showFlash("Added " + token.symbol);
|
||||
renderTop10();
|
||||
@@ -110,7 +117,7 @@ function init(_ctx) {
|
||||
decimals: parseInt(opt.dataset.decimals, 10),
|
||||
name: opt.dataset.name || opt.dataset.symbol,
|
||||
};
|
||||
state.trackedTokens.push(token);
|
||||
trackToken(token);
|
||||
await saveState();
|
||||
showFlash("Added " + token.symbol);
|
||||
renderTop10();
|
||||
@@ -156,7 +163,7 @@ function init(_ctx) {
|
||||
return;
|
||||
}
|
||||
log.infof("Adding token", info.symbol, addr);
|
||||
state.trackedTokens.push({
|
||||
trackToken({
|
||||
address: addr,
|
||||
symbol: info.symbol,
|
||||
decimals: info.decimals,
|
||||
|
||||
Reference in New Issue
Block a user