nonce.mjs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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-rpc.monad.xyz';
  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 getNonces(privateKeys) {
  13. let countNum = 1;
  14. for (const privateKey of privateKeys) {
  15. try {
  16. const wallet = new ethers.Wallet(privateKey, provider);
  17. //获取最新的 nonce
  18. const nonceLatest = await provider.getTransactionCount(wallet, 'latest');
  19. const noncePending = await provider.getTransactionCount(wallet, 'pending');
  20. const countNumStr = String(countNum).padStart(2, '0');
  21. console.log(`${countNumStr} address: ${wallet.address}`);
  22. console.log(`pending nonce: ${noncePending} ; latest nonce: ${nonceLatest}`);
  23. console.log('-----------------------------');
  24. countNum++;
  25. } catch (error) {
  26. console.error('获取 nonce 时发生错误:', error.message);
  27. }
  28. }
  29. }
  30. async function main() {
  31. try {
  32. const data = await fs.readFile(filePath, 'utf8');
  33. const privateKeys = data.split('\n').filter(line => line.trim() !== '');
  34. await getNonces(privateKeys);
  35. } catch (err) {
  36. console.error('读取文件时发生错误:', err);
  37. process.exit(1);
  38. }
  39. }
  40. main();