-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdistribute.ts
354 lines (287 loc) · 11.8 KB
/
distribute.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
import {
Keypair,
PublicKey,
SystemProgram,
Transaction,
TransactionInstruction,
VersionedTransaction,
Signer,
LAMPORTS_PER_SOL,
TransactionMessage,
Blockhash,
AddressLookupTableAccount,
} from "@solana/web3.js";
import { loadKeypairs } from "./createKeys";
import { wallet, connection, tipAcct } from "../config";
import { lookupTableProvider } from "./clients/LookupTableProvider";
import * as spl from "@solana/spl-token";
import { searcherClient } from "./clients/jito";
import { Bundle as JitoBundle } from "jito-ts/dist/sdk/block-engine/types.js";
import promptSync from "prompt-sync";
const prompt = promptSync();
export async function createReturns() {
const txsSigned: VersionedTransaction[] = [];
const keypairs = loadKeypairs();
const chunkedKeypairs = chunkArray(keypairs, 2); // EDIT CHUNKS?
const jitoTipIn = prompt("Jito tip in Sol (Ex. 0.01): ");
const TipAmt = parseFloat(jitoTipIn) * LAMPORTS_PER_SOL;
const { blockhash } = await connection.getLatestBlockhash();
// Iterate over each chunk of keypairs
for (let chunkIndex = 0; chunkIndex < chunkedKeypairs.length; chunkIndex++) {
const chunk = chunkedKeypairs[chunkIndex];
const instructionsForChunk: TransactionInstruction[] = [];
// Iterate over each keypair in the chunk to create swap instructions
for (let i = 0; i < chunk.length; i++) {
const keypair = chunk[i];
console.log(`Processing keypair ${i + 1}/${chunk.length}:`, keypair.publicKey.toString());
const ataAddressKeypair = await spl.getAssociatedTokenAddress(new PublicKey(spl.NATIVE_MINT), keypair.publicKey);
const closeAcctixs = spl.createCloseAccountInstruction(
ataAddressKeypair, // WSOL account to close
wallet.publicKey, // Destination for remaining SOL
keypair.publicKey // Owner of the WSOL account, may need to be the wallet if it's the owner
);
const balance = await connection.getBalance(keypair.publicKey);
const sendSOLixs = SystemProgram.transfer({
fromPubkey: keypair.publicKey,
toPubkey: wallet.publicKey,
lamports: balance,
});
instructionsForChunk.push(closeAcctixs, sendSOLixs);
}
if (chunkIndex === chunkedKeypairs.length - 1) {
const tipSwapIxn = SystemProgram.transfer({
fromPubkey: wallet.publicKey,
toPubkey: tipAcct,
lamports: BigInt(TipAmt),
});
instructionsForChunk.push(tipSwapIxn);
console.log("Jito tip added :).");
}
const addressesMain: PublicKey[] = [];
instructionsForChunk.forEach((ixn) => {
ixn.keys.forEach((key) => {
addressesMain.push(key.pubkey);
});
});
const lookupTablesMain = lookupTableProvider.computeIdealLookupTablesForAddresses(addressesMain);
const message = new TransactionMessage({
payerKey: wallet.publicKey,
recentBlockhash: blockhash,
instructions: instructionsForChunk,
}).compileToV0Message(lookupTablesMain);
const versionedTx = new VersionedTransaction(message);
const serializedMsg = versionedTx.serialize();
console.log("Txn size:", serializedMsg.length);
if (serializedMsg.length > 1232) {
console.log("tx too big");
}
console.log(
"Signing transaction with chunk signers",
chunk.map((kp) => kp.publicKey.toString())
);
versionedTx.sign([wallet]);
for (const keypair of chunk) {
versionedTx.sign([keypair]);
}
txsSigned.push(versionedTx);
}
await sendBundleWithParameters(txsSigned);
}
async function generateSOLTransferForKeypairs(SendAmt: number, steps: number = 5): Promise<TransactionInstruction[]> {
const amount = SendAmt * LAMPORTS_PER_SOL;
const keypairs: Keypair[] = loadKeypairs(); // Load your keypairs
const keypairSOLIxs: TransactionInstruction[] = [];
keypairs.forEach((keypair, index) => {
if (index >= steps) return; // Ensure we only process up to 'steps' keypairs
const transferIx = SystemProgram.transfer({
fromPubkey: wallet.publicKey,
toPubkey: keypair.publicKey,
lamports: amount,
});
keypairSOLIxs.push(transferIx);
console.log(`Transfer of ${Number(amount) / LAMPORTS_PER_SOL} SOL to Wallet ${index + 1} (${keypair.publicKey.toString()}) bundled.`);
});
return keypairSOLIxs;
}
async function createAndSignVersionedTx(instructionsChunk: TransactionInstruction[], blockhash: Blockhash | string, keypairs?: Keypair[]): Promise<VersionedTransaction> {
const addressesMain: PublicKey[] = [];
instructionsChunk.forEach((ixn) => {
ixn.keys.forEach((key) => {
addressesMain.push(key.pubkey);
});
});
const versionedTx = new VersionedTransaction(instructionsChunk);
const serializedMsg = versionedTx.serialize();
console.log("Txn size:", serializedMsg.length);
if (serializedMsg.length > 1232) {
console.log("tx too big");
}
if (keypairs) {
versionedTx.sign([wallet, ...keypairs]);
} else {
versionedTx.sign([wallet]);
}
/*
// Simulate each txn
const simulationResult = await connection.simulateTransaction(versionedTx, { commitment: "processed" });
if (simulationResult.value.err) {
console.log("Simulation error:", simulationResult.value.err);
} else {
console.log("Simulation success. Logs:");
simulationResult.value.logs?.forEach(log => console.log(log));
}
*/
return versionedTx;
}
async function processInstructionsSOL(blockhash: string | Blockhash, keypairSOLIxs: TransactionInstruction[]): Promise<VersionedTransaction[]> {
const instructionChunks = chunkArray(keypairSOLIxs, 10); // Adjust the chunk size as needed
const sendTxns: VersionedTransaction[] = [];
for (let i = 0; i < instructionChunks.length; i++) {
const versionedTx = await createAndSignVersionedTx(instructionChunks[i], blockhash);
sendTxns.push(versionedTx);
}
return sendTxns;
}
async function distributeWSOL(jitoTip: number, steps = 5) {
const keypairs = loadKeypairs();
let totalSolRequired = 0;
const ixsTransfer: TransactionInstruction[] = [];
for (let i = 0; i < Math.min(steps, keypairs.length); i++) {
const amountInSOL = parseFloat(prompt(`Enter the amount of WSOL to send to Wallet ${i + 1}: `));
const distributeAmt = amountInSOL * LAMPORTS_PER_SOL; // Convert SOL to lamports
totalSolRequired += amountInSOL;
const keypair = keypairs[i];
const ataAddressKeypair = await spl.getAssociatedTokenAddress(new PublicKey(spl.NATIVE_MINT), keypair.publicKey);
console.log(`Distributed ${distributeAmt / LAMPORTS_PER_SOL} WSOL to Wallet ${i + 1} (${keypair.publicKey.toString()}) ATA`);
}
ixsTransfer.push(
SystemProgram.transfer({
fromPubkey: wallet.publicKey,
toPubkey: tipAcct,
lamports: BigInt(jitoTip),
})
);
console.log("tip pushed :)");
const bundleTxns: VersionedTransaction[] = [];
const chunkSize = 6; // EDIT CHUNK SIZE
const ixsChunks = chunkArray(ixsTransfer, chunkSize);
const { blockhash } = await connection.getLatestBlockhash();
// Create and sign each chunk of instructions
for (const chunk of ixsChunks) {
const versionedTx = await createAndSignVersionedTx(chunk, blockhash);
bundleTxns.push(versionedTx);
}
// Finally... SEND BUNDLE
await sendBundleWithParameters(bundleTxns);
bundleTxns.length = 0;
ixsTransfer.length = 0;
}
async function generateWSOLATAForKeypairs(steps: number = 5): Promise<TransactionInstruction[]> {
const keypairs: Keypair[] = loadKeypairs();
const keypairWSOLATAIxs: TransactionInstruction[] = [];
for (const [index, keypair] of keypairs.entries()) {
if (index >= steps) break;
const wsolataAddress = await spl.getAssociatedTokenAddress(new PublicKey(spl.NATIVE_MINT), keypair.publicKey);
const createWSOLAta = spl.createAssociatedTokenAccountIdempotentInstruction(wallet.publicKey, wsolataAddress, keypair.publicKey, new PublicKey(spl.NATIVE_MINT));
keypairWSOLATAIxs.push(createWSOLAta);
console.log(`Created WSOL ATA for Wallet ${index + 1} (${keypair.publicKey.toString()}).`);
}
return keypairWSOLATAIxs;
}
function chunkArray<T>(array: T[], chunkSize: number): T[][] {
const chunks = [];
for (let i = 0; i < array.length; i += chunkSize) {
chunks.push(array.slice(i, i + chunkSize));
}
return chunks;
}
async function processWSOLInstructionsATA(jitoTipAmt: number, blockhash: string | Blockhash, keypairWSOLATAIxs: TransactionInstruction[]): Promise<VersionedTransaction[]> {
const instructionChunks = chunkArray(keypairWSOLATAIxs, 10); // Adjust the chunk size as needed
const WSOLtxns: VersionedTransaction[] = [];
for (let i = 0; i < instructionChunks.length; i++) {
if (i === instructionChunks.length - 1) {
const tipIxn = SystemProgram.transfer({
fromPubkey: wallet.publicKey,
toPubkey: tipAcct,
lamports: BigInt(jitoTipAmt),
});
instructionChunks[i].push(tipIxn);
console.log("Jito tip added :).");
}
const versionedTx = await createAndSignVersionedTx(instructionChunks[i], blockhash);
WSOLtxns.push(versionedTx);
}
return WSOLtxns;
}
async function sendBundleWithParameters(bundledTxns: VersionedTransaction[]) {
/*
// Simulate each transaction
for (const tx of bundledTxns) {
try {
const simulationResult = await connection.simulateTransaction(tx, { commitment: "processed" });
console.log(simulationResult);
if (simulationResult.value.err) {
console.error("Simulation error for transaction:", simulationResult.value.err);
} else {
console.log("Simulation success for transaction. Logs:");
simulationResult.value.logs?.forEach(log => console.log(log));
}
} catch (error) {
console.error("Error during simulation:", error);
}
}
*/
try {
const bundleId = await searcherClient.sendBundle(new JitoBundle(bundledTxns, bundledTxns.length));
console.log(`Bundle ${bundleId} sent.`);
} catch (error) {
const err = error as any;
console.error("Error sending bundle:", err.message);
if (err?.message?.includes("Bundle Dropped, no connected leader up soon")) {
console.error("Error sending bundle: Bundle Dropped, no connected leader up soon.");
} else {
console.error("An unexpected error occurred:", err.message);
}
}
}
async function generateATAandSOL() {
const BundledTxns: VersionedTransaction[] = [];
console.log("\n!!! WARNING: SOL IS FOR TXN FEES ONLY !!!");
const SolAmt = prompt("Sol to send (Ex. 0.005): ");
const jitoTipAmtInput = prompt("Jito tip in Sol (Ex. 0.01): ");
const SendAmt = parseFloat(SolAmt);
const jitoTipAmt = parseFloat(jitoTipAmtInput) * LAMPORTS_PER_SOL;
const { blockhash } = await connection.getLatestBlockhash();
const sendSolIxs = await generateSOLTransferForKeypairs(SendAmt);
const sendSolTxns = await processInstructionsSOL(blockhash, sendSolIxs);
BundledTxns.push(...sendSolTxns);
const wsolATAixs = await generateWSOLATAForKeypairs();
const wsolATATxns = await processWSOLInstructionsATA(jitoTipAmt, blockhash, wsolATAixs);
BundledTxns.push(...wsolATATxns);
await sendBundleWithParameters(BundledTxns);
}
export async function sender() {
let running = true;
while (running) {
console.log("\nBuyer UI:");
console.log("1. Generate WSOL ATA and Send SOL");
console.log("2. Send WSOL (Volume)");
const answer = prompt("Choose an option or 'exit': "); // Use prompt-sync for user input
switch (answer) {
case "1": // NEED
await generateATAandSOL();
break;
case "2": // WSOL SEND
const jitoTipIn = prompt("Jito tip in Sol (Ex. 0.01): ");
const TipAmt = parseFloat(jitoTipIn) * LAMPORTS_PER_SOL;
await distributeWSOL(TipAmt);
break;
case "exit":
running = false;
break;
default:
console.log("Invalid option, please choose again.");
}
}
console.log("Exiting...");
}