solforge 0.1.6 → 0.2.0

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.
Files changed (194) hide show
  1. package/.agi/agi.sqlite +0 -0
  2. package/.claude/settings.local.json +9 -0
  3. package/.github/workflows/release-binaries.yml +133 -0
  4. package/.tmp/.787ebcdbf7b8fde8-00000000.hm +0 -0
  5. package/.tmp/.bffe6efebdf8aedc-00000000.hm +0 -0
  6. package/AGENTS.md +271 -0
  7. package/CLAUDE.md +106 -0
  8. package/PROJECT_STRUCTURE.md +124 -0
  9. package/README.md +367 -393
  10. package/SOLANA_KIT_GUIDE.md +251 -0
  11. package/SOLFORGE.md +119 -0
  12. package/biome.json +34 -0
  13. package/bun.lock +743 -0
  14. package/docs/bun-single-file-executable.md +585 -0
  15. package/docs/cli-plan.md +154 -0
  16. package/docs/data-indexing-plan.md +214 -0
  17. package/docs/gui-roadmap.md +202 -0
  18. package/drizzle/0000_friendly_millenium_guard.sql +53 -0
  19. package/drizzle/0001_stale_sentinels.sql +2 -0
  20. package/drizzle/meta/0000_snapshot.json +329 -0
  21. package/drizzle/meta/0001_snapshot.json +345 -0
  22. package/drizzle/meta/_journal.json +20 -0
  23. package/drizzle.config.ts +12 -0
  24. package/index.ts +21 -0
  25. package/mint.sh +47 -0
  26. package/package.json +45 -69
  27. package/postcss.config.js +6 -0
  28. package/rpc-server.ts.backup +519 -0
  29. package/server/index.ts +5 -0
  30. package/server/lib/base58.ts +33 -0
  31. package/server/lib/faucet.ts +110 -0
  32. package/server/lib/spl-token.ts +57 -0
  33. package/server/methods/TEMPLATE.md +117 -0
  34. package/server/methods/account/get-account-info.ts +90 -0
  35. package/server/methods/account/get-balance.ts +27 -0
  36. package/server/methods/account/get-multiple-accounts.ts +83 -0
  37. package/server/methods/account/get-parsed-account-info.ts +21 -0
  38. package/server/methods/account/index.ts +12 -0
  39. package/server/methods/account/parsers/index.ts +52 -0
  40. package/server/methods/account/parsers/loader-upgradeable.ts +66 -0
  41. package/server/methods/account/parsers/spl-token.ts +237 -0
  42. package/server/methods/account/parsers/system.ts +4 -0
  43. package/server/methods/account/request-airdrop.ts +219 -0
  44. package/server/methods/admin/adopt-mint-authority.ts +94 -0
  45. package/server/methods/admin/clone-program-accounts.ts +55 -0
  46. package/server/methods/admin/clone-program.ts +152 -0
  47. package/server/methods/admin/clone-token-accounts.ts +117 -0
  48. package/server/methods/admin/clone-token-mint.ts +82 -0
  49. package/server/methods/admin/create-mint.ts +114 -0
  50. package/server/methods/admin/create-token-account.ts +137 -0
  51. package/server/methods/admin/helpers.ts +70 -0
  52. package/server/methods/admin/index.ts +10 -0
  53. package/server/methods/admin/list-mints.ts +21 -0
  54. package/server/methods/admin/load-program.ts +52 -0
  55. package/server/methods/admin/mint-to.ts +278 -0
  56. package/server/methods/block/get-block-height.ts +5 -0
  57. package/server/methods/block/get-block.ts +35 -0
  58. package/server/methods/block/get-blocks-with-limit.ts +23 -0
  59. package/server/methods/block/get-latest-blockhash.ts +12 -0
  60. package/server/methods/block/get-slot.ts +5 -0
  61. package/server/methods/block/index.ts +6 -0
  62. package/server/methods/block/is-blockhash-valid.ts +23 -0
  63. package/server/methods/epoch/get-cluster-nodes.ts +17 -0
  64. package/server/methods/epoch/get-epoch-info.ts +16 -0
  65. package/server/methods/epoch/get-epoch-schedule.ts +15 -0
  66. package/server/methods/epoch/get-highest-snapshot-slot.ts +9 -0
  67. package/server/methods/epoch/get-leader-schedule.ts +8 -0
  68. package/server/methods/epoch/get-max-retransmit-slot.ts +9 -0
  69. package/server/methods/epoch/get-max-shred-insert-slot.ts +9 -0
  70. package/server/methods/epoch/get-slot-leader.ts +6 -0
  71. package/server/methods/epoch/get-slot-leaders.ts +9 -0
  72. package/server/methods/epoch/get-stake-activation.ts +9 -0
  73. package/server/methods/epoch/get-stake-minimum-delegation.ts +9 -0
  74. package/server/methods/epoch/get-vote-accounts.ts +19 -0
  75. package/server/methods/epoch/index.ts +13 -0
  76. package/server/methods/epoch/minimum-ledger-slot.ts +5 -0
  77. package/server/methods/fee/get-fee-calculator-for-blockhash.ts +12 -0
  78. package/server/methods/fee/get-fee-for-message.ts +8 -0
  79. package/server/methods/fee/get-fee-rate-governor.ts +16 -0
  80. package/server/methods/fee/get-fees.ts +14 -0
  81. package/server/methods/fee/get-recent-prioritization-fees.ts +22 -0
  82. package/server/methods/fee/index.ts +5 -0
  83. package/server/methods/get-address-lookup-table.ts +31 -0
  84. package/server/methods/index.ts +265 -0
  85. package/server/methods/performance/get-recent-performance-samples.ts +25 -0
  86. package/server/methods/performance/get-transaction-count.ts +5 -0
  87. package/server/methods/performance/index.ts +2 -0
  88. package/server/methods/program/get-block-commitment.ts +9 -0
  89. package/server/methods/program/get-block-production.ts +14 -0
  90. package/server/methods/program/get-block-time.ts +21 -0
  91. package/server/methods/program/get-blocks.ts +11 -0
  92. package/server/methods/program/get-first-available-block.ts +9 -0
  93. package/server/methods/program/get-genesis-hash.ts +6 -0
  94. package/server/methods/program/get-identity.ts +6 -0
  95. package/server/methods/program/get-inflation-governor.ts +15 -0
  96. package/server/methods/program/get-inflation-rate.ts +10 -0
  97. package/server/methods/program/get-inflation-reward.ts +12 -0
  98. package/server/methods/program/get-largest-accounts.ts +8 -0
  99. package/server/methods/program/get-parsed-program-accounts.ts +12 -0
  100. package/server/methods/program/get-parsed-token-accounts-by-delegate.ts +12 -0
  101. package/server/methods/program/get-parsed-token-accounts-by-owner.ts +12 -0
  102. package/server/methods/program/get-program-accounts.ts +221 -0
  103. package/server/methods/program/get-supply.ts +13 -0
  104. package/server/methods/program/get-token-account-balance.ts +64 -0
  105. package/server/methods/program/get-token-accounts-by-delegate.ts +81 -0
  106. package/server/methods/program/get-token-accounts-by-owner.ts +390 -0
  107. package/server/methods/program/get-token-largest-accounts.ts +80 -0
  108. package/server/methods/program/get-token-supply.ts +38 -0
  109. package/server/methods/program/index.ts +21 -0
  110. package/server/methods/solforge/index.ts +155 -0
  111. package/server/methods/system/get-health.ts +5 -0
  112. package/server/methods/system/get-minimum-balance-for-rent-exemption.ts +13 -0
  113. package/server/methods/system/get-version.ts +9 -0
  114. package/server/methods/system/index.ts +3 -0
  115. package/server/methods/transaction/get-confirmed-transaction.ts +11 -0
  116. package/server/methods/transaction/get-parsed-transaction.ts +21 -0
  117. package/server/methods/transaction/get-signature-statuses.ts +72 -0
  118. package/server/methods/transaction/get-signatures-for-address.ts +45 -0
  119. package/server/methods/transaction/get-transaction.ts +428 -0
  120. package/server/methods/transaction/index.ts +7 -0
  121. package/server/methods/transaction/send-transaction.ts +232 -0
  122. package/server/methods/transaction/simulate-transaction.ts +56 -0
  123. package/server/rpc-server.ts +474 -0
  124. package/server/types.ts +74 -0
  125. package/server/ws-server.ts +171 -0
  126. package/sf.config.json +38 -0
  127. package/src/cli/bootstrap.ts +67 -0
  128. package/src/cli/commands/airdrop.ts +37 -0
  129. package/src/cli/commands/config.ts +39 -0
  130. package/src/cli/commands/mint.ts +187 -0
  131. package/src/cli/commands/program-clone.ts +124 -0
  132. package/src/cli/commands/program-load.ts +64 -0
  133. package/src/cli/commands/rpc-start.ts +46 -0
  134. package/src/cli/commands/token-adopt-authority.ts +37 -0
  135. package/src/cli/commands/token-clone.ts +113 -0
  136. package/src/cli/commands/token-create.ts +81 -0
  137. package/src/cli/main.ts +130 -0
  138. package/src/cli/run-solforge.ts +98 -0
  139. package/src/cli/setup-utils.ts +54 -0
  140. package/src/cli/setup-wizard.ts +256 -0
  141. package/src/cli/utils/args.ts +15 -0
  142. package/src/config/index.ts +130 -0
  143. package/src/db/index.ts +83 -0
  144. package/src/db/schema/accounts.ts +23 -0
  145. package/src/db/schema/address-signatures.ts +31 -0
  146. package/src/db/schema/index.ts +5 -0
  147. package/src/db/schema/meta-kv.ts +9 -0
  148. package/src/db/schema/transactions.ts +29 -0
  149. package/src/db/schema/tx-accounts.ts +33 -0
  150. package/src/db/tx-store.ts +229 -0
  151. package/src/gui/public/app.css +1 -0
  152. package/src/gui/public/index.html +19 -0
  153. package/src/gui/server.ts +297 -0
  154. package/src/gui/src/api.ts +127 -0
  155. package/src/gui/src/app.tsx +390 -0
  156. package/src/gui/src/components/airdrop-mint-form.tsx +216 -0
  157. package/src/gui/src/components/clone-program-modal.tsx +183 -0
  158. package/src/gui/src/components/clone-token-modal.tsx +211 -0
  159. package/src/gui/src/components/modal.tsx +127 -0
  160. package/src/gui/src/components/programs-panel.tsx +112 -0
  161. package/src/gui/src/components/status-panel.tsx +122 -0
  162. package/src/gui/src/components/tokens-panel.tsx +116 -0
  163. package/src/gui/src/hooks/use-interval.ts +17 -0
  164. package/src/gui/src/index.css +529 -0
  165. package/src/gui/src/main.tsx +17 -0
  166. package/src/migrations-bundled.ts +17 -0
  167. package/src/rpc/start.ts +44 -0
  168. package/tailwind.config.js +27 -0
  169. package/test-client.ts +120 -0
  170. package/tmp/inspect-html.ts +4 -0
  171. package/tmp/response-test.ts +5 -0
  172. package/tmp/test-html.ts +5 -0
  173. package/tmp/test-server.ts +13 -0
  174. package/tsconfig.json +24 -23
  175. package/LICENSE +0 -21
  176. package/scripts/postinstall.cjs +0 -103
  177. package/src/api-server-entry.ts +0 -109
  178. package/src/commands/add-program.ts +0 -337
  179. package/src/commands/init.ts +0 -122
  180. package/src/commands/list.ts +0 -136
  181. package/src/commands/mint.ts +0 -336
  182. package/src/commands/start.ts +0 -878
  183. package/src/commands/status.ts +0 -99
  184. package/src/commands/stop.ts +0 -406
  185. package/src/config/manager.ts +0 -157
  186. package/src/index.ts +0 -188
  187. package/src/services/api-server.ts +0 -532
  188. package/src/services/port-manager.ts +0 -177
  189. package/src/services/process-registry.ts +0 -154
  190. package/src/services/program-cloner.ts +0 -317
  191. package/src/services/token-cloner.ts +0 -809
  192. package/src/services/validator.ts +0 -295
  193. package/src/types/config.ts +0 -110
  194. package/src/utils/shell.ts +0 -110
@@ -1,878 +0,0 @@
1
- import chalk from "chalk";
2
- import ora from "ora";
3
- import { spawn } from "child_process";
4
- import { existsSync, readFileSync } from "fs";
5
- import { join } from "path";
6
- import { runCommand, checkSolanaTools } from "../utils/shell.js";
7
- import { configManager } from "../config/manager.js";
8
- import { TokenCloner } from "../services/token-cloner.js";
9
- import { ProgramCloner } from "../services/program-cloner.js";
10
- import { processRegistry } from "../services/process-registry.js";
11
- import { portManager } from "../services/port-manager.js";
12
-
13
- import type { Config, TokenConfig, ProgramConfig } from "../types/config.js";
14
- import type { ClonedToken } from "../services/token-cloner.js";
15
- import type { RunningValidator } from "../services/process-registry.js";
16
-
17
- function generateValidatorId(name: string): string {
18
- const timestamp = Date.now();
19
- const randomSuffix = Math.random().toString(36).substring(2, 8);
20
- const safeName = name.replace(/[^a-zA-Z0-9]/g, "-").toLowerCase();
21
- return `${safeName}-${timestamp}-${randomSuffix}`;
22
- }
23
-
24
- export async function startCommand(
25
- debug: boolean = false,
26
- network: boolean = false
27
- ): Promise<void> {
28
- // Check prerequisites
29
- const tools = await checkSolanaTools();
30
- if (!tools.solana) {
31
- console.error(chalk.red("āŒ solana CLI not found"));
32
- console.log(
33
- chalk.yellow(
34
- "šŸ’” Install it with: sh -c \"$(curl --proto '=https' --tlsv1.2 -sSfL https://solana-install.solana.workers.dev | bash)\""
35
- )
36
- );
37
- process.exit(1);
38
- }
39
-
40
- // Load configuration
41
- let config: Config;
42
- try {
43
- await configManager.load("./sf.config.json");
44
- config = configManager.getConfig();
45
- } catch (error) {
46
- console.error(chalk.red("āŒ Failed to load sf.config.json"));
47
- console.error(
48
- chalk.red(error instanceof Error ? error.message : String(error))
49
- );
50
- console.log(
51
- chalk.yellow("šŸ’” Run `solforge init` to create a configuration")
52
- );
53
- process.exit(1);
54
- }
55
-
56
- // Check if validator is already running on configured ports first
57
- const checkResult = await runCommand(
58
- "curl",
59
- [
60
- "-s",
61
- "-X",
62
- "POST",
63
- `http://127.0.0.1:${config.localnet.port}`,
64
- "-H",
65
- "Content-Type: application/json",
66
- "-d",
67
- '{"jsonrpc":"2.0","id":1,"method":"getHealth"}',
68
- ],
69
- { silent: true, debug: false }
70
- );
71
-
72
- if (checkResult.success && checkResult.stdout.includes("ok")) {
73
- console.log(chalk.yellow("āš ļø Validator is already running"));
74
- console.log(
75
- chalk.cyan(`🌐 RPC URL: http://127.0.0.1:${config.localnet.port}`)
76
- );
77
- console.log(
78
- chalk.cyan(
79
- `šŸ’° Faucet URL: http://127.0.0.1:${config.localnet.faucetPort}`
80
- )
81
- );
82
-
83
- // Clone tokens if needed, even when validator is already running
84
- let clonedTokens: ClonedToken[] = [];
85
- if (config.tokens.length > 0) {
86
- const tokenCloner = new TokenCloner();
87
-
88
- // Check which tokens are already cloned and which need to be cloned
89
- const { existingTokens, tokensToClone } = await checkExistingClonedTokens(
90
- config.tokens,
91
- tokenCloner
92
- );
93
-
94
- if (existingTokens.length > 0) {
95
- console.log(
96
- chalk.green(`šŸ“ Found ${existingTokens.length} already cloned tokens`)
97
- );
98
- if (debug) {
99
- existingTokens.forEach((token: ClonedToken) => {
100
- console.log(
101
- chalk.gray(
102
- ` āœ“ ${token.config.symbol} (${token.config.mainnetMint})`
103
- )
104
- );
105
- });
106
- }
107
- clonedTokens.push(...existingTokens);
108
- }
109
-
110
- if (tokensToClone.length > 0) {
111
- console.log(
112
- chalk.yellow(
113
- `šŸ“¦ Cloning ${tokensToClone.length} new tokens from mainnet...\n`
114
- )
115
- );
116
- try {
117
- const newlyClonedTokens = await tokenCloner.cloneTokens(
118
- tokensToClone,
119
- config.localnet.rpc,
120
- debug
121
- );
122
- clonedTokens.push(...newlyClonedTokens);
123
- console.log(
124
- chalk.green(
125
- `āœ… Successfully cloned ${newlyClonedTokens.length} new tokens\n`
126
- )
127
- );
128
- } catch (error) {
129
- console.error(chalk.red("āŒ Failed to clone tokens:"));
130
- console.error(
131
- chalk.red(error instanceof Error ? error.message : String(error))
132
- );
133
- console.log(
134
- chalk.yellow(
135
- "šŸ’” You can start without tokens by removing them from sf.config.json"
136
- )
137
- );
138
- process.exit(1);
139
- }
140
- } else if (existingTokens.length > 0) {
141
- console.log(
142
- chalk.green("āœ… All tokens already cloned, skipping clone step\n")
143
- );
144
- }
145
- }
146
-
147
- // Airdrop SOL to mint authority if tokens were cloned (even when validator already running)
148
- if (clonedTokens.length > 0) {
149
- console.log(chalk.yellow("\nšŸ’ø Airdropping SOL to mint authority..."));
150
- const rpcUrl = `http://127.0.0.1:${config.localnet.port}`;
151
-
152
- try {
153
- await airdropSolToMintAuthority(clonedTokens[0], rpcUrl, debug);
154
- console.log(chalk.green("āœ… SOL airdropped successfully!"));
155
- } catch (error) {
156
- console.error(chalk.red("āŒ Failed to airdrop SOL:"));
157
- console.error(
158
- chalk.red(error instanceof Error ? error.message : String(error))
159
- );
160
- console.log(
161
- chalk.yellow(
162
- "šŸ’” You may need to manually airdrop SOL for fee payments"
163
- )
164
- );
165
- }
166
- }
167
-
168
- // Still mint tokens if any were cloned
169
- if (clonedTokens.length > 0) {
170
- console.log(chalk.yellow("\nšŸ’° Minting tokens..."));
171
- const tokenCloner = new TokenCloner();
172
- const rpcUrl = `http://127.0.0.1:${config.localnet.port}`;
173
-
174
- if (debug) {
175
- console.log(
176
- chalk.gray(`šŸ› Minting ${clonedTokens.length} tokens to recipients:`)
177
- );
178
- clonedTokens.forEach((token, index) => {
179
- console.log(
180
- chalk.gray(
181
- ` ${index + 1}. ${token.config.symbol} (${
182
- token.config.mainnetMint
183
- }) - ${token.config.mintAmount} tokens`
184
- )
185
- );
186
- });
187
- console.log(chalk.gray(`🌐 Using RPC: ${rpcUrl}`));
188
- }
189
-
190
- try {
191
- await tokenCloner.mintTokensToRecipients(clonedTokens, rpcUrl, debug);
192
- console.log(chalk.green("āœ… Token minting completed!"));
193
-
194
- if (debug) {
195
- console.log(
196
- chalk.gray(
197
- "šŸ› All tokens have been minted to their respective recipients"
198
- )
199
- );
200
- }
201
- } catch (error) {
202
- console.error(chalk.red("āŒ Failed to mint tokens:"));
203
- console.error(
204
- chalk.red(error instanceof Error ? error.message : String(error))
205
- );
206
- console.log(
207
- chalk.yellow("šŸ’” Validator is still running, you can mint manually")
208
- );
209
- }
210
- }
211
- return;
212
- }
213
-
214
- // Generate unique ID for this validator instance
215
- const validatorId = generateValidatorId(config.name);
216
-
217
- // Get available ports (only if validator is not already running)
218
- const ports = await portManager.getRecommendedPorts(config);
219
- if (
220
- ports.rpcPort !== config.localnet.port ||
221
- ports.faucetPort !== config.localnet.faucetPort
222
- ) {
223
- console.log(
224
- chalk.yellow(
225
- `āš ļø Configured ports not available, using: RPC ${ports.rpcPort}, Faucet ${ports.faucetPort}`
226
- )
227
- );
228
- // Update config with available ports
229
- config.localnet.port = ports.rpcPort;
230
- config.localnet.faucetPort = ports.faucetPort;
231
- }
232
-
233
- console.log(chalk.blue(`šŸš€ Starting ${config.name} (${validatorId})...\n`));
234
- console.log(chalk.gray(`šŸ“” RPC Port: ${config.localnet.port}`));
235
- console.log(chalk.gray(`šŸ’° Faucet Port: ${config.localnet.faucetPort}\n`));
236
-
237
- // Programs will be cloned automatically by validator using --clone-program flags
238
- if (config.programs.length > 0) {
239
- console.log(
240
- chalk.cyan(
241
- `šŸ”§ Will clone ${config.programs.length} programs from mainnet during startup\n`
242
- )
243
- );
244
- }
245
-
246
- // Clone tokens after programs
247
- let clonedTokens: ClonedToken[] = [];
248
- if (config.tokens.length > 0) {
249
- const tokenCloner = new TokenCloner();
250
-
251
- // Check which tokens are already cloned and which need to be cloned
252
- const { existingTokens, tokensToClone } = await checkExistingClonedTokens(
253
- config.tokens,
254
- tokenCloner
255
- );
256
-
257
- if (existingTokens.length > 0) {
258
- console.log(
259
- chalk.green(`šŸ“ Found ${existingTokens.length} already cloned tokens`)
260
- );
261
- if (debug) {
262
- existingTokens.forEach((token: ClonedToken) => {
263
- console.log(
264
- chalk.gray(
265
- ` āœ“ ${token.config.symbol} (${token.config.mainnetMint})`
266
- )
267
- );
268
- });
269
- }
270
- clonedTokens.push(...existingTokens);
271
- }
272
-
273
- if (tokensToClone.length > 0) {
274
- console.log(
275
- chalk.yellow(
276
- `šŸ“¦ Cloning ${tokensToClone.length} new tokens from mainnet...\n`
277
- )
278
- );
279
- try {
280
- const newlyClonedTokens = await tokenCloner.cloneTokens(
281
- tokensToClone,
282
- config.localnet.rpc,
283
- debug
284
- );
285
- clonedTokens.push(...newlyClonedTokens);
286
- console.log(
287
- chalk.green(
288
- `āœ… Successfully cloned ${newlyClonedTokens.length} new tokens\n`
289
- )
290
- );
291
- } catch (error) {
292
- console.error(chalk.red("āŒ Failed to clone tokens:"));
293
- console.error(
294
- chalk.red(error instanceof Error ? error.message : String(error))
295
- );
296
- console.log(
297
- chalk.yellow(
298
- "šŸ’” You can start without tokens by removing them from sf.config.json"
299
- )
300
- );
301
- process.exit(1);
302
- }
303
- } else if (existingTokens.length > 0) {
304
- console.log(
305
- chalk.green("āœ… All tokens already cloned, skipping clone step\n")
306
- );
307
- }
308
- }
309
-
310
- // Build validator command arguments
311
- const args = buildValidatorArgs(config, clonedTokens);
312
-
313
- console.log(chalk.gray("Command to run:"));
314
- console.log(chalk.gray(`solana-test-validator ${args.join(" ")}\n`));
315
-
316
- if (debug) {
317
- console.log(chalk.yellow("šŸ› Debug mode enabled"));
318
- console.log(chalk.gray("Full command details:"));
319
- console.log(chalk.gray(` Command: solana-test-validator`));
320
- console.log(chalk.gray(` Arguments: ${JSON.stringify(args, null, 2)}`));
321
- }
322
-
323
- // Start the validator
324
- const spinner = ora("Starting Solana test validator...").start();
325
-
326
- try {
327
- // Start validator in background
328
- const validatorProcess = await startValidatorInBackground(
329
- "solana-test-validator",
330
- args,
331
- debug
332
- );
333
-
334
- // Wait for validator to be ready
335
- spinner.text = "Waiting for validator to be ready...";
336
- await waitForValidatorReady(
337
- `http://127.0.0.1:${config.localnet.port}`,
338
- debug
339
- );
340
-
341
- // Find an available port for the API server
342
- let apiServerPort = 3000;
343
- while (!(await portManager.isPortAvailable(apiServerPort))) {
344
- apiServerPort++;
345
- if (apiServerPort > 3100) {
346
- throw new Error("Could not find available port for API server");
347
- }
348
- }
349
-
350
- // Start the API server as a background process
351
- let apiServerPid: number | undefined;
352
- let apiResult: { success: boolean; error?: string } = {
353
- success: false,
354
- error: "Not started",
355
- };
356
-
357
- try {
358
- const currentDir = process.cwd();
359
- const testpilotDir = join(__dirname, "..", "..");
360
- const apiServerScript = join(testpilotDir, "src", "api-server-entry.ts");
361
- const configPath =
362
- configManager.getConfigPath() ?? join(currentDir, "sf.config.json");
363
- const workDir = join(currentDir, ".solforge");
364
-
365
- // Start API server in background using runCommand with nohup
366
- const hostFlag = network ? ` --host "0.0.0.0"` : "";
367
- 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}"${hostFlag} > /dev/null 2>&1 &`;
368
-
369
- const startResult = await runCommand("sh", ["-c", apiServerCommand], {
370
- silent: !debug,
371
- debug: debug,
372
- });
373
-
374
- if (startResult.success) {
375
- // Wait a moment for the API server to start
376
- await new Promise((resolve) => setTimeout(resolve, 3000));
377
-
378
- // Test if the API server is responding
379
- try {
380
- const healthCheckHost = network ? "0.0.0.0" : "127.0.0.1";
381
- const response = await fetch(
382
- `http://${healthCheckHost}:${apiServerPort}/api/health`
383
- );
384
- if (response.ok) {
385
- apiResult = { success: true };
386
- // Get the PID of the API server process
387
- const pidResult = await runCommand(
388
- "pgrep",
389
- ["-f", `api-server-entry.*--port ${apiServerPort}`],
390
- { silent: true, debug: false }
391
- );
392
- if (pidResult.success && pidResult.stdout.trim()) {
393
- const pidLine = pidResult.stdout.trim().split("\n")[0];
394
- if (pidLine) {
395
- apiServerPid = parseInt(pidLine);
396
- }
397
- }
398
- } else {
399
- apiResult = {
400
- success: false,
401
- error: `Health check failed: ${response.status}`,
402
- };
403
- }
404
- } catch (error) {
405
- apiResult = {
406
- success: false,
407
- error: `Health check failed: ${
408
- error instanceof Error ? error.message : String(error)
409
- }`,
410
- };
411
- }
412
- } else {
413
- apiResult = {
414
- success: false,
415
- error: `Failed to start API server: ${
416
- startResult.stderr || "Unknown error"
417
- }`,
418
- };
419
- }
420
- } catch (error) {
421
- apiResult = {
422
- success: false,
423
- error: error instanceof Error ? error.message : String(error),
424
- };
425
- }
426
-
427
- if (!apiResult.success) {
428
- console.warn(
429
- chalk.yellow("āš ļø Failed to start API server:", apiResult.error)
430
- );
431
- }
432
-
433
- // Register the running validator
434
- const runningValidator: RunningValidator = {
435
- id: validatorId,
436
- name: config.name,
437
- pid: validatorProcess.pid!,
438
- rpcPort: config.localnet.port,
439
- faucetPort: config.localnet.faucetPort,
440
- rpcUrl: `http://127.0.0.1:${config.localnet.port}`,
441
- faucetUrl: `http://127.0.0.1:${config.localnet.faucetPort}`,
442
- configPath: configManager.getConfigPath() || "./sf.config.json",
443
- startTime: new Date(),
444
- status: "running",
445
- apiServerPort: apiResult.success ? apiServerPort : undefined,
446
- apiServerUrl: apiResult.success
447
- ? `http://${network ? "0.0.0.0" : "127.0.0.1"}:${apiServerPort}`
448
- : undefined,
449
- apiServerPid: apiResult.success ? apiServerPid : undefined,
450
- };
451
-
452
- processRegistry.register(runningValidator);
453
-
454
- // Validator is now ready
455
- spinner.succeed("Validator started successfully!");
456
-
457
- console.log(chalk.green("āœ… Localnet is running!"));
458
- console.log(chalk.gray(`šŸ†” Validator ID: ${validatorId}`));
459
- console.log(
460
- chalk.cyan(`🌐 RPC URL: http://127.0.0.1:${config.localnet.port}`)
461
- );
462
- console.log(
463
- chalk.cyan(
464
- `šŸ’° Faucet URL: http://127.0.0.1:${config.localnet.faucetPort}`
465
- )
466
- );
467
- if (apiResult.success) {
468
- const displayHost = network ? "0.0.0.0" : "127.0.0.1";
469
- console.log(
470
- chalk.cyan(`šŸš€ API Server: http://${displayHost}:${apiServerPort}/api`)
471
- );
472
- if (network) {
473
- console.log(
474
- chalk.yellow(
475
- " 🌐 Network mode enabled - API server accessible from other devices"
476
- )
477
- );
478
- }
479
- }
480
-
481
- // Airdrop SOL to mint authority if tokens were cloned
482
- if (clonedTokens.length > 0) {
483
- console.log(chalk.yellow("\nšŸ’ø Airdropping SOL to mint authority..."));
484
- const rpcUrl = `http://127.0.0.1:${config.localnet.port}`;
485
-
486
- try {
487
- await airdropSolToMintAuthority(clonedTokens[0], rpcUrl, debug);
488
- console.log(chalk.green("āœ… SOL airdropped successfully!"));
489
- } catch (error) {
490
- console.error(chalk.red("āŒ Failed to airdrop SOL:"));
491
- console.error(
492
- chalk.red(error instanceof Error ? error.message : String(error))
493
- );
494
- console.log(
495
- chalk.yellow(
496
- "šŸ’” You may need to manually airdrop SOL for fee payments"
497
- )
498
- );
499
- }
500
- }
501
-
502
- // Mint tokens if any were cloned
503
- if (clonedTokens.length > 0) {
504
- console.log(chalk.yellow("\nšŸ’° Minting tokens..."));
505
- const tokenCloner = new TokenCloner();
506
- const rpcUrl = `http://127.0.0.1:${config.localnet.port}`;
507
-
508
- if (debug) {
509
- console.log(
510
- chalk.gray(`šŸ› Minting ${clonedTokens.length} tokens to recipients:`)
511
- );
512
- clonedTokens.forEach((token, index) => {
513
- console.log(
514
- chalk.gray(
515
- ` ${index + 1}. ${token.config.symbol} (${
516
- token.config.mainnetMint
517
- }) - ${token.config.mintAmount} tokens`
518
- )
519
- );
520
- });
521
- console.log(chalk.gray(`🌐 Using RPC: ${rpcUrl}`));
522
- }
523
-
524
- try {
525
- await tokenCloner.mintTokensToRecipients(clonedTokens, rpcUrl, debug);
526
- console.log(chalk.green("āœ… Token minting completed!"));
527
-
528
- if (debug) {
529
- console.log(
530
- chalk.gray(
531
- "šŸ› All tokens have been minted to their respective recipients"
532
- )
533
- );
534
- }
535
- } catch (error) {
536
- console.error(chalk.red("āŒ Failed to mint tokens:"));
537
- console.error(
538
- chalk.red(error instanceof Error ? error.message : String(error))
539
- );
540
- console.log(
541
- chalk.yellow("šŸ’” Validator is still running, you can mint manually")
542
- );
543
- }
544
- }
545
-
546
- if (config.tokens.length > 0) {
547
- console.log(chalk.yellow("\nšŸŖ™ Cloned tokens:"));
548
- config.tokens.forEach((token) => {
549
- console.log(
550
- chalk.gray(
551
- ` - ${token.symbol}: ${token.mainnetMint} (${token.mintAmount} minted)`
552
- )
553
- );
554
- });
555
- }
556
-
557
- if (config.programs.length > 0) {
558
- console.log(chalk.yellow("\nšŸ“¦ Cloned programs:"));
559
- config.programs.forEach((program) => {
560
- const name =
561
- program.name || program.mainnetProgramId.slice(0, 8) + "...";
562
- console.log(chalk.gray(` - ${name}: ${program.mainnetProgramId}`));
563
- });
564
- }
565
-
566
- console.log(chalk.blue("\nšŸ’” Tips:"));
567
- console.log(
568
- chalk.gray(" - Run `solforge list` to see all running validators")
569
- );
570
- console.log(
571
- chalk.gray(" - Run `solforge status` to check validator status")
572
- );
573
- console.log(
574
- chalk.gray(
575
- ` - Run \`solforge stop ${validatorId}\` to stop this validator`
576
- )
577
- );
578
- console.log(
579
- chalk.gray(" - Run `solforge stop --all` to stop all validators")
580
- );
581
- if (apiResult.success) {
582
- const endpointHost = network ? "0.0.0.0" : "127.0.0.1";
583
- console.log(chalk.blue("\nšŸ”Œ API Endpoints:"));
584
- console.log(
585
- chalk.gray(
586
- ` - GET http://${endpointHost}:${apiServerPort}/api/tokens - List cloned tokens`
587
- )
588
- );
589
- console.log(
590
- chalk.gray(
591
- ` - GET http://${endpointHost}:${apiServerPort}/api/programs - List cloned programs`
592
- )
593
- );
594
- console.log(
595
- chalk.gray(
596
- ` - POST http://${endpointHost}:${apiServerPort}/api/tokens/{mintAddress}/mint - Mint tokens`
597
- )
598
- );
599
- console.log(
600
- chalk.gray(
601
- ` - POST http://${endpointHost}:${apiServerPort}/api/airdrop - Airdrop SOL`
602
- )
603
- );
604
- console.log(
605
- chalk.gray(
606
- ` - GET http://${endpointHost}:${apiServerPort}/api/wallet/{address}/balances - Get balances`
607
- )
608
- );
609
- }
610
- } catch (error) {
611
- spinner.fail("Failed to start validator");
612
- console.error(chalk.red("āŒ Unexpected error:"));
613
- console.error(
614
- chalk.red(error instanceof Error ? error.message : String(error))
615
- );
616
- process.exit(1);
617
- }
618
- }
619
-
620
- function buildValidatorArgs(
621
- config: Config,
622
- clonedTokens: ClonedToken[] = []
623
- ): string[] {
624
- const args: string[] = [];
625
-
626
- // Basic configuration
627
- args.push("--rpc-port", config.localnet.port.toString());
628
- args.push("--faucet-port", config.localnet.faucetPort.toString());
629
- args.push("--bind-address", config.localnet.bindAddress);
630
-
631
- if (config.localnet.reset) {
632
- args.push("--reset");
633
- }
634
-
635
- if (config.localnet.quiet) {
636
- args.push("--quiet");
637
- } else {
638
- // Always use quiet mode to prevent log spam in background
639
- args.push("--quiet");
640
- }
641
-
642
- // Add ledger size limit
643
- args.push("--limit-ledger-size", config.localnet.limitLedgerSize.toString());
644
-
645
- // Add cloned token accounts (using modified JSON files)
646
- if (clonedTokens.length > 0) {
647
- const tokenCloner = new TokenCloner();
648
- const tokenArgs = tokenCloner.getValidatorArgs(clonedTokens);
649
- args.push(...tokenArgs);
650
- }
651
-
652
- // Clone programs from mainnet using built-in validator flags
653
- for (const program of config.programs) {
654
- if (program.upgradeable) {
655
- args.push("--clone-upgradeable-program", program.mainnetProgramId);
656
- } else {
657
- // Use --clone for regular programs (non-upgradeable)
658
- args.push("--clone", program.mainnetProgramId);
659
- }
660
- }
661
-
662
- // If we're cloning programs, specify the source cluster
663
- if (config.programs.length > 0) {
664
- args.push("--url", config.localnet.rpc);
665
- }
666
-
667
- return args;
668
- }
669
-
670
- /**
671
- * Start the validator in the background
672
- */
673
- async function startValidatorInBackground(
674
- command: string,
675
- args: string[],
676
- debug: boolean
677
- ): Promise<{ pid: number }> {
678
- return new Promise((resolve, reject) => {
679
- if (debug) {
680
- console.log(chalk.gray(`Starting ${command} in background...`));
681
- console.log(chalk.gray(`Command: ${command} ${args.join(" ")}`));
682
- }
683
-
684
- const child = spawn(command, args, {
685
- detached: true,
686
- stdio: "ignore", // Always ignore stdio to ensure it runs in background
687
- });
688
-
689
- child.on("error", (error) => {
690
- reject(new Error(`Failed to start validator: ${error.message}`));
691
- });
692
-
693
- // Give the validator a moment to start
694
- setTimeout(() => {
695
- if (child.pid) {
696
- child.unref(); // Allow parent to exit without waiting for child
697
- if (debug) {
698
- console.log(
699
- chalk.gray(`āœ… Validator started with PID: ${child.pid}`)
700
- );
701
- }
702
- resolve({ pid: child.pid });
703
- } else {
704
- reject(new Error("Validator failed to start"));
705
- }
706
- }, 1000);
707
- });
708
- }
709
-
710
- /**
711
- * Wait for the validator to be ready by polling the RPC endpoint
712
- */
713
- async function waitForValidatorReady(
714
- rpcUrl: string,
715
- debug: boolean,
716
- maxAttempts: number = 30
717
- ): Promise<void> {
718
- let lastError: string = "";
719
-
720
- for (let attempt = 1; attempt <= maxAttempts; attempt++) {
721
- try {
722
- if (debug) {
723
- console.log(
724
- chalk.gray(`Attempt ${attempt}/${maxAttempts}: Checking ${rpcUrl}`)
725
- );
726
- }
727
-
728
- const result = await runCommand(
729
- "curl",
730
- [
731
- "-s",
732
- "-X",
733
- "POST",
734
- rpcUrl,
735
- "-H",
736
- "Content-Type: application/json",
737
- "-d",
738
- '{"jsonrpc":"2.0","id":1,"method":"getHealth"}',
739
- ],
740
- { silent: true, debug: false }
741
- );
742
-
743
- if (result.success && result.stdout.includes("ok")) {
744
- if (debug) {
745
- console.log(
746
- chalk.green(`āœ… Validator is ready after ${attempt} attempts`)
747
- );
748
- }
749
- return;
750
- }
751
-
752
- // Store the last error for better diagnostics
753
- if (!result.success) {
754
- lastError = result.stderr || "Unknown error";
755
- }
756
- } catch (error) {
757
- lastError = error instanceof Error ? error.message : String(error);
758
- }
759
-
760
- if (attempt < maxAttempts) {
761
- await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait 1 second
762
- }
763
- }
764
-
765
- // Provide better error message
766
- let errorMsg = `Validator failed to become ready after ${maxAttempts} attempts`;
767
- if (lastError.includes("Connection refused")) {
768
- errorMsg += `\nšŸ’” This usually means:\n - Port ${
769
- rpcUrl.split(":")[2]
770
- } is already in use\n - Run 'pkill -f solana-test-validator' to kill existing validators`;
771
- } else if (lastError) {
772
- errorMsg += `\nLast error: ${lastError}`;
773
- }
774
-
775
- throw new Error(errorMsg);
776
- }
777
-
778
- /**
779
- * Airdrop SOL to the mint authority for fee payments
780
- */
781
- async function airdropSolToMintAuthority(
782
- clonedToken: any,
783
- rpcUrl: string,
784
- debug: boolean = false
785
- ): Promise<void> {
786
- if (debug) {
787
- console.log(
788
- chalk.gray(`Airdropping 10 SOL to ${clonedToken.mintAuthority.publicKey}`)
789
- );
790
- }
791
-
792
- const airdropResult = await runCommand(
793
- "solana",
794
- ["airdrop", "10", clonedToken.mintAuthority.publicKey, "--url", rpcUrl],
795
- { silent: !debug, debug }
796
- );
797
-
798
- if (!airdropResult.success) {
799
- throw new Error(
800
- `Failed to airdrop SOL: ${airdropResult.stderr || airdropResult.stdout}`
801
- );
802
- }
803
-
804
- if (debug) {
805
- console.log(chalk.gray("SOL airdrop completed"));
806
- }
807
- }
808
-
809
- /**
810
- * Check for existing cloned tokens and return what's already cloned vs what needs to be cloned
811
- */
812
- async function checkExistingClonedTokens(
813
- tokens: TokenConfig[],
814
- tokenCloner: TokenCloner
815
- ): Promise<{ existingTokens: ClonedToken[]; tokensToClone: TokenConfig[] }> {
816
- const existingTokens: ClonedToken[] = [];
817
- const tokensToClone: TokenConfig[] = [];
818
- const workDir = ".solforge";
819
-
820
- // Check for shared mint authority
821
- const sharedMintAuthorityPath = join(workDir, "shared-mint-authority.json");
822
- let sharedMintAuthority: { publicKey: string; secretKey: number[] } | null =
823
- null;
824
-
825
- if (existsSync(sharedMintAuthorityPath)) {
826
- try {
827
- const fileContent = JSON.parse(
828
- readFileSync(sharedMintAuthorityPath, "utf8")
829
- );
830
-
831
- if (Array.isArray(fileContent)) {
832
- // New format: file contains just the secret key array
833
- const { Keypair } = await import("@solana/web3.js");
834
- const keypair = Keypair.fromSecretKey(new Uint8Array(fileContent));
835
- sharedMintAuthority = {
836
- publicKey: keypair.publicKey.toBase58(),
837
- secretKey: Array.from(keypair.secretKey),
838
- };
839
-
840
- // Check metadata for consistency
841
- const metadataPath = join(workDir, "shared-mint-authority-meta.json");
842
- if (existsSync(metadataPath)) {
843
- const metadata = JSON.parse(readFileSync(metadataPath, "utf8"));
844
- if (metadata.publicKey !== sharedMintAuthority.publicKey) {
845
- sharedMintAuthority.publicKey = metadata.publicKey;
846
- }
847
- }
848
- } else {
849
- // Old format: file contains {publicKey, secretKey}
850
- sharedMintAuthority = fileContent;
851
- }
852
- } catch (error) {
853
- // If we can't read the shared mint authority, treat all tokens as needing to be cloned
854
- sharedMintAuthority = null;
855
- }
856
- }
857
-
858
- for (const token of tokens) {
859
- const tokenDir = join(workDir, `token-${token.symbol.toLowerCase()}`);
860
- const modifiedAccountPath = join(tokenDir, "modified.json");
861
-
862
- // Check if this token has already been cloned
863
- if (existsSync(modifiedAccountPath) && sharedMintAuthority) {
864
- // Token appears to be already cloned
865
- existingTokens.push({
866
- config: token,
867
- mintAuthorityPath: sharedMintAuthorityPath,
868
- modifiedAccountPath,
869
- mintAuthority: sharedMintAuthority,
870
- });
871
- } else {
872
- // Token needs to be cloned
873
- tokensToClone.push(token);
874
- }
875
- }
876
-
877
- return { existingTokens, tokensToClone };
878
- }