-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
85 lines (72 loc) · 2.73 KB
/
index.mjs
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
import {
Connection,
PublicKey,
Transaction,
SystemProgram,
LAMPORTS_PER_SOL,
sendAndConfirmTransaction,
Keypair
} from '@solana/web3.js';
import fs from 'fs/promises';
const connection = new Connection("https://api.testnet.solana.com/", "confirmed");
// Function to read accounts from a JSON file
async function readAccountsFromFile(filePath) {
try {
const data = await fs.readFile(filePath, 'utf8');
return JSON.parse(data);
} catch (error) {
console.error("Error reading accounts from file:", error);
return {};
}
}
// Function to validate Solana addresses
function validateSolanaAddress(address) {
try {
new PublicKey(address);
return true;
} catch (error) {
return false;
}
}
// Function to log invalid addresses to a file
async function logInvalidAddress(address, invalidAddressesPath) {
try {
const entry = `${address}\n`;
await fs.appendFile(invalidAddressesPath, entry, 'utf8');
} catch (error) {
console.error(`Error writing invalid address ${address} to file:`, error);
}
}
async function sendCoinsToAccounts(sender, senderKeyPair, filePath, invalidAddressesPath) {
try {
const accounts = await readAccountsFromFile(filePath);
const transaction = new Transaction();
for (const [recipientAddress, amount] of Object.entries(accounts)) {
if (!validateSolanaAddress(recipientAddress)) {
console.log(`Invalid Solana address: ${recipientAddress}`);
await logInvalidAddress(recipientAddress, invalidAddressesPath);
continue;
}
const recipientPublicKey = new PublicKey(recipientAddress);
const lamports = amount * LAMPORTS_PER_SOL; // Convert SOL to lamports
console.log("Sending", amount, "SOL to", recipientAddress);
const transferInstruction = SystemProgram.transfer({
fromPubkey: sender,
toPubkey: recipientPublicKey,
lamports,
});
transaction.add(transferInstruction);
}
// Sign and send the transaction
console.log("Sending transaction...");
const signature = await sendAndConfirmTransaction(connection, transaction, [senderKeyPair]);
console.log("Transaction confirmed with signature:", signature);
} catch (error) {
console.error("Error during transaction:", error);
}
}
// Example usage (make sure to replace with actual data)
const secretKeyUint8Array = Uint8Array.from(Buffer.from('', 'hex'));
const senderKeyPair = Keypair.fromSecretKey(secretKeyUint8Array);
const senderPublicKey = new PublicKey('');
sendCoinsToAccounts(senderPublicKey, senderKeyPair, 'rewards.json', 'invalid_addresses.json');