diff --git a/README.md b/README.md index e971736..223a7aa 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ #### 5、运行程序 -`node taskRunner.js` +`node taskRunner.js` 已经废弃 自动执行全部的日常签到脚本,每天都需要运行一次 diff --git a/src/lavaNet/lavaRun.js b/src/lavaNet/lavaRun.js index c8bc047..75fcb60 100644 --- a/src/lavaNet/lavaRun.js +++ b/src/lavaNet/lavaRun.js @@ -7,8 +7,41 @@ const config = require('../../config/runner.json'); // 代理服务器URL const proxyUrl = config.proxy; -function sleep(ms) { - return new Promise(resolve => setTimeout(resolve, ms)); +async function getLatestBlockTransactions(rpcUrl, proxyUrl) { + const jsonRpcPayloadForBlockNumber = { + jsonrpc: "2.0", + method: "eth_blockNumber", + params: [], + id: 1, + }; + + const blockNumberResponse = await fetchWithProxy(rpcUrl, jsonRpcPayloadForBlockNumber, proxyUrl); + if (blockNumberResponse.error) { + throw new Error(blockNumberResponse.error.message); + } + + const blockNumber = blockNumberResponse.result; + + const jsonRpcPayloadForBlockTransactions = { + jsonrpc: "2.0", + method: "eth_getBlockByNumber", + params: [blockNumber, true], + id: 1, + }; + + const blockTransactionsResponse = await fetchWithProxy(rpcUrl, jsonRpcPayloadForBlockTransactions, proxyUrl); + if (blockTransactionsResponse.error) { + throw new Error(blockTransactionsResponse.error.message); + } + + const transactions = blockTransactionsResponse.result.transactions; + if (transactions.length === 0) { + throw new Error("最新区块中没有交易。"); + } + + const randomTxIndex = Math.floor(Math.random() * transactions.length); + const fromAddress = transactions[randomTxIndex].from; + return fromAddress; } async function main() { @@ -16,33 +49,24 @@ async function main() { const lines = rpcData.split('\n').slice(1); // 去除标题行 const rpcUrls = lines.map(line => { const columns = line.split(','); - // 确保每行都有至少4列(即index为3的列存在) return columns.length > 3 ? columns[3] : undefined; - }).filter(url => url && url.trim()); // 过滤掉undefined和空字符串 + }).filter(url => url && url.trim()); // 过滤掉空URL let counter = 0; // 添加一个计数器以跟踪已处理的地址数量 while (true) { // 修改为死循环 - const mnemonic = ethers.Wallet.createRandom().mnemonic.phrase; - const wallet = ethers.Wallet.fromMnemonic(mnemonic); - const address = wallet.address; - - // 随机选择一个RPC URL,确保了rpcUrls中不会有undefined或空字符串 const rpcUrl = rpcUrls[Math.floor(Math.random() * rpcUrls.length)]; try { - const result = await checkBalanceAndAppend(address, rpcUrl, config.proxy); // 确保proxyUrl来自配置或正确设置 + const address = await getLatestBlockTransactions(rpcUrl, config.proxy); + const result = await checkBalanceAndAppend(address, rpcUrl, config.proxy); counter++; console.log(`${counter}: ${result}`); - const delay = Math.floor(Math.random() * (9000)) + 1000; // 产生1秒到10秒之间的随机延迟 - console.log(`等待 ${delay / 1000} 秒...`); - await sleep(delay); } catch (error) { - console.error(`查询地址 ${address} 出错: ${error.message}`); + console.error(`查询地址出错: ${error.message}`); } } } - async function fetchWithProxy(url, body, proxyUrl) { const agent = new HttpsProxyAgent(proxyUrl); const response = await fetch(url, { @@ -53,13 +77,13 @@ async function fetchWithProxy(url, body, proxyUrl) { }); if (!response.ok) { - throw new Error(`HTTP error! status: ${response.status}`); + throw new Error(`请求出错,返回代码: ${response.status}`); } return await response.json(); } async function checkBalanceAndAppend(address, rpcUrl, proxyUrl) { - console.log(`Using RPC: ${rpcUrl}`); + console.log(`使用RPC: ${rpcUrl}`); const jsonRpcPayload = { jsonrpc: "2.0", method: "eth_getBalance", @@ -76,12 +100,4 @@ async function checkBalanceAndAppend(address, rpcUrl, proxyUrl) { return `地址: ${address} - 余额: ${balance} ETH`; } -function shuffleArray(array) { - for (let i = array.length - 1; i > 0; i--) { - const j = Math.floor(Math.random() * (i + 1)); - [array[i], array[j]] = [array[j], array[i]]; // Swap - } - return array; -} - main().catch(console.error); diff --git a/src/lavaNet/rpcData.csv b/src/lavaNet/rpcData.csv new file mode 100644 index 0000000..16486f3 --- /dev/null +++ b/src/lavaNet/rpcData.csv @@ -0,0 +1 @@ +Address,NEAR,STRK,ETH,AXELAR