From 0b102f49c2cd18ecf0deb74a9952dde3b836849a Mon Sep 17 00:00:00 2001 From: sneak Date: Wed, 25 Feb 2026 17:01:33 +0700 Subject: [PATCH] Fetch real ETH balances from RPC on popup open Uses ethers JsonRpcProvider to call eth_getBalance for every address on popup open. Balances update in the background and re-render the wallet list when done. Default RPC is eth.llamarpc.com, configurable in settings. --- src/popup/index.js | 47 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/popup/index.js b/src/popup/index.js index e349e35..e658f15 100644 --- a/src/popup/index.js +++ b/src/popup/index.js @@ -1,5 +1,11 @@ // AutistMask popup UI — view management and event wiring -const { Mnemonic, HDNodeWallet, Wallet } = require("ethers"); +const { + Mnemonic, + HDNodeWallet, + Wallet, + JsonRpcProvider, + formatEther, +} = require("ethers"); const DEBUG = true; const DEBUG_MNEMONIC = @@ -113,6 +119,41 @@ function addressFromPrivateKey(key) { return w.address; } +// -- balance fetching -- +function getProvider() { + return new JsonRpcProvider(state.rpcUrl); +} + +function formatBalance(wei) { + const eth = formatEther(wei); + // Show up to 6 decimal places, trim trailing zeros + const parts = eth.split("."); + if (parts.length === 1) return eth + ".0"; + const dec = parts[1].slice(0, 6).replace(/0+$/, "") || "0"; + return parts[0] + "." + dec; +} + +async function refreshBalances() { + const provider = getProvider(); + const updates = []; + for (const wallet of state.wallets) { + for (const addr of wallet.addresses) { + updates.push( + provider + .getBalance(addr.address) + .then((bal) => { + addr.balance = formatBalance(bal); + }) + .catch(() => { + // leave balance unchanged on error + }), + ); + } + } + await Promise.all(updates); + await saveState(); +} + // -- render wallet list on main view -- function renderWalletList() { const container = $("wallet-list"); @@ -268,6 +309,10 @@ async function init() { } else { renderWalletList(); showView("main"); + // Fetch balances in the background, re-render when done + refreshBalances().then(() => { + renderWalletList(); + }); } // -- Welcome --