genlayer 0.18.1 → 0.18.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/CHANGELOG.md +9 -0
- package/dist/index.js +49 -29
- package/package.json +1 -1
- package/src/commands/contracts/call.ts +7 -49
- package/src/commands/contracts/deploy.ts +3 -2
- package/src/commands/contracts/index.ts +36 -11
- package/src/commands/contracts/write.ts +49 -0
- package/src/commands/general/init.ts +15 -20
- package/src/commands/general/start.ts +14 -10
- package/src/commands/keygen/create.ts +2 -2
- package/tests/actions/call.test.ts +22 -86
- package/tests/actions/create.test.ts +8 -8
- package/tests/actions/init.test.ts +173 -102
- package/tests/actions/start.test.ts +48 -24
- package/tests/actions/write.test.ts +102 -0
- package/tests/commands/write.test.ts +76 -0
- package/tests/libs/baseAction.test.ts +37 -26
- package/tests/services/simulator.test.ts +98 -102
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
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
4
|
|
|
5
5
|
vi.mock("genlayer-js");
|
|
6
6
|
|
|
@@ -11,7 +11,7 @@ describe("CallAction", () => {
|
|
|
11
11
|
writeContract: vi.fn(),
|
|
12
12
|
waitForTransactionReceipt: vi.fn(),
|
|
13
13
|
getContractSchema: vi.fn(),
|
|
14
|
-
initializeConsensusSmartContract: vi.fn()
|
|
14
|
+
initializeConsensusSmartContract: vi.fn(),
|
|
15
15
|
};
|
|
16
16
|
|
|
17
17
|
const mockPrivateKey = "mocked_private_key";
|
|
@@ -19,7 +19,7 @@ describe("CallAction", () => {
|
|
|
19
19
|
beforeEach(() => {
|
|
20
20
|
vi.clearAllMocks();
|
|
21
21
|
vi.mocked(createClient).mockReturnValue(mockClient as any);
|
|
22
|
-
vi.mocked(createAccount).mockReturnValue({
|
|
22
|
+
vi.mocked(createAccount).mockReturnValue({privateKey: mockPrivateKey} as any);
|
|
23
23
|
callActions = new CallAction();
|
|
24
24
|
vi.spyOn(callActions as any, "getPrivateKey").mockResolvedValue(mockPrivateKey);
|
|
25
25
|
|
|
@@ -34,10 +34,9 @@ describe("CallAction", () => {
|
|
|
34
34
|
});
|
|
35
35
|
|
|
36
36
|
test("calls readContract successfully", async () => {
|
|
37
|
-
const options = {
|
|
37
|
+
const options = {args: [1, 2, "Hello"]};
|
|
38
38
|
const mockResult = "mocked_result";
|
|
39
39
|
|
|
40
|
-
vi.mocked(mockClient.getContractSchema).mockResolvedValue({ methods: { getData: { readonly: true } } });
|
|
41
40
|
vi.mocked(mockClient.readContract).mockResolvedValue(mockResult);
|
|
42
41
|
|
|
43
42
|
await callActions.call({
|
|
@@ -51,65 +50,24 @@ describe("CallAction", () => {
|
|
|
51
50
|
functionName: "getData",
|
|
52
51
|
args: [1, 2, "Hello"],
|
|
53
52
|
});
|
|
54
|
-
expect(callActions["succeedSpinner"]).toHaveBeenCalledWith(
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
const options = { args: [42, "Update"] };
|
|
59
|
-
const mockHash = "0xMockedTransactionHash";
|
|
60
|
-
const mockReceipt = { status: "success" };
|
|
61
|
-
|
|
62
|
-
vi.mocked(mockClient.getContractSchema).mockResolvedValue({ methods: { updateData: { readonly: false } } });
|
|
63
|
-
vi.mocked(mockClient.writeContract).mockResolvedValue(mockHash);
|
|
64
|
-
vi.mocked(mockClient.waitForTransactionReceipt).mockResolvedValue(mockReceipt);
|
|
65
|
-
|
|
66
|
-
await callActions.call({
|
|
67
|
-
contractAddress: "0xMockedContract",
|
|
68
|
-
method: "updateData",
|
|
69
|
-
...options,
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
expect(mockClient.writeContract).toHaveBeenCalledWith({
|
|
73
|
-
address: "0xMockedContract",
|
|
74
|
-
functionName: "updateData",
|
|
75
|
-
args: [42, "Update"],
|
|
76
|
-
value: 0n,
|
|
77
|
-
});
|
|
78
|
-
expect(callActions["log"]).toHaveBeenCalledWith("Write transaction hash:", mockHash);
|
|
79
|
-
expect(callActions["succeedSpinner"]).toHaveBeenCalledWith("Write operation successfully executed", mockReceipt);
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
test("fails when method is not found", async () => {
|
|
83
|
-
vi.mocked(mockClient.getContractSchema).mockResolvedValue({ methods: { updateData: { readonly: false } } });
|
|
84
|
-
|
|
85
|
-
await callActions.call({ contractAddress: "0xMockedContract", method: "getData", args: [] });
|
|
86
|
-
|
|
87
|
-
expect(callActions["failSpinner"]).toHaveBeenCalledWith("method getData not found.");
|
|
53
|
+
expect(callActions["succeedSpinner"]).toHaveBeenCalledWith(
|
|
54
|
+
"Read operation successfully executed",
|
|
55
|
+
"mocked_result",
|
|
56
|
+
);
|
|
88
57
|
});
|
|
89
58
|
|
|
90
59
|
test("handles readContract errors", async () => {
|
|
91
|
-
vi.mocked(mockClient.getContractSchema).mockResolvedValue({ methods: { getData: { readonly: true } } });
|
|
92
60
|
vi.mocked(mockClient.readContract).mockRejectedValue(new Error("Mocked read error"));
|
|
93
61
|
|
|
94
|
-
await callActions.call({
|
|
62
|
+
await callActions.call({contractAddress: "0xMockedContract", method: "getData", args: [1]});
|
|
95
63
|
|
|
96
64
|
expect(callActions["failSpinner"]).toHaveBeenCalledWith("Error during read operation", expect.any(Error));
|
|
97
65
|
});
|
|
98
66
|
|
|
99
|
-
test("handles writeContract errors", async () => {
|
|
100
|
-
vi.mocked(mockClient.getContractSchema).mockResolvedValue({ methods: { updateData: { readonly: false } } });
|
|
101
|
-
vi.mocked(mockClient.writeContract).mockRejectedValue(new Error("Mocked write error"));
|
|
102
|
-
|
|
103
|
-
await callActions.call({ contractAddress: "0xMockedContract", method: "updateData", args: [1] });
|
|
104
|
-
|
|
105
|
-
expect(callActions["failSpinner"]).toHaveBeenCalledWith("Error during write operation", expect.any(Error));
|
|
106
|
-
});
|
|
107
|
-
|
|
108
67
|
test("uses custom RPC URL when provided", async () => {
|
|
109
|
-
const options = {
|
|
68
|
+
const options = {args: [1, 2, "Hello"], rpc: "https://custom-rpc-url.com"};
|
|
110
69
|
const mockResult = "mocked_result";
|
|
111
70
|
|
|
112
|
-
vi.mocked(mockClient.getContractSchema).mockResolvedValue({ methods: { getData: { readonly: true } } });
|
|
113
71
|
vi.mocked(mockClient.readContract).mockResolvedValue(mockResult);
|
|
114
72
|
|
|
115
73
|
await callActions.call({
|
|
@@ -118,41 +76,19 @@ describe("CallAction", () => {
|
|
|
118
76
|
...options,
|
|
119
77
|
});
|
|
120
78
|
|
|
121
|
-
expect(createClient).toHaveBeenCalledWith(
|
|
122
|
-
|
|
123
|
-
|
|
79
|
+
expect(createClient).toHaveBeenCalledWith(
|
|
80
|
+
expect.objectContaining({
|
|
81
|
+
endpoint: "https://custom-rpc-url.com",
|
|
82
|
+
}),
|
|
83
|
+
);
|
|
124
84
|
expect(mockClient.readContract).toHaveBeenCalledWith({
|
|
125
85
|
address: "0xMockedContract",
|
|
126
86
|
functionName: "getData",
|
|
127
87
|
args: [1, 2, "Hello"],
|
|
128
88
|
});
|
|
129
|
-
expect(callActions["succeedSpinner"]).toHaveBeenCalledWith(
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
const options = { args: [42, "Update"], rpc: "https://custom-rpc-url.com" };
|
|
134
|
-
const mockHash = "0xMockedTransactionHash";
|
|
135
|
-
const mockReceipt = { status: "success" };
|
|
136
|
-
|
|
137
|
-
vi.mocked(mockClient.getContractSchema).mockResolvedValue({ methods: { updateData: { readonly: false } } });
|
|
138
|
-
vi.mocked(mockClient.writeContract).mockResolvedValue(mockHash);
|
|
139
|
-
vi.mocked(mockClient.waitForTransactionReceipt).mockResolvedValue(mockReceipt);
|
|
140
|
-
|
|
141
|
-
await callActions.call({
|
|
142
|
-
contractAddress: "0xMockedContract",
|
|
143
|
-
method: "updateData",
|
|
144
|
-
...options,
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
expect(createClient).toHaveBeenCalledWith(expect.objectContaining({
|
|
148
|
-
endpoint: "https://custom-rpc-url.com"
|
|
149
|
-
}));
|
|
150
|
-
expect(mockClient.writeContract).toHaveBeenCalledWith({
|
|
151
|
-
address: "0xMockedContract",
|
|
152
|
-
functionName: "updateData",
|
|
153
|
-
args: [42, "Update"],
|
|
154
|
-
value: 0n,
|
|
155
|
-
});
|
|
156
|
-
expect(callActions["succeedSpinner"]).toHaveBeenCalledWith("Write operation successfully executed", mockReceipt);
|
|
89
|
+
expect(callActions["succeedSpinner"]).toHaveBeenCalledWith(
|
|
90
|
+
"Read operation successfully executed",
|
|
91
|
+
"mocked_result",
|
|
92
|
+
);
|
|
157
93
|
});
|
|
158
|
-
});
|
|
94
|
+
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import {describe, test, vi, beforeEach, afterEach, expect} from "vitest";
|
|
2
|
+
import {KeypairCreator} from "../../src/commands/keygen/create";
|
|
3
|
+
import {writeFileSync, existsSync} from "fs";
|
|
4
|
+
import {ethers} from "ethers";
|
|
5
5
|
|
|
6
6
|
vi.mock("fs");
|
|
7
7
|
|
|
@@ -33,7 +33,7 @@ describe("KeypairCreator", () => {
|
|
|
33
33
|
vi.spyOn(keypairCreator as any, "succeedSpinner").mockImplementation(() => {});
|
|
34
34
|
vi.spyOn(keypairCreator as any, "failSpinner").mockImplementation(() => {});
|
|
35
35
|
vi.spyOn(keypairCreator as any, "writeConfig").mockImplementation(() => {});
|
|
36
|
-
vi.spyOn(keypairCreator as any, "getFilePath").mockImplementation(
|
|
36
|
+
vi.spyOn(keypairCreator as any, "getFilePath").mockImplementation(fileName => `/mocked/path/${fileName}`);
|
|
37
37
|
vi.mocked(ethers.Wallet.createRandom).mockReturnValue(mockWallet);
|
|
38
38
|
});
|
|
39
39
|
|
|
@@ -43,14 +43,14 @@ describe("KeypairCreator", () => {
|
|
|
43
43
|
|
|
44
44
|
test("successfully creates and saves a keypair", () => {
|
|
45
45
|
vi.mocked(existsSync).mockReturnValue(false);
|
|
46
|
-
const options = {
|
|
46
|
+
const options = {output: "keypair.json", overwrite: false};
|
|
47
47
|
|
|
48
48
|
keypairCreator.createKeypairAction(options);
|
|
49
49
|
|
|
50
50
|
expect(keypairCreator["startSpinner"]).toHaveBeenCalledWith("Creating keypair...");
|
|
51
51
|
expect(mockKeypairManager.createKeypair).toHaveBeenCalledWith(options.output, options.overwrite);
|
|
52
52
|
expect(keypairCreator["succeedSpinner"]).toHaveBeenCalledWith(
|
|
53
|
-
"Keypair successfully created and saved to: keypair.json"
|
|
53
|
+
"Keypair successfully created and saved to: keypair.json",
|
|
54
54
|
);
|
|
55
55
|
});
|
|
56
56
|
|
|
@@ -60,7 +60,7 @@ describe("KeypairCreator", () => {
|
|
|
60
60
|
throw mockError;
|
|
61
61
|
});
|
|
62
62
|
|
|
63
|
-
keypairCreator.createKeypairAction({
|
|
63
|
+
keypairCreator.createKeypairAction({output: "keypair.json", overwrite: true});
|
|
64
64
|
|
|
65
65
|
expect(keypairCreator["failSpinner"]).toHaveBeenCalledWith("Failed to generate keypair", mockError);
|
|
66
66
|
});
|