Fetch real ETH balances from RPC on popup open
All checks were successful
check / check (push) Successful in 13s
All checks were successful
check / check (push) Successful in 13s
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.
This commit is contained in:
@@ -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 --
|
||||
|
||||
Reference in New Issue
Block a user