-
Notifications
You must be signed in to change notification settings - Fork 3
/
gather.ts
78 lines (70 loc) · 3.3 KB
/
gather.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
import base58 from "bs58"
import { readJson, retrieveEnvVariable, sleep } from "./src/utils"
import { ComputeBudgetProgram, Connection, Keypair, SystemProgram, Transaction, TransactionInstruction, sendAndConfirmTransaction } from "@solana/web3.js"
import { TOKEN_PROGRAM_ID, createAssociatedTokenAccountIdempotentInstruction, createCloseAccountInstruction, createTransferCheckedInstruction, getAssociatedTokenAddress } from "@solana/spl-token";
import { SPL_ACCOUNT_LAYOUT, TokenAccount } from "@raydium-io/raydium-sdk";
const rpcUrl = retrieveEnvVariable("RPC_URL");
const mainKpStr = retrieveEnvVariable('MAIN_KP');
const connection = new Connection(rpcUrl, { commitment: "processed" });
const mainKp = Keypair.fromSecretKey(base58.decode(mainKpStr))
const main = async () => {
const walletsStr = readJson()
const wallets = walletsStr.map(walletStr => Keypair.fromSecretKey(base58.decode(walletStr)))
wallets.map(async (kp, i) => {
try {
await sleep(i * 100)
const accountInfo = await connection.getAccountInfo(kp.publicKey)
const tokenAccounts = await connection.getTokenAccountsByOwner(kp.publicKey, {
programId: TOKEN_PROGRAM_ID,
},
"confirmed"
)
const ixs: TransactionInstruction[] = []
const accounts: TokenAccount[] = [];
if (accountInfo) {
const solBal = await connection.getBalance(kp.publicKey)
ixs.push(
SystemProgram.transfer({
fromPubkey: kp.publicKey,
toPubkey: mainKp.publicKey,
lamports: solBal
})
)
}
if (tokenAccounts.value.length > 0)
for (const { pubkey, account } of tokenAccounts.value) {
accounts.push({
pubkey,
programId: account.owner,
accountInfo: SPL_ACCOUNT_LAYOUT.decode(account.data),
});
}
for (let j = 0; j < accounts.length; j++) {
const baseAta = await getAssociatedTokenAddress(accounts[j].accountInfo.mint, mainKp.publicKey)
const tokenAccount = accounts[j].pubkey
const tokenBalance = (await connection.getTokenAccountBalance(accounts[j].pubkey)).value
ixs.push(createAssociatedTokenAccountIdempotentInstruction(mainKp.publicKey, baseAta, mainKp.publicKey, accounts[j].accountInfo.mint))
ixs.push(createTransferCheckedInstruction(tokenAccount, accounts[j].accountInfo.mint, baseAta, kp.publicKey, BigInt(tokenBalance.amount), tokenBalance.decimals))
ixs.push(createCloseAccountInstruction(tokenAccount, mainKp.publicKey, kp.publicKey))
}
// console.log('----------------------')
if (ixs.length) {
const tx = new Transaction().add(
ComputeBudgetProgram.setComputeUnitPrice({ microLamports: 220_000 }),
ComputeBudgetProgram.setComputeUnitLimit({ units: 350_000 }),
...ixs,
)
tx.feePayer = mainKp.publicKey
tx.recentBlockhash = (await connection.getLatestBlockhash()).blockhash
// console.log(await connection.simulateTransaction(tx))
const sig = await sendAndConfirmTransaction(connection, tx, [mainKp, kp], { commitment: "confirmed" })
console.log(`Closed and gathered SOL from wallets ${i} : https://solscan.io/tx/${sig}`)
return
}
} catch (error) {
console.log("transaction error : ")
return
}
})
}
main()