first commit

This commit is contained in:
jack
2025-11-12 16:47:21 +08:00
commit 266ba871a4
43 changed files with 3747 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
0x4b833cf91c19c4d9434ffed4b18714ef032d2acf44caec110a1c2c7db151eb65
+83
View File
@@ -0,0 +1,83 @@
import {ethers} from "ethers";
// 配置 provider,明确指定 chainId 并禁用 ENS
const provider = new ethers.JsonRpcProvider("https://testnet.dplabs-internal.com");
const privateKey = "0x4b833cf91c19c4d9434ffed4b18714ef032d2acf44caec110a1c2c7db151eb65";
const wallet = new ethers.Wallet(privateKey, provider);
const contractAddress = "0x76aaada469d23216be5f7c596fa25f282ff9b364";
const contractABI = [
"function deposit() public"
];
async function validateSetup() {
try {
// 验证钱包地址
if (!ethers.isAddress(wallet.address)) {
throw new Error("Invalid wallet address derived from private key");
}
console.log("Wallet address:", wallet.address);
// 检查账户余额
const balance = await provider.getBalance(wallet.address);
console.log("Wallet balance:", ethers.formatEther(balance), "PHRS");
if (balance === 0n) {
throw new Error("Wallet has no balance to pay for gas");
}
// 验证合约地址
if (!ethers.isAddress(contractAddress)) {
throw new Error("Invalid contract address");
}
// 检查合约是否部署
const code = await provider.getCode(contractAddress);
if (code === "0x") {
throw new Error("No contract deployed at the specified address");
}
console.log("Contract is deployed at:", contractAddress);
// 检查网络连接
const network = await provider.getNetwork();
console.log("Connected to network:", network);
} catch (error) {
console.error("Validation failed:", error.message);
process.exit(1);
}
}
// 发送交易
async function deposit() {
try {
// 验证设置
await validateSetup();
// 初始化合约
const contract = new ethers.Contract(contractAddress, contractABI, wallet);
// 获取交易参数
const nonce = await provider.getTransactionCount(wallet.address, "pending");
const gasPrice = (await provider.getFeeData()).gasPrice;
// 发送交易
const tx = await contract.deposit({
nonce,
gasPrice,
gasLimit: 100000 // 根据合约需求调整
});
console.log("Transaction sent:", tx.hash);
// 等待交易确认
const receipt = await tx.wait();
console.log("Transaction confirmed in block:", receipt.blockNumber);
} catch (error) {
console.error("Error sending transaction:", error);
process.exit(1);
}
}
// 执行 deposit 函数
deposit();
+65
View File
@@ -0,0 +1,65 @@
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();