forked from dante4rt/sonic-odyssey-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
141 lines (126 loc) · 4.07 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
const fs = require('fs');
const readlineSync = require('readline-sync');
const colors = require('colors');
const {
sendSol,
generateRandomAddresses,
getKeypairFromSeed,
getKeypairFromPrivateKey,
PublicKey,
connection,
LAMPORTS_PER_SOL,
} = require('./src/solanaUtils');
const { displayHeader } = require('./src/displayUtils');
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
(async () => {
displayHeader();
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'));
}
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;
}
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) || amountToSend < rentExemptionAmount) {
console.log(
colors.red(
`Invalid amount specified. The amount must be at least ${rentExemptionAmount} SOL to avoid rent issues.`
)
);
console.log(
colors.yellow(
`Suggested amount to send: ${Math.max(
0.001,
rentExemptionAmount
)} SOL`
)
);
}
} while (isNaN(amountToSend) || amountToSend < rentExemptionAmount);
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'));
}
for (const [index, seedOrKey] of seedPhrasesOrKeys.entries()) {
let fromKeypair;
if (method === '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);
}
}
})();