-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
225 lines (183 loc) · 8.32 KB
/
index.js
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
const { Keypair, Connection, Transaction, PublicKey,
sendAndConfirmTransaction,
SystemProgram,
Wallet
} = require('@solana/web3.js');
const {
createTransferInstruction,
getOrCreateAssociatedTokenAccount,
TOKEN_PROGRAM_ID, } = require('@solana/spl-token')
const bs58 = require('bs58');
const fetch = require('node-fetch');
const fs = require('fs');
const delay = require('delay');
require('dotenv').config()
const rpcUrl = process.env.RPC_URL;
const connection = new Connection(rpcUrl, 'max');
const tokenAddress = process.env.TOKEN_ADDRESS;
const tokenSymbol = process.env.TOKEN_SYMBOL;
const checkToken = (payload) => new Promise((resolve, reject) => {
fetch('https://api.mainnet-beta.solana.com', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
})
.then(res => res.json())
.then(res => resolve(res))
.catch(err => reject(err))
});
function solToLamports(solAmount) {
const lamportsPerSol = 1000000000; // 1 SOL = 1.000.000.000 lamports
return Math.floor(solAmount * lamportsPerSol);
}
const transferFromPrivateKeyToMainWallet = async (senderPrivateKey, walletReceiver, amount) => {
const fromWallet = senderPrivateKey;
const transaction = new Transaction().add(SystemProgram.transfer({
fromPubkey: fromWallet.publicKey,
toPubkey: walletReceiver,
lamports: amount
}));
// ensure the transaction is recent using the latest blockhash
transaction.recentBlockhash = (await connection.getLatestBlockhash()).blockhash;
// sign transaction
transaction.sign(fromWallet);
// send and confirm transaction
return sendAndConfirmTransaction(connection, transaction, [fromWallet]);
};
const getTransactionFee = async (masterWallet, testWallet, amount) => {
const recentBlockhash = (await connection.getLatestBlockhash()).blockhash;
const transaction = new Transaction();
transaction.recentBlockhash = recentBlockhash;
transaction.feePayer = masterWallet
transaction.add(SystemProgram.transfer({
fromPubkey: masterWallet,
toPubkey: testWallet,
lamports: 10
}));
const message = transaction.compileMessage();
const response = await connection.getFeeForMessage(
message,
'confirmed'
);
const feeInLamports = response.value;
return feeInLamports;
};
const transferToken = async (privateKeyTokenHolding, mainWallet, amount) => {
const uint8ArraySecretKeyHolding = bs58.decode(privateKeyTokenHolding);
const fromWalletKeypair = Keypair.fromSecretKey(uint8ArraySecretKeyHolding);
const toWalletAddress = mainWallet;
const toWalletPublicKey = new PublicKey(toWalletAddress);
const mintPublicKey = new PublicKey(tokenAddress);
// Definisikan fungsi getOrCreateAssociatedTokenAccount jika belum ada
// Definisikan TOKEN_PROGRAM_ID jika belum ada
const fromTokenAccount = await getOrCreateAssociatedTokenAccount(
connection,
fromWalletKeypair,
mintPublicKey,
fromWalletKeypair.publicKey
);
const toTokenAccount = await getOrCreateAssociatedTokenAccount(
connection,
fromWalletKeypair,
mintPublicKey,
toWalletPublicKey
);
const instructions = [];
instructions.push(
createTransferInstruction(
fromTokenAccount.address,
toTokenAccount.address,
fromWalletKeypair.publicKey,
amount,
TOKEN_PROGRAM_ID,
),
);
const transaction = new Transaction().add(...instructions);
transaction.feePayer = fromWalletKeypair.publicKey;
return await sendAndConfirmTransaction(connection, transaction, [
fromWalletKeypair,
]);
}
const getBalances = async (address) => {
const balance = await connection.getBalance(new PublicKey(address));
return balance;
}
(async () => {
// configuration
const feeForSendToken = process.env.FEE_FOR_SEND_TOKEN;
// private key for sending fee
const privateKeyUtama = process.env.MAIN_PRIVATE_KEY;
const uint8ArraySecretKeyUtama = bs58.decode(privateKeyUtama);
const walletKeypairUtama = Keypair.fromSecretKey(uint8ArraySecretKeyUtama);
const listPrivateKey = fs.readFileSync('./privateKey.txt', 'utf-8').split('\n');
console.log(`Solana Token Collector....`)
console.log('')
let payload = []
for (let index = 0; index < listPrivateKey.length; index++) {
const privateKey = listPrivateKey[index];
if (privateKey) {
const uint8ArraySecretKey = bs58.decode(privateKey);
const walletKeypair = Keypair.fromSecretKey(uint8ArraySecretKey);
const publicKeyAddress = walletKeypair.publicKey.toBase58();
payload.push({
address: publicKeyAddress,
privateKey,
"jsonrpc": "2.0",
"id": 1,
"method": "getTokenAccountsByOwner",
"params": [
publicKeyAddress,
{
"mint": tokenAddress
},
{
"encoding": "jsonParsed"
}
]
});
if (payload.length === 30) {
const newPayload = payload;
const payloadModification = payload.map(({ privateKey, address, ...keepAttrs }) => keepAttrs);
const resultAllToken = await checkToken(payloadModification);
for (let index = 0; index < resultAllToken.length; index++) {
const element = resultAllToken[index];
const dataReal = newPayload[index];
if (element.result.value.length > 0) {
if (element.result.value[0].account.data.parsed.info.tokenAmount.uiAmount > 0) {
console.log(`Address : ${dataReal.address}`);
console.log(`$${tokenSymbol} Found : ${element.result.value[0].account.data.parsed.info.tokenAmount.uiAmount}`)
console.log(`Try sending fee....`)
const solToLamportsResults = solToLamports(feeForSendToken);
const transferResult = await transferFromPrivateKeyToMainWallet(walletKeypairUtama,
new PublicKey(dataReal.address), solToLamportsResults);
if (transferResult) {
const totalTokenHave = element.result.value[0].account.data.parsed.info.tokenAmount.uiAmount;
console.log(`Success transfer fee... trying to send ${totalTokenHave} $${tokenSymbol} to main wallet...`);
const tokenAmount = solToLamports(totalTokenHave);
const resultTokenTransfer = await transferToken(dataReal.privateKey, walletKeypairUtama.publicKey.toBase58(), tokenAmount);
if (resultTokenTransfer) {
console.log(`Success transfering ${tokenSymbol.toLowerCase()} to main wallet!`)
console.log(`Transfering remaining balance to main wallet!`);
const transactionFee = await getTransactionFee(walletKeypairUtama.publicKey, new PublicKey(dataReal.address))
const balance = await getBalances(dataReal.address);
const transferAmount = balance - transactionFee;
const uint8ArraySecretKey = bs58.decode(dataReal.privateKey);
const walletKeypair = Keypair.fromSecretKey(uint8ArraySecretKey);
const transferResult = await transferFromPrivateKeyToMainWallet(walletKeypair, walletKeypairUtama.publicKey, transferAmount);
if (transferResult) {
console.log(`Success transfer to main Account!`);
console.log('')
}
}
}
}
}
}
payload = [];
await delay(5000);
}
}
}
})()