-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
294 lines (294 loc) · 12.7 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.mainKp = exports.solanaConnection = void 0;
const spl_token_1 = require("@solana/spl-token");
const web3_js_1 = require("@solana/web3.js");
const constants_1 = require("./constants");
const utils_1 = require("./utils");
const bs58_1 = __importDefault(require("bs58"));
const swapOnlyAmm_1 = require("./utils/swapOnlyAmm");
const legacy_1 = require("./executor/legacy");
const jito_1 = require("./executor/jito");
exports.solanaConnection = new web3_js_1.Connection(constants_1.RPC_ENDPOINT, {
wsEndpoint: constants_1.RPC_WEBSOCKET_ENDPOINT, commitment: "confirmed"
});
exports.mainKp = web3_js_1.Keypair.fromSecretKey(bs58_1.default.decode(constants_1.PRIVATE_KEY));
const baseMint = new web3_js_1.PublicKey(constants_1.TOKEN_MINT);
const distritbutionNum = constants_1.DISTRIBUTE_WALLET_NUM > 20 ? 20 : constants_1.DISTRIBUTE_WALLET_NUM;
const jitoCommitment = "confirmed";
const main = () => __awaiter(void 0, void 0, void 0, function* () {
const solBalance = yield exports.solanaConnection.getBalance(exports.mainKp.publicKey);
console.log(`Volume bot is running`);
console.log(`Wallet address: ${exports.mainKp.publicKey.toBase58()}`);
console.log(`Pool token mint: ${baseMint.toBase58()}`);
console.log(`Wallet SOL balance: ${(solBalance / web3_js_1.LAMPORTS_PER_SOL).toFixed(3)}SOL`);
console.log(`Buying wait time max: ${constants_1.BUY_INTERVAL_MAX}s`);
console.log(`Buying wait time min: ${constants_1.BUY_INTERVAL_MIN}s`);
console.log(`Selling wait time max: ${constants_1.SELL_INTERVAL_MAX}s`);
console.log(`Selling wait time min: ${constants_1.SELL_INTERVAL_MIN}s`);
console.log(`Buy upper limit percent: ${constants_1.BUY_UPPER_PERCENT}%`);
console.log(`Buy lower limit percent: ${constants_1.BUY_LOWER_PERCENT}%`);
console.log(`Distribute SOL to ${distritbutionNum} wallets`);
let data = null;
if (solBalance < (constants_1.BUY_LOWER_PERCENT + 0.002) * distritbutionNum) {
console.log("Sol balance is not enough for distribution");
}
data = yield distributeSol(exports.solanaConnection, exports.mainKp, distritbutionNum);
if (data == null || data.length == 0) {
console.log("Distribution failed");
return;
}
data.map((_a, i_1) => __awaiter(void 0, [_a, i_1], void 0, function* ({ kp }, i) {
yield (0, utils_1.sleep)(i * 5000);
let srcKp = kp;
while (true) {
const BUY_WAIT_INTERVAL = Math.round(Math.random() * (constants_1.BUY_INTERVAL_MAX - constants_1.BUY_INTERVAL_MIN) + constants_1.BUY_INTERVAL_MIN);
const SELL_WAIT_INTERVAL = Math.round(Math.random() * (constants_1.SELL_INTERVAL_MAX - constants_1.SELL_INTERVAL_MIN) + constants_1.SELL_INTERVAL_MIN);
let buyAmount = 3_000_000;
let i = 0;
while (true) {
try {
if (i > 10) {
console.log("Error in buy transaction");
return;
}
const result = yield buy(srcKp, baseMint, buyAmount);
if (result) {
break;
}
else {
i++;
yield (0, utils_1.sleep)(2000);
}
}
catch (error) {
i++;
}
}
yield (0, utils_1.sleep)(BUY_WAIT_INTERVAL * 1000);
let j = 0;
while (true) {
if (j > 10) {
console.log("Error in sell transaction");
return;
}
const result = yield sell(baseMint, srcKp);
if (result) {
break;
}
else {
j++;
yield (0, utils_1.sleep)(2000);
}
}
yield (0, utils_1.sleep)(SELL_WAIT_INTERVAL * 1000);
const balance = yield exports.solanaConnection.getBalance(srcKp.publicKey);
if (balance < 5 * 10 ** 6) {
console.log("Sub wallet balance is not enough to continue volume swap");
return;
}
let k = 0;
while (true) {
try {
if (k > 5) {
console.log("Failed to transfer SOL to new wallet in one of sub wallet");
return;
}
const destinationKp = web3_js_1.Keypair.generate();
const baseAta = (0, spl_token_1.getAssociatedTokenAddressSync)(baseMint, srcKp.publicKey);
(0, utils_1.saveDataToFile)([{
privateKey: bs58_1.default.encode(destinationKp.secretKey),
pubkey: destinationKp.publicKey.toBase58(),
}]);
const tx = new web3_js_1.Transaction().add(web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({ units: 600000 }), web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({ microLamports: 20000 }), (0, spl_token_1.createCloseAccountInstruction)(baseAta, srcKp.publicKey, srcKp.publicKey), web3_js_1.SystemProgram.transfer({
fromPubkey: srcKp.publicKey,
toPubkey: destinationKp.publicKey,
lamports: balance - 17000 + 2039280
}));
tx.feePayer = srcKp.publicKey;
tx.recentBlockhash = (yield exports.solanaConnection.getLatestBlockhash()).blockhash;
const sig = yield (0, web3_js_1.sendAndConfirmTransaction)(exports.solanaConnection, tx, [srcKp], { skipPreflight: true, commitment: "finalized" });
srcKp = destinationKp;
console.log(`Transferred SOL to new wallet after buy and sell, https://solscan.io/tx/${sig}`);
break;
}
catch (error) {
k++;
}
}
}
}));
});
const distributeSol = (connection, mainKp, distritbutionNum) => __awaiter(void 0, void 0, void 0, function* () {
const data = [];
const wallets = [];
try {
const sendSolTx = [];
sendSolTx.push(web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({ units: 100000 }), web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({ microLamports: 250000 }));
const mainSolBal = yield connection.getBalance(mainKp.publicKey);
if (mainSolBal <= 4 * 10 ** 6) {
console.log("Main wallet balance is not enough");
return [];
}
let solAmount = 7_000_000
for (let i = 0; i < distritbutionNum; i++) {
const wallet = web3_js_1.Keypair.generate();
wallets.push({ kp: wallet, buyAmount: solAmount });
sendSolTx.push(web3_js_1.SystemProgram.transfer({
fromPubkey: mainKp.publicKey,
toPubkey: wallet.publicKey,
lamports: solAmount
}));
}
wallets.map((wallet) => {
data.push({
privateKey: bs58_1.default.encode(wallet.kp.secretKey),
pubkey: wallet.kp.publicKey.toBase58(),
});
});
try {
(0, utils_1.saveDataToFile)(data);
}
catch (error) {
}
let index = 0;
while (true) {
try {
if (index > 5) {
console.log("Error in distribution");
return null;
}
const siTx = new web3_js_1.Transaction().add(...sendSolTx);
const latestBlockhash = yield exports.solanaConnection.getLatestBlockhash();
siTx.feePayer = mainKp.publicKey;
siTx.recentBlockhash = latestBlockhash.blockhash;
const messageV0 = new web3_js_1.TransactionMessage({
payerKey: mainKp.publicKey,
recentBlockhash: latestBlockhash.blockhash,
instructions: sendSolTx,
}).compileToV0Message();
const transaction = new web3_js_1.VersionedTransaction(messageV0);
transaction.sign([mainKp]);
let txSig;
if (constants_1.JITO_MODE) {
txSig = yield (0, jito_1.executeJitoTx)([transaction], mainKp, jitoCommitment);
}
else {
txSig = yield (0, legacy_1.execute)(transaction, latestBlockhash, 1);
}
if (txSig) {
const distibuteTx = txSig ? `https://solscan.io/tx/${txSig}` : '';
console.log("SOL distributed ", distibuteTx);
break;
}
index++;
}
catch (error) {
index++;
}
}
console.log("Success in distribution");
return wallets;
}
catch (error) {
console.log(`Failed to transfer SOL`);
return null;
}
});
const buy = (newWallet, baseMint, buyAmount) => __awaiter(void 0, void 0, void 0, function* () {
let solBalance = 0;
try {
solBalance = yield exports.solanaConnection.getBalance(newWallet.publicKey);
}
catch (error) {
console.log("Error getting balance of wallet");
return null;
}
if (solBalance == 0) {
return null;
}
try {
let buyTx = yield (0, swapOnlyAmm_1.getBuyTxWithJupiter)(newWallet, baseMint, buyAmount);
if (buyTx == null) {
console.log(`Error getting buy transaction`);
return null;
}
let txSig;
if (constants_1.JITO_MODE) {
txSig = yield (0, jito_1.executeJitoTx)([buyTx], exports.mainKp, jitoCommitment);
}
else {
const latestBlockhash = yield exports.solanaConnection.getLatestBlockhash();
txSig = yield (0, legacy_1.execute)(buyTx, latestBlockhash, 1);
}
if (txSig) {
const tokenBuyTx = txSig ? `https://solscan.io/tx/${txSig}` : '';
console.log("Success in buy transaction: ", tokenBuyTx);
return tokenBuyTx;
}
else {
return null;
}
}
catch (error) {
return null;
}
});
const sell = (baseMint, wallet) => __awaiter(void 0, void 0, void 0, function* () {
try {
const data = (0, utils_1.readJson)();
if (data.length == 0) {
yield (0, utils_1.sleep)(1000);
return null;
}
const tokenAta = yield (0, spl_token_1.getAssociatedTokenAddress)(baseMint, wallet.publicKey);
const tokenBalInfo = yield exports.solanaConnection.getTokenAccountBalance(tokenAta);
if (!tokenBalInfo) {
console.log("Balance incorrect");
return null;
}
const tokenBalance = tokenBalInfo.value.amount;
try {
let sellTx = yield (0, swapOnlyAmm_1.getSellTxWithJupiter)(wallet, baseMint, tokenBalance);
if (sellTx == null) {
console.log(`Error getting buy transaction`);
return null;
}
let txSig;
if (constants_1.JITO_MODE) {
txSig = yield (0, jito_1.executeJitoTx)([sellTx], exports.mainKp, jitoCommitment);
}
else {
const latestBlockhash = yield exports.solanaConnection.getLatestBlockhash();
txSig = yield (0, legacy_1.execute)(sellTx, latestBlockhash, 1);
}
if (txSig) {
const tokenSellTx = txSig ? `https://solscan.io/tx/${txSig}` : '';
console.log("Success in sell transaction: ", tokenSellTx);
return tokenSellTx;
}
else {
return null;
}
}
catch (error) {
return null;
}
}
catch (error) {
return null;
}
});
main();