genlayer 0.33.1 → 0.34.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.
Files changed (128) hide show
  1. package/dist/index.js +68 -31
  2. package/package.json +11 -2
  3. package/.eslintignore +0 -2
  4. package/.github/workflows/cli-docs.yml +0 -121
  5. package/.github/workflows/publish-beta.yml +0 -41
  6. package/.github/workflows/publish.yml +0 -43
  7. package/.github/workflows/validate-code.yml +0 -47
  8. package/.prettierignore +0 -19
  9. package/.prettierrc +0 -12
  10. package/.release-it.json +0 -64
  11. package/CHANGELOG.md +0 -419
  12. package/CLAUDE.md +0 -55
  13. package/CONTRIBUTING.md +0 -117
  14. package/docker-compose.yml +0 -154
  15. package/docs/delegator-guide.md +0 -203
  16. package/docs/validator-guide.md +0 -329
  17. package/esbuild.config.dev.js +0 -17
  18. package/esbuild.config.js +0 -22
  19. package/esbuild.config.prod.js +0 -17
  20. package/eslint.config.js +0 -60
  21. package/renovate.json +0 -22
  22. package/src/commands/account/create.ts +0 -29
  23. package/src/commands/account/export.ts +0 -106
  24. package/src/commands/account/import.ts +0 -135
  25. package/src/commands/account/index.ts +0 -126
  26. package/src/commands/account/list.ts +0 -34
  27. package/src/commands/account/lock.ts +0 -39
  28. package/src/commands/account/remove.ts +0 -30
  29. package/src/commands/account/send.ts +0 -156
  30. package/src/commands/account/show.ts +0 -74
  31. package/src/commands/account/unlock.ts +0 -51
  32. package/src/commands/account/use.ts +0 -21
  33. package/src/commands/config/getSetReset.ts +0 -51
  34. package/src/commands/config/index.ts +0 -30
  35. package/src/commands/contracts/call.ts +0 -39
  36. package/src/commands/contracts/code.ts +0 -33
  37. package/src/commands/contracts/deploy.ts +0 -157
  38. package/src/commands/contracts/index.ts +0 -86
  39. package/src/commands/contracts/schema.ts +0 -31
  40. package/src/commands/contracts/write.ts +0 -49
  41. package/src/commands/general/index.ts +0 -45
  42. package/src/commands/general/init.ts +0 -179
  43. package/src/commands/general/start.ts +0 -116
  44. package/src/commands/general/stop.ts +0 -26
  45. package/src/commands/localnet/index.ts +0 -100
  46. package/src/commands/localnet/validators.ts +0 -269
  47. package/src/commands/network/index.ts +0 -29
  48. package/src/commands/network/setNetwork.ts +0 -77
  49. package/src/commands/scaffold/index.ts +0 -16
  50. package/src/commands/scaffold/new.ts +0 -34
  51. package/src/commands/staking/StakingAction.ts +0 -267
  52. package/src/commands/staking/delegatorClaim.ts +0 -41
  53. package/src/commands/staking/delegatorExit.ts +0 -56
  54. package/src/commands/staking/delegatorJoin.ts +0 -44
  55. package/src/commands/staking/index.ts +0 -346
  56. package/src/commands/staking/setIdentity.ts +0 -78
  57. package/src/commands/staking/setOperator.ts +0 -46
  58. package/src/commands/staking/stakingInfo.ts +0 -584
  59. package/src/commands/staking/validatorClaim.ts +0 -43
  60. package/src/commands/staking/validatorDeposit.ts +0 -48
  61. package/src/commands/staking/validatorExit.ts +0 -63
  62. package/src/commands/staking/validatorHistory.ts +0 -298
  63. package/src/commands/staking/validatorJoin.ts +0 -47
  64. package/src/commands/staking/validatorPrime.ts +0 -73
  65. package/src/commands/staking/wizard.ts +0 -809
  66. package/src/commands/transactions/appeal.ts +0 -39
  67. package/src/commands/transactions/index.ts +0 -39
  68. package/src/commands/transactions/receipt.ts +0 -90
  69. package/src/commands/update/index.ts +0 -25
  70. package/src/commands/update/ollama.ts +0 -103
  71. package/src/lib/actions/BaseAction.ts +0 -295
  72. package/src/lib/clients/jsonRpcClient.ts +0 -41
  73. package/src/lib/clients/system.ts +0 -73
  74. package/src/lib/config/ConfigFileManager.ts +0 -194
  75. package/src/lib/config/KeychainManager.ts +0 -89
  76. package/src/lib/config/simulator.ts +0 -68
  77. package/src/lib/config/text.ts +0 -2
  78. package/src/lib/errors/missingRequirement.ts +0 -9
  79. package/src/lib/errors/versionRequired.ts +0 -9
  80. package/src/lib/interfaces/ISimulatorService.ts +0 -37
  81. package/src/lib/services/simulator.ts +0 -351
  82. package/src/types/node-fetch.d.ts +0 -1
  83. package/tests/actions/appeal.test.ts +0 -99
  84. package/tests/actions/call.test.ts +0 -94
  85. package/tests/actions/code.test.ts +0 -87
  86. package/tests/actions/create.test.ts +0 -65
  87. package/tests/actions/deploy.test.ts +0 -420
  88. package/tests/actions/getSetReset.test.ts +0 -88
  89. package/tests/actions/init.test.ts +0 -467
  90. package/tests/actions/lock.test.ts +0 -86
  91. package/tests/actions/new.test.ts +0 -80
  92. package/tests/actions/ollama.test.ts +0 -193
  93. package/tests/actions/receipt.test.ts +0 -261
  94. package/tests/actions/schema.test.ts +0 -94
  95. package/tests/actions/setNetwork.test.ts +0 -160
  96. package/tests/actions/staking.test.ts +0 -279
  97. package/tests/actions/start.test.ts +0 -235
  98. package/tests/actions/stop.test.ts +0 -77
  99. package/tests/actions/unlock.test.ts +0 -139
  100. package/tests/actions/validators.test.ts +0 -750
  101. package/tests/actions/write.test.ts +0 -102
  102. package/tests/commands/account.test.ts +0 -146
  103. package/tests/commands/appeal.test.ts +0 -58
  104. package/tests/commands/call.test.ts +0 -78
  105. package/tests/commands/code.test.ts +0 -69
  106. package/tests/commands/config.test.ts +0 -54
  107. package/tests/commands/deploy.test.ts +0 -83
  108. package/tests/commands/init.test.ts +0 -101
  109. package/tests/commands/localnet.test.ts +0 -131
  110. package/tests/commands/network.test.ts +0 -60
  111. package/tests/commands/new.test.ts +0 -68
  112. package/tests/commands/receipt.test.ts +0 -142
  113. package/tests/commands/schema.test.ts +0 -67
  114. package/tests/commands/staking.test.ts +0 -329
  115. package/tests/commands/stop.test.ts +0 -27
  116. package/tests/commands/up.test.ts +0 -105
  117. package/tests/commands/update.test.ts +0 -45
  118. package/tests/commands/write.test.ts +0 -76
  119. package/tests/index.test.ts +0 -56
  120. package/tests/libs/baseAction.test.ts +0 -516
  121. package/tests/libs/configFileManager.test.ts +0 -117
  122. package/tests/libs/jsonRpcClient.test.ts +0 -59
  123. package/tests/libs/keychainManager.test.ts +0 -156
  124. package/tests/libs/system.test.ts +0 -148
  125. package/tests/services/simulator.test.ts +0 -705
  126. package/tests/utils.ts +0 -13
  127. package/tsconfig.json +0 -120
  128. package/vitest.config.ts +0 -12
@@ -1,351 +0,0 @@
1
- import Docker, {ContainerInfo} from "dockerode";
2
- import * as fs from "fs";
3
- import * as dotenv from "dotenv";
4
- import * as path from "path";
5
- import * as semver from "semver";
6
- import updateCheck from "update-check";
7
- import { fileURLToPath } from "url";
8
- import pkg from '../../../package.json'
9
-
10
- import {rpcClient} from "../clients/jsonRpcClient";
11
- import {
12
- DEFAULT_RUN_SIMULATOR_COMMAND,
13
- DEFAULT_RUN_DOCKER_COMMAND,
14
- STARTING_TIMEOUT_WAIT_CYLCE,
15
- STARTING_TIMEOUT_ATTEMPTS,
16
- AI_PROVIDERS_CONFIG,
17
- AiProviders,
18
- VERSION_REQUIREMENTS,
19
- CONTAINERS_NAME_PREFIX,
20
- IMAGES_NAME_PREFIX,
21
- GENLAYER_REQUIRED_CONTAINERS
22
- } from "../config/simulator";
23
- import {
24
- checkCommand,
25
- getVersion,
26
- executeCommand,
27
- openUrl,
28
- } from "../clients/system";
29
- import {MissingRequirementError} from "../errors/missingRequirement";
30
-
31
- import {
32
- ISimulatorService,
33
- WaitForSimulatorToBeReadyResultType,
34
- } from "../interfaces/ISimulatorService";
35
- import {VersionRequiredError} from "../errors/versionRequired";
36
-
37
-
38
- function sleep(millliseconds: number): Promise<void> {
39
- return new Promise(resolve => setTimeout(resolve, millliseconds));
40
- }
41
-
42
- export class SimulatorService implements ISimulatorService {
43
- private profileOptions: string
44
- private docker: Docker;
45
- public location: string;
46
-
47
- constructor() {
48
- const __filename = fileURLToPath(import.meta.url);
49
- this.location = path.resolve(path.dirname(__filename), '..');
50
- this.profileOptions = "";
51
- this.docker = new Docker();
52
- }
53
-
54
- private readEnvConfigValue(key: string): string {
55
- const envFilePath = path.join(this.location, ".env");
56
- // Transform the config string to object
57
- const envConfig = dotenv.parse(fs.readFileSync(envFilePath, "utf8"));
58
- return envConfig[key];
59
- }
60
-
61
- private async getGenlayerContainers(): Promise<ContainerInfo[]> {
62
- const containers = await this.docker.listContainers({ all: true });
63
- return containers.filter(container =>
64
- container.Names.some(name =>
65
- name.startsWith(CONTAINERS_NAME_PREFIX)
66
- )
67
- );
68
- }
69
-
70
- private async stopAndRemoveContainers(remove: boolean = false): Promise<void> {
71
- const genlayerContainers = await this.getGenlayerContainers();
72
-
73
- for (const containerInfo of genlayerContainers) {
74
- const container = this.docker.getContainer(containerInfo.Id);
75
- if (containerInfo.State === "running") {
76
- await container.stop();
77
- }
78
-
79
- const isOllamaContainer = containerInfo.Names.some(name => name.includes("ollama"));
80
-
81
- if (remove && !isOllamaContainer) {
82
- await container.remove();
83
- }
84
- }
85
- }
86
-
87
- public addConfigToEnvFile(newConfig: Record<string, string>): void {
88
- const envFilePath = path.join(this.location, ".env");
89
-
90
- // Transform the config string to object
91
- const envConfig = dotenv.parse(fs.readFileSync(envFilePath, "utf8"));
92
- Object.keys(newConfig).forEach(key => {
93
- envConfig[key] = newConfig[key];
94
- });
95
-
96
- // Transform the updated config object back into a string
97
- const updatedConfig = Object.keys(envConfig)
98
- .map(key => {
99
- return `${key}=${envConfig[key]}`;
100
- })
101
- .join("\n");
102
-
103
- // Write the new .env file
104
- fs.writeFileSync(envFilePath, updatedConfig);
105
- }
106
-
107
- public setComposeOptions(headless: boolean, ollama: boolean = false): void {
108
- let profiles = [];
109
-
110
- if (!headless) {
111
- profiles.push("frontend");
112
- }
113
-
114
- if (ollama) {
115
- profiles.push("ollama");
116
- }
117
-
118
- this.profileOptions = profiles.length > 0 ? `--profile ${profiles.join(" --profile ")}` : "";
119
- }
120
-
121
- public getComposeOptions(): string {
122
- return this.profileOptions;
123
- }
124
-
125
- public async checkCliVersion(): Promise<void> {
126
- const update = await updateCheck(pkg);
127
- if (update && update.latest !== pkg.version) {
128
- console.warn(`\nA new version (${update.latest}) is available! You're using version ${pkg.version}.\nRun npm install -g genlayer to update\n`);
129
- }
130
- }
131
-
132
- public async checkInstallRequirements(): Promise<Record<string, boolean>> {
133
- const requirementsInstalled = {
134
- docker: false,
135
- };
136
-
137
- try {
138
- await checkCommand("docker --version", "docker");
139
- requirementsInstalled.docker = true;
140
- } catch (error: any) {
141
- if (!(error instanceof MissingRequirementError)) {
142
- throw error;
143
- }
144
- }
145
-
146
- if (requirementsInstalled.docker) {
147
- try {
148
- await this.docker.ping()
149
- } catch (error: any) {
150
- await executeCommand(DEFAULT_RUN_DOCKER_COMMAND);
151
- }
152
- }
153
-
154
- return requirementsInstalled;
155
- }
156
-
157
- public async checkVersionRequirements(): Promise<Record<string, string>> {
158
- const missingVersions = {
159
- docker: "",
160
- node: "",
161
- };
162
-
163
- try {
164
- await this.checkVersion(VERSION_REQUIREMENTS.node, "node");
165
- } catch (error: any) {
166
- missingVersions.node = VERSION_REQUIREMENTS.node;
167
- if (!(error instanceof VersionRequiredError)) {
168
- throw error;
169
- }
170
- }
171
-
172
- try {
173
- await this.checkVersion(VERSION_REQUIREMENTS.docker, "docker");
174
- } catch (error: any) {
175
- missingVersions.docker = VERSION_REQUIREMENTS.docker;
176
- if (!(error instanceof VersionRequiredError)) {
177
- throw error;
178
- }
179
- }
180
-
181
- return missingVersions;
182
- }
183
-
184
- public async checkVersion(minVersion: string, toolName: string): Promise<void> {
185
- const version = await getVersion(toolName);
186
-
187
- if (!semver.satisfies(version, `>=${minVersion}`)) {
188
- throw new VersionRequiredError(toolName, minVersion);
189
- }
190
- }
191
-
192
- public runSimulator(): Promise<{stdout: string; stderr: string}> {
193
- const commandsByPlatform = DEFAULT_RUN_SIMULATOR_COMMAND(this.location, this.getComposeOptions());
194
- return executeCommand(commandsByPlatform);
195
- }
196
-
197
- public async waitForSimulatorToBeReady(
198
- retries: number = STARTING_TIMEOUT_ATTEMPTS,
199
- ): Promise<WaitForSimulatorToBeReadyResultType> {
200
- try {
201
- const response = await rpcClient.request({method: "ping", params: []});
202
-
203
- //Compatibility with current simulator version
204
- if (response?.result === "OK" || response?.result?.status === "OK" || response?.result?.data?.status === "OK") {
205
- return { initialized: true };
206
- }
207
- if (retries > 0) {
208
- await sleep(STARTING_TIMEOUT_WAIT_CYLCE);
209
- return this.waitForSimulatorToBeReady(retries - 1);
210
- }
211
- } catch (error: any) {
212
- if (
213
- (error.name === "FetchError" ||
214
- error.message.includes("Fetch Error") ||
215
- error.message.includes("ECONNRESET") ||
216
- error.message.includes("ECONNREFUSED") ||
217
- error.message.includes("socket hang up")) &&
218
- retries > 0
219
- ) {
220
- await sleep(STARTING_TIMEOUT_WAIT_CYLCE * 2);
221
- return this.waitForSimulatorToBeReady(retries - 1);
222
- }
223
- return {initialized: false, errorCode: "ERROR", errorMessage: error.message};
224
- }
225
-
226
- return {initialized: false, errorCode: "TIMEOUT"};
227
- }
228
-
229
- public createRandomValidators(numValidators: number, llmProviders: AiProviders[]): Promise<any> {
230
- return rpcClient.request({
231
- method: "sim_createRandomValidators",
232
- params: [numValidators, 1, 10, llmProviders],
233
- });
234
- }
235
-
236
- public deleteAllValidators(): Promise<any> {
237
- return rpcClient.request({method: "sim_deleteAllValidators", params: []});
238
- }
239
-
240
- public getAiProvidersOptions(withHint: boolean = true, excludeProviders: AiProviders[] = []): Array<{name: string; value: string}> {
241
- return Object.values(AI_PROVIDERS_CONFIG)
242
- .filter(providerConfig => !excludeProviders.includes(providerConfig.cliOptionValue as AiProviders))
243
- .map(providerConfig => {
244
- return {
245
- name: `${providerConfig.name}${withHint ? ` ${providerConfig.hint}` : ""}`,
246
- value: providerConfig.cliOptionValue,
247
- };
248
- });
249
- }
250
-
251
- public getFrontendUrl(): string {
252
- const frontendPort = this.readEnvConfigValue("FRONTEND_PORT");
253
- return `http://localhost:${frontendPort}`;
254
- }
255
-
256
- public async openFrontend(): Promise<boolean> {
257
- await openUrl(this.getFrontendUrl());
258
- return true;
259
- }
260
-
261
- public async stopDockerContainers(): Promise<void> {
262
- await this.stopAndRemoveContainers(false);
263
- }
264
-
265
- public async resetDockerContainers(): Promise<void> {
266
- await this.stopAndRemoveContainers(true);
267
- }
268
-
269
- public async resetDockerImages(): Promise<void> {
270
- const images = await this.docker.listImages();
271
- const genlayerImages = images.filter(image =>
272
- image.RepoTags?.some(tag => tag.startsWith(IMAGES_NAME_PREFIX))
273
- );
274
-
275
- for (const imageInfo of genlayerImages) {
276
- const image = this.docker.getImage(imageInfo.Id);
277
- await image.remove({force: true});
278
- }
279
- }
280
-
281
- public async resetDockerVolumes(): Promise<void> {
282
- const volumes = await this.docker.listVolumes();
283
- const genlayerVolumes = volumes.Volumes?.filter(volume =>
284
- volume.Name.startsWith('genlayer_')
285
- ) || [];
286
-
287
- for (const volumeInfo of genlayerVolumes) {
288
- const volume = this.docker.getVolume(volumeInfo.Name);
289
- await volume.remove({force: true});
290
- }
291
- }
292
-
293
- public async cleanDatabase(): Promise<boolean> {
294
-
295
- try {
296
- await rpcClient.request({method: "sim_clearDbTables", params: [['current_state', 'transactions']]});
297
- }catch (error) {
298
- console.error(error);
299
- }
300
- return true;
301
- }
302
-
303
- public normalizeLocalnetVersion(version: string) {
304
-
305
- if (!version.startsWith('v')) {
306
- version = 'v' + version;
307
- }
308
-
309
- const versionRegex = /^v(\d+)\.(\d+)\.(\d+)(-.+)?$/;
310
- const match = version.match(versionRegex);
311
-
312
- if (!match) {
313
- console.error('Invalid version format. Expected format: v0.0.0 or v0.0.0-suffix');
314
- process.exit(1);
315
- }
316
-
317
- return version
318
- }
319
-
320
- public compareVersions(version1: string, version2: string): number {
321
- const v1 = version1.replace(/^v/, '');
322
- const v2 = version2.replace(/^v/, '');
323
-
324
- const parts1 = v1.split('-')[0].split('.').map(Number);
325
- const parts2 = v2.split('-')[0].split('.').map(Number);
326
-
327
- for (let i = 0; i < Math.max(parts1.length, parts2.length); i++) {
328
- const part1 = parts1[i] || 0;
329
- const part2 = parts2[i] || 0;
330
-
331
- if (part1 < part2) return -1;
332
- if (part1 > part2) return 1;
333
- }
334
-
335
- return 0;
336
- }
337
-
338
- public async isLocalnetRunning(): Promise<boolean> {
339
- const genlayerContainers = await this.getGenlayerContainers();
340
- const runningContainers = genlayerContainers.filter(container => container.State === "running");
341
-
342
- return GENLAYER_REQUIRED_CONTAINERS.every(requiredContainer =>
343
- runningContainers.some(container =>
344
- container.Names.some(name => name.includes(requiredContainer))
345
- )
346
- );
347
- }
348
-
349
- }
350
-
351
- export default new SimulatorService();
@@ -1 +0,0 @@
1
- declare module "node-fetch";
@@ -1,99 +0,0 @@
1
- import {describe, test, vi, beforeEach, afterEach, expect} from "vitest";
2
- import {createClient, createAccount} from "genlayer-js";
3
- import type {TransactionHash} from "genlayer-js/types";
4
- import {AppealAction} from "../../src/commands/transactions/appeal";
5
-
6
- vi.mock("genlayer-js");
7
-
8
- describe("AppealAction", () => {
9
- let appealAction: AppealAction;
10
- const mockClient = {
11
- appealTransaction: vi.fn(),
12
- waitForTransactionReceipt: vi.fn(),
13
- initializeConsensusSmartContract: vi.fn(),
14
- };
15
-
16
- const mockPrivateKey = "mocked_private_key";
17
- const mockTxId = "0x1234567890123456789012345678901234567890123456789012345678901234" as TransactionHash;
18
-
19
- beforeEach(() => {
20
- vi.clearAllMocks();
21
- vi.mocked(createClient).mockReturnValue(mockClient as any);
22
- vi.mocked(createAccount).mockReturnValue({privateKey: mockPrivateKey} as any);
23
- appealAction = new AppealAction();
24
- vi.spyOn(appealAction as any, "getAccount").mockResolvedValue({privateKey: mockPrivateKey});
25
-
26
- vi.spyOn(appealAction as any, "startSpinner").mockImplementation(() => {});
27
- vi.spyOn(appealAction as any, "succeedSpinner").mockImplementation(() => {});
28
- vi.spyOn(appealAction as any, "failSpinner").mockImplementation(() => {});
29
- });
30
-
31
- afterEach(() => {
32
- vi.restoreAllMocks();
33
- });
34
-
35
- test("calls appealTransaction successfully", async () => {
36
- const mockReceipt = {status: "success"};
37
-
38
- vi.mocked(mockClient.waitForTransactionReceipt).mockResolvedValue(mockReceipt);
39
-
40
- await appealAction.appeal({
41
- txId: mockTxId,
42
- });
43
-
44
- expect(mockClient.appealTransaction).toHaveBeenCalledWith({
45
- txId: mockTxId,
46
- });
47
- expect(appealAction["succeedSpinner"]).toHaveBeenCalledWith(
48
- "Appeal operation successfully executed",
49
- mockReceipt,
50
- );
51
- });
52
-
53
- test("handles appealTransaction errors", async () => {
54
- vi.mocked(mockClient.appealTransaction).mockRejectedValue(new Error("Mocked appeal error"));
55
-
56
- await appealAction.appeal({txId: mockTxId});
57
-
58
- expect(appealAction["failSpinner"]).toHaveBeenCalledWith(
59
- "Error during appeal operation",
60
- expect.any(Error),
61
- );
62
- });
63
-
64
- test("uses custom RPC URL for appeal operations", async () => {
65
- const rpcUrl = "https://custom-rpc-url.com";
66
- const mockReceipt = {status: "success"};
67
-
68
- vi.mocked(mockClient.waitForTransactionReceipt).mockResolvedValue(mockReceipt);
69
-
70
- await appealAction.appeal({
71
- txId: mockTxId,
72
- rpc: rpcUrl,
73
- });
74
-
75
- expect(createClient).toHaveBeenCalledWith(
76
- expect.objectContaining({
77
- endpoint: rpcUrl,
78
- }),
79
- );
80
- expect(mockClient.appealTransaction).toHaveBeenCalledWith({
81
- txId: mockTxId,
82
- });
83
- expect(appealAction["succeedSpinner"]).toHaveBeenCalledWith(
84
- "Appeal operation successfully executed",
85
- mockReceipt,
86
- );
87
- });
88
-
89
- test("initializes consensus smart contract before appeal", async () => {
90
- const mockReceipt = {status: "success"};
91
-
92
- vi.mocked(mockClient.waitForTransactionReceipt).mockResolvedValue(mockReceipt);
93
-
94
- await appealAction.appeal({txId: mockTxId});
95
-
96
- expect(mockClient.initializeConsensusSmartContract).toHaveBeenCalledTimes(1);
97
- expect(mockClient.appealTransaction).toHaveBeenCalled();
98
- });
99
- });
@@ -1,94 +0,0 @@
1
- import {describe, test, vi, beforeEach, afterEach, expect} from "vitest";
2
- import {createClient, createAccount} from "genlayer-js";
3
- import {CallAction} from "../../src/commands/contracts/call";
4
-
5
- vi.mock("genlayer-js");
6
-
7
- describe("CallAction", () => {
8
- let callActions: CallAction;
9
- const mockClient = {
10
- readContract: vi.fn(),
11
- writeContract: vi.fn(),
12
- waitForTransactionReceipt: vi.fn(),
13
- getContractSchema: vi.fn(),
14
- initializeConsensusSmartContract: vi.fn(),
15
- };
16
-
17
- const mockPrivateKey = "mocked_private_key";
18
-
19
- beforeEach(() => {
20
- vi.clearAllMocks();
21
- vi.mocked(createClient).mockReturnValue(mockClient as any);
22
- vi.mocked(createAccount).mockReturnValue({privateKey: mockPrivateKey} as any);
23
- callActions = new CallAction();
24
- vi.spyOn(callActions as any, "getAccount").mockResolvedValue({privateKey: mockPrivateKey});
25
-
26
- vi.spyOn(callActions as any, "startSpinner").mockImplementation(() => {});
27
- vi.spyOn(callActions as any, "succeedSpinner").mockImplementation(() => {});
28
- vi.spyOn(callActions as any, "failSpinner").mockImplementation(() => {});
29
- vi.spyOn(callActions as any, "log").mockImplementation(() => {});
30
- });
31
-
32
- afterEach(() => {
33
- vi.restoreAllMocks();
34
- });
35
-
36
- test("calls readContract successfully", async () => {
37
- const options = {args: [1, 2, "Hello"]};
38
- const mockResult = "mocked_result";
39
-
40
- vi.mocked(mockClient.readContract).mockResolvedValue(mockResult);
41
-
42
- await callActions.call({
43
- contractAddress: "0xMockedContract",
44
- method: "getData",
45
- ...options,
46
- });
47
-
48
- expect(mockClient.readContract).toHaveBeenCalledWith({
49
- address: "0xMockedContract",
50
- functionName: "getData",
51
- args: [1, 2, "Hello"],
52
- });
53
- expect(callActions["succeedSpinner"]).toHaveBeenCalledWith(
54
- "Read operation successfully executed",
55
- "mocked_result",
56
- );
57
- });
58
-
59
- test("handles readContract errors", async () => {
60
- vi.mocked(mockClient.readContract).mockRejectedValue(new Error("Mocked read error"));
61
-
62
- await callActions.call({contractAddress: "0xMockedContract", method: "getData", args: [1]});
63
-
64
- expect(callActions["failSpinner"]).toHaveBeenCalledWith("Error during read operation", expect.any(Error));
65
- });
66
-
67
- test("uses custom RPC URL when provided", async () => {
68
- const options = {args: [1, 2, "Hello"], rpc: "https://custom-rpc-url.com"};
69
- const mockResult = "mocked_result";
70
-
71
- vi.mocked(mockClient.readContract).mockResolvedValue(mockResult);
72
-
73
- await callActions.call({
74
- contractAddress: "0xMockedContract",
75
- method: "getData",
76
- ...options,
77
- });
78
-
79
- expect(createClient).toHaveBeenCalledWith(
80
- expect.objectContaining({
81
- endpoint: "https://custom-rpc-url.com",
82
- }),
83
- );
84
- expect(mockClient.readContract).toHaveBeenCalledWith({
85
- address: "0xMockedContract",
86
- functionName: "getData",
87
- args: [1, 2, "Hello"],
88
- });
89
- expect(callActions["succeedSpinner"]).toHaveBeenCalledWith(
90
- "Read operation successfully executed",
91
- "mocked_result",
92
- );
93
- });
94
- });
@@ -1,87 +0,0 @@
1
- import {describe, test, vi, beforeEach, afterEach, expect} from "vitest";
2
- import {createClient, createAccount} from "genlayer-js";
3
- import {CodeAction} from "../../src/commands/contracts/code";
4
-
5
- vi.mock("genlayer-js");
6
-
7
- describe("CodeAction", () => {
8
- let codeAction: CodeAction;
9
- const mockClient = {
10
- getContractCode: vi.fn(),
11
- initializeConsensusSmartContract: vi.fn(),
12
- };
13
-
14
- const mockPrivateKey = "mocked_private_key";
15
-
16
- beforeEach(() => {
17
- vi.clearAllMocks();
18
- vi.mocked(createClient).mockReturnValue(mockClient as any);
19
- vi.mocked(createAccount).mockReturnValue({privateKey: mockPrivateKey} as any);
20
- codeAction = new CodeAction();
21
- vi.spyOn(codeAction as any, "getAccount").mockResolvedValue({privateKey: mockPrivateKey});
22
-
23
- vi.spyOn(codeAction as any, "startSpinner").mockImplementation(() => {});
24
- vi.spyOn(codeAction as any, "succeedSpinner").mockImplementation(() => {});
25
- vi.spyOn(codeAction as any, "failSpinner").mockImplementation(() => {});
26
- vi.spyOn(codeAction as any, "log").mockImplementation(() => {});
27
- });
28
-
29
- afterEach(() => {
30
- vi.restoreAllMocks();
31
- });
32
-
33
- test("gets contract code successfully", async () => {
34
- const mockResult = "0x600160...";
35
-
36
- vi.mocked(mockClient.getContractCode).mockResolvedValue(mockResult as any);
37
-
38
- await codeAction.code({
39
- contractAddress: "0xMockedContract",
40
- });
41
-
42
- expect(mockClient.getContractCode).toHaveBeenCalledWith("0xMockedContract");
43
- expect(codeAction["succeedSpinner"]).toHaveBeenCalledWith(
44
- "Contract code retrieved successfully",
45
- mockResult,
46
- );
47
- });
48
-
49
- test("handles getContractCode errors", async () => {
50
- vi.mocked(mockClient.getContractCode).mockRejectedValue(new Error("Mocked code error"));
51
-
52
- await codeAction.code({contractAddress: "0xMockedContract"});
53
-
54
- expect(codeAction["failSpinner"]).toHaveBeenCalledWith("Error retrieving contract code", expect.any(Error));
55
- });
56
-
57
- test("uses custom RPC URL when provided", async () => {
58
- const mockResult = "0x600160...";
59
- vi.mocked(mockClient.getContractCode).mockResolvedValue(mockResult as any);
60
-
61
- await codeAction.code({
62
- contractAddress: "0xMockedContract",
63
- rpc: "https://custom-rpc-url.com",
64
- });
65
-
66
- expect(createClient).toHaveBeenCalledWith(
67
- expect.objectContaining({
68
- endpoint: "https://custom-rpc-url.com",
69
- }),
70
- );
71
- expect(mockClient.getContractCode).toHaveBeenCalledWith("0xMockedContract");
72
- expect(codeAction["succeedSpinner"]).toHaveBeenCalledWith(
73
- "Contract code retrieved successfully",
74
- mockResult,
75
- );
76
- });
77
-
78
- test("initializes consensus smart contract", async () => {
79
- vi.mocked(mockClient.getContractCode).mockResolvedValue("0x" as any);
80
-
81
- await codeAction.code({contractAddress: "0xMockedContract"});
82
-
83
- expect(mockClient.initializeConsensusSmartContract).toHaveBeenCalled();
84
- });
85
- });
86
-
87
-
@@ -1,65 +0,0 @@
1
- import {describe, test, vi, beforeEach, afterEach, expect} from "vitest";
2
- import {CreateAccountAction} from "../../src/commands/account/create";
3
- import {readFileSync, existsSync} from "fs";
4
- import os from "os";
5
-
6
- vi.mock("fs");
7
- vi.mock("os");
8
-
9
- describe("CreateAccountAction", () => {
10
- let createAction: CreateAccountAction;
11
- const mockKeystorePath = "/mocked/home/.genlayer/keystores/main.json";
12
-
13
- beforeEach(() => {
14
- vi.clearAllMocks();
15
- // Setup mocks before creating the action (needed for constructor)
16
- vi.mocked(os.homedir).mockReturnValue("/mocked/home");
17
- vi.mocked(existsSync).mockReturnValue(true);
18
- vi.mocked(readFileSync).mockReturnValue(JSON.stringify({activeAccount: "default"}));
19
-
20
- createAction = new CreateAccountAction();
21
-
22
- // Mock the BaseAction methods
23
- vi.spyOn(createAction as any, "startSpinner").mockImplementation(() => {});
24
- vi.spyOn(createAction as any, "succeedSpinner").mockImplementation(() => {});
25
- vi.spyOn(createAction as any, "failSpinner").mockImplementation(() => {});
26
- vi.spyOn(createAction as any, "createKeypairByName").mockResolvedValue("0x1234567890abcdef");
27
- vi.spyOn(createAction as any, "setActiveAccount").mockImplementation(() => {});
28
- vi.spyOn(createAction as any, "getKeystorePath").mockReturnValue(mockKeystorePath);
29
- });
30
-
31
- afterEach(() => {
32
- vi.restoreAllMocks();
33
- });
34
-
35
- test("successfully creates and saves an encrypted keystore", async () => {
36
- const options = {name: "main", overwrite: false, setActive: true};
37
-
38
- await createAction.execute(options);
39
-
40
- expect(createAction["startSpinner"]).toHaveBeenCalledWith("Creating account 'main'...");
41
- expect(createAction["createKeypairByName"]).toHaveBeenCalledWith("main", false);
42
- expect(createAction["setActiveAccount"]).toHaveBeenCalledWith("main");
43
- expect(createAction["succeedSpinner"]).toHaveBeenCalledWith(
44
- `Account 'main' created at: ${mockKeystorePath}`,
45
- );
46
- });
47
-
48
- test("handles errors during keystore creation", async () => {
49
- const mockError = new Error("Mocked creation error");
50
- vi.spyOn(createAction as any, "createKeypairByName").mockRejectedValue(mockError);
51
-
52
- await createAction.execute({name: "main", overwrite: true});
53
-
54
- expect(createAction["failSpinner"]).toHaveBeenCalledWith("Failed to create account", mockError);
55
- });
56
-
57
- test("skips setting active account when setActive is false", async () => {
58
- const options = {name: "validator", overwrite: false, setActive: false};
59
-
60
- await createAction.execute(options);
61
-
62
- expect(createAction["setActiveAccount"]).not.toHaveBeenCalled();
63
- expect(createAction["succeedSpinner"]).toHaveBeenCalled();
64
- });
65
- });