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.
66 lines
2.1 KiB
66 lines
2.1 KiB
import { ethers } from "ethers";
|
|
|
|
// 设置 RPC URL
|
|
const rpcUrl = "https://testnet-rpc.monad.xyz";
|
|
|
|
// 初始化 ethers.js 提供器
|
|
const provider = new ethers.JsonRpcProvider(rpcUrl);
|
|
|
|
// 检查是否连接成功
|
|
async function checkConnection() {
|
|
try {
|
|
const network = await provider.getNetwork();
|
|
console.log(`已成功连接到链节点,网络ID为: ${network.chainId}。正在查询钱包余额...`);
|
|
return true;
|
|
} catch (error) {
|
|
console.log("无法连接到链节点,请检查 URL 是否正确");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 替换为实际的钱包地址列表
|
|
const walletAddresses = [
|
|
"0x10A43E7Fe77E2D84adBeC26cF0bFc6f403841266",
|
|
"0x70D5EE1DfddD3726f0D71F4CD5a8ef43aC651a75"
|
|
];
|
|
|
|
async function queryBalances() {
|
|
const isConnected = await checkConnection();
|
|
if (!isConnected) {
|
|
return;
|
|
}
|
|
|
|
for (const walletAddress of walletAddresses) {
|
|
let retryCount = 0;
|
|
const maxRetries = 3;
|
|
const delay = 2000; // 延迟时间,单位为毫秒
|
|
|
|
let resultMessage = "";
|
|
let resultBalance = -1;
|
|
|
|
while (retryCount < maxRetries) {
|
|
try {
|
|
// 查询余额
|
|
const balance = await provider.getBalance(walletAddress);
|
|
// 将余额从 Wei 转换为 Ether
|
|
const balanceEth = ethers.formatUnits(balance, 18);
|
|
resultMessage = `Wallet address: ${walletAddress}, balance: ${balanceEth} Token`;
|
|
resultBalance = balanceEth;
|
|
console.log(resultMessage);
|
|
break;
|
|
} catch (e) {
|
|
console.log(`查询钱包 ${walletAddress} 余额时发生错误: ${e.message}`);
|
|
retryCount += 1;
|
|
console.log(`正在重试...(第 ${retryCount} 次)`);
|
|
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
}
|
|
}
|
|
|
|
if (retryCount === maxRetries) {
|
|
resultMessage = `钱包 ${walletAddress} 查询余额失败,已达到最大重试次数。`;
|
|
console.log(resultMessage);
|
|
}
|
|
}
|
|
}
|
|
|
|
queryBalances(); |