-
Notifications
You must be signed in to change notification settings - Fork 277
/
index.ts
190 lines (165 loc) · 5.19 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
import { NATIVE_MINT, getAssociatedTokenAddress } from '@solana/spl-token';
import {
Keypair,
PublicKey,
LAMPORTS_PER_SOL,
} from '@solana/web3.js';
import BN from 'bn.js';
import {
BUY_AMOUNT,
SWAP_ROUTING,
BUY_INTERVAL_MAX,
BUY_INTERVAL_MIN,
BUY_LOWER_AMOUNT,
BUY_UPPER_AMOUNT,
IS_RANDOM,
SOLANA_CONNECTION,
ADDITIONAL_FEE,
TOKEN_MINT,
POOL_ID
} from './config';
import { execute } from './utils/executor/legacy';
import { getBuyTx, getBuyTxWithJupiter, getSellTx, getSellTxWithJupiter } from './utils/swap';
import { readJson, sleep } from './utils/utils';
import base58 from 'bs58'
import { VersionedTransaction } from '@solana/web3.js';
const RETRY_DELAY_MS = 2000;
const MAX_RETRY = 10;
const main = () => {
const wallets: any[] = readJson("./wallets.json")
let temp_wallets: { kp: Keypair }[] = []
for (let i = 0; i < wallets.length; i++) {
temp_wallets.push({
kp: Keypair.fromSecretKey(base58.decode(wallets[i].privateKey)),
})
}
processWallets(
temp_wallets,
temp_wallets.length,
new PublicKey(TOKEN_MINT),
new PublicKey(POOL_ID)
)
}
const processWallets = async (
wallets: { kp: Keypair }[],
numUsedWallets: number,
baseMint: PublicKey,
poolId: PublicKey,
) => {
await Promise.all(
wallets.slice(0, numUsedWallets).map(async ({ kp }, i) => {
await sleep(((BUY_INTERVAL_MAX + BUY_INTERVAL_MIN) * i) / 2); // Optional delay
let active = true;
while (active) {
await performBuyAndSell(kp, baseMint, poolId, i);
await sleep(
5000 +
numUsedWallets * (Math.random() * (BUY_INTERVAL_MAX - BUY_INTERVAL_MIN) + BUY_INTERVAL_MIN),
);
}
}),
);
};
const performBuyAndSell = async (
kp: Keypair,
baseMint: PublicKey,
poolId: PublicKey,
index: number
) => {
const solBalance = (await SOLANA_CONNECTION.getBalance(kp.publicKey)) / LAMPORTS_PER_SOL;
if (solBalance < ADDITIONAL_FEE) {
console.log(`Wallet ${kp.publicKey.toBase58()} has insufficient balance (${solBalance} SOL).`);
return;
}
const buyAmount = IS_RANDOM
? Number((Math.random() * (BUY_UPPER_AMOUNT - BUY_LOWER_AMOUNT) + BUY_LOWER_AMOUNT).toFixed(6))
: BUY_AMOUNT;
console.log(`Wallet ${index}: Attempting to buy...`);
console.log("buyAmount", buyAmount)
await retryTransaction(() => buy(kp, baseMint, poolId, buyAmount), MAX_RETRY);
await sleep(10000);
console.log(`Wallet ${index}: Attempting to sell...`);
await retryTransaction(() => sell(kp, baseMint, poolId, true), MAX_RETRY);
};
const retryTransaction = async (fn: () => Promise<boolean>, maxRetry: number) => {
let attempt = 0;
while (attempt < maxRetry) {
const result = await fn();
if (result) {
// transaction successful
return;
}
attempt++;
console.error(`Transaction failed (Attempt ${attempt}/${maxRetry}), retrying...`);
await sleep(RETRY_DELAY_MS);
}
console.error('Max retry attempts reached, aborting transaction.');
};
const buy = async (newWallet: Keypair, baseMint: PublicKey, poolId: PublicKey, buyAmount: number): Promise<boolean> => {
let solBalance: number = 0;
try {
solBalance = await SOLANA_CONNECTION.getBalance(newWallet.publicKey);
} catch (error) {
console.log('Error getting balance of wallet');
return false;
}
if (solBalance == 0) {
return false;
}
try {
let tx;
if (SWAP_ROUTING) tx = await getBuyTxWithJupiter(newWallet, baseMint, buyAmount);
else tx = await getBuyTx(SOLANA_CONNECTION, newWallet, baseMint, NATIVE_MINT, buyAmount, poolId.toBase58());
if (tx == null) {
console.log(`Error getting buy transaction`);
return false;
}
const latestBlockHash = await SOLANA_CONNECTION.getLatestBlockhash();
const txSig = await execute(tx, latestBlockHash);
if(txSig === "") {
return false;
}else{
return true;
}
} catch (error) {
return false;
}
};
const sell = async (
wallet: Keypair,
baseMint: PublicKey,
poolId: PublicKey,
isHalfSell: boolean = false,
): Promise<boolean> => {
try {
const tokenAta = await getAssociatedTokenAddress(baseMint, wallet.publicKey);
const tokenBalInfo = await SOLANA_CONNECTION.getTokenAccountBalance(tokenAta);
console.log("tokenBalInfo", tokenBalInfo)
if (!tokenBalInfo) {
console.log('Balance incorrect');
return false;
}
let tokenBalance = tokenBalInfo.value.amount;
console.log(tokenBalance);
if(tokenBalance === '0') return false;
if (isHalfSell) tokenBalance = new BN(tokenBalInfo.value.amount).div(new BN(2)).toString();
let sellTx: VersionedTransaction | null;
if (SWAP_ROUTING) sellTx = await getSellTxWithJupiter(wallet, baseMint, tokenBalance);
else sellTx = await getSellTx(SOLANA_CONNECTION, wallet, baseMint, NATIVE_MINT, tokenBalance, poolId.toBase58());
if (sellTx == null) {
console.log(`Error getting sell transaction`);
return false;
}
const latestBlockHashForSell = await SOLANA_CONNECTION.getLatestBlockhash();
const txSig = await execute(sellTx, latestBlockHashForSell, false);
if(txSig === "") {
return false;
}else{
return true;
}
} catch (error) {
console.log("Error sell=>", error)
return false;
}
};
main()