forked from BlackBeard085/x1console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetbalances.js
151 lines (135 loc) · 5.62 KB
/
getbalances.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
const fs = require('fs');
const { exec } = require('child_process'); // for executing shell commands
const { Connection, PublicKey } = require('@solana/web3.js');
const readline = require('readline');
const path = require('path'); // for handling file paths
const os = require('os'); // for getting home directory
const SOLANA_CLUSTER = 'https://xolana.xen.network'; // Change to your desired cluster (mainnet, testnet, etc.)
const CONFIG_FILE = 'wallets.json'; // JSON file to store wallet addresses
const TRANSFER_AMOUNT = 2; // Amount in SOL to transfer when funding
// Set up readline to get user input
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Function to ask for wallet address input
async function askForWalletAddress(name) {
return new Promise((resolve) => {
rl.question(`Please enter the address for ${name} wallet: `, (address) => {
resolve(address);
});
});
}
// Function to save wallet addresses to a JSON file
async function saveWallets(wallets) {
fs.writeFileSync(CONFIG_FILE, JSON.stringify(wallets, null, 2));
console.log('Wallet addresses saved to wallets.json');
}
// Function to load wallet addresses from a JSON file
function loadWallets() {
if (fs.existsSync(CONFIG_FILE)) {
const data = fs.readFileSync(CONFIG_FILE);
return JSON.parse(data);
}
return null;
}
// Function to attempt to load wallets from an alternative directory
function loadWalletsFromBackup() {
const backupPath = path.join(os.homedir(), 'x1/solanalabs', CONFIG_FILE);
if (fs.existsSync(backupPath)) {
console.log(`Found ${CONFIG_FILE} in backup directory. Copying to current directory...`);
fs.copyFileSync(backupPath, CONFIG_FILE);
console.log(`${CONFIG_FILE} copied to current directory.`);
return loadWallets(); // Load the wallets after copying
}
return null;
}
// Function to get balances of the wallets
async function getBalances(wallets) {
const connection = new Connection(SOLANA_CLUSTER, 'confirmed');
for (const wallet of wallets) {
try {
const publicKey = new PublicKey(wallet.address);
const balance = await connection.getBalance(publicKey);
console.log(`Wallet: ${wallet.name} (${wallet.address})\nBalance: ${(balance / 1e9).toFixed(2)} SOL\n`); // Balance on the next line
wallet.balance = balance / 1e9; // Store the balance in SOL for later checks
} catch (error) {
console.error(`Error retrieving balance for wallet ${wallet.address}:`, error);
}
}
}
// Function to transfer SOL between wallets
function transferSOL(fromWallet, toWallet, amount) {
return new Promise((resolve, reject) => {
exec(`solana transfer ${toWallet} ${amount} --allow-unfunded-recipient`, (error, stdout, stderr) => {
if (error) {
console.error(`Error executing transfer: ${stderr}`);
return reject(stderr);
}
console.log(stdout);
resolve();
});
});
}
// Main function to run the program
async function main() {
let wallets = loadWallets();
if (!wallets) {
console.log(`No wallet addresses found in current directory. Looking for ${CONFIG_FILE} in backup directory...`);
wallets = loadWalletsFromBackup();
}
if (!wallets) {
console.log('No wallet addresses found. You will be prompted to enter them.');
wallets = [
{ name: 'Id', address: await askForWalletAddress('Id') },
{ name: 'Identity', address: await askForWalletAddress('Identity') },
{ name: 'Stake', address: await askForWalletAddress('Stake') },
{ name: 'Vote', address: await askForWalletAddress('Vote') },
];
rl.close(); // Close the readline interface
await saveWallets(wallets);
} else {
console.log('Loaded wallet addresses from wallets.json:');
wallets.forEach(wallet => {
console.log(`- ${wallet.name}: ${wallet.address}`);
});
}
await getBalances(wallets);
const idWallet = wallets.find(w => w.name === 'Id');
const identityWallet = wallets.find(w => w.name === 'Identity');
const stakeWallet = wallets.find(w => w.name === 'Stake');
// Check balances of Identity and Stake wallets
const needsFunding = [];
if (identityWallet.balance < 1) {
needsFunding.push(identityWallet);
}
if (stakeWallet.balance < 1) {
needsFunding.push(stakeWallet);
}
// If any wallets need funding
if (needsFunding.length > 0) {
// Check if the Id wallet has enough balance for the transfers
if (idWallet.balance >= needsFunding.length * TRANSFER_AMOUNT) {
// Loop through the wallets that need funding and transfer 2 SOL to each
for (const wallet of needsFunding) {
console.log(`\nSending ${TRANSFER_AMOUNT} SOL to ${wallet.address}`);
await transferSOL(idWallet.address, wallet.address, TRANSFER_AMOUNT);
}
console.log(`\nChecking balances again...`);
await getBalances(wallets);
// Final check to see if both accounts are now funded
if (identityWallet.balance >= 1 && stakeWallet.balance >= 1) {
console.log("Accounts well funded");
process.exit(0);
}
} else {
console.log("Not enough funds in the Id wallet to perform the transfers.");
process.exit(1);
}
} else {
console.log("Accounts well funded");
process.exit(0);
}
}
// Start the program
main();