-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
118 lines (96 loc) · 4.43 KB
/
script.js
File metadata and controls
118 lines (96 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
document.addEventListener("DOMContentLoaded", function () {
const executeButton = document.getElementById("execute-button");
const pauseInput = document.getElementById("pause-input");
executeButton.addEventListener("click", function () {
// Disable the button and update appearance
executeButton.disabled = true;
executeButton.innerText = "正在交易";
executeButton.style.backgroundColor = "#ccc";
scheduleTransaction(pauseInput.value); // 传递秒数给 scheduleTransaction
});
});
async function scheduleTransaction(pauseSeconds) {
const rpc = document.getElementById("rpc-input").value;
const privateKeys = document.getElementById("private-key-input").value.trim().split("\n");
const nums = parseInt(document.getElementById("nums-input").value);
const data = document.getElementById("data-input").value;
// Clear result textarea
document.getElementById("result-text").value = "";
// Set up Web3 instance
const w3 = new Web3(new Web3.providers.HttpProvider(rpc));
// Check if the node is listening
const isNodeListening = await w3.eth.net.isListening();
if (!isNodeListening) {
printResult("连接失败 请重新开启脚本/更换rpc节点");
return;
}
//
const numAccounts = privateKeys.length;
for (let i = 0; i < numAccounts; i++) {
// Print Account information
printResult(`---- Account ${i + 1} ----`);
const privateKey = privateKeys[i].trim();
if (privateKey !== "") {
await executeTransaction(privateKey, rpc, nums, data, pauseSeconds); // 将 pauseSeconds 传递给 executeTransaction
}
}
// Enable the button and reset appearance after all transactions are completed
const executeButton = document.getElementById("execute-button");
executeButton.disabled = false;
executeButton.innerText = "执行交易";
executeButton.style.backgroundColor = "";
printResult("----全部交易已执行完成----");
}
async function executeTransaction(privateKey, rpc, nums, data, pauseSeconds) {
const w3 = new Web3(new Web3.providers.HttpProvider(rpc));
const fromAddress = w3.eth.accounts.privateKeyToAccount(privateKey).address;
try {
const isNodeListening = await w3.eth.net.isListening();
if (!isNodeListening) {
printResult("连接失败 请重新开启脚本/更换rpc节点");
return;
}
const nonce = await w3.eth.getTransactionCount(fromAddress);
for (let i = 0; i < nums; i++) {
const gasPrice = await w3.eth.getGasPrice();
const adjustedGasPrice = Math.round(gasPrice * 1.1);
const chainId = await w3.eth.getChainId();
const gas = chainId === 42161 ? 6000000 : 50000;
const transaction = {
from: fromAddress,
to: fromAddress,
value: w3.utils.toWei("0", "ether"),
nonce: nonce + i,
gas: gas,
gasPrice: adjustedGasPrice,
data: w3.utils.toHex(data),
chainId: chainId,
};
// Print transaction information
if (i === 0) {
printResult(`连接成功 开始发送交易 (账号:${fromAddress})`);
printResult(`当前nonce: ${transaction.nonce}, 发送地址: ${fromAddress}`);
}
const signedTransaction = await w3.eth.accounts.signTransaction(transaction, privateKey);
try {
const receipt = await w3.eth.sendSignedTransaction(signedTransaction.rawTransaction);
printResult(`交易成功,Hash: ${receipt.transactionHash}, nonce: ${transaction.nonce}`);
} catch (error) {
console.error("Error sending transaction:", error);
printResult(`交易失败,nonce: ${transaction.nonce}, Error: ${error.message}`);
}
await pauseForSeconds(pauseSeconds);
}
} catch (error) {
console.error("Error checking node connection:", error);
printResult("连接失败 请重新开启脚本/更换rpc节点");
}
}
function printResult(message) {
const resultText = document.getElementById("result-text");
resultText.value += message + "\n";
resultText.scrollTop = resultText.scrollHeight;
}
async function pauseForSeconds(seconds) {
return new Promise(resolve => setTimeout(resolve, seconds * 1000));
}