solforge 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,336 @@
1
+ import { Command } from "commander";
2
+ import chalk from "chalk";
3
+ import { existsSync, readFileSync } from "fs";
4
+ import { join } from "path";
5
+ import { input, select } from "@inquirer/prompts";
6
+ import { runCommand } from "../utils/shell";
7
+ import { Keypair, PublicKey } from "@solana/web3.js";
8
+
9
+ interface TokenConfig {
10
+ symbol: string;
11
+ mainnetMint: string;
12
+ mintAmount: number;
13
+ }
14
+
15
+ interface ClonedToken {
16
+ config: TokenConfig;
17
+ mintAuthorityPath: string;
18
+ modifiedAccountPath: string;
19
+ mintAuthority: {
20
+ publicKey: string;
21
+ secretKey: number[];
22
+ };
23
+ }
24
+
25
+ export const mintCommand = new Command()
26
+ .name("mint")
27
+ .description("Interactively mint tokens to any wallet address")
28
+ .option("--rpc-url <url>", "RPC URL to use", "http://127.0.0.1:8899")
29
+ .option("--symbol <symbol>", "Token symbol to mint")
30
+ .option("--wallet <address>", "Wallet address to mint to")
31
+ .option("--amount <amount>", "Amount to mint")
32
+ .action(async (options) => {
33
+ try {
34
+ console.log(chalk.blue("šŸŖ™ Interactive Token Minting"));
35
+ console.log(chalk.gray("Mint tokens to any wallet address\n"));
36
+
37
+ // Check if solforge data exists
38
+ const workDir = ".solforge";
39
+ if (!existsSync(workDir)) {
40
+ console.error(
41
+ chalk.red("āŒ No solforge data found. Run 'solforge start' first.")
42
+ );
43
+ process.exit(1);
44
+ }
45
+
46
+ // Load available tokens
47
+ const tokens = await loadAvailableTokens(workDir);
48
+
49
+ if (tokens.length === 0) {
50
+ console.error(
51
+ chalk.red(
52
+ "āŒ No tokens found. Run 'solforge start' first to clone tokens."
53
+ )
54
+ );
55
+ process.exit(1);
56
+ }
57
+
58
+ // Display available tokens
59
+ console.log(chalk.cyan("šŸ“‹ Available Tokens:"));
60
+ tokens.forEach((token, index) => {
61
+ console.log(
62
+ chalk.gray(
63
+ ` ${index + 1}. ${token.config.symbol} (${
64
+ token.config.mainnetMint
65
+ })`
66
+ )
67
+ );
68
+ });
69
+ console.log();
70
+
71
+ // Select token (or use provided symbol)
72
+ let selectedToken: ClonedToken;
73
+ if (options.symbol) {
74
+ const token = tokens.find(
75
+ (t) => t.config.symbol.toLowerCase() === options.symbol.toLowerCase()
76
+ );
77
+ if (!token) {
78
+ console.error(
79
+ chalk.red(`āŒ Token ${options.symbol} not found in cloned tokens`)
80
+ );
81
+ process.exit(1);
82
+ }
83
+ selectedToken = token;
84
+ console.log(
85
+ chalk.gray(`Selected token: ${selectedToken.config.symbol}`)
86
+ );
87
+ } else {
88
+ selectedToken = await select({
89
+ message: "Select a token to mint:",
90
+ choices: tokens.map((token) => ({
91
+ name: `${token.config.symbol} (${token.config.mainnetMint})`,
92
+ value: token,
93
+ })),
94
+ });
95
+ }
96
+
97
+ // Get recipient address (or use provided wallet)
98
+ let recipientAddress: string;
99
+ if (options.wallet) {
100
+ recipientAddress = options.wallet;
101
+ // Validate wallet address
102
+ try {
103
+ new PublicKey(recipientAddress);
104
+ } catch {
105
+ console.error(chalk.red("āŒ Invalid wallet address"));
106
+ process.exit(1);
107
+ }
108
+ console.log(chalk.gray(`Recipient wallet: ${recipientAddress}`));
109
+ } else {
110
+ recipientAddress = await input({
111
+ message: "Enter recipient wallet address:",
112
+ validate: (value: string) => {
113
+ if (!value.trim()) {
114
+ return "Please enter a valid address";
115
+ }
116
+ try {
117
+ new PublicKey(value.trim());
118
+ return true;
119
+ } catch {
120
+ return "Please enter a valid Solana address";
121
+ }
122
+ },
123
+ });
124
+ }
125
+
126
+ // Get amount to mint (or use provided amount)
127
+ let amount: string;
128
+ if (options.amount) {
129
+ const num = parseFloat(options.amount);
130
+ if (isNaN(num) || num <= 0) {
131
+ console.error(chalk.red("āŒ Invalid amount"));
132
+ process.exit(1);
133
+ }
134
+ amount = options.amount;
135
+ console.log(chalk.gray(`Amount to mint: ${amount}`));
136
+ } else {
137
+ amount = await input({
138
+ message: "Enter amount to mint:",
139
+ validate: (value: string) => {
140
+ const num = parseFloat(value);
141
+ if (isNaN(num) || num <= 0) {
142
+ return "Please enter a valid positive number";
143
+ }
144
+ return true;
145
+ },
146
+ });
147
+ }
148
+
149
+ // Confirm minting if interactive
150
+ if (!options.symbol || !options.wallet || !options.amount) {
151
+ const confirm = await input({
152
+ message: `Confirm minting ${amount} ${selectedToken.config.symbol} to ${recipientAddress}? (y/N):`,
153
+ default: "N",
154
+ });
155
+
156
+ if (confirm.toLowerCase() !== "y" && confirm.toLowerCase() !== "yes") {
157
+ console.log(chalk.yellow("Minting cancelled."));
158
+ process.exit(0);
159
+ }
160
+ }
161
+
162
+ console.log(chalk.blue("šŸš€ Starting mint..."));
163
+
164
+ // Execute mint
165
+ await mintTokenToWallet(
166
+ selectedToken,
167
+ recipientAddress,
168
+ parseFloat(amount),
169
+ options.rpcUrl
170
+ );
171
+
172
+ console.log(
173
+ chalk.green(
174
+ `āœ… Successfully minted ${amount} ${selectedToken.config.symbol} to ${recipientAddress}`
175
+ )
176
+ );
177
+ } catch (error) {
178
+ console.error(chalk.red(`āŒ Mint failed: ${error}`));
179
+ process.exit(1);
180
+ }
181
+ });
182
+
183
+ async function loadAvailableTokens(workDir: string): Promise<ClonedToken[]> {
184
+ const tokens: ClonedToken[] = [];
185
+
186
+ try {
187
+ // Load token config from sf.config.json
188
+ const configPath = "sf.config.json";
189
+ if (!existsSync(configPath)) {
190
+ throw new Error("sf.config.json not found in current directory");
191
+ }
192
+
193
+ const config = JSON.parse(readFileSync(configPath, "utf8"));
194
+ const tokenConfigs: TokenConfig[] = config.tokens || [];
195
+
196
+ // Load shared mint authority
197
+ const sharedMintAuthorityPath = join(workDir, "shared-mint-authority.json");
198
+ if (!existsSync(sharedMintAuthorityPath)) {
199
+ throw new Error("Shared mint authority not found");
200
+ }
201
+
202
+ const secretKeyArray = JSON.parse(
203
+ readFileSync(sharedMintAuthorityPath, "utf8")
204
+ );
205
+ const mintAuthorityKeypair = Keypair.fromSecretKey(
206
+ new Uint8Array(secretKeyArray)
207
+ );
208
+ const mintAuthority = {
209
+ publicKey: mintAuthorityKeypair.publicKey.toBase58(),
210
+ secretKey: Array.from(mintAuthorityKeypair.secretKey),
211
+ };
212
+
213
+ // Build cloned tokens list
214
+ for (const tokenConfig of tokenConfigs) {
215
+ const tokenDir = join(
216
+ workDir,
217
+ `token-${tokenConfig.symbol.toLowerCase()}`
218
+ );
219
+ const modifiedAccountPath = join(tokenDir, "modified.json");
220
+
221
+ if (existsSync(modifiedAccountPath)) {
222
+ tokens.push({
223
+ config: tokenConfig,
224
+ mintAuthorityPath: sharedMintAuthorityPath,
225
+ modifiedAccountPath,
226
+ mintAuthority,
227
+ });
228
+ }
229
+ }
230
+
231
+ return tokens;
232
+ } catch (error) {
233
+ throw new Error(`Failed to load tokens: ${error}`);
234
+ }
235
+ }
236
+
237
+ export async function mintTokenToWallet(
238
+ token: ClonedToken,
239
+ walletAddress: string,
240
+ amount: number,
241
+ rpcUrl: string
242
+ ): Promise<void> {
243
+ // Check if associated token account already exists (same pattern as token-cloner.ts)
244
+ console.log(chalk.gray(`šŸ” Checking for existing token account...`));
245
+
246
+ const checkAccountsResult = await runCommand(
247
+ "spl-token",
248
+ ["accounts", "--owner", walletAddress, "--url", rpcUrl, "--output", "json"],
249
+ { silent: true }
250
+ );
251
+
252
+ let tokenAccountAddress = "";
253
+
254
+ if (checkAccountsResult.success && checkAccountsResult.stdout) {
255
+ try {
256
+ const accountsData = JSON.parse(checkAccountsResult.stdout);
257
+
258
+ // Look for existing token account for this mint
259
+ for (const account of accountsData.accounts || []) {
260
+ if (account.mint === token.config.mainnetMint) {
261
+ tokenAccountAddress = account.address;
262
+ console.log(
263
+ chalk.gray(
264
+ `ā„¹ļø Found existing token account: ${tokenAccountAddress}`
265
+ )
266
+ );
267
+ break;
268
+ }
269
+ }
270
+ } catch (error) {
271
+ // No existing accounts found or parsing error, will create new account
272
+ }
273
+ }
274
+
275
+ if (!tokenAccountAddress) {
276
+ // Account doesn't exist, create it (same pattern as token-cloner.ts)
277
+ console.log(chalk.gray(`šŸ”§ Creating token account...`));
278
+
279
+ const createAccountResult = await runCommand(
280
+ "spl-token",
281
+ [
282
+ "create-account",
283
+ token.config.mainnetMint,
284
+ "--owner",
285
+ walletAddress,
286
+ "--fee-payer",
287
+ token.mintAuthorityPath,
288
+ "--url",
289
+ rpcUrl,
290
+ ],
291
+ { silent: false }
292
+ );
293
+
294
+ if (!createAccountResult.success) {
295
+ throw new Error(
296
+ `Failed to create token account: ${createAccountResult.stderr}`
297
+ );
298
+ }
299
+
300
+ // Extract token account address from create-account output
301
+ const match = createAccountResult.stdout.match(/Creating account (\S+)/);
302
+ tokenAccountAddress = match?.[1] || "";
303
+
304
+ if (!tokenAccountAddress) {
305
+ throw new Error(
306
+ "Failed to determine token account address from create-account output"
307
+ );
308
+ }
309
+
310
+ console.log(chalk.gray(`āœ… Created token account: ${tokenAccountAddress}`));
311
+ }
312
+
313
+ // Now mint to the token account (same pattern as token-cloner.ts)
314
+ console.log(chalk.gray(`šŸ’° Minting ${amount} tokens...`));
315
+
316
+ const result = await runCommand(
317
+ "spl-token",
318
+ [
319
+ "mint",
320
+ token.config.mainnetMint,
321
+ amount.toString(),
322
+ tokenAccountAddress,
323
+ "--mint-authority",
324
+ token.mintAuthorityPath,
325
+ "--fee-payer",
326
+ token.mintAuthorityPath,
327
+ "--url",
328
+ rpcUrl,
329
+ ],
330
+ { silent: false }
331
+ );
332
+
333
+ if (!result.success) {
334
+ throw new Error(`Failed to mint tokens: ${result.stderr}`);
335
+ }
336
+ }
@@ -9,6 +9,7 @@ import { TokenCloner } from "../services/token-cloner.js";
9
9
  import { ProgramCloner } from "../services/program-cloner.js";
10
10
  import { processRegistry } from "../services/process-registry.js";
11
11
  import { portManager } from "../services/port-manager.js";
12
+
12
13
  import type { Config, TokenConfig, ProgramConfig } from "../types/config.js";
13
14
  import type { ClonedToken } from "../services/token-cloner.js";
14
15
  import type { RunningValidator } from "../services/process-registry.js";
@@ -334,6 +335,93 @@ export async function startCommand(debug: boolean = false): Promise<void> {
334
335
  debug
335
336
  );
336
337
 
338
+ // Find an available port for the API server
339
+ let apiServerPort = 3000;
340
+ while (!(await portManager.isPortAvailable(apiServerPort))) {
341
+ apiServerPort++;
342
+ if (apiServerPort > 3100) {
343
+ throw new Error("Could not find available port for API server");
344
+ }
345
+ }
346
+
347
+ // Start the API server as a background process
348
+ let apiServerPid: number | undefined;
349
+ let apiResult: { success: boolean; error?: string } = {
350
+ success: false,
351
+ error: "Not started",
352
+ };
353
+
354
+ try {
355
+ const currentDir = process.cwd();
356
+ const testpilotDir = join(__dirname, "..", "..");
357
+ const apiServerScript = join(testpilotDir, "src", "api-server-entry.ts");
358
+ const configPath =
359
+ configManager.getConfigPath() ?? join(currentDir, "sf.config.json");
360
+ const workDir = join(currentDir, ".solforge");
361
+
362
+ // Start API server in background using runCommand with nohup
363
+ const apiServerCommand = `nohup bun run "${apiServerScript}" --port ${apiServerPort} --config "${configPath}" --rpc-url "http://127.0.0.1:${config.localnet.port}" --faucet-url "http://127.0.0.1:${config.localnet.faucetPort}" --work-dir "${workDir}" > /dev/null 2>&1 &`;
364
+
365
+ const startResult = await runCommand("sh", ["-c", apiServerCommand], {
366
+ silent: !debug,
367
+ debug: debug,
368
+ });
369
+
370
+ if (startResult.success) {
371
+ // Wait a moment for the API server to start
372
+ await new Promise((resolve) => setTimeout(resolve, 3000));
373
+
374
+ // Test if the API server is responding
375
+ try {
376
+ const response = await fetch(
377
+ `http://127.0.0.1:${apiServerPort}/api/health`
378
+ );
379
+ if (response.ok) {
380
+ apiResult = { success: true };
381
+ // Get the PID of the API server process
382
+ const pidResult = await runCommand(
383
+ "pgrep",
384
+ ["-f", `api-server-entry.*--port ${apiServerPort}`],
385
+ { silent: true, debug: false }
386
+ );
387
+ if (pidResult.success && pidResult.stdout.trim()) {
388
+ apiServerPid = parseInt(pidResult.stdout.trim().split("\n")[0]);
389
+ }
390
+ } else {
391
+ apiResult = {
392
+ success: false,
393
+ error: `Health check failed: ${response.status}`,
394
+ };
395
+ }
396
+ } catch (error) {
397
+ apiResult = {
398
+ success: false,
399
+ error: `Health check failed: ${
400
+ error instanceof Error ? error.message : String(error)
401
+ }`,
402
+ };
403
+ }
404
+ } else {
405
+ apiResult = {
406
+ success: false,
407
+ error: `Failed to start API server: ${
408
+ startResult.stderr || "Unknown error"
409
+ }`,
410
+ };
411
+ }
412
+ } catch (error) {
413
+ apiResult = {
414
+ success: false,
415
+ error: error instanceof Error ? error.message : String(error),
416
+ };
417
+ }
418
+
419
+ if (!apiResult.success) {
420
+ console.warn(
421
+ chalk.yellow("āš ļø Failed to start API server:", apiResult.error)
422
+ );
423
+ }
424
+
337
425
  // Register the running validator
338
426
  const runningValidator: RunningValidator = {
339
427
  id: validatorId,
@@ -346,6 +434,11 @@ export async function startCommand(debug: boolean = false): Promise<void> {
346
434
  configPath: configManager.getConfigPath() || "./sf.config.json",
347
435
  startTime: new Date(),
348
436
  status: "running",
437
+ apiServerPort: apiResult.success ? apiServerPort : undefined,
438
+ apiServerUrl: apiResult.success
439
+ ? `http://127.0.0.1:${apiServerPort}`
440
+ : undefined,
441
+ apiServerPid: apiResult.success ? apiServerPid : undefined,
349
442
  };
350
443
 
351
444
  processRegistry.register(runningValidator);
@@ -363,6 +456,11 @@ export async function startCommand(debug: boolean = false): Promise<void> {
363
456
  `šŸ’° Faucet URL: http://127.0.0.1:${config.localnet.faucetPort}`
364
457
  )
365
458
  );
459
+ if (apiResult.success) {
460
+ console.log(
461
+ chalk.cyan(`šŸš€ API Server: http://127.0.0.1:${apiServerPort}/api`)
462
+ );
463
+ }
366
464
 
367
465
  // Airdrop SOL to mint authority if tokens were cloned
368
466
  if (clonedTokens.length > 0) {
@@ -464,6 +562,34 @@ export async function startCommand(debug: boolean = false): Promise<void> {
464
562
  console.log(
465
563
  chalk.gray(" - Run `solforge stop --all` to stop all validators")
466
564
  );
565
+ if (apiResult.success) {
566
+ console.log(chalk.blue("\nšŸ”Œ API Endpoints:"));
567
+ console.log(
568
+ chalk.gray(
569
+ ` - GET http://127.0.0.1:${apiServerPort}/api/tokens - List cloned tokens`
570
+ )
571
+ );
572
+ console.log(
573
+ chalk.gray(
574
+ ` - GET http://127.0.0.1:${apiServerPort}/api/programs - List cloned programs`
575
+ )
576
+ );
577
+ console.log(
578
+ chalk.gray(
579
+ ` - POST http://127.0.0.1:${apiServerPort}/api/tokens/{symbol}/mint - Mint tokens`
580
+ )
581
+ );
582
+ console.log(
583
+ chalk.gray(
584
+ ` - POST http://127.0.0.1:${apiServerPort}/api/airdrop - Airdrop SOL`
585
+ )
586
+ );
587
+ console.log(
588
+ chalk.gray(
589
+ ` - GET http://127.0.0.1:${apiServerPort}/api/wallet/{address}/balances - Get balances`
590
+ )
591
+ );
592
+ }
467
593
  } catch (error) {
468
594
  spinner.fail("Failed to start validator");
469
595
  console.error(chalk.red("āŒ Unexpected error:"));
@@ -1,6 +1,7 @@
1
1
  import chalk from "chalk";
2
2
  import { select } from "@inquirer/prompts";
3
3
  import { processRegistry } from "../services/process-registry.js";
4
+
4
5
  import type { RunningValidator } from "../services/process-registry.js";
5
6
 
6
7
  export async function stopCommand(
@@ -54,9 +55,7 @@ export async function stopCommand(
54
55
  console.log(
55
56
  chalk.gray("šŸ’” Use `solforge stop --all` to stop all validators")
56
57
  );
57
- console.log(
58
- chalk.gray("šŸ’” Use `solforge list` to see running validators")
59
- );
58
+ console.log(chalk.gray("šŸ’” Use `solforge list` to see running validators"));
60
59
  return;
61
60
  }
62
61
 
@@ -109,6 +108,24 @@ async function stopValidator(
109
108
  forceKill: boolean = false
110
109
  ): Promise<{ success: boolean; error?: string }> {
111
110
  try {
111
+ // Stop the API server first if it has a PID
112
+ if (validator.apiServerPid) {
113
+ try {
114
+ process.kill(validator.apiServerPid, "SIGTERM");
115
+ console.log(
116
+ chalk.gray(`šŸ“” Stopped API server (PID: ${validator.apiServerPid})`)
117
+ );
118
+ } catch (error) {
119
+ console.log(
120
+ chalk.yellow(
121
+ `āš ļø Warning: Failed to stop API server: ${
122
+ error instanceof Error ? error.message : String(error)
123
+ }`
124
+ )
125
+ );
126
+ }
127
+ }
128
+
112
129
  // Check if process is still running
113
130
  const isRunning = await processRegistry.isProcessRunning(validator.pid);
114
131
  if (!isRunning) {
package/src/index.ts CHANGED
@@ -7,7 +7,7 @@ import { resolve } from "path";
7
7
  import { initCommand } from "./commands/init.js";
8
8
  import { statusCommand } from "./commands/status.js";
9
9
  import { startCommand } from "./commands/start.js";
10
- import { transferCommand } from "./commands/transfer.js";
10
+ import { mintCommand } from "./commands/mint.js";
11
11
  import { listCommand } from "./commands/list.js";
12
12
  import { stopCommand, killCommand } from "./commands/stop.js";
13
13
  import { addProgramCommand } from "./commands/add-program.js";
@@ -94,7 +94,7 @@ program
94
94
  await statusCommand();
95
95
  });
96
96
 
97
- program.addCommand(transferCommand);
97
+ program.addCommand(mintCommand);
98
98
 
99
99
  program
100
100
  .command("reset")