-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
229 lines (202 loc) · 6.61 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
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 {
loadChains,
selectChain,
selectNetworkType,
} = require('./src/chainUtils');
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 networkType = selectNetworkType();
const chains = loadChains(networkType);
const selectedChain = selectChain(chains);
console.log(colors.green(`✅ You have selected: ${selectedChain.name}`));
console.log(colors.green(`🛠 RPC URL: ${selectedChain.rpcUrl}`));
console.log(colors.green(`🔗 Chain ID: ${selectedChain.chainId}`));
const provider = new ethers.JsonRpcProvider(selectedChain.rpcUrl);
const privateKeys = JSON.parse(fs.readFileSync('privateKeys.json'));
const transactionCount = readlineSync.questionInt(
'Enter the number of transactions you want to send for each address: '
);
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.001', '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'
)} ${selectedChain.symbol}`
)
);
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();
for (let i = 1; i <= transactionCount; i++) {
const receiverWallet = ethers.Wallet.createRandom();
const receiverAddress = receiverWallet.address;
console.log(
colors.white(`\n🆕 Generated 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'
);
/* --------------------------- TEMPORARY DISABLED --------------------------- */
// const gasPrice = ethers.parseUnits(
// (Math.random() * (0.0015 - 0.0009) + 0.0009).toFixed(9).toString(),
// 'gwei'
// );
let gasPrice;
try {
gasPrice = (await provider.getFeeData()).gasPrice;
} catch (error) {
console.log(
colors.red('❌ Failed to fetch gas price from the network.')
);
continue;
}
const transaction = {
to: receiverAddress,
value: amountToSend,
gasLimit: 21000,
gasPrice: gasPrice,
chainId: parseInt(selectedChain.chainId),
};
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')
)} ${selectedChain.symbol}`
)
);
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()}`)
);
console.log(
colors.green(
` Transaction hash: ${selectedChain.explorer}/tx/${receipt.hash}`
)
);
} 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}`)
);
}
console.log(colors.green('All transactions completed.'));
console.log(colors.green('Subscribe: https://t.me/HappyCuanAirdrop.'));
process.exit(0);
};
main().catch((error) => {
console.error(colors.red('🚨 An unexpected error occurred:'), error);
process.exit(1);
});