-
Notifications
You must be signed in to change notification settings - Fork 170
/
index.js
193 lines (171 loc) · 5.22 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
const fs = require('fs');
const readlineSync = require('readline-sync');
const colors = require('colors');
const cron = require('cron');
const {
sendSol,
generateRandomAddresses,
getKeypairFromSeed,
getKeypairFromPrivateKey,
PublicKey,
connection,
LAMPORTS_PER_SOL,
delay,
} = require('./src/solanaUtils');
const { displayHeader, getNetworkTypeFromUser } = require('./src/displayUtils');
async function transferSol(
seedPhrasesOrKeys,
addressCount,
amountToSend,
delayBetweenTx
) {
const randomAddresses = generateRandomAddresses(addressCount);
let rentExemptionAmount;
try {
rentExemptionAmount =
(await connection.getMinimumBalanceForRentExemption(0)) /
LAMPORTS_PER_SOL;
console.log(
colors.yellow(
`Minimum balance required for rent exemption: ${rentExemptionAmount} SOL`
)
);
} catch (error) {
console.error(
colors.red(
'Failed to fetch minimum balance for rent exemption. Using default value.'
)
);
rentExemptionAmount = 0.001;
}
for (const [index, seedOrKey] of seedPhrasesOrKeys.entries()) {
let fromKeypair;
if (seedPhrasesOrKeys === '0') {
fromKeypair = await getKeypairFromSeed(seedOrKey);
} else {
fromKeypair = getKeypairFromPrivateKey(seedOrKey);
}
console.log(
colors.yellow(
`Sending SOL from account ${
index + 1
}: ${fromKeypair.publicKey.toString()}`
)
);
for (const address of randomAddresses) {
const toPublicKey = new PublicKey(address);
try {
await sendSol(fromKeypair, toPublicKey, amountToSend);
console.log(
colors.green(`Successfully sent ${amountToSend} SOL to ${address}`)
);
} catch (error) {
console.error(colors.red(`Failed to send SOL to ${address}:`), error);
}
await delay(delayBetweenTx);
}
console.log();
}
}
function setupCronJob(
seedPhrasesOrKeys,
addressCount,
amountToSend,
delayBetweenTx
) {
console.log(colors.green('Setting up cron job to run every 24 hours...'));
const cronJob = new cron.CronJob('0 0 * * *', async () => {
console.log(colors.blue('Running scheduled transfer...'));
await transferSol(
seedPhrasesOrKeys,
addressCount,
amountToSend,
delayBetweenTx
);
});
cronJob.start();
console.log(colors.green('Cron job scheduled successfully!'));
console.log();
}
(async () => {
displayHeader();
await getNetworkTypeFromUser();
console.log();
const method = readlineSync.question(
'Select input method (0 for seed phrase, 1 for private key): '
);
let seedPhrasesOrKeys;
if (method === '0') {
seedPhrasesOrKeys = JSON.parse(fs.readFileSync('accounts.json', 'utf-8'));
if (!Array.isArray(seedPhrasesOrKeys) || seedPhrasesOrKeys.length === 0) {
throw new Error(
colors.red('accounts.json is not set correctly or is empty')
);
}
} else if (method === '1') {
seedPhrasesOrKeys = JSON.parse(
fs.readFileSync('privateKeys.json', 'utf-8')
);
if (!Array.isArray(seedPhrasesOrKeys) || seedPhrasesOrKeys.length === 0) {
throw new Error(
colors.red('privateKeys.json is not set correctly or is empty')
);
}
} else {
throw new Error(colors.red('Invalid input method selected'));
}
const defaultAddressCount = 100;
const addressCountInput = readlineSync.question(
`How many random addresses do you want to generate? (default is ${defaultAddressCount}): `
);
const addressCount = addressCountInput
? parseInt(addressCountInput, 10)
: defaultAddressCount;
if (isNaN(addressCount) || addressCount <= 0) {
throw new Error(colors.red('Invalid number of addresses specified'));
}
let amountToSend;
do {
const amountInput = readlineSync.question(
'Enter the amount of SOL to send (default is 0.001 SOL): '
);
amountToSend = amountInput ? parseFloat(amountInput) : 0.001;
if (isNaN(amountToSend)) {
console.log(colors.red('Invalid amount specified.'));
}
} while (isNaN(amountToSend));
const defaultDelay = 1000;
const delayInput = readlineSync.question(
`Enter the delay between transactions in milliseconds (default is ${defaultDelay}ms): `
);
const delayBetweenTx = delayInput ? parseInt(delayInput, 10) : defaultDelay;
if (isNaN(delayBetweenTx) || delayBetweenTx < 0) {
throw new Error(colors.red('Invalid delay specified'));
}
const executionMode = readlineSync.question(
'Do you want to run this script one-time or auto every 24 hours? (0 for one-time, 1 for auto): '
);
if (executionMode === '0') {
console.log(colors.yellow('Running one-time transfer...'));
await transferSol(
seedPhrasesOrKeys,
addressCount,
amountToSend,
delayBetweenTx
);
console.log(colors.green('Transfer complete. Exiting...'));
} else if (executionMode === '1') {
console.log(
colors.yellow('Running first-time transfer and setting up auto mode...')
);
await transferSol(
seedPhrasesOrKeys,
addressCount,
amountToSend,
delayBetweenTx
);
setupCronJob(seedPhrasesOrKeys, addressCount, amountToSend, delayBetweenTx);
} else {
console.error(colors.red('Invalid selection. Exiting...'));
}
})();