genlayer 0.0.31 → 0.0.32-beta.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.
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.0",
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,14 +1,17 @@
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")
@@ -16,7 +19,8 @@ export function initializeGeneralCommands(program: Command) {
16
19
  .option("--no-reset-accounts", "Don't restart the database for accouts and transactions", true)
17
20
  .option("--reset-validators", "Remove all current validators and create new random ones", false)
18
21
  .option("--numValidators <numValidators>", "Number of validators", "5")
19
- .action(startAction);
22
+ .option("--branch <branch>", "Branch", "main")
23
+ .action((options: StartActionOptions) => startAction(options, simulatorService));
20
24
 
21
25
  return program;
22
26
  }
@@ -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,16 +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();
180
+ await simulatorService.pullOllamaModel();
182
181
  }
183
182
 
184
183
  // Initialize the database
185
184
  console.log("Initializing the database...");
186
185
  try {
187
186
  //remove everything from the database
188
- await clearAccountsAndTransactionsDatabase();
187
+ await simulatorService.clearAccountsAndTransactionsDatabase();
189
188
 
190
- const {createResponse, tablesResponse} = await initializeDatabase();
189
+ const {createResponse, tablesResponse} = await simulatorService.initializeDatabase();
191
190
  if (!createResponse || !tablesResponse) {
192
191
  console.error("Unable to initialize the database. Please try again.");
193
192
  return;
@@ -201,9 +200,9 @@ export async function initAction(options: InitActionOptions) {
201
200
  console.log("Initializing validators...");
202
201
  try {
203
202
  //remove all validators
204
- await deleteAllValidators();
203
+ await simulatorService.deleteAllValidators();
205
204
  // create random validators
206
- await createRandomValidators(Number(options.numValidators), selectedLlmProviders);
205
+ await simulatorService.createRandomValidators(Number(options.numValidators), selectedLlmProviders);
207
206
  } catch (error) {
208
207
  console.error("Unable to initialize the validators.");
209
208
  console.error(error);
@@ -212,10 +211,11 @@ export async function initAction(options: InitActionOptions) {
212
211
 
213
212
  // Simulator ready
214
213
  console.log(
215
- `GenLayer simulator initialized successfully! Go to ${getFrontendUrl()} in your browser to access it.`,
214
+ `GenLayer simulator initialized successfully! Go to ${simulatorService.getFrontendUrl()} in your browser to access it.`,
216
215
  );
217
216
  try {
218
- openFrontend();
217
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
218
+ simulatorService.openFrontend();
219
219
  } catch (error) {
220
220
  console.error(error);
221
221
  }
@@ -1,26 +1,16 @@
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
6
  resetAccounts: string;
18
7
  resetValidators: string;
19
8
  numValidators: number;
9
+ branch: string;
20
10
  }
21
11
 
22
- export async function startAction(options: StartActionOptions) {
23
- const {resetAccounts, resetValidators, numValidators} = options;
12
+ export async function startAction(options: StartActionOptions, simulatorService: ISimulatorService) {
13
+ const {resetAccounts, resetValidators, numValidators, branch} = options;
24
14
 
25
15
  const restartAccountsHintText = resetAccounts
26
16
  ? "restarting the accounts and transactions database"
@@ -35,7 +25,7 @@ export async function startAction(options: StartActionOptions) {
35
25
  // Update the simulator to the latest version
36
26
  console.log(`Updating GenLayer Simulator...`);
37
27
  try {
38
- await updateSimulator();
28
+ await simulatorService.updateSimulator(branch);
39
29
  } catch (error) {
40
30
  console.error(error);
41
31
  return;
@@ -44,14 +34,14 @@ export async function startAction(options: StartActionOptions) {
44
34
  // Run the GenLayer Simulator
45
35
  console.log("Running the GenLayer Simulator...");
46
36
  try {
47
- runSimulator();
37
+ await simulatorService.runSimulator();
48
38
  } catch (error) {
49
39
  console.error(error);
50
40
  return;
51
41
  }
52
42
 
53
43
  try {
54
- const {initialized, errorCode, errorMessage} = await waitForSimulatorToBeReady();
44
+ const {initialized, errorCode, errorMessage} = await simulatorService.waitForSimulatorToBeReady();
55
45
  if (!initialized && errorCode === "ERROR") {
56
46
  console.log(errorMessage);
57
47
  console.error("Unable to initialize the GenLayer simulator. Please try again.");
@@ -74,9 +64,9 @@ export async function startAction(options: StartActionOptions) {
74
64
  console.log("Initializing the database...");
75
65
  try {
76
66
  //remove everything from the database
77
- await clearAccountsAndTransactionsDatabase();
67
+ await simulatorService.clearAccountsAndTransactionsDatabase();
78
68
 
79
- const {createResponse, tablesResponse} = await initializeDatabase();
69
+ const {createResponse, tablesResponse} = await simulatorService.initializeDatabase();
80
70
  if (!createResponse || !tablesResponse) {
81
71
  console.error("Unable to initialize the database. Please try again.");
82
72
  return;
@@ -93,13 +83,13 @@ export async function startAction(options: StartActionOptions) {
93
83
  console.log("Initializing validators...");
94
84
  try {
95
85
  //remove all validators
96
- await deleteAllValidators();
86
+ await simulatorService.deleteAllValidators();
97
87
  const questions = [
98
88
  {
99
89
  type: "checkbox",
100
90
  name: "selectedLlmProviders",
101
91
  message: "Select which LLM providers do you want to use:",
102
- choices: getAiProvidersOptions(false),
92
+ choices: simulatorService.getAiProvidersOptions(false),
103
93
  validate: function (answer: string[]) {
104
94
  if (answer.length < 1) {
105
95
  return "You must choose at least one option.";
@@ -113,7 +103,10 @@ export async function startAction(options: StartActionOptions) {
113
103
  const llmProvidersAnswer = await inquirer.prompt(questions);
114
104
 
115
105
  // create random validators
116
- await createRandomValidators(Number(options.numValidators), llmProvidersAnswer.selectedLlmProviders);
106
+ await simulatorService.createRandomValidators(
107
+ Number(options.numValidators),
108
+ llmProvidersAnswer.selectedLlmProviders,
109
+ );
117
110
  } catch (error) {
118
111
  console.error("Unable to initialize the validators.");
119
112
  console.error(error);
@@ -124,10 +117,10 @@ export async function startAction(options: StartActionOptions) {
124
117
 
125
118
  // Simulator ready
126
119
  console.log(
127
- `GenLayer simulator initialized successfully! Go to ${getFrontendUrl()} in your browser to access it.`,
120
+ `GenLayer simulator initialized successfully! Go to ${simulatorService.getFrontendUrl()} in your browser to access it.`,
128
121
  );
129
122
  try {
130
- openFrontend();
123
+ simulatorService.openFrontend();
131
124
  } catch (error) {
132
125
  console.error(error);
133
126
  }
@@ -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,38 @@
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
+ clearAccountsAndTransactionsDatabase(): Promise<any>;
15
+ initializeDatabase(): Promise<InitializeDatabaseResultType>;
16
+ createRandomValidators(numValidators: number, llmProviders: AiProviders[]): Promise<any>;
17
+ deleteAllValidators(): Promise<any>;
18
+ getAiProvidersOptions(withHint: boolean): Array<{name: string; value: string}>;
19
+ getFrontendUrl(): string;
20
+ openFrontend(): Promise<boolean>;
21
+ resetDockerContainers(): Promise<boolean>;
22
+ resetDockerImages(): Promise<boolean>;
23
+ }
24
+
25
+ export type DownloadSimulatorResultType = {
26
+ wasInstalled: boolean;
27
+ };
28
+
29
+ export type WaitForSimulatorToBeReadyResultType = {
30
+ initialized: boolean;
31
+ errorCode?: "TIMEOUT" | "ERROR";
32
+ errorMessage?: string;
33
+ };
34
+
35
+ export type InitializeDatabaseResultType = {
36
+ createResponse: any;
37
+ tablesResponse: any;
38
+ };