0

I have a txt file with around 1300 ethereum addreses (1 each line) that I have saved during the years. Now I am running a node since some time and would like to utilize it to automatically check the balance of each address quickly. Of course I can manually import or manually check it but it got to be a way to automate it :D

How can I utilize my ethereum node to use the check balance function, one by one, from wallet.txt, and I guess inform somehow about if the address balance is above 0.

Manually via the geth javascript console I could use

web3.fromWei(web3.eth.getBalance('0xbd3Afb0bB76683eCb4225F9DBc91f998713C3b01'));

Result:

0.483296293327556759

OR via terminal

curl -X POST localhost:8545 \
-H "Content-Type: application/json" \
--data \
'
{
  "jsonrpc": "2.0",
  "method": "eth_getBalance",
  "params": ["0xbd3Afb0bB76683eCb4225F9DBc91f998713C3b01", "latest"],
  "id": 1
}
' 

Result:

{"jsonrpc":"2.0","result":"0x728d934e8054bc7","id":1}

(a hex number which is ok i can convert it manually - if 0 then its 0x0)

But! Is there any way to automate this :D?

Thanks in advance!!

1 Answer 1

0

You can just connect to your geth node and run the same js code in a loop:

const fs = require("fs");
const ethers = require("ethers");

let provider = new ethers.providers.JsonRpcProvider("http://localhost:8545");

let addresses = fs.readFileSync("wallet.txt", "utf-8").split("\n");

addresses = addresses.map(address => address.trim());

async function fetchBalances() {
  for (let address of addresses) {
    if (address.length === 0) continue;
    try {
      let balance = await provider.getBalance(address);
      let balanceInEther = ethers.utils.formatEther(balance);
      if (balanceInEther === "0.0") continue;
      console.log(`Balance of ${address}: ${balanceInEther} Ether`);
    } catch (error) {
      console.log(`Error fetching balance for address ${address}: ${error.message}`);
    }
  }
}

fetchBalances();
1
  • Thanks a lot :) So kind of you! Worked wonderful. Commented Jun 12, 2023 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.