-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
236 lines (216 loc) · 7.04 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
import {
createCloseAccountInstruction,
getAssociatedTokenAddress,
getAssociatedTokenAddressSync,
} from '@solana/spl-token'
import {
Keypair,
Connection,
PublicKey,
LAMPORTS_PER_SOL,
SystemProgram,
VersionedTransaction,
TransactionInstruction,
TransactionMessage,
ComputeBudgetProgram,
Transaction,
sendAndConfirmTransaction,
Commitment
} 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,
JITO_MODE,
SOL_AMOUNT_TO_DISTRIBUTE,
DISTRIBUTE_INTERVAL_MIN,
DISTRIBUTE_INTERVAL_MAX
} from './constants'
import { Data, readJson, saveDataToFile, saveNewFile, sleep } from './utils'
import base58 from 'bs58'
import { getBuyTxWithJupiter, getSellTxWithJupiter } from './utils/swapOnlyAmm'
import { execute } from './executor/legacy'
import { executeJitoTx } from './executor/jito'
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 distritbutionNum = DISTRIBUTE_WALLET_NUM > 20 ? 20 : DISTRIBUTE_WALLET_NUM
const jitoCommitment: Commitment = "confirmed"
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`)
if (solBalance < SOL_AMOUNT_TO_DISTRIBUTE * LAMPORTS_PER_SOL) {
console.log("Sol balance is not enough for distribution")
}
// main part
for (; ;) {
try {
console.log("---- New round of distribution ---- \n")
let data: {
kp: Keypair;
buyAmount: number;
}[] | null = null
data = await distributeSol(solanaConnection, mainKp, distritbutionNum)
if (data == null || data.length == 0) {
console.log("Distribution failed")
continue
}
const interval = Math.floor((DISTRIBUTE_INTERVAL_MIN + Math.random() * (DISTRIBUTE_INTERVAL_MAX - DISTRIBUTE_INTERVAL_MIN)) * 1000)
await sleep(interval)
} catch (error) {
console.log("Error in one of the steps")
}
}
}
const distributeSol = async (connection: Connection, mainKp: Keypair, distritbutionNum: number) => {
const data: Data[] = []
const wallets = []
try {
const sendSolTx: TransactionInstruction[] = []
sendSolTx.push(
ComputeBudgetProgram.setComputeUnitPrice({ microLamports: 1_000_000 }),
ComputeBudgetProgram.setComputeUnitLimit({ units: 12_000 })
)
const mainSolBal = await connection.getBalance(mainKp.publicKey)
if (mainSolBal <= 8 * 10 ** 6 * DISTRIBUTE_WALLET_NUM) {
console.log("Main wallet balance is not enough")
return []
}
let solAmount = Math.floor(SOL_AMOUNT_TO_DISTRIBUTE * 10 ** 9 / distritbutionNum)
try {
saveDataToFile(data)
} catch (error) {
console.log("DistributeSol tx error")
}
try {
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])
let txSig
if (JITO_MODE) {
txSig = await executeJitoTx([transaction], mainKp, jitoCommitment)
} else {
txSig = await execute(transaction, latestBlockhash, 1)
}
if (txSig) {
const distibuteTx = txSig ? `https://solscan.io/tx/${txSig}` : ''
console.log("SOL distributed ", distibuteTx)
}
} catch (error) {
console.log("Distribution error")
return null
}
console.log("Success in distribution")
return wallets
} catch (error) {
console.log(`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")
return null
}
if (solBalance == 0) {
return null
}
try {
let buyTx = await getBuyTxWithJupiter(newWallet, baseMint, buyAmount)
if (buyTx == null) {
console.log(`Error getting buy transaction`)
return null
}
let txSig
if (JITO_MODE) {
txSig = await executeJitoTx([buyTx], mainKp, jitoCommitment)
} else {
const latestBlockhash = await solanaConnection.getLatestBlockhash()
txSig = await 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) {
console.log("Buy transaction 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.amount
try {
let sellTx = await getSellTxWithJupiter(wallet, baseMint, tokenBalance)
if (sellTx == null) {
console.log(`Error getting buy transaction`)
return null
}
let txSig
if (JITO_MODE) {
txSig = await executeJitoTx([sellTx], mainKp, jitoCommitment)
} else {
const latestBlockhash = await solanaConnection.getLatestBlockhash()
txSig = await 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) {
console.log("Sell transaction error")
return null
}
} catch (error) {
return null
}
}
main()