solforge 0.2.0 ā 0.2.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.
- package/LICENSE +21 -0
- package/docs/API.md +379 -0
- package/docs/CONFIGURATION.md +407 -0
- package/package.json +67 -45
- package/src/api-server-entry.ts +109 -0
- package/src/commands/add-program.ts +337 -0
- package/src/commands/init.ts +122 -0
- package/src/commands/list.ts +136 -0
- package/src/commands/mint.ts +288 -0
- package/src/commands/start.ts +877 -0
- package/src/commands/status.ts +99 -0
- package/src/commands/stop.ts +406 -0
- package/src/config/manager.ts +157 -0
- package/src/gui/public/build/main.css +1 -0
- package/src/gui/public/build/main.js +303 -0
- package/src/gui/public/build/main.js.txt +231 -0
- package/src/index.ts +188 -0
- package/src/services/api-server.ts +485 -0
- package/src/services/port-manager.ts +177 -0
- package/src/services/process-registry.ts +154 -0
- package/src/services/program-cloner.ts +317 -0
- package/src/services/token-cloner.ts +809 -0
- package/src/services/validator.ts +295 -0
- package/src/types/config.ts +110 -0
- package/src/utils/shell.ts +110 -0
- package/src/utils/token-loader.ts +115 -0
- package/.agi/agi.sqlite +0 -0
- package/.claude/settings.local.json +0 -9
- package/.github/workflows/release-binaries.yml +0 -133
- package/.tmp/.787ebcdbf7b8fde8-00000000.hm +0 -0
- package/.tmp/.bffe6efebdf8aedc-00000000.hm +0 -0
- package/AGENTS.md +0 -271
- package/CLAUDE.md +0 -106
- package/PROJECT_STRUCTURE.md +0 -124
- package/SOLANA_KIT_GUIDE.md +0 -251
- package/SOLFORGE.md +0 -119
- package/biome.json +0 -34
- package/bun.lock +0 -743
- package/drizzle/0000_friendly_millenium_guard.sql +0 -53
- package/drizzle/0001_stale_sentinels.sql +0 -2
- package/drizzle/meta/0000_snapshot.json +0 -329
- package/drizzle/meta/0001_snapshot.json +0 -345
- package/drizzle/meta/_journal.json +0 -20
- package/drizzle.config.ts +0 -12
- package/index.ts +0 -21
- package/mint.sh +0 -47
- package/postcss.config.js +0 -6
- package/rpc-server.ts.backup +0 -519
- package/sf.config.json +0 -38
- package/tailwind.config.js +0 -27
- package/test-client.ts +0 -120
- package/tmp/inspect-html.ts +0 -4
- package/tmp/response-test.ts +0 -5
- package/tmp/test-html.ts +0 -5
- package/tmp/test-server.ts +0 -13
- package/tsconfig.json +0 -29
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { processRegistry } from "./process-registry.js";
|
|
2
|
+
|
|
3
|
+
export interface PortAllocation {
|
|
4
|
+
rpcPort: number;
|
|
5
|
+
faucetPort: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export class PortManager {
|
|
9
|
+
private readonly defaultRpcPort = 8899;
|
|
10
|
+
private readonly defaultFaucetPort = 9900;
|
|
11
|
+
private readonly portRangeStart = 8000;
|
|
12
|
+
private readonly portRangeEnd = 9999;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Get the next available port pair (RPC + Faucet)
|
|
16
|
+
*/
|
|
17
|
+
async getAvailablePorts(preferredRpcPort?: number): Promise<PortAllocation> {
|
|
18
|
+
const usedPorts = this.getUsedPorts();
|
|
19
|
+
|
|
20
|
+
// If preferred port is specified and available, use it
|
|
21
|
+
if (preferredRpcPort && !this.isPortUsed(preferredRpcPort, usedPorts)) {
|
|
22
|
+
const faucetPort = this.findAvailableFaucetPort(
|
|
23
|
+
preferredRpcPort,
|
|
24
|
+
usedPorts
|
|
25
|
+
);
|
|
26
|
+
if (faucetPort) {
|
|
27
|
+
return { rpcPort: preferredRpcPort, faucetPort };
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Otherwise, find the next available ports
|
|
32
|
+
return this.findNextAvailablePorts(usedPorts);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Check if a specific port is available
|
|
37
|
+
*/
|
|
38
|
+
async isPortAvailable(port: number): Promise<boolean> {
|
|
39
|
+
const usedPorts = this.getUsedPorts();
|
|
40
|
+
return (
|
|
41
|
+
!this.isPortUsed(port, usedPorts) &&
|
|
42
|
+
(await this.checkPortActuallyFree(port))
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Get all currently used ports from running validators
|
|
48
|
+
*/
|
|
49
|
+
private getUsedPorts(): Set<number> {
|
|
50
|
+
const validators = processRegistry.getRunning();
|
|
51
|
+
const usedPorts = new Set<number>();
|
|
52
|
+
|
|
53
|
+
validators.forEach((validator) => {
|
|
54
|
+
usedPorts.add(validator.rpcPort);
|
|
55
|
+
usedPorts.add(validator.faucetPort);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
return usedPorts;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Check if a port is in the used ports set
|
|
63
|
+
*/
|
|
64
|
+
private isPortUsed(port: number, usedPorts: Set<number>): boolean {
|
|
65
|
+
return usedPorts.has(port);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Find an available faucet port for a given RPC port
|
|
70
|
+
*/
|
|
71
|
+
private findAvailableFaucetPort(
|
|
72
|
+
rpcPort: number,
|
|
73
|
+
usedPorts: Set<number>
|
|
74
|
+
): number | null {
|
|
75
|
+
// Try default offset first (faucet = rpc + 1001)
|
|
76
|
+
let faucetPort = rpcPort + 1001;
|
|
77
|
+
if (
|
|
78
|
+
!this.isPortUsed(faucetPort, usedPorts) &&
|
|
79
|
+
this.isPortInRange(faucetPort)
|
|
80
|
+
) {
|
|
81
|
+
return faucetPort;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Try other offsets
|
|
85
|
+
const offsets = [1000, 1002, 1003, 1004, 1005, 999, 998, 997];
|
|
86
|
+
for (const offset of offsets) {
|
|
87
|
+
faucetPort = rpcPort + offset;
|
|
88
|
+
if (
|
|
89
|
+
!this.isPortUsed(faucetPort, usedPorts) &&
|
|
90
|
+
this.isPortInRange(faucetPort)
|
|
91
|
+
) {
|
|
92
|
+
return faucetPort;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Search in the entire range
|
|
97
|
+
for (let port = this.portRangeStart; port <= this.portRangeEnd; port++) {
|
|
98
|
+
if (!this.isPortUsed(port, usedPorts)) {
|
|
99
|
+
return port;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Find the next available port pair
|
|
108
|
+
*/
|
|
109
|
+
private findNextAvailablePorts(usedPorts: Set<number>): PortAllocation {
|
|
110
|
+
// Start from default ports if available
|
|
111
|
+
if (!this.isPortUsed(this.defaultRpcPort, usedPorts)) {
|
|
112
|
+
const faucetPort = this.findAvailableFaucetPort(
|
|
113
|
+
this.defaultRpcPort,
|
|
114
|
+
usedPorts
|
|
115
|
+
);
|
|
116
|
+
if (faucetPort) {
|
|
117
|
+
return { rpcPort: this.defaultRpcPort, faucetPort };
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Search for available RPC port
|
|
122
|
+
for (
|
|
123
|
+
let rpcPort = this.portRangeStart;
|
|
124
|
+
rpcPort <= this.portRangeEnd;
|
|
125
|
+
rpcPort++
|
|
126
|
+
) {
|
|
127
|
+
if (!this.isPortUsed(rpcPort, usedPorts)) {
|
|
128
|
+
const faucetPort = this.findAvailableFaucetPort(rpcPort, usedPorts);
|
|
129
|
+
if (faucetPort) {
|
|
130
|
+
return { rpcPort, faucetPort };
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
throw new Error("No available port pairs found in the specified range");
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Check if port is within allowed range
|
|
140
|
+
*/
|
|
141
|
+
private isPortInRange(port: number): boolean {
|
|
142
|
+
return port >= this.portRangeStart && port <= this.portRangeEnd;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Actually check if a port is free by attempting to bind to it
|
|
147
|
+
*/
|
|
148
|
+
private async checkPortActuallyFree(port: number): Promise<boolean> {
|
|
149
|
+
return new Promise((resolve) => {
|
|
150
|
+
const net = require("net");
|
|
151
|
+
const server = net.createServer();
|
|
152
|
+
|
|
153
|
+
server.listen(port, (err: any) => {
|
|
154
|
+
if (err) {
|
|
155
|
+
resolve(false);
|
|
156
|
+
} else {
|
|
157
|
+
server.once("close", () => resolve(true));
|
|
158
|
+
server.close();
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
server.on("error", () => resolve(false));
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Get recommended ports for a configuration
|
|
168
|
+
*/
|
|
169
|
+
async getRecommendedPorts(config: {
|
|
170
|
+
localnet: { port: number; faucetPort: number };
|
|
171
|
+
}): Promise<PortAllocation> {
|
|
172
|
+
return this.getAvailablePorts(config.localnet.port);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Singleton instance
|
|
177
|
+
export const portManager = new PortManager();
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { writeFileSync, readFileSync, existsSync } from "fs";
|
|
2
|
+
import { join } from "path";
|
|
3
|
+
import { homedir } from "os";
|
|
4
|
+
import type { Config } from "../types/config.js";
|
|
5
|
+
|
|
6
|
+
export interface RunningValidator {
|
|
7
|
+
id: string;
|
|
8
|
+
name: string;
|
|
9
|
+
pid: number;
|
|
10
|
+
rpcPort: number;
|
|
11
|
+
faucetPort: number;
|
|
12
|
+
rpcUrl: string;
|
|
13
|
+
faucetUrl: string;
|
|
14
|
+
configPath: string;
|
|
15
|
+
startTime: Date;
|
|
16
|
+
status: "running" | "stopped" | "error";
|
|
17
|
+
apiServerPort?: number;
|
|
18
|
+
apiServerUrl?: string;
|
|
19
|
+
apiServerPid?: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class ProcessRegistry {
|
|
23
|
+
private registryPath: string;
|
|
24
|
+
|
|
25
|
+
constructor() {
|
|
26
|
+
// Store registry in user's home directory
|
|
27
|
+
this.registryPath = join(homedir(), ".solforge", "running-validators.json");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Get all running validators
|
|
32
|
+
*/
|
|
33
|
+
getRunning(): RunningValidator[] {
|
|
34
|
+
if (!existsSync(this.registryPath)) {
|
|
35
|
+
return [];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
try {
|
|
39
|
+
const content = readFileSync(this.registryPath, "utf-8");
|
|
40
|
+
const validators = JSON.parse(content) as RunningValidator[];
|
|
41
|
+
|
|
42
|
+
// Convert startTime strings back to Date objects
|
|
43
|
+
return validators.map((v) => ({
|
|
44
|
+
...v,
|
|
45
|
+
startTime: new Date(v.startTime),
|
|
46
|
+
}));
|
|
47
|
+
} catch {
|
|
48
|
+
return [];
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Register a new running validator
|
|
54
|
+
*/
|
|
55
|
+
register(validator: RunningValidator): void {
|
|
56
|
+
const validators = this.getRunning();
|
|
57
|
+
|
|
58
|
+
// Remove any existing entry with the same ID
|
|
59
|
+
const updated = validators.filter((v) => v.id !== validator.id);
|
|
60
|
+
updated.push(validator);
|
|
61
|
+
|
|
62
|
+
this.save(updated);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Unregister a validator
|
|
67
|
+
*/
|
|
68
|
+
unregister(id: string): void {
|
|
69
|
+
const validators = this.getRunning();
|
|
70
|
+
const updated = validators.filter((v) => v.id !== id);
|
|
71
|
+
this.save(updated);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Update validator status
|
|
76
|
+
*/
|
|
77
|
+
updateStatus(id: string, status: RunningValidator["status"]): void {
|
|
78
|
+
const validators = this.getRunning();
|
|
79
|
+
const validator = validators.find((v) => v.id === id);
|
|
80
|
+
|
|
81
|
+
if (validator) {
|
|
82
|
+
validator.status = status;
|
|
83
|
+
this.save(validators);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Get validator by ID
|
|
89
|
+
*/
|
|
90
|
+
getById(id: string): RunningValidator | undefined {
|
|
91
|
+
return this.getRunning().find((v) => v.id === id);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Get validator by PID
|
|
96
|
+
*/
|
|
97
|
+
getByPid(pid: number): RunningValidator | undefined {
|
|
98
|
+
return this.getRunning().find((v) => v.pid === pid);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Get validator by port
|
|
103
|
+
*/
|
|
104
|
+
getByPort(port: number): RunningValidator | undefined {
|
|
105
|
+
return this.getRunning().find(
|
|
106
|
+
(v) => v.rpcPort === port || v.faucetPort === port
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Check if a process is actually running
|
|
112
|
+
*/
|
|
113
|
+
async isProcessRunning(pid: number): Promise<boolean> {
|
|
114
|
+
try {
|
|
115
|
+
// Send signal 0 to check if process exists
|
|
116
|
+
process.kill(pid, 0);
|
|
117
|
+
return true;
|
|
118
|
+
} catch {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Clean up dead processes from registry
|
|
125
|
+
*/
|
|
126
|
+
async cleanup(): Promise<void> {
|
|
127
|
+
const validators = this.getRunning();
|
|
128
|
+
const active: RunningValidator[] = [];
|
|
129
|
+
|
|
130
|
+
for (const validator of validators) {
|
|
131
|
+
if (await this.isProcessRunning(validator.pid)) {
|
|
132
|
+
active.push(validator);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
this.save(active);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Save validators to registry file
|
|
141
|
+
*/
|
|
142
|
+
private save(validators: RunningValidator[]): void {
|
|
143
|
+
// Ensure directory exists
|
|
144
|
+
const dir = join(homedir(), ".solforge");
|
|
145
|
+
if (!existsSync(dir)) {
|
|
146
|
+
require("fs").mkdirSync(dir, { recursive: true });
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
writeFileSync(this.registryPath, JSON.stringify(validators, null, 2));
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Singleton instance
|
|
154
|
+
export const processRegistry = new ProcessRegistry();
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
import { readFileSync, writeFileSync, existsSync, mkdirSync } from "fs";
|
|
2
|
+
import { join } from "path";
|
|
3
|
+
import chalk from "chalk";
|
|
4
|
+
import { Connection, PublicKey } from "@solana/web3.js";
|
|
5
|
+
import { runCommand } from "../utils/shell.js";
|
|
6
|
+
import type { ProgramConfig } from "../types/config.js";
|
|
7
|
+
|
|
8
|
+
export class ProgramCloner {
|
|
9
|
+
private workDir: string;
|
|
10
|
+
|
|
11
|
+
constructor(workDir: string = ".solforge") {
|
|
12
|
+
this.workDir = workDir;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Clone programs for validator startup (saved as .so files)
|
|
17
|
+
*/
|
|
18
|
+
async clonePrograms(
|
|
19
|
+
programs: ProgramConfig[],
|
|
20
|
+
targetCluster: string = "mainnet-beta"
|
|
21
|
+
): Promise<
|
|
22
|
+
Array<{
|
|
23
|
+
success: boolean;
|
|
24
|
+
program: ProgramConfig;
|
|
25
|
+
error?: string;
|
|
26
|
+
filePath?: string;
|
|
27
|
+
}>
|
|
28
|
+
> {
|
|
29
|
+
console.log(chalk.cyan("\nš§ Cloning programs from mainnet..."));
|
|
30
|
+
|
|
31
|
+
if (!existsSync(this.workDir)) {
|
|
32
|
+
mkdirSync(this.workDir, { recursive: true });
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const programsDir = join(this.workDir, "programs");
|
|
36
|
+
if (!existsSync(programsDir)) {
|
|
37
|
+
mkdirSync(programsDir, { recursive: true });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const results = [];
|
|
41
|
+
|
|
42
|
+
for (const program of programs) {
|
|
43
|
+
console.log(
|
|
44
|
+
chalk.gray(
|
|
45
|
+
` š¦ Processing ${program.name || program.mainnetProgramId}...`
|
|
46
|
+
)
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
try {
|
|
50
|
+
// Clone dependencies first
|
|
51
|
+
if (program.dependencies && program.dependencies.length > 0) {
|
|
52
|
+
console.log(
|
|
53
|
+
chalk.gray(
|
|
54
|
+
` š Cloning ${program.dependencies.length} dependencies...`
|
|
55
|
+
)
|
|
56
|
+
);
|
|
57
|
+
for (const depId of program.dependencies) {
|
|
58
|
+
await this.cloneSingleProgram(depId, programsDir, targetCluster);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Clone the main program
|
|
63
|
+
const result = await this.cloneSingleProgram(
|
|
64
|
+
program.mainnetProgramId,
|
|
65
|
+
programsDir,
|
|
66
|
+
targetCluster,
|
|
67
|
+
program.name
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
results.push({
|
|
71
|
+
success: true,
|
|
72
|
+
program,
|
|
73
|
+
filePath: result.filePath,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
console.log(chalk.gray(` ā Cloned to ${result.filePath}`));
|
|
77
|
+
} catch (error) {
|
|
78
|
+
console.error(
|
|
79
|
+
chalk.red(` ā Failed to clone ${program.mainnetProgramId}`)
|
|
80
|
+
);
|
|
81
|
+
console.error(
|
|
82
|
+
chalk.red(
|
|
83
|
+
` ${error instanceof Error ? error.message : String(error)}`
|
|
84
|
+
)
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
results.push({
|
|
88
|
+
success: false,
|
|
89
|
+
program,
|
|
90
|
+
error: error instanceof Error ? error.message : String(error),
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const successful = results.filter((r) => r.success).length;
|
|
96
|
+
console.log(
|
|
97
|
+
chalk.cyan(`\nā
Cloned ${successful}/${programs.length} programs`)
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
return results;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Clone a single program from mainnet
|
|
105
|
+
*/
|
|
106
|
+
private async cloneSingleProgram(
|
|
107
|
+
programId: string,
|
|
108
|
+
outputDir: string,
|
|
109
|
+
cluster: string = "mainnet-beta",
|
|
110
|
+
name?: string
|
|
111
|
+
): Promise<{ filePath: string }> {
|
|
112
|
+
const fileName = name
|
|
113
|
+
? `${name.toLowerCase().replace(/\s+/g, "-")}.so`
|
|
114
|
+
: `${programId}.so`;
|
|
115
|
+
const outputPath = join(outputDir, fileName);
|
|
116
|
+
|
|
117
|
+
// Skip if already exists
|
|
118
|
+
if (existsSync(outputPath)) {
|
|
119
|
+
return { filePath: outputPath };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Use solana account command to fetch program data
|
|
123
|
+
const rpcUrl = this.getClusterUrl(cluster);
|
|
124
|
+
const accountResult = await runCommand(
|
|
125
|
+
"solana",
|
|
126
|
+
["account", programId, "--output", "json", "--url", rpcUrl],
|
|
127
|
+
{ silent: true }
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
if (!accountResult.success) {
|
|
131
|
+
throw new Error(
|
|
132
|
+
`Failed to fetch program account: ${accountResult.stderr}`
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
try {
|
|
137
|
+
const accountData = JSON.parse(accountResult.stdout);
|
|
138
|
+
const programData = accountData.account.data;
|
|
139
|
+
|
|
140
|
+
if (!programData || programData[1] !== "base64") {
|
|
141
|
+
throw new Error("Invalid program data format");
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Decode base64 program data
|
|
145
|
+
const binaryData = Buffer.from(programData[0], "base64");
|
|
146
|
+
|
|
147
|
+
// Write as .so file
|
|
148
|
+
writeFileSync(outputPath, binaryData);
|
|
149
|
+
|
|
150
|
+
return { filePath: outputPath };
|
|
151
|
+
} catch (error) {
|
|
152
|
+
throw new Error(
|
|
153
|
+
`Failed to process program data: ${
|
|
154
|
+
error instanceof Error ? error.message : String(error)
|
|
155
|
+
}`
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Generate validator arguments for cloned programs
|
|
162
|
+
*/
|
|
163
|
+
generateValidatorArgs(
|
|
164
|
+
clonedPrograms: Array<{
|
|
165
|
+
success: boolean;
|
|
166
|
+
program: ProgramConfig;
|
|
167
|
+
filePath?: string;
|
|
168
|
+
}>
|
|
169
|
+
): string[] {
|
|
170
|
+
const args: string[] = [];
|
|
171
|
+
|
|
172
|
+
for (const result of clonedPrograms) {
|
|
173
|
+
if (result.success && result.filePath) {
|
|
174
|
+
args.push("--bpf-program");
|
|
175
|
+
args.push(result.program.mainnetProgramId);
|
|
176
|
+
args.push(result.filePath);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return args;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Deploy program to running validator (hot deployment)
|
|
185
|
+
*/
|
|
186
|
+
async deployToRunningValidator(
|
|
187
|
+
programId: string,
|
|
188
|
+
rpcUrl: string,
|
|
189
|
+
name?: string
|
|
190
|
+
): Promise<{ success: boolean; deployedAddress?: string; error?: string }> {
|
|
191
|
+
try {
|
|
192
|
+
console.log(
|
|
193
|
+
chalk.cyan(`\nš Hot deploying program ${name || programId}...`)
|
|
194
|
+
);
|
|
195
|
+
|
|
196
|
+
// First, clone the program if we don't have it
|
|
197
|
+
const programsDir = join(this.workDir, "programs");
|
|
198
|
+
if (!existsSync(programsDir)) {
|
|
199
|
+
mkdirSync(programsDir, { recursive: true });
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const cloneResult = await this.cloneSingleProgram(
|
|
203
|
+
programId,
|
|
204
|
+
programsDir,
|
|
205
|
+
"mainnet-beta",
|
|
206
|
+
name
|
|
207
|
+
);
|
|
208
|
+
|
|
209
|
+
// Deploy to running validator using solana program deploy
|
|
210
|
+
console.log(chalk.gray(" š¤ Deploying to validator..."));
|
|
211
|
+
|
|
212
|
+
const deployResult = await runCommand(
|
|
213
|
+
"solana",
|
|
214
|
+
[
|
|
215
|
+
"program",
|
|
216
|
+
"deploy",
|
|
217
|
+
cloneResult.filePath,
|
|
218
|
+
"--program-id",
|
|
219
|
+
programId,
|
|
220
|
+
"--url",
|
|
221
|
+
rpcUrl,
|
|
222
|
+
],
|
|
223
|
+
{ silent: false }
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
if (!deployResult.success) {
|
|
227
|
+
return {
|
|
228
|
+
success: false,
|
|
229
|
+
error: `Deployment failed: ${
|
|
230
|
+
deployResult.stderr || deployResult.stdout
|
|
231
|
+
}`,
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
console.log(
|
|
236
|
+
chalk.green(` ā
Successfully deployed ${name || programId}`)
|
|
237
|
+
);
|
|
238
|
+
|
|
239
|
+
return {
|
|
240
|
+
success: true,
|
|
241
|
+
deployedAddress: programId,
|
|
242
|
+
};
|
|
243
|
+
} catch (error) {
|
|
244
|
+
return {
|
|
245
|
+
success: false,
|
|
246
|
+
error: error instanceof Error ? error.message : String(error),
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Get cluster RPC URL
|
|
253
|
+
*/
|
|
254
|
+
private getClusterUrl(cluster: string): string {
|
|
255
|
+
switch (cluster) {
|
|
256
|
+
case "mainnet-beta":
|
|
257
|
+
return "https://api.mainnet-beta.solana.com";
|
|
258
|
+
case "devnet":
|
|
259
|
+
return "https://api.devnet.solana.com";
|
|
260
|
+
case "testnet":
|
|
261
|
+
return "https://api.testnet.solana.com";
|
|
262
|
+
default:
|
|
263
|
+
return cluster; // Assume it's a custom URL
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Verify program exists on cluster
|
|
269
|
+
*/
|
|
270
|
+
async verifyProgram(
|
|
271
|
+
programId: string,
|
|
272
|
+
cluster: string = "mainnet-beta"
|
|
273
|
+
): Promise<boolean> {
|
|
274
|
+
try {
|
|
275
|
+
const connection = new Connection(this.getClusterUrl(cluster));
|
|
276
|
+
const programAccount = await connection.getAccountInfo(
|
|
277
|
+
new PublicKey(programId)
|
|
278
|
+
);
|
|
279
|
+
return programAccount !== null && programAccount.executable;
|
|
280
|
+
} catch {
|
|
281
|
+
return false;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Get program info from cluster
|
|
287
|
+
*/
|
|
288
|
+
async getProgramInfo(
|
|
289
|
+
programId: string,
|
|
290
|
+
cluster: string = "mainnet-beta"
|
|
291
|
+
): Promise<{
|
|
292
|
+
exists: boolean;
|
|
293
|
+
executable?: boolean;
|
|
294
|
+
owner?: string;
|
|
295
|
+
size?: number;
|
|
296
|
+
}> {
|
|
297
|
+
try {
|
|
298
|
+
const connection = new Connection(this.getClusterUrl(cluster));
|
|
299
|
+
const programAccount = await connection.getAccountInfo(
|
|
300
|
+
new PublicKey(programId)
|
|
301
|
+
);
|
|
302
|
+
|
|
303
|
+
if (!programAccount) {
|
|
304
|
+
return { exists: false };
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
return {
|
|
308
|
+
exists: true,
|
|
309
|
+
executable: programAccount.executable,
|
|
310
|
+
owner: programAccount.owner.toBase58(),
|
|
311
|
+
size: programAccount.data.length,
|
|
312
|
+
};
|
|
313
|
+
} catch (error) {
|
|
314
|
+
return { exists: false };
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|