balanceAndNonce.mjs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import {ethers} from "ethers";
  2. import fs from 'fs/promises';
  3. import path from 'path';
  4. import {fileURLToPath} from 'url';
  5. // 使用 ethers.js 的 Provider
  6. const rpcUrl = 'https://testnet.dplabs-internal.com';
  7. const provider = new ethers.JsonRpcProvider(rpcUrl);
  8. // 读取私钥文件
  9. const __filename = fileURLToPath(import.meta.url);
  10. const __dirname = path.dirname(__filename);
  11. const filePath = path.join(__dirname, './AccountList.txt');
  12. async function getBalance(privateKey) {
  13. try {
  14. const wallet = new ethers.Wallet(privateKey, provider);
  15. // 获取账户余额
  16. const balance = await provider.getBalance(wallet.address);
  17. // 将余额从 wei 转换为 ether
  18. const balanceInEther = ethers.formatEther(balance);
  19. console.log(`Address: ${wallet.address}`);
  20. console.log(`Balance: ${balanceInEther} PHRS`);
  21. } catch (error) {
  22. console.error(`Error getting balance for private key: ${privateKey}`);
  23. console.error(error);
  24. }
  25. }
  26. async function getNonces(privateKey, countNum) {
  27. try {
  28. const wallet = new ethers.Wallet(privateKey, provider);
  29. // 获取最新的 nonce
  30. const nonceLatest = await provider.getTransactionCount(wallet.address, 'latest');
  31. const noncePending = await provider.getTransactionCount(wallet.address, 'pending');
  32. console.log(`pending nonce: ${noncePending} ; latest nonce: ${nonceLatest}`);
  33. } catch (error) {
  34. console.error('获取 nonce 时发生错误:', error.message);
  35. }
  36. }
  37. async function main() {
  38. let privateKeys;
  39. try {
  40. const data = await fs.readFile(filePath, 'utf8');
  41. privateKeys = data.split('\n').filter(line => line.trim() !== '');
  42. } catch (err) {
  43. console.error('读取文件时发生错误:', err);
  44. process.exit(1);
  45. }
  46. let countNum = 1;
  47. for (const privateKey of privateKeys) {
  48. await getBalance(privateKey);
  49. await getNonces(privateKey);
  50. console.log('-----------------------------');
  51. countNum++;
  52. }
  53. }
  54. main();