| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- import {ethers} from "ethers";
- import fs from 'fs/promises';
- import path from 'path';
- import {fileURLToPath} from 'url';
- // 使用 ethers.js 的 Provider
- const rpcUrl = 'https://testnet-rpc.monad.xyz';
- const provider = new ethers.JsonRpcProvider(rpcUrl);
- // 读取私钥文件
- const __filename = fileURLToPath(import.meta.url);
- const __dirname = path.dirname(__filename);
- const filePath = path.join(__dirname, './AccountList.txt');
- async function getNonces(privateKeys) {
- let countNum = 1;
- for (const privateKey of privateKeys) {
- try {
- const wallet = new ethers.Wallet(privateKey, provider);
- //获取最新的 nonce
- const nonceLatest = await provider.getTransactionCount(wallet, 'latest');
- const noncePending = await provider.getTransactionCount(wallet, 'pending');
- const countNumStr = String(countNum).padStart(2, '0');
- console.log(`${countNumStr} address: ${wallet.address}`);
- console.log(`pending nonce: ${noncePending} ; latest nonce: ${nonceLatest}`);
- console.log('-----------------------------');
- countNum++;
- } catch (error) {
- console.error('获取 nonce 时发生错误:', error.message);
- }
- }
- }
- async function main() {
- try {
- const data = await fs.readFile(filePath, 'utf8');
- const privateKeys = data.split('\n').filter(line => line.trim() !== '');
- await getNonces(privateKeys);
- } catch (err) {
- console.error('读取文件时发生错误:', err);
- process.exit(1);
- }
- }
- main();
|