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();