You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
web3Interactive/project/pharos/balanceAndNonce.mjs

65 lines
2.0 KiB

import {ethers} from "ethers";
import fs from 'fs/promises';
import path from 'path';
import {fileURLToPath} from 'url';
// 使用 ethers.js 的 Provider
const rpcUrl = 'https://testnet.dplabs-internal.com';
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 getBalance(privateKey) {
try {
const wallet = new ethers.Wallet(privateKey, provider);
// 获取账户余额
const balance = await provider.getBalance(wallet.address);
// 将余额从 wei 转换为 ether
const balanceInEther = ethers.formatEther(balance);
console.log(`Address: ${wallet.address}`);
console.log(`Balance: ${balanceInEther} PHRS`);
} catch (error) {
console.error(`Error getting balance for private key: ${privateKey}`);
console.error(error);
}
}
async function getNonces(privateKey, countNum) {
try {
const wallet = new ethers.Wallet(privateKey, provider);
// 获取最新的 nonce
const nonceLatest = await provider.getTransactionCount(wallet.address, 'latest');
const noncePending = await provider.getTransactionCount(wallet.address, 'pending');
console.log(`pending nonce: ${noncePending} ; latest nonce: ${nonceLatest}`);
} catch (error) {
console.error('获取 nonce 时发生错误:', error.message);
}
}
async function main() {
let privateKeys;
try {
const data = await fs.readFile(filePath, 'utf8');
privateKeys = data.split('\n').filter(line => line.trim() !== '');
} catch (err) {
console.error('读取文件时发生错误:', err);
process.exit(1);
}
let countNum = 1;
for (const privateKey of privateKeys) {
await getBalance(privateKey);
await getNonces(privateKey);
console.log('-----------------------------');
countNum++;
}
}
main();