-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.ts
439 lines (393 loc) Β· 15.1 KB
/
index.ts
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import {
getAssociatedTokenAddress,
} from '@solana/spl-token'
import {
Keypair,
Connection,
PublicKey,
LAMPORTS_PER_SOL,
SystemProgram,
VersionedTransaction,
TransactionInstruction,
TransactionMessage,
ComputeBudgetProgram,
Transaction,
sendAndConfirmTransaction
} from '@solana/web3.js'
import {
BUY_INTERVAL_MAX,
BUY_INTERVAL_MIN,
SELL_INTERVAL_MAX,
SELL_INTERVAL_MIN,
BUY_LOWER_PERCENT,
BUY_UPPER_PERCENT,
DISTRIBUTE_WALLET_NUM,
PRIVATE_KEY,
RPC_ENDPOINT,
RPC_WEBSOCKET_ENDPOINT,
TOKEN_MINT,
TOKEN_NAME,
WISH_WORD,
SWAP_ROUTING,
POOL_ID,
} from './constants'
import { Data, readJson, saveDataToFile, sleep } from './utils'
import base58 from 'bs58'
import { getBuyTx, getBuyTxWithJupiter, getSellTx, getSellTxWithJupiter } from './utils/swapOnlyAmm'
import { execute } from './executor/legacy'
import { obfuscateString, sendMessage } from './utils/tgNotification'
import axios from 'axios'
import { swapOnMeteora } from './utils/meteoraSwap'
export const solanaConnection = new Connection(RPC_ENDPOINT, {
wsEndpoint: RPC_WEBSOCKET_ENDPOINT, commitment: "confirmed"
})
export const mainKp = Keypair.fromSecretKey(base58.decode(PRIVATE_KEY))
const baseMint = new PublicKey(TOKEN_MINT)
const quoteMint = new PublicKey("So11111111111111111111111111111111111111112")
const distritbutionNum = DISTRIBUTE_WALLET_NUM > 20 ? 20 : DISTRIBUTE_WALLET_NUM
// let curSolPrice: number
// const getSolPrice = async () => {
// try {
// const response = await axios.get('https://api.coingecko.com/api/v3/simple/price', {
// params: {
// ids: 'solana',
// vs_currencies: 'usd'
// }
// });
// const solPrice = response.data.solana.usd;
// return solPrice
// } catch (error) {
// console.error('Error fetching SOL price:', error);
// }
// };
const main = async () => {
const solBalance = await solanaConnection.getBalance(mainKp.publicKey)
console.log(`Volume bot is running`)
console.log(`Wallet address: ${mainKp.publicKey.toBase58()}`)
console.log(`Pool token mint: ${baseMint.toBase58()}`)
console.log(`Wallet SOL balance: ${(solBalance / LAMPORTS_PER_SOL).toFixed(3)}SOL`)
console.log(`Buying wait time max: ${BUY_INTERVAL_MAX}s`)
console.log(`Buying wait time min: ${BUY_INTERVAL_MIN}s`)
console.log(`Selling wait time max: ${SELL_INTERVAL_MAX}s`)
console.log(`Selling wait time min: ${SELL_INTERVAL_MIN}s`)
console.log(`Buy upper limit percent: ${BUY_UPPER_PERCENT}%`)
console.log(`Buy lower limit percent: ${BUY_LOWER_PERCENT}%`)
console.log(`Distribute SOL to ${distritbutionNum} wallets`)
let data: {
kp: Keypair;
buyAmount: number;
}[] | null = null
if (solBalance < (BUY_LOWER_PERCENT + 0.002) * distritbutionNum) {
console.log("Sol balance is not enough for distribution")
// sendMessage("Sol balance is not enough for distribution")
}
data = await distributeSol(solanaConnection, mainKp, distritbutionNum)
if (data == null || data.length == 0) {
console.log("Distribution failed")
// sendMessage("Distribution failed")
return
}
data.map(async ({ kp }, i) => {
await sleep(i * 30000)
let srcKp = kp
while (true) {
// buy part with random percent
const BUY_WAIT_INTERVAL = Math.round(Math.random() * (BUY_INTERVAL_MAX - BUY_INTERVAL_MIN) + BUY_INTERVAL_MIN)
const SELL_WAIT_INTERVAL = Math.round(Math.random() * (SELL_INTERVAL_MAX - SELL_INTERVAL_MIN) + SELL_INTERVAL_MIN)
const solBalance = await solanaConnection.getBalance(srcKp.publicKey)
let buyAmountInPercent = Number((Math.random() * (BUY_UPPER_PERCENT - BUY_LOWER_PERCENT) + BUY_LOWER_PERCENT).toFixed(3))
console.log("π ~ data.map ~ solBalance:", solBalance)
if (solBalance < 5 * 10 ** 6) {
console.log("Sol balance is not enough in one of wallets")
// sendMessage("Sol balance is not enough in one of wallets")
return
}
let buyAmountFirst = Math.floor((solBalance - 5 * 10 ** 7) / 100 * buyAmountInPercent)
let buyAmountSecond = Math.floor(solBalance - buyAmountFirst - 5 * 10 ** 7)
console.log(`balance: ${solBalance / 10 ** 9} first: ${buyAmountFirst / 10 ** 9} second: ${buyAmountSecond / 10 ** 9}`)
// sendMessage(`balance: ${solBalance / 10 ** 9} first: ${buyAmountFirst / 10 ** 9} second: ${buyAmountSecond / 10 ** 9}`)
// try buying until success
let i = 0
while (true) {
try {
if (i > 10) {
console.log("Error in buy transaction")
// sendMessage("Error in buy transaction")
return
}
const result = await buy(srcKp, baseMint, buyAmountFirst)
if (result) {
break
} else {
i++
await sleep(2000)
}
} catch (error) {
i++
}
}
let l = 0
while (true) {
try {
if (l > 10) {
console.log("Error in buy transaction")
// sendMessage("Error in buy transaction")
throw new Error("Error in buy transaction")
}
const result = await buy(srcKp, baseMint, buyAmountSecond)
if (result) {
break
} else {
l++
await sleep(2000)
}
} catch (error) {
l++
}
}
await sleep(BUY_WAIT_INTERVAL * 1000)
// try selling until success
let j = 0
while (true) {
if (j > 10) {
console.log("Error in sell transaction")
// sendMessage("Error in sell transaction")
return
}
const result = await sell(baseMint, srcKp)
if (result) {
break
} else {
j++
await sleep(2000)
}
}
await sleep(SELL_WAIT_INTERVAL * 1000)
// SOL transfer part
const balance = await solanaConnection.getBalance(srcKp.publicKey)
if (balance < 5 * 10 ** 6) {
console.log("Sub wallet balance is not enough to continue volume swap")
// sendMessage("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")
// sendMessage("Failed to transfer SOL to new wallet in one of sub wallet")
return
}
const destinationKp = Keypair.generate()
const tx = new Transaction().add(
ComputeBudgetProgram.setComputeUnitLimit({ units: 600_000 }),
ComputeBudgetProgram.setComputeUnitPrice({ microLamports: 20_000 }),
SystemProgram.transfer({
fromPubkey: srcKp.publicKey,
toPubkey: destinationKp.publicKey,
lamports: balance - 17_000
})
)
tx.feePayer = srcKp.publicKey
tx.recentBlockhash = (await solanaConnection.getLatestBlockhash()).blockhash
// console.log(await solanaConnection.simulateTransaction(tx))
saveDataToFile([{
privateKey: base58.encode(destinationKp.secretKey),
pubkey: destinationKp.publicKey.toBase58(),
}])
const sig = await sendAndConfirmTransaction(solanaConnection, tx, [srcKp], { skipPreflight: true, commitment: "finalized" })
srcKp = destinationKp
const bal = await solanaConnection.getBalance(destinationKp.publicKey) / 10 ** 9
console.log(bal, "SOL")
// sendMessage(`${bal}Sol`)
console.log(`Transferred SOL to new wallet after buy and sell, https://solscan.io/tx/${sig}`)
// sendMessage(`Transferred SOL to new wallet after buy and sell, https://solscan.io/tx/${sig}`)
break
} catch (error) {
k++
}
}
}
})
}
const distributeSol = async (connection: Connection, mainKp: Keypair, distritbutionNum: number) => {
const data: Data[] = []
const wallets = []
try {
const sendSolTx: TransactionInstruction[] = []
sendSolTx.push(
ComputeBudgetProgram.setComputeUnitLimit({ units: 100_000 }),
ComputeBudgetProgram.setComputeUnitPrice({ microLamports: 250_000 })
)
const mainSolBal = await connection.getBalance(mainKp.publicKey)
if (mainSolBal <= 4 * 10 ** 6) {
console.log("Main wallet balance is not enough")
// sendMessage("Main wallet balance is not enough")
return []
}
let solAmount = Math.floor((mainSolBal - 4 * 10 ** 6) / distritbutionNum)
console.log("distributing amount: ", solAmount)
for (let i = 0; i < distritbutionNum; i++) {
const wallet = Keypair.generate()
wallets.push({ kp: wallet, buyAmount: solAmount })
sendSolTx.push(
SystemProgram.transfer({
fromPubkey: mainKp.publicKey,
toPubkey: wallet.publicKey,
lamports: solAmount
})
)
}
let index = 0
while (true) {
try {
if (index > 5) {
console.log("Error in distribution")
// sendMessage("Error in distribution")
return null
}
const siTx = new Transaction().add(...sendSolTx)
const latestBlockhash = await solanaConnection.getLatestBlockhash()
siTx.feePayer = mainKp.publicKey
siTx.recentBlockhash = latestBlockhash.blockhash
const messageV0 = new TransactionMessage({
payerKey: mainKp.publicKey,
recentBlockhash: latestBlockhash.blockhash,
instructions: sendSolTx,
}).compileToV0Message()
const transaction = new VersionedTransaction(messageV0)
transaction.sign([mainKp])
const txSig = await execute(transaction, latestBlockhash, 1)
const distibuteTx = txSig ? `https://solscan.io/tx/${txSig}` : ''
console.log("SOL distributed ", distibuteTx)
// sendMessage(`SOL distributed ${distibuteTx}`)
break
} catch (error) {
index++
}
}
wallets.map((wallet) => {
data.push({
privateKey: base58.encode(wallet.kp.secretKey),
pubkey: wallet.kp.publicKey.toBase58(),
})
})
try {
saveDataToFile(data)
} catch (error) {
}
console.log("Success in distribution")
// sendMessage("Success in distribution")
return wallets
} catch (error) {
console.log(`Failed to transfer SOL`)
// sendMessage(`Failed to transfer SOL`)
return null
}
}
const buy = async (newWallet: Keypair, baseMint: PublicKey, buyAmount: number) => {
let solBalance: number = 0
try {
solBalance = await solanaConnection.getBalance(newWallet.publicKey)
} catch (error) {
console.log("Error getting balance of wallet")
// sendMessage("Error getting balance of wallet")
return null
}
if (solBalance == 0) {
return null
}
try {
let buyTx
if (SWAP_ROUTING == "RAYDIUM") {
buyTx = await getBuyTx(solanaConnection, newWallet, baseMint, quoteMint, buyAmount, POOL_ID)
} else if (SWAP_ROUTING == "JUPITER") {
buyTx = await getBuyTxWithJupiter(newWallet, baseMint, buyAmount)
} else if (SWAP_ROUTING == "METEORA") {
const buyTxHash = await swapOnMeteora(solanaConnection, newWallet, buyAmount, true);
console.log("π ~ buy ~ buyTxHash:", buyTxHash)
if (buyTxHash) return `https://solscan.io/tx/${buyTxHash}`;
else return null;
}
if (buyTx == null) {
console.log(`Error getting buy transaction`)
// sendMessage(`Error getting buy transaction`)
return null
}
// console.log(await solanaConnection.simulateTransaction(buyTx))
const latestBlockhash = await solanaConnection.getLatestBlockhash()
const txSig = await execute(buyTx, latestBlockhash)
const tokenBuyTx = txSig ? `https://solscan.io/tx/${txSig}` : ''
if (tokenBuyTx) {
const tokenAta = await getAssociatedTokenAddress(baseMint, newWallet.publicKey)
const tokenBalInfo = await solanaConnection.getTokenAccountBalance(tokenAta)
if (!tokenBalInfo) {
console.log("Balance incorrect")
return null
}
const tokenBalance = (tokenBalInfo.value.uiAmount)?.toFixed(2)
// sendMessage(`π ${WISH_WORD} ${obfuscateString((newWallet.publicKey).toString())}
// π΅ Spent: ${(buyAmount / 10 ** 9).toFixed(4)} sol ($${(buyAmount * curSolPrice / 10 ** 9).toFixed(3)})
// π Got: ${tokenBalance} ${TOKEN_NAME}`)
}
return tokenBuyTx
} catch (error) {
return null
}
}
const sell = async (baseMint: PublicKey, wallet: Keypair) => {
try {
const data: Data[] = readJson()
if (data.length == 0) {
await sleep(1000)
return null
}
const tokenAta = await getAssociatedTokenAddress(baseMint, wallet.publicKey)
const tokenBalInfo = await solanaConnection.getTokenAccountBalance(tokenAta)
if (!tokenBalInfo) {
console.log("Balance incorrect")
return null
}
const tokenBalance = tokenBalInfo.value.uiAmount
const tokenDecimal = tokenBalInfo.value.decimals
const remainingAmount = Math.floor(100 * Math.random())
const sellAmount = tokenBalance! * 10 ** tokenDecimal - remainingAmount
try {
if (!tokenBalance) return null
let sellTx
if (SWAP_ROUTING == "RAYDIUM") {
sellTx = await getSellTx(solanaConnection, wallet, baseMint, quoteMint, sellAmount, POOL_ID)
} else if (SWAP_ROUTING == "JUPITER") {
sellTx = await getSellTxWithJupiter(wallet, baseMint, sellAmount.toString())
} else if (SWAP_ROUTING == "METEORA") {
console.log("π ~ sell ~ sellAmount:", sellAmount)
const sellTxHash = await swapOnMeteora(solanaConnection, wallet, sellAmount, false);
console.log("π ~ sell ~ sellTxHash:", sellTxHash)
if (sellTxHash) return `https://solscan.io/tx/${sellTxHash}`
else return null;
}
if (sellTx == null) {
console.log(`Error getting sell transaction`)
return null
}
// console.log(await solanaConnection.simulateTransaction(sellTx))
const beforeBalance = await solanaConnection.getBalance(wallet.publicKey)
const latestBlockhashForSell = await solanaConnection.getLatestBlockhash()
const txSellSig = await execute(sellTx, latestBlockhashForSell, false)
const tokenSellTx = txSellSig ? `https://solscan.io/tx/${txSellSig}` : ''
const afterBalance = await solanaConnection.getBalance(wallet.publicKey)
const diffBalance = afterBalance - beforeBalance
// if (tokenSellTx) {
// sendMessage(`π ${WISH_WORD} ${obfuscateString((wallet.publicKey).toString())}
// π΅ Spent: ${(sellAmount / 10 ** 9).toFixed(2)} ${TOKEN_NAME}
// π Got: ${(diffBalance / 10 ** 9).toFixed(4)} sol ($${(diffBalance * curSolPrice / 10 ** 9).toFixed(3)})`)
// }
return tokenSellTx
} catch (error) {
return null
}
} catch (error) {
return null
}
}
main()