nac-presale/test-onchain.mjs

114 lines
3.6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 测试链上数据读取
* 直接调用BSC和ETH合约查看能读到哪些数据
*/
import { ethers } from "ethers";
const BSC_PRESALE = "0xc65e7a2738ed884db8d26a6eb2fecf7daca2e90c";
const ETH_PRESALE = "0x85AB2F2d9f7ca7ecB272b5E8726c70f3fd45D1E3";
// 尝试多种可能的函数名
const TEST_ABI = [
"function totalUSDTRaised() view returns (uint256)",
"function totalTokensSold() view returns (uint256)",
"function weiRaised() view returns (uint256)",
"function tokensSold() view returns (uint256)",
"function usdtRaised() view returns (uint256)",
"function totalRaised() view returns (uint256)",
"function amountRaised() view returns (uint256)",
"function hardCap() view returns (uint256)",
"function cap() view returns (uint256)",
"function owner() view returns (address)",
"function paused() view returns (bool)",
];
async function testContract(name, address, rpcUrl) {
console.log(`\n=== 测试 ${name} 合约 ===`);
console.log(`地址: ${address}`);
console.log(`RPC: ${rpcUrl}`);
try {
const provider = new ethers.JsonRpcProvider(rpcUrl, undefined, {
staticNetwork: true,
polling: false,
});
// 先检查合约是否存在
const code = await provider.getCode(address);
if (code === "0x") {
console.log("❌ 该地址没有合约代码!合约地址可能错误。");
return;
}
console.log(`✅ 合约存在,字节码长度: ${code.length} 字符`);
const contract = new ethers.Contract(address, TEST_ABI, provider);
// 逐个测试函数
const functions = [
"totalUSDTRaised",
"totalTokensSold",
"weiRaised",
"tokensSold",
"usdtRaised",
"totalRaised",
"amountRaised",
"hardCap",
"cap",
"owner",
"paused",
];
for (const fn of functions) {
try {
const result = await contract[fn]();
console.log(`${fn}() = ${result}`);
} catch (e) {
console.log(`${fn}() 不存在或调用失败`);
}
}
// 获取合约事件日志最近100个块
const latestBlock = await provider.getBlockNumber();
console.log(`\n当前区块高度: ${latestBlock}`);
// 查找Transfer事件USDT转入
const usdtAddress = name === "BSC"
? "0x55d398326f99059fF775485246999027B3197955"
: "0xdAC17F958D2ee523a2206206994597C13D831ec7";
const usdtAbi = ["event Transfer(address indexed from, address indexed to, uint256 value)"];
const usdtContract = new ethers.Contract(usdtAddress, usdtAbi, provider);
console.log(`\n查询最近1000个块内转入预售合约的USDT...`);
const fromBlock = latestBlock - 1000;
const filter = usdtContract.filters.Transfer(null, address);
try {
const events = await usdtContract.queryFilter(filter, fromBlock, latestBlock);
console.log(`找到 ${events.length} 笔USDT转入记录`);
let totalUsdt = 0n;
for (const event of events.slice(-5)) {
const args = event.args;
const amount = args[2];
totalUsdt += amount;
const decimals = name === "BSC" ? 18 : 6;
console.log(` ${args[0]}${ethers.formatUnits(amount, decimals)} USDT`);
}
} catch (e) {
console.log(`查询事件失败: ${e}`);
}
} catch (e) {
console.error(`测试失败: ${e}`);
}
}
// 测试BSC
await testContract("BSC", BSC_PRESALE, "https://bsc-dataseed1.binance.org/");
// 测试ETH
await testContract("ETH", ETH_PRESALE, "https://eth.llamarpc.com");
console.log("\n=== 测试完成 ===");