forked from dante4rt/rivalz-ai-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
227 lines (205 loc) · 6.72 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
require('colors');
const fs = require('fs');
const readlineSync = require('readline-sync');
const inquirer = require('inquirer');
const { displayHeader, checkBalance } = require('./src/utils');
const { createWallet, createContract } = require('./src/wallet');
const { claimFragmentz } = require('./src/claim');
const { RPC_URL } = require('./src/utils');
const { JsonRpcProvider, ethers } = require('ethers');
const moment = require('moment');
const { CronJob } = require('cron');
const CONTRACT_ADDRESS = '0xF0a66d18b46D4D5dd9947914ab3B2DDbdC19C2C0';
let recurringSettings = {};
async function claimProcess(seedPhrasesOrKeys, method, provider, numClaims) {
for (const keyOrPhrase of seedPhrasesOrKeys) {
let wallet;
if (method === '0') {
wallet = createWallet(keyOrPhrase, provider);
} else {
wallet = createWallet(keyOrPhrase, provider);
}
const senderAddress = wallet.address;
console.log(`Processing transactions for address: ${senderAddress}`.cyan);
const contract = createContract(wallet, CONTRACT_ADDRESS);
try {
await claimFragmentz(contract, numClaims);
console.log(
`Successfully claimed ${numClaims} Fragmentz for ${senderAddress}`.green
);
} catch (error) {
console.log(
`[ ${moment().format(
'HH:mm:ss'
)} ] Error claiming Fragmentz for ${senderAddress}: ${error.message}`
.red
);
}
}
}
async function setupRecurringClaim(
seedPhrasesOrKeys,
method,
provider,
numClaims
) {
console.log('Setting up recurring claim every 12 hours...'.green);
const job = new CronJob('0 */12 * * *', async function () {
await claimProcess(seedPhrasesOrKeys, method, provider, numClaims);
console.log(
`[ ${moment().format('HH:mm:ss')} ] Recurring claim executed.`.green
);
});
job.start();
console.log('Cron job successfully set up.'.green);
}
async function main() {
displayHeader();
const provider = new JsonRpcProvider(RPC_URL);
while (true) {
const { action } = await inquirer.prompt([
{
type: 'list',
name: 'action',
message: 'Select an option:',
choices: [
{ name: 'Check balance', value: '0' },
{ name: 'Claim Fragmentz', value: '1' },
{ name: 'Exit', value: '2' },
],
},
]);
if (action === '2') {
console.log('Exiting...'.cyan);
break;
}
try {
if (action === '0') {
const privateKeys = JSON.parse(
fs.readFileSync('privateKeys.json', 'utf-8')
);
if (!Array.isArray(privateKeys) || privateKeys.length === 0) {
throw new Error(
'privateKeys.json is not set correctly or is empty'.red
);
}
for (const privateKey of privateKeys) {
const wallet = createWallet(privateKey, provider);
const senderAddress = wallet.address;
const balance = await checkBalance(provider, senderAddress);
console.log(
`Address: ${senderAddress} - Balance: ${ethers.formatEther(
balance
)} ETH`.yellow
);
console.log(
`Claim faucet here: https://rivalz2.hub.caldera.xyz/`.yellow
);
}
} else if (action === '1') {
let claimOption, method, seedPhrasesOrKeys;
if (recurringSettings.claimOption === '2') {
claimOption = '2';
method = recurringSettings.method;
seedPhrasesOrKeys = recurringSettings.seedPhrasesOrKeys;
} else {
({ claimOption } = await inquirer.prompt([
{
type: 'list',
name: 'claimOption',
message: 'Select claim type:',
choices: [
{ name: 'One-time claim', value: '1' },
{ name: '12 hours recurring claim', value: '2' },
],
},
]));
({ method } = await inquirer.prompt([
{
type: 'list',
name: 'method',
message: 'Select input method:',
choices: [
{ name: 'Use mnemonics', value: '0' },
{ name: 'Use private keys', value: '1' },
],
},
]));
if (method === '0') {
seedPhrasesOrKeys = JSON.parse(
fs.readFileSync('accounts.json', 'utf-8')
);
if (
!Array.isArray(seedPhrasesOrKeys) ||
seedPhrasesOrKeys.length === 0
) {
throw new Error(
'accounts.json is not set correctly or is empty'.red
);
}
} else if (method === '1') {
seedPhrasesOrKeys = JSON.parse(
fs.readFileSync('privateKeys.json', 'utf-8')
);
if (
!Array.isArray(seedPhrasesOrKeys) ||
seedPhrasesOrKeys.length === 0
) {
throw new Error(
'privateKeys.json is not set correctly or is empty'.red
);
}
} else {
throw new Error('Invalid input method selected'.red);
}
}
const numClaims = readlineSync.questionInt(
'How many Fragmentz do you want to claim? '
);
console.log('');
await claimProcess(seedPhrasesOrKeys, method, provider, numClaims);
console.log('\nInitial claim completed.'.green);
if (claimOption === '2') {
recurringSettings = { claimOption, method, seedPhrasesOrKeys };
await setupRecurringClaim(
seedPhrasesOrKeys,
method,
provider,
numClaims
);
console.log(
'Bot is now running in idle mode. It will perform claims every 12 hours.'
.green
);
break;
}
} else {
console.log(
'Invalid input. Please enter 0 to check balance, 1 to claim Fragmentz, or 2 to exit.'
.red
);
}
} catch (error) {
console.log(
`[ ${moment().format('HH:mm:ss')} ] Error in main loop: ${
error.message
}`.red
);
} finally {
if (action === '1' && claimOption === '2') {
console.log(
'\nRecurring claim is set up and running every 12 hours.'.green
);
console.log('Bot is now running in idle mode.'.green);
} else if (action === '1' && claimOption === '1') {
console.log('\nOne-time claim completed.'.green);
} else if (action === '0') {
console.log('\nBalance check completed.'.green);
} else {
console.log('\nExiting the bot. See you next time!'.green);
}
console.log('Subscribe: https://t.me/HappyCuanAirdrop'.green);
}
}
}
main();