genlayer 0.0.31 → 0.0.32-beta.1

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/jest.config.js CHANGED
@@ -1,8 +1,4 @@
1
1
  /** @type {import('ts-jest').JestConfigWithTsJest} */
2
2
  module.exports = {
3
- preset: "ts-jest",
4
- testEnvironment: "node",
5
- moduleNameMapper: {
6
- "^@/(.*)$": "<rootDir>/src/$1",
7
- },
3
+ preset: "ts-jest/presets/default-esm",
8
4
  };
package/package.json CHANGED
@@ -1,16 +1,17 @@
1
1
  {
2
2
  "name": "genlayer",
3
- "version": "0.0.31",
3
+ "version": "0.0.32-beta.1",
4
4
  "description": "GenLayer Command Line Tool",
5
5
  "main": "src/index.ts",
6
6
  "bin": {
7
7
  "genlayer": "./dist/index.js"
8
8
  },
9
9
  "scripts": {
10
- "test": "jest",
10
+ "test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --watch",
11
11
  "dev": "cross-env NODE_ENV=development node esbuild.config.js",
12
12
  "build": "cross-env NODE_ENV=production node esbuild.config.js",
13
- "release": "release-it --ci"
13
+ "release": "release-it --ci",
14
+ "release-beta": "release-it --ci --preRelease=beta"
14
15
  },
15
16
  "repository": {
16
17
  "type": "git",
@@ -47,7 +48,7 @@
47
48
  "jest": "^29.7.0",
48
49
  "prettier": "^3.2.5",
49
50
  "release-it": "^17.2.0",
50
- "ts-jest": "^29.1.2",
51
+ "ts-jest": "^29.1.3",
51
52
  "ts-node": "^10.9.2",
52
53
  "typescript": "^5.4.5"
53
54
  },
@@ -55,7 +56,7 @@
55
56
  "commander": "^12.0.0",
56
57
  "dotenv": "^16.4.5",
57
58
  "inquirer": "^9.2.19",
58
- "node-fetch": "^3.3.2",
59
+ "node-fetch": "^2.7.0",
59
60
  "open": "^10.1.0",
60
61
  "uuid": "^9.0.1"
61
62
  }
@@ -1,22 +1,25 @@
1
1
  import {Command} from "commander";
2
2
 
3
- import {initAction} from "./init";
4
- import {startAction} from "./start";
3
+ import simulatorService from "../../lib/services/simulator";
4
+
5
+ import {initAction, InitActionOptions} from "./init";
6
+ import {startAction, StartActionOptions} from "./start";
5
7
 
6
8
  export function initializeGeneralCommands(program: Command) {
7
9
  program
8
10
  .command("init")
9
11
  .description("Initialize the GenLayer Environment")
10
- .option("-n, --numValidators <numValidators>", "Number of validators", "5")
11
- .action(initAction);
12
+ .option("--numValidators <numValidators>", "Number of validators", "5")
13
+ .option("--branch <branch>", "Branch", "main")
14
+ .action((options: InitActionOptions) => initAction(options, simulatorService));
12
15
 
13
16
  program
14
17
  .command("up")
15
18
  .description("Starts GenLayer's simulator")
16
- .option("--no-reset-accounts", "Don't restart the database for accouts and transactions", true)
17
19
  .option("--reset-validators", "Remove all current validators and create new random ones", false)
18
20
  .option("--numValidators <numValidators>", "Number of validators", "5")
19
- .action(startAction);
21
+ .option("--branch <branch>", "Branch", "main")
22
+ .action((options: StartActionOptions) => startAction(options, simulatorService));
20
23
 
21
24
  return program;
22
25
  }
@@ -1,27 +1,10 @@
1
1
  import inquirer from "inquirer";
2
2
 
3
- import {AI_PROVIDERS_CONFIG, AiProviders} from "@/lib/config/simulator";
4
- import {
5
- initializeDatabase,
6
- checkRequirements,
7
- downloadSimulator,
8
- configSimulator,
9
- runSimulator,
10
- waitForSimulatorToBeReady,
11
- updateSimulator,
12
- clearAccountsAndTransactionsDatabase,
13
- createRandomValidators,
14
- deleteAllValidators,
15
- pullOllamaModel,
16
- getAiProvidersOptions,
17
- getSimulatorLocation,
18
- getFrontendUrl,
19
- openFrontend,
20
- resetDockerContainers,
21
- resetDockerImages,
22
- } from "@/lib/services/simulator";
3
+ import {ISimulatorService} from "../../lib/interfaces/ISimulatorService";
4
+ import {AI_PROVIDERS_CONFIG, AiProviders} from "../../lib/config/simulator";
23
5
  export interface InitActionOptions {
24
6
  numValidators: number;
7
+ branch: string;
25
8
  }
26
9
 
27
10
  function getRequirementsErrorMessage({git, docker}: Record<string, boolean>): string {
@@ -37,12 +20,10 @@ function getRequirementsErrorMessage({git, docker}: Record<string, boolean>): st
37
20
  return "";
38
21
  }
39
22
 
40
- export async function initAction(options: InitActionOptions) {
41
- console.log(`Initializing GenLayer CLI with ${options.numValidators} validators`);
42
-
23
+ export async function initAction(options: InitActionOptions, simulatorService: ISimulatorService) {
43
24
  // Check if git and docker are installed
44
25
  try {
45
- const {git, docker} = await checkRequirements();
26
+ const {git, docker} = await simulatorService.checkRequirements();
46
27
  const errorMessage = getRequirementsErrorMessage({git, docker});
47
28
  if (errorMessage) {
48
29
  console.log(
@@ -56,11 +37,28 @@ export async function initAction(options: InitActionOptions) {
56
37
  return;
57
38
  }
58
39
 
40
+ // Ask for confirmation on reseting the GenLayer Simulator from GitHub
41
+ const resetAnswers = await inquirer.prompt([
42
+ {
43
+ type: "confirm",
44
+ name: "confirmReset",
45
+ message: `This command is going to reset GenLayer docker images and containers, providers API Keys, and GenLayer database (accounts, transactions, and validators). Do you want to continue?`,
46
+ default: true,
47
+ },
48
+ ]);
49
+
50
+ if (!resetAnswers.confirmReset) {
51
+ console.log("Aborted!");
52
+ return;
53
+ }
54
+
55
+ console.log(`Initializing GenLayer CLI with ${options.numValidators} validators`);
56
+
59
57
  // Reset Docker containers and images
60
58
  console.log(`Resetting Docker containers and images...`);
61
59
  try {
62
- await resetDockerContainers();
63
- await resetDockerImages();
60
+ await simulatorService.resetDockerContainers();
61
+ await simulatorService.resetDockerImages();
64
62
  } catch (error) {
65
63
  console.error(error);
66
64
  return;
@@ -71,7 +69,7 @@ export async function initAction(options: InitActionOptions) {
71
69
  {
72
70
  type: "confirm",
73
71
  name: "confirmDownload",
74
- message: `This action is going to download the GenLayer Simulator from GitHub into "${getSimulatorLocation()}". Do you want to continue?`,
72
+ message: `This action is going to download the GenLayer Simulator from GitHub (branch ${options.branch}) into "${simulatorService.getSimulatorLocation()}". Do you want to continue?`,
75
73
  default: true,
76
74
  },
77
75
  ]);
@@ -84,9 +82,9 @@ export async function initAction(options: InitActionOptions) {
84
82
  // Download the GenLayer Simulator from GitHub
85
83
  console.log(`Downloading GenLayer Simulator from GitHub...`);
86
84
  try {
87
- const {wasInstalled} = await downloadSimulator();
85
+ const {wasInstalled} = await simulatorService.downloadSimulator(options.branch);
88
86
  if (wasInstalled) {
89
- await updateSimulator();
87
+ await simulatorService.updateSimulator(options.branch);
90
88
  }
91
89
  } catch (error) {
92
90
  console.error(error);
@@ -99,7 +97,7 @@ export async function initAction(options: InitActionOptions) {
99
97
  type: "checkbox",
100
98
  name: "selectedLlmProviders",
101
99
  message: "Select which LLM providers do you want to use:",
102
- choices: getAiProvidersOptions(true),
100
+ choices: simulatorService.getAiProvidersOptions(true),
103
101
  validate: function (answer: string[]) {
104
102
  if (answer.length < 1) {
105
103
  return "You must choose at least one option.";
@@ -141,7 +139,7 @@ export async function initAction(options: InitActionOptions) {
141
139
 
142
140
  console.log("Configuring GenLayer Simulator environment...");
143
141
  try {
144
- await configSimulator(aiProvidersEnvVars);
142
+ await simulatorService.configSimulator(aiProvidersEnvVars);
145
143
  } catch (error) {
146
144
  console.error(error);
147
145
  return;
@@ -150,14 +148,15 @@ export async function initAction(options: InitActionOptions) {
150
148
  // Run the GenLayer Simulator
151
149
  console.log("Running the GenLayer Simulator...");
152
150
  try {
153
- runSimulator();
151
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
152
+ await simulatorService.runSimulator();
154
153
  } catch (error) {
155
154
  console.error(error);
156
155
  return;
157
156
  }
158
157
 
159
158
  try {
160
- const {initialized, errorCode, errorMessage} = await waitForSimulatorToBeReady();
159
+ const {initialized, errorCode, errorMessage} = await simulatorService.waitForSimulatorToBeReady();
161
160
  if (!initialized && errorCode === "ERROR") {
162
161
  console.log(errorMessage);
163
162
  console.error("Unable to initialize the GenLayer simulator. Please try again.");
@@ -178,32 +177,16 @@ export async function initAction(options: InitActionOptions) {
178
177
  // Ollama doesn't need changes in configuration, we just run it
179
178
  if (selectedLlmProviders.includes("ollama")) {
180
179
  console.log("Pulling llama3 from Ollama...");
181
- await pullOllamaModel();
182
- }
183
-
184
- // Initialize the database
185
- console.log("Initializing the database...");
186
- try {
187
- //remove everything from the database
188
- await clearAccountsAndTransactionsDatabase();
189
-
190
- const {createResponse, tablesResponse} = await initializeDatabase();
191
- if (!createResponse || !tablesResponse) {
192
- console.error("Unable to initialize the database. Please try again.");
193
- return;
194
- }
195
- } catch (error) {
196
- console.error(error);
197
- return;
180
+ await simulatorService.pullOllamaModel();
198
181
  }
199
182
 
200
183
  // Initializing validators
201
184
  console.log("Initializing validators...");
202
185
  try {
203
186
  //remove all validators
204
- await deleteAllValidators();
187
+ await simulatorService.deleteAllValidators();
205
188
  // create random validators
206
- await createRandomValidators(Number(options.numValidators), selectedLlmProviders);
189
+ await simulatorService.createRandomValidators(Number(options.numValidators), selectedLlmProviders);
207
190
  } catch (error) {
208
191
  console.error("Unable to initialize the validators.");
209
192
  console.error(error);
@@ -212,10 +195,11 @@ export async function initAction(options: InitActionOptions) {
212
195
 
213
196
  // Simulator ready
214
197
  console.log(
215
- `GenLayer simulator initialized successfully! Go to ${getFrontendUrl()} in your browser to access it.`,
198
+ `GenLayer simulator initialized successfully! Go to ${simulatorService.getFrontendUrl()} in your browser to access it.`,
216
199
  );
217
200
  try {
218
- openFrontend();
201
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
202
+ simulatorService.openFrontend();
219
203
  } catch (error) {
220
204
  console.error(error);
221
205
  }
@@ -1,41 +1,26 @@
1
1
  import inquirer from "inquirer";
2
2
 
3
- import {
4
- updateSimulator,
5
- runSimulator,
6
- waitForSimulatorToBeReady,
7
- deleteAllValidators,
8
- createRandomValidators,
9
- clearAccountsAndTransactionsDatabase,
10
- initializeDatabase,
11
- getFrontendUrl,
12
- openFrontend,
13
- getAiProvidersOptions,
14
- } from "@/lib/services/simulator";
3
+ import {ISimulatorService} from "../../lib/interfaces/ISimulatorService";
15
4
 
16
5
  export interface StartActionOptions {
17
- resetAccounts: string;
18
6
  resetValidators: string;
19
7
  numValidators: number;
8
+ branch: string;
20
9
  }
21
10
 
22
- export async function startAction(options: StartActionOptions) {
23
- const {resetAccounts, resetValidators, numValidators} = options;
24
-
25
- const restartAccountsHintText = resetAccounts
26
- ? "restarting the accounts and transactions database"
27
- : "keeping the accounts and transactions records";
11
+ export async function startAction(options: StartActionOptions, simulatorService: ISimulatorService) {
12
+ const {resetValidators, numValidators, branch} = options;
28
13
 
29
14
  const restartValidatorsHintText = resetValidators
30
- ? `and creating new ${numValidators} random validators`
31
- : "and keeping the existing validators";
15
+ ? `creating new ${numValidators} random validators`
16
+ : "keeping the existing validators";
32
17
 
33
- console.log(`Starting GenLayer simulator ${restartAccountsHintText} ${restartValidatorsHintText}`);
18
+ console.log(`Starting GenLayer simulator ${restartValidatorsHintText}`);
34
19
 
35
20
  // Update the simulator to the latest version
36
21
  console.log(`Updating GenLayer Simulator...`);
37
22
  try {
38
- await updateSimulator();
23
+ await simulatorService.updateSimulator(branch);
39
24
  } catch (error) {
40
25
  console.error(error);
41
26
  return;
@@ -44,14 +29,14 @@ export async function startAction(options: StartActionOptions) {
44
29
  // Run the GenLayer Simulator
45
30
  console.log("Running the GenLayer Simulator...");
46
31
  try {
47
- runSimulator();
32
+ await simulatorService.runSimulator();
48
33
  } catch (error) {
49
34
  console.error(error);
50
35
  return;
51
36
  }
52
37
 
53
38
  try {
54
- const {initialized, errorCode, errorMessage} = await waitForSimulatorToBeReady();
39
+ const {initialized, errorCode, errorMessage} = await simulatorService.waitForSimulatorToBeReady();
55
40
  if (!initialized && errorCode === "ERROR") {
56
41
  console.log(errorMessage);
57
42
  console.error("Unable to initialize the GenLayer simulator. Please try again.");
@@ -69,37 +54,18 @@ export async function startAction(options: StartActionOptions) {
69
54
  return;
70
55
  }
71
56
 
72
- if (resetAccounts) {
73
- // Initialize the database
74
- console.log("Initializing the database...");
75
- try {
76
- //remove everything from the database
77
- await clearAccountsAndTransactionsDatabase();
78
-
79
- const {createResponse, tablesResponse} = await initializeDatabase();
80
- if (!createResponse || !tablesResponse) {
81
- console.error("Unable to initialize the database. Please try again.");
82
- return;
83
- }
84
- } catch (error) {
85
- console.error(error);
86
- return;
87
- }
88
- console.log("Database successfully reset...");
89
- }
90
-
91
57
  if (resetValidators) {
92
58
  // Initializing validators
93
59
  console.log("Initializing validators...");
94
60
  try {
95
61
  //remove all validators
96
- await deleteAllValidators();
62
+ await simulatorService.deleteAllValidators();
97
63
  const questions = [
98
64
  {
99
65
  type: "checkbox",
100
66
  name: "selectedLlmProviders",
101
67
  message: "Select which LLM providers do you want to use:",
102
- choices: getAiProvidersOptions(false),
68
+ choices: simulatorService.getAiProvidersOptions(false),
103
69
  validate: function (answer: string[]) {
104
70
  if (answer.length < 1) {
105
71
  return "You must choose at least one option.";
@@ -113,7 +79,10 @@ export async function startAction(options: StartActionOptions) {
113
79
  const llmProvidersAnswer = await inquirer.prompt(questions);
114
80
 
115
81
  // create random validators
116
- await createRandomValidators(Number(options.numValidators), llmProvidersAnswer.selectedLlmProviders);
82
+ await simulatorService.createRandomValidators(
83
+ Number(options.numValidators),
84
+ llmProvidersAnswer.selectedLlmProviders,
85
+ );
117
86
  } catch (error) {
118
87
  console.error("Unable to initialize the validators.");
119
88
  console.error(error);
@@ -124,10 +93,10 @@ export async function startAction(options: StartActionOptions) {
124
93
 
125
94
  // Simulator ready
126
95
  console.log(
127
- `GenLayer simulator initialized successfully! Go to ${getFrontendUrl()} in your browser to access it.`,
96
+ `GenLayer simulator initialized successfully! Go to ${simulatorService.getFrontendUrl()} in your browser to access it.`,
128
97
  );
129
98
  try {
130
- openFrontend();
99
+ simulatorService.openFrontend();
131
100
  } catch (error) {
132
101
  console.error(error);
133
102
  }
@@ -3,7 +3,7 @@ import {ChildProcess, PromiseWithChild, exec} from "child_process";
3
3
  import os from "os";
4
4
  import open from "open";
5
5
 
6
- import {RunningPlatform, AVAILABLE_PLATFORMS} from "@/lib/config/simulator";
6
+ import {RunningPlatform, AVAILABLE_PLATFORMS} from "../config/simulator";
7
7
  import {MissingRequirementError} from "../errors/missingRequirement";
8
8
 
9
9
  const asyncExec = util.promisify(exec);
@@ -0,0 +1,36 @@
1
+ import {AiProviders} from "../config/simulator";
2
+
3
+ export interface ISimulatorService {
4
+ getSimulatorLocation(): string;
5
+ readEnvConfigValue(key: string): string;
6
+ addConfigToEnvFile(newConfig: Record<string, string>): void;
7
+ checkRequirements(): Promise<Record<string, boolean>>;
8
+ downloadSimulator(branch?: string): Promise<DownloadSimulatorResultType>;
9
+ updateSimulator(branch?: string): Promise<boolean>;
10
+ pullOllamaModel(): Promise<boolean>;
11
+ configSimulator(newConfig: Record<string, string>): Promise<boolean>;
12
+ runSimulator(): Promise<{stdout: string; stderr: string}>;
13
+ waitForSimulatorToBeReady(retries?: number): Promise<WaitForSimulatorToBeReadyResultType>;
14
+ createRandomValidators(numValidators: number, llmProviders: AiProviders[]): Promise<any>;
15
+ deleteAllValidators(): Promise<any>;
16
+ getAiProvidersOptions(withHint: boolean): Array<{name: string; value: string}>;
17
+ getFrontendUrl(): string;
18
+ openFrontend(): Promise<boolean>;
19
+ resetDockerContainers(): Promise<boolean>;
20
+ resetDockerImages(): Promise<boolean>;
21
+ }
22
+
23
+ export type DownloadSimulatorResultType = {
24
+ wasInstalled: boolean;
25
+ };
26
+
27
+ export type WaitForSimulatorToBeReadyResultType = {
28
+ initialized: boolean;
29
+ errorCode?: "TIMEOUT" | "ERROR";
30
+ errorMessage?: string;
31
+ };
32
+
33
+ export type InitializeDatabaseResultType = {
34
+ createResponse: any;
35
+ tablesResponse: any;
36
+ };