genlayer 0.10.1 → 0.10.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,274 @@
1
+ import inquirer from "inquirer";
2
+ import { rpcClient } from "../../lib/clients/jsonRpcClient";
3
+ import { BaseAction } from "../../lib/actions/BaseAction";
4
+
5
+ export interface ValidatorOptions {
6
+ address?: string;
7
+ }
8
+
9
+ export interface UpdateValidatorOptions {
10
+ address: string;
11
+ stake?: string;
12
+ provider?: string;
13
+ model?: string;
14
+ config?: string;
15
+ }
16
+
17
+ export interface CreateRandomValidatorsOptions {
18
+ count: string;
19
+ providers: string[];
20
+ models: string[];
21
+ }
22
+
23
+ export interface CreateValidatorOptions {
24
+ stake: string;
25
+ config?: string;
26
+ model?: string;
27
+ provider?: string;
28
+ }
29
+
30
+ export class ValidatorsAction extends BaseAction {
31
+ public async getValidator(options: ValidatorOptions): Promise<void> {
32
+ try {
33
+ if (options.address) {
34
+ console.log(`Fetching validator with address: ${options.address}`);
35
+
36
+ const result = await rpcClient.request({
37
+ method: "sim_getValidator",
38
+ params: [options.address],
39
+ });
40
+
41
+ console.log("Validator Details:", result.result);
42
+ } else {
43
+ console.log("Fetching all validators...");
44
+
45
+ const result = await rpcClient.request({
46
+ method: "sim_getAllValidators",
47
+ params: [],
48
+ });
49
+
50
+ console.log("All Validators:", result.result);
51
+ }
52
+ } catch (error) {
53
+ console.error("Error fetching validators:", error);
54
+ }
55
+ }
56
+
57
+ public async deleteValidator(options: ValidatorOptions): Promise<void> {
58
+ try {
59
+ if (options.address) {
60
+ await this.confirmPrompt(`This command will delete the validator with the address: ${options.address}. Do you want to continue?`);
61
+ console.log(`Deleting validator with address: ${options.address}`);
62
+
63
+ const result = await rpcClient.request({
64
+ method: "sim_deleteValidator",
65
+ params: [options.address],
66
+ });
67
+
68
+ console.log("Deleted Address:", result.result);
69
+ } else {
70
+ await this.confirmPrompt(`This command will delete all validators. Do you want to continue?`);
71
+ console.log("Deleting all validators...");
72
+
73
+ await rpcClient.request({
74
+ method: "sim_deleteAllValidators",
75
+ params: [],
76
+ });
77
+
78
+ console.log("Successfully deleted all validators");
79
+ }
80
+ } catch (error) {
81
+ console.error("Error deleting validators:", error);
82
+ }
83
+ }
84
+
85
+ public async countValidators(): Promise<void> {
86
+ try {
87
+ console.log("Counting all validators...");
88
+
89
+ const result = await rpcClient.request({
90
+ method: "sim_countValidators",
91
+ params: [],
92
+ });
93
+
94
+ console.log("Total Validators:", result.result);
95
+ } catch (error) {
96
+ console.error("Error counting validators:", error);
97
+ }
98
+ }
99
+
100
+ public async updateValidator(options: UpdateValidatorOptions): Promise<void> {
101
+ try {
102
+ console.log(`Fetching validator with address: ${options.address}...`);
103
+ const currentValidator = await rpcClient.request({
104
+ method: "sim_getValidator",
105
+ params: [options.address],
106
+ });
107
+
108
+ if (!currentValidator.result) {
109
+ throw new Error(`Validator with address ${options.address} not found.`);
110
+ }
111
+
112
+ console.log("Current Validator Details:", currentValidator.result);
113
+
114
+ const parsedStake = options.stake
115
+ ? parseInt(options.stake, 10)
116
+ : currentValidator.result.stake;
117
+
118
+ if (isNaN(parsedStake) || parsedStake < 0) {
119
+ return console.error("Invalid stake value. Stake must be a positive integer.");
120
+ }
121
+
122
+ const updatedValidator = {
123
+ address: options.address,
124
+ stake: options.stake || currentValidator.result.stake,
125
+ provider: options.provider || currentValidator.result.provider,
126
+ model: options.model || currentValidator.result.model,
127
+ config: options.config ? JSON.parse(options.config) : currentValidator.result.config,
128
+ };
129
+
130
+ console.log("Updated Validator Details:", updatedValidator);
131
+
132
+ const result = await rpcClient.request({
133
+ method: "sim_updateValidator",
134
+ params: [
135
+ updatedValidator.address,
136
+ updatedValidator.stake,
137
+ updatedValidator.provider,
138
+ updatedValidator.model,
139
+ updatedValidator.config,
140
+ ],
141
+ });
142
+
143
+ console.log("Validator successfully updated:", result.result);
144
+ } catch (error) {
145
+ console.error("Error updating validator:", error);
146
+ }
147
+ }
148
+
149
+ public async createRandomValidators(options: CreateRandomValidatorsOptions): Promise<void> {
150
+ try {
151
+ const count = parseInt(options.count, 10);
152
+ if (isNaN(count) || count < 1) {
153
+ return console.error("Invalid count. Please provide a positive integer.");
154
+ }
155
+
156
+ console.log(`Creating ${count} random validator(s)...`);
157
+ console.log(`Providers: ${options.providers.length > 0 ? options.providers.join(", ") : "None"}`);
158
+ console.log(`Models: ${options.models.length > 0 ? options.models.join(", ") : "None"}`);
159
+
160
+ const result = await rpcClient.request({
161
+ method: "sim_createRandomValidators",
162
+ params: [count, 1, 10, options.providers, options.models],
163
+ });
164
+
165
+ console.log("Random validators successfully created:", result.result);
166
+ } catch (error) {
167
+ console.error("Error creating random validators:", error);
168
+ }
169
+ }
170
+
171
+ public async createValidator(options: CreateValidatorOptions): Promise<void> {
172
+ try {
173
+ const stake = parseInt(options.stake, 10);
174
+ if (isNaN(stake) || stake < 1) {
175
+ return console.error("Invalid stake. Please provide a positive integer.");
176
+ }
177
+
178
+ if (options.model && !options.provider) {
179
+ return console.error("You must specify a provider if using a model.");
180
+ }
181
+
182
+ console.log("Fetching available providers and models...");
183
+
184
+ const providersAndModels = await rpcClient.request({
185
+ method: "sim_getProvidersAndModels",
186
+ params: [],
187
+ });
188
+
189
+ if (!providersAndModels.result || providersAndModels.result.length === 0) {
190
+ return console.error("No providers or models available.");
191
+ }
192
+
193
+ const availableProviders = [
194
+ ...new Map(
195
+ providersAndModels.result
196
+ .filter((entry: any) => entry.is_available)
197
+ .map((entry: any) => [entry.provider, entry])
198
+ ).values(),
199
+ ];
200
+
201
+ let provider = options.provider
202
+
203
+ if(!provider){
204
+ const { selectedProvider } = await inquirer.prompt([
205
+ {
206
+ type: "list",
207
+ name: "selectedProvider",
208
+ message: "Select a provider:",
209
+ choices: availableProviders.map((entry: any) => entry.provider),
210
+ },
211
+ ]);
212
+
213
+ provider = selectedProvider;
214
+ }
215
+
216
+ const availableModels = providersAndModels.result.filter(
217
+ (entry: any) => entry.provider === provider && entry.is_model_available
218
+ );
219
+
220
+ if (availableModels.length === 0) {
221
+ return console.error("No models available for the selected provider.");
222
+ }
223
+
224
+ let model = options.model;
225
+
226
+ if(!model){
227
+ const { selectedModel } = await inquirer.prompt([
228
+ {
229
+ type: "list",
230
+ name: "selectedModel",
231
+ message: "Select a model:",
232
+ choices: availableModels.map((entry: any) => entry.model),
233
+ },
234
+ ]);
235
+
236
+ model = selectedModel;
237
+ }
238
+
239
+ const modelDetails = availableModels.find(
240
+ (entry: any) => entry.model === model
241
+ );
242
+
243
+ if (!modelDetails) {
244
+ return console.error("Selected model details not found.");
245
+ }
246
+
247
+ const config = options.config ? JSON.parse(options.config) : modelDetails.config;
248
+
249
+ console.log("Creating validator with the following details:");
250
+ console.log(`Stake: ${stake}`);
251
+ console.log(`Provider: ${modelDetails.provider}`);
252
+ console.log(`Model: ${modelDetails.model}`);
253
+ console.log(`Config:`, config);
254
+ console.log(`Plugin:`, modelDetails.plugin);
255
+ console.log(`Plugin Config:`, modelDetails.plugin_config);
256
+
257
+ const result = await rpcClient.request({
258
+ method: "sim_createValidator",
259
+ params: [
260
+ stake,
261
+ modelDetails.provider,
262
+ modelDetails.model,
263
+ config,
264
+ modelDetails.plugin,
265
+ modelDetails.plugin_config,
266
+ ],
267
+ });
268
+
269
+ console.log("Validator successfully created:", result.result);
270
+ } catch (error) {
271
+ console.error("Error creating validator:", error);
272
+ }
273
+ }
274
+ }
package/src/index.ts CHANGED
@@ -6,6 +6,7 @@ import { initializeGeneralCommands } from "../src/commands/general";
6
6
  import { initializeKeygenCommands } from "../src/commands/keygen";
7
7
  import { initializeContractsCommands } from "../src/commands/contracts";
8
8
  import { initializeConfigCommands } from "../src/commands/config";
9
+ import {initializeValidatorCommands} from "../src/commands/validators";
9
10
  import { initializeUpdateCommands } from "../src/commands/update";
10
11
 
11
12
  export function initializeCLI() {
@@ -15,6 +16,7 @@ export function initializeCLI() {
15
16
  initializeContractsCommands(program);
16
17
  initializeConfigCommands(program);
17
18
  initializeUpdateCommands(program)
19
+ initializeValidatorCommands(program);
18
20
  program.parse(process.argv);
19
21
  }
20
22
 
@@ -0,0 +1,19 @@
1
+ import inquirer from "inquirer";
2
+
3
+ export class BaseAction {
4
+ protected async confirmPrompt(message: string): Promise<void> {
5
+ const answer = await inquirer.prompt([
6
+ {
7
+ type: "confirm",
8
+ name: "confirmAction",
9
+ message: message,
10
+ default: true,
11
+ },
12
+ ]);
13
+
14
+ if (!answer.confirmAction) {
15
+ console.log("Operation aborted!");
16
+ process.exit(0);
17
+ }
18
+ }
19
+ }
@@ -16,27 +16,26 @@ export class JsonRpcClient {
16
16
  }
17
17
 
18
18
  async request({method, params}: JsonRPCParams): Promise<any | null> {
19
- try {
20
- const response = await fetch(this.serverUrl, {
21
- method: "POST",
22
- headers: {
23
- "Content-Type": "application/json",
24
- },
25
- body: JSON.stringify({
26
- jsonrpc: "2.0",
27
- id: uuidv4(),
28
- method,
29
- params,
30
- }),
31
- });
32
-
33
- if (response.ok) {
34
- return response.json();
35
- }
36
- } catch (error: any) {
37
- throw new Error(`Fetch Error: ${error.message}`);
19
+ const response = await fetch(this.serverUrl, {
20
+ method: "POST",
21
+ headers: {
22
+ "Content-Type": "application/json",
23
+ },
24
+ body: JSON.stringify({
25
+ jsonrpc: "2.0",
26
+ id: uuidv4(),
27
+ method,
28
+ params,
29
+ }),
30
+ });
31
+
32
+ if (response.ok) {
33
+ return response.json();
38
34
  }
39
- return null;
35
+ const result = await response.json();
36
+
37
+ throw new Error(result?.error?.message || response.statusText);
38
+
40
39
  }
41
40
  }
42
41
  export const rpcClient = new JsonRpcClient(DEFAULT_JSON_RPC_URL);
@@ -1,4 +1,4 @@
1
- export const localnetCompatibleVersion = "v0.34.3";
1
+ export const localnetCompatibleVersion = "v0.35.2";
2
2
  export const DEFAULT_JSON_RPC_URL = "http://localhost:4000/api";
3
3
  export const CONTAINERS_NAME_PREFIX = "/genlayer-";
4
4
  export const IMAGES_NAME_PREFIX = "yeagerai";
@@ -190,13 +190,13 @@ export class SimulatorService implements ISimulatorService {
190
190
 
191
191
  public createRandomValidators(numValidators: number, llmProviders: AiProviders[]): Promise<any> {
192
192
  return rpcClient.request({
193
- method: "create_random_validators",
193
+ method: "sim_createRandomValidators",
194
194
  params: [numValidators, 1, 10, llmProviders],
195
195
  });
196
196
  }
197
197
 
198
198
  public deleteAllValidators(): Promise<any> {
199
- return rpcClient.request({method: "delete_all_validators", params: []});
199
+ return rpcClient.request({method: "sim_deleteAllValidators", params: []});
200
200
  }
201
201
 
202
202
  public getAiProvidersOptions(withHint: boolean = true): Array<{name: string; value: string}> {