-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathindex.js
186 lines (161 loc) · 5.2 KB
/
index.js
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const { ethers } = require('ethers');
const colors = require('colors');
const fs = require('fs');
const readlineSync = require('readline-sync');
const checkBalance = require('./src/checkBalance');
const displayHeader = require('./src/displayHeader');
const sleep = require('./src/sleep');
const rpcUrl = 'https://rpc-testnet.unit0.dev';
const MAX_RETRIES = 5;
const RETRY_DELAY = 5000;
async function retry(fn, maxRetries = MAX_RETRIES, delay = RETRY_DELAY) {
for (let i = 0; i < maxRetries; i++) {
try {
return await fn();
} catch (error) {
if (i === maxRetries - 1) throw error;
console.log(
colors.yellow(`Error occurred. Retrying... (${i + 1}/${maxRetries})`)
);
await sleep(delay);
}
}
}
const main = async () => {
displayHeader();
const privateKeys = JSON.parse(fs.readFileSync('privateKeys.json'));
const provider = new ethers.JsonRpcProvider(rpcUrl);
for (const privateKey of privateKeys) {
const wallet = new ethers.Wallet(privateKey, provider);
const senderAddress = wallet.address;
console.log(
colors.cyan(`Processing transactions for address: ${senderAddress}`)
);
let senderBalance;
try {
senderBalance = await retry(() => checkBalance(provider, senderAddress));
} catch (error) {
console.log(
colors.red(
`Failed to check balance for ${senderAddress}. Skipping to next address.`
)
);
continue;
}
if (senderBalance < ethers.parseUnits('0.01', 'ether')) {
console.log(
colors.red('Insufficient or zero balance. Skipping to next address.')
);
continue;
}
let continuePrintingBalance = true;
const printSenderBalance = async () => {
while (continuePrintingBalance) {
try {
senderBalance = await retry(() =>
checkBalance(provider, senderAddress)
);
console.log(
colors.blue(
`Current Balance: ${ethers.formatUnits(
senderBalance,
'ether'
)} ETH`
)
);
if (senderBalance < ethers.parseUnits('0.01', 'ether')) {
console.log(colors.red('Insufficient balance for transactions.'));
continuePrintingBalance = false;
}
} catch (error) {
console.log(colors.red(`Failed to check balance: ${error.message}`));
}
await sleep(5000);
}
};
printSenderBalance();
const transactionCount = readlineSync.questionInt(
`Enter the number of transactions you want to send for address ${senderAddress}: `
);
for (let i = 1; i <= transactionCount; i++) {
const receiverWallet = ethers.Wallet.createRandom();
const receiverAddress = receiverWallet.address;
console.log(colors.white(`\nGenerated address ${i}: ${receiverAddress}`));
const amountToSend = ethers.parseUnits(
(Math.random() * (0.0000001 - 0.00000001) + 0.00000001)
.toFixed(10)
.toString(),
'ether'
);
const gasPrice = ethers.parseUnits(
(Math.random() * (0.0015 - 0.0009) + 0.0009).toFixed(9).toString(),
'gwei'
);
const transaction = {
to: receiverAddress,
value: amountToSend,
gasLimit: 21000,
gasPrice: gasPrice,
chainId: 88817,
};
let tx;
try {
tx = await retry(() => wallet.sendTransaction(transaction));
} catch (error) {
console.log(colors.red(`Failed to send transaction: ${error.message}`));
continue;
}
console.log(colors.white(`Transaction ${i}:`));
console.log(colors.white(` Hash: ${colors.green(tx.hash)}`));
console.log(colors.white(` From: ${colors.green(senderAddress)}`));
console.log(colors.white(` To: ${colors.green(receiverAddress)}`));
console.log(
colors.white(
` Amount: ${colors.green(
ethers.formatUnits(amountToSend, 'ether')
)} ETH`
)
);
console.log(
colors.white(
` Gas Price: ${colors.green(
ethers.formatUnits(gasPrice, 'gwei')
)} Gwei`
)
);
await sleep(15000);
let receipt;
try {
receipt = await retry(() => provider.getTransactionReceipt(tx.hash));
if (receipt) {
if (receipt.status === 1) {
console.log(colors.green('Transaction Success!'));
console.log(colors.green(` Block Number: ${receipt.blockNumber}`));
console.log(
colors.green(` Gas Used: ${receipt.gasUsed.toString()}`)
);
} else {
console.log(colors.red('Transaction FAILED'));
}
} else {
console.log(
colors.yellow(
'Transaction is still pending after multiple retries.'
)
);
}
} catch (error) {
console.log(
colors.red(`Error checking transaction status: ${error.message}`)
);
}
console.log();
}
console.log(
colors.green(`Finished transactions for address: ${senderAddress}`)
);
}
};
main().catch((error) => {
console.error(colors.red('An unexpected error occurred:'), error);
});