quantumcoin 7.0.2 → 7.0.4

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 (155) hide show
  1. package/.github/workflows/publish-npmjs.yaml +22 -22
  2. package/.gitignore +15 -15
  3. package/LICENSE +21 -21
  4. package/README-SDK.md +756 -756
  5. package/README.md +165 -152
  6. package/SPEC.md +3845 -3845
  7. package/config.d.ts +50 -50
  8. package/config.js +115 -115
  9. package/examples/AllSolidityTypes.sol +184 -184
  10. package/examples/SimpleIERC20.sol +74 -74
  11. package/examples/events.js +41 -35
  12. package/examples/events.ts +35 -0
  13. package/examples/example-generator-sdk-js.js +100 -95
  14. package/examples/example-generator-sdk-js.ts +77 -0
  15. package/examples/example-generator-sdk-ts.js +100 -95
  16. package/examples/example-generator-sdk-ts.ts +77 -0
  17. package/examples/example.js +72 -61
  18. package/examples/example.ts +61 -0
  19. package/examples/offline-signing.js +79 -0
  20. package/examples/offline-signing.ts +66 -0
  21. package/examples/package-lock.json +48 -57
  22. package/examples/package.json +32 -16
  23. package/examples/read-operations.js +32 -27
  24. package/examples/read-operations.ts +31 -0
  25. package/examples/sdk-generator-erc20.inline.json +251 -251
  26. package/examples/solidity-types.ts +43 -43
  27. package/examples/wallet-offline.js +35 -29
  28. package/examples/wallet-offline.ts +34 -0
  29. package/generate-sdk.js +1824 -1383
  30. package/index.js +12 -12
  31. package/package.json +95 -75
  32. package/scripts/copy-declarations.js +31 -0
  33. package/scripts/run-all-one-by-one.js +151 -0
  34. package/src/abi/fragments.d.ts +42 -42
  35. package/src/abi/fragments.js +63 -63
  36. package/src/abi/index.d.ts +13 -13
  37. package/src/abi/index.js +9 -9
  38. package/src/abi/interface.d.ts +128 -132
  39. package/src/abi/interface.js +590 -590
  40. package/src/abi/js-abi-coder.d.ts +8 -0
  41. package/src/abi/js-abi-coder.js +474 -474
  42. package/src/constants.d.ts +66 -61
  43. package/src/constants.js +101 -94
  44. package/src/contract/contract-factory.d.ts +28 -28
  45. package/src/contract/contract-factory.js +105 -105
  46. package/src/contract/contract.d.ts +113 -105
  47. package/src/contract/contract.js +354 -312
  48. package/src/contract/index.d.ts +9 -9
  49. package/src/contract/index.js +9 -9
  50. package/src/errors/index.d.ts +92 -92
  51. package/src/errors/index.js +188 -188
  52. package/src/generator/index.d.ts +74 -0
  53. package/src/generator/index.js +1404 -1201
  54. package/src/index.d.ts +125 -127
  55. package/src/index.js +41 -41
  56. package/src/internal/hex.d.ts +61 -61
  57. package/src/internal/hex.js +144 -144
  58. package/src/providers/extra-providers.d.ts +139 -128
  59. package/src/providers/extra-providers.js +600 -575
  60. package/src/providers/index.d.ts +17 -16
  61. package/src/providers/index.js +10 -10
  62. package/src/providers/json-rpc-provider.d.ts +12 -12
  63. package/src/providers/json-rpc-provider.js +79 -79
  64. package/src/providers/provider.d.ts +207 -196
  65. package/src/providers/provider.js +392 -359
  66. package/src/types/index.d.ts +214 -462
  67. package/src/types/index.js +9 -9
  68. package/src/utils/address.d.ts +72 -72
  69. package/src/utils/address.js +181 -182
  70. package/src/utils/encoding.d.ts +120 -120
  71. package/src/utils/encoding.js +306 -306
  72. package/src/utils/hashing.d.ts +82 -76
  73. package/src/utils/hashing.js +313 -298
  74. package/src/utils/index.d.ts +65 -55
  75. package/src/utils/index.js +13 -13
  76. package/src/utils/result.d.ts +57 -57
  77. package/src/utils/result.js +128 -128
  78. package/src/utils/rlp.d.ts +12 -12
  79. package/src/utils/rlp.js +200 -200
  80. package/src/utils/units.d.ts +29 -29
  81. package/src/utils/units.js +107 -107
  82. package/src/wallet/index.d.ts +10 -10
  83. package/src/wallet/index.js +8 -8
  84. package/src/wallet/wallet.d.ts +160 -160
  85. package/src/wallet/wallet.js +483 -489
  86. package/test/e2e/all-solidity-types.dynamic.test.js +207 -200
  87. package/test/e2e/all-solidity-types.dynamic.test.ts +191 -0
  88. package/test/e2e/all-solidity-types.fixtures.js +231 -231
  89. package/test/e2e/all-solidity-types.generated-sdks.e2e.test.js +387 -361
  90. package/test/e2e/all-solidity-types.generated-sdks.e2e.test.ts +350 -0
  91. package/test/e2e/helpers.js +59 -47
  92. package/test/e2e/signing-context-and-fee.e2e.test.js +137 -0
  93. package/test/e2e/signing-context-and-fee.e2e.test.ts +128 -0
  94. package/test/e2e/simple-erc20.generated-sdks.e2e.test.js +168 -144
  95. package/test/e2e/simple-erc20.generated-sdks.e2e.test.ts +141 -0
  96. package/test/e2e/transactional.test.js +245 -191
  97. package/test/e2e/transactional.test.ts +208 -0
  98. package/test/e2e/typed-generator.e2e.test.js +407 -402
  99. package/test/e2e/typed-generator.e2e.test.ts +337 -0
  100. package/test/fixtures/ConstructorParam.sol +23 -23
  101. package/test/fixtures/MultiContracts.sol +37 -37
  102. package/test/fixtures/SimpleStorage.sol +18 -18
  103. package/test/fixtures/StakingContract.abi.json +1 -1
  104. package/test/integration/ipc-provider.test.js +49 -44
  105. package/test/integration/ipc-provider.test.ts +44 -0
  106. package/test/integration/provider.test.js +88 -72
  107. package/test/integration/provider.test.ts +85 -0
  108. package/test/integration/ws-provider.test.js +41 -33
  109. package/test/integration/ws-provider.test.ts +38 -0
  110. package/test/security/malformed-input.test.js +37 -31
  111. package/test/security/malformed-input.test.ts +35 -0
  112. package/test/unit/_encrypted-output.txt +6 -0
  113. package/test/unit/_log-encrypted-jsons.js +45 -0
  114. package/test/unit/_write-keystore-fixture.js +16 -0
  115. package/test/unit/abi-interface.test.js +103 -98
  116. package/test/unit/abi-interface.test.ts +102 -0
  117. package/test/unit/address-wallet.test.js +355 -257
  118. package/test/unit/address-wallet.test.ts +342 -0
  119. package/test/unit/browser-provider.test.js +85 -82
  120. package/test/unit/browser-provider.test.ts +79 -0
  121. package/test/unit/contract.test.js +85 -82
  122. package/test/unit/contract.test.ts +83 -0
  123. package/test/unit/encoding-units-rlp.test.js +92 -89
  124. package/test/unit/encoding-units-rlp.test.ts +91 -0
  125. package/test/unit/errors.test.js +77 -74
  126. package/test/unit/errors.test.ts +76 -0
  127. package/test/unit/filter-by-blockhash.test.js +55 -52
  128. package/test/unit/filter-by-blockhash.test.ts +54 -0
  129. package/test/unit/fixtures/encrypted-keystores-48-32-36.js +9 -0
  130. package/test/unit/generate-contract-cli.test.js +42 -39
  131. package/test/unit/generate-contract-cli.test.ts +41 -0
  132. package/test/unit/generate-sdk-artifacts-json.test.js +113 -110
  133. package/test/unit/generate-sdk-artifacts-json.test.ts +110 -0
  134. package/test/unit/generator.test.js +102 -98
  135. package/test/unit/generator.test.ts +101 -0
  136. package/test/unit/hashing.test.js +68 -54
  137. package/test/unit/hashing.test.ts +67 -0
  138. package/test/unit/init.test.js +39 -36
  139. package/test/unit/init.test.ts +38 -0
  140. package/test/unit/interface.test.js +56 -53
  141. package/test/unit/interface.test.ts +54 -0
  142. package/test/unit/internal-hex.test.js +50 -47
  143. package/test/unit/internal-hex.test.ts +49 -0
  144. package/test/unit/populate-transaction.test.js +65 -0
  145. package/test/unit/populate-transaction.test.ts +64 -0
  146. package/test/unit/providers.test.js +200 -144
  147. package/test/unit/providers.test.ts +196 -0
  148. package/test/unit/result.test.js +80 -77
  149. package/test/unit/result.test.ts +79 -0
  150. package/test/unit/solidity-types.test.js +49 -46
  151. package/test/unit/solidity-types.test.ts +39 -0
  152. package/test/unit/utils.test.js +57 -54
  153. package/test/unit/utils.test.ts +56 -0
  154. package/test/verbose-logger.js +74 -0
  155. package/tsconfig.build.json +14 -0
@@ -1,402 +1,407 @@
1
- /**
2
- * @testCategory e2e
3
- * @blockchainRequired write
4
- * @transactional true
5
- *
6
- * End-to-end test for the typed contract generator:
7
- * - Compiles a Solidity contract with a constructor parameter
8
- * - Generates a standalone typed-contract package into a temp folder
9
- * - Runs `npm install`
10
- * - Verifies tests/examples were generated
11
- * - Runs the generated package tests against the provided JSON-RPC URL
12
- */
13
-
14
- const { describe, it } = require("node:test");
15
- const assert = require("node:assert/strict");
16
- const fs = require("node:fs");
17
- const path = require("node:path");
18
- const os = require("node:os");
19
- const { execFileSync, spawnSync } = require("node:child_process");
20
-
21
- const { getRpcUrl, getChainId, getSolcPath, assertSolcExists } = require("./helpers");
22
-
23
- function getNpmCmd() {
24
- return process.platform === "win32" ? "npm.cmd" : "npm";
25
- }
26
-
27
- function run(cmd, args, cwd, env) {
28
- const res = spawnSync(cmd, args, {
29
- cwd,
30
- env,
31
- encoding: "utf8",
32
- stdio: "pipe",
33
- shell: false,
34
- windowsHide: true,
35
- });
36
- if (res.error) throw res.error;
37
- return res;
38
- }
39
-
40
- function _quoteIfNeeded(s) {
41
- if (typeof s !== "string") return s;
42
- return /[ \t"]/g.test(s) ? `"${s.replace(/"/g, '\\"')}"` : s;
43
- }
44
-
45
- function runNpm(args, cwd, env) {
46
- if (process.platform === "win32") {
47
- const cmd = `${getNpmCmd()} ${args.map(_quoteIfNeeded).join(" ")}`;
48
- return run("cmd.exe", ["/d", "/s", "/c", cmd], cwd, env);
49
- }
50
- return run(getNpmCmd(), args, cwd, env);
51
- }
52
-
53
- function compileSolidity({ solcPath, solPath, contractName }) {
54
- const out = execFileSync(solcPath, ["--optimize", "--combined-json", "abi,bin", solPath], { encoding: "utf8" });
55
- const parsed = JSON.parse(out);
56
- const key = Object.keys(parsed.contracts || {}).find((k) => k.endsWith(`:${contractName}`));
57
- if (!key) throw new Error(`Compiled contract ${contractName} not found in solc output`);
58
- const c = parsed.contracts[key];
59
- const abi = JSON.parse(c.abi);
60
- const bin = c.bin || "";
61
- return { abi, bin };
62
- }
63
-
64
- describe("typed contract generator package e2e", () => {
65
- it("generates a package and runs its transactional tests", async (t) => {
66
- const rpcUrl = getRpcUrl();
67
- if (!rpcUrl) {
68
- t.skip("QC_RPC_URL not provided");
69
- return;
70
- }
71
-
72
- const chainId = getChainId();
73
- const solcPath = getSolcPath();
74
- assertSolcExists(solcPath);
75
-
76
- const repoRoot = path.resolve(__dirname, "..", "..");
77
- const fixtureSol = path.join(repoRoot, "test", "fixtures", "ConstructorParam.sol");
78
- const contractName = "ConstructorParam";
79
-
80
- const tmpBase = fs.mkdtempSync(path.join(os.tmpdir(), "qcgen-"));
81
- const pkgName = `qcgen-${Date.now()}`;
82
- const pkgDir = tmpBase;
83
- const pkgRoot = path.join(pkgDir, pkgName);
84
-
85
- let succeeded = false;
86
- try {
87
- // 1) Compile fixture to ABI + BIN
88
- const { abi, bin } = compileSolidity({ solcPath, solPath: fixtureSol, contractName });
89
-
90
- const abiPath = path.join(tmpBase, `${contractName}.abi.json`);
91
- const binPath = path.join(tmpBase, `${contractName}.bin`);
92
- fs.writeFileSync(abiPath, JSON.stringify(abi, null, 2), "utf8");
93
- fs.writeFileSync(binPath, bin.trim().startsWith("0x") ? bin.trim() : `0x${bin.trim()}`, "utf8");
94
-
95
- // 2) Generate package
96
- const generatorCli = path.join(repoRoot, "generate-sdk.js");
97
- const gen = run(
98
- process.execPath,
99
- [
100
- generatorCli,
101
- "--abi",
102
- abiPath,
103
- "--bin",
104
- binPath,
105
- "--name",
106
- contractName,
107
- "--create-package",
108
- "--package-dir",
109
- pkgDir,
110
- "--package-name",
111
- pkgName,
112
- "--package-description",
113
- "Temporary typed-contract package generated by quantumcoin.js e2e tests",
114
- "--package-author",
115
- "quantumcoin.js test",
116
- "--package-license",
117
- "MIT",
118
- "--package-version",
119
- "0.0.1",
120
- "--non-interactive",
121
- ],
122
- repoRoot,
123
- process.env,
124
- );
125
- assert.equal(gen.status, 0, `generator failed:\n${gen.stdout}\n${gen.stderr}`);
126
-
127
- // 3) Verify expected files exist
128
- assert.ok(fs.existsSync(path.join(pkgRoot, "package.json")));
129
- assert.ok(fs.existsSync(path.join(pkgRoot, "tsconfig.json")));
130
- assert.ok(fs.existsSync(path.join(pkgRoot, "src", `${contractName}.ts`)));
131
- assert.ok(fs.existsSync(path.join(pkgRoot, "src", `${contractName}__factory.ts`)));
132
- assert.ok(fs.existsSync(path.join(pkgRoot, "test", "e2e", `${contractName}.e2e.test.js`)));
133
- assert.ok(fs.existsSync(path.join(pkgRoot, "examples", "deploy.js")));
134
- assert.ok(fs.existsSync(path.join(pkgRoot, "examples", "read-operations.js")));
135
- assert.ok(fs.existsSync(path.join(pkgRoot, "examples", "write-operations.js")));
136
- assert.ok(fs.existsSync(path.join(pkgRoot, "examples", "events.js")));
137
- assert.ok(fs.existsSync(path.join(pkgRoot, "index.js")));
138
- assert.ok(fs.existsSync(path.join(pkgRoot, "index.d.ts")));
139
- assert.ok(fs.existsSync(path.join(pkgRoot, "README.md")));
140
-
141
- // This test uses ABI+BIN input, so Solidity source comments are not available here.
142
- // Ensure the doc build ran and included the contract name.
143
- const readme = fs.readFileSync(path.join(pkgRoot, "README.md"), "utf8");
144
- assert.ok(readme.includes(contractName));
145
-
146
- // 4) Install deps (generator runs install+build, but keep this idempotent)
147
- if (!fs.existsSync(path.join(pkgRoot, "node_modules"))) {
148
- const install = runNpm(["install", "--no-fund", "--no-audit"], pkgRoot, process.env);
149
- assert.equal(install.status, 0, `npm install failed:\n${install.stdout}\n${install.stderr}`);
150
- }
151
-
152
- // 5) Run generated tests against the configured chain
153
- const env = {
154
- ...process.env,
155
- QC_RPC_URL: rpcUrl,
156
- QC_CHAIN_ID: String(chainId),
157
- };
158
- const testRun = runNpm(["test"], pkgRoot, env);
159
- assert.equal(testRun.status, 0, `generated package tests failed:\n${testRun.stdout}\n${testRun.stderr}`);
160
-
161
- succeeded = true;
162
- } finally {
163
- // Keep artifacts on failure for debugging.
164
- if (succeeded) {
165
- fs.rmSync(tmpBase, { recursive: true, force: true });
166
- } else {
167
- // eslint-disable-next-line no-console
168
- console.error("Generated package kept at:", pkgRoot);
169
- }
170
- }
171
- }, { timeout: 1_800_000 });
172
-
173
- it("generates a package from artifacts JSON input (multiple contracts) and runs its transactional tests", async (t) => {
174
- const rpcUrl = getRpcUrl();
175
- if (!rpcUrl) {
176
- t.skip("QC_RPC_URL not provided");
177
- return;
178
- }
179
-
180
- const chainId = getChainId();
181
- const solcPath = getSolcPath();
182
- assertSolcExists(solcPath);
183
-
184
- const repoRoot = path.resolve(__dirname, "..", "..");
185
- const fixtureCtorSol = path.join(repoRoot, "test", "fixtures", "ConstructorParam.sol");
186
- const fixtureMultiSol = path.join(repoRoot, "test", "fixtures", "MultiContracts.sol");
187
-
188
- const tmpBase = fs.mkdtempSync(path.join(os.tmpdir(), "qcgen-json-"));
189
- const pkgName = `qcgen-json-${Date.now()}`;
190
- const pkgDir = tmpBase;
191
- const pkgRoot = path.join(pkgDir, pkgName);
192
-
193
- let succeeded = false;
194
- try {
195
- // Compile 2 contracts into ABI+BIN pairs
196
- const ctorName = "ConstructorParam";
197
- const alphaName = "Alpha";
198
-
199
- const { abi: ctorAbi, bin: ctorBin } = compileSolidity({ solcPath, solPath: fixtureCtorSol, contractName: ctorName });
200
- const { abi: alphaAbi, bin: alphaBin } = compileSolidity({ solcPath, solPath: fixtureMultiSol, contractName: alphaName });
201
-
202
- const ctorAbiPath = path.join(tmpBase, `${ctorName}.abi.json`);
203
- const ctorBinPath = path.join(tmpBase, `${ctorName}.bin`);
204
- fs.writeFileSync(ctorAbiPath, JSON.stringify(ctorAbi, null, 2), "utf8");
205
- fs.writeFileSync(ctorBinPath, ctorBin.trim().startsWith("0x") ? ctorBin.trim() : `0x${ctorBin.trim()}`, "utf8");
206
-
207
- const alphaAbiPath = path.join(tmpBase, `${alphaName}.abi.json`);
208
- const alphaBinPath = path.join(tmpBase, `${alphaName}.bin`);
209
- fs.writeFileSync(alphaAbiPath, JSON.stringify(alphaAbi, null, 2), "utf8");
210
- fs.writeFileSync(alphaBinPath, alphaBin.trim().startsWith("0x") ? alphaBin.trim() : `0x${alphaBin.trim()}`, "utf8");
211
-
212
- const artifactsJsonPath = path.join(tmpBase, "artifacts.json");
213
- fs.writeFileSync(
214
- artifactsJsonPath,
215
- JSON.stringify(
216
- [
217
- { abi: path.basename(alphaAbiPath), bin: path.basename(alphaBinPath) },
218
- { abi: path.basename(ctorAbiPath), bin: path.basename(ctorBinPath) },
219
- ],
220
- null,
221
- 2,
222
- ),
223
- "utf8",
224
- );
225
-
226
- // Generate package using artifacts JSON input
227
- const generatorCli = path.join(repoRoot, "generate-sdk.js");
228
- const gen = run(
229
- process.execPath,
230
- [
231
- generatorCli,
232
- "--artifacts-json",
233
- artifactsJsonPath,
234
- "--create-package",
235
- "--package-dir",
236
- pkgDir,
237
- "--package-name",
238
- pkgName,
239
- "--package-description",
240
- "Temporary typed-contract package generated from artifacts JSON input",
241
- "--package-author",
242
- "quantumcoin.js test",
243
- "--package-license",
244
- "MIT",
245
- "--package-version",
246
- "0.0.1",
247
- "--non-interactive",
248
- ],
249
- repoRoot,
250
- process.env,
251
- );
252
- assert.equal(gen.status, 0, `generator failed:\n${gen.stdout}\n${gen.stderr}`);
253
-
254
- // Verify multiple contracts were generated
255
- assert.ok(fs.existsSync(path.join(pkgRoot, "src", "Alpha.ts")));
256
- assert.ok(fs.existsSync(path.join(pkgRoot, "src", "Alpha__factory.ts")));
257
- assert.ok(fs.existsSync(path.join(pkgRoot, "src", "ConstructorParam.ts")));
258
- assert.ok(fs.existsSync(path.join(pkgRoot, "src", "ConstructorParam__factory.ts")));
259
- assert.ok(fs.existsSync(path.join(pkgRoot, "src", "index.ts")));
260
- assert.ok(fs.existsSync(path.join(pkgRoot, "index.js")));
261
- assert.ok(fs.existsSync(path.join(pkgRoot, "index.d.ts")));
262
- assert.ok(fs.existsSync(path.join(pkgRoot, "README.md")));
263
-
264
- // Verify tests exist for each contract
265
- assert.ok(fs.existsSync(path.join(pkgRoot, "test", "e2e", "Alpha.e2e.test.js")));
266
- assert.ok(fs.existsSync(path.join(pkgRoot, "test", "e2e", "ConstructorParam.e2e.test.js")));
267
-
268
- // Multi-contract packages use per-contract example filenames.
269
- assert.ok(fs.existsSync(path.join(pkgRoot, "examples", "deploy-Alpha.js")));
270
- assert.ok(fs.existsSync(path.join(pkgRoot, "examples", "deploy-ConstructorParam.js")));
271
-
272
- const readme = fs.readFileSync(path.join(pkgRoot, "README.md"), "utf8");
273
- assert.ok(readme.includes("Alpha"));
274
- assert.ok(readme.includes("ConstructorParam"));
275
-
276
- // Install deps + run tests against chain
277
- if (!fs.existsSync(path.join(pkgRoot, "node_modules"))) {
278
- const install = runNpm(["install", "--no-fund", "--no-audit"], pkgRoot, process.env);
279
- assert.equal(install.status, 0, `npm install failed:\n${install.stdout}\n${install.stderr}`);
280
- }
281
-
282
- const env = {
283
- ...process.env,
284
- QC_RPC_URL: rpcUrl,
285
- QC_CHAIN_ID: String(chainId),
286
- };
287
- const testRun = runNpm(["test"], pkgRoot, env);
288
- assert.equal(testRun.status, 0, `generated package tests failed:\n${testRun.stdout}\n${testRun.stderr}`);
289
-
290
- succeeded = true;
291
- } finally {
292
- if (succeeded) {
293
- fs.rmSync(tmpBase, { recursive: true, force: true });
294
- } else {
295
- // eslint-disable-next-line no-console
296
- console.error("Generated package kept at:", pkgRoot);
297
- }
298
- }
299
- }, { timeout: 1_800_000 });
300
-
301
- it("generates a package from Solidity input (multiple contracts) and runs its transactional tests", async (t) => {
302
- const rpcUrl = getRpcUrl();
303
- if (!rpcUrl) {
304
- t.skip("QC_RPC_URL not provided");
305
- return;
306
- }
307
-
308
- const chainId = getChainId();
309
- const solcPath = getSolcPath();
310
- assertSolcExists(solcPath);
311
-
312
- const repoRoot = path.resolve(__dirname, "..", "..");
313
- const fixtureSol = path.join(repoRoot, "test", "fixtures", "MultiContracts.sol");
314
-
315
- const tmpBase = fs.mkdtempSync(path.join(os.tmpdir(), "qcgen-sol-"));
316
- const pkgName = `qcgen-sol-${Date.now()}`;
317
- const pkgDir = tmpBase;
318
- const pkgRoot = path.join(pkgDir, pkgName);
319
-
320
- let succeeded = false;
321
- try {
322
- // Generate package directly from Solidity sources
323
- const generatorCli = path.join(repoRoot, "generate-sdk.js");
324
- const gen = run(
325
- process.execPath,
326
- [
327
- generatorCli,
328
- "--sol",
329
- fixtureSol,
330
- "--solc",
331
- solcPath,
332
- "--create-package",
333
- "--package-dir",
334
- pkgDir,
335
- "--package-name",
336
- pkgName,
337
- "--package-description",
338
- "Temporary typed-contract package generated from Solidity input",
339
- "--package-author",
340
- "quantumcoin.js test",
341
- "--package-license",
342
- "MIT",
343
- "--package-version",
344
- "0.0.1",
345
- "--non-interactive",
346
- ],
347
- repoRoot,
348
- process.env,
349
- );
350
- assert.equal(gen.status, 0, `generator failed:\n${gen.stdout}\n${gen.stderr}`);
351
-
352
- // Verify multiple contracts were generated
353
- assert.ok(fs.existsSync(path.join(pkgRoot, "src", "Alpha.ts")));
354
- assert.ok(fs.existsSync(path.join(pkgRoot, "src", "Alpha__factory.ts")));
355
- assert.ok(fs.existsSync(path.join(pkgRoot, "src", "Beta.ts")));
356
- assert.ok(fs.existsSync(path.join(pkgRoot, "src", "Beta__factory.ts")));
357
- assert.ok(fs.existsSync(path.join(pkgRoot, "src", "index.ts")));
358
- assert.ok(fs.existsSync(path.join(pkgRoot, "index.js")));
359
- assert.ok(fs.existsSync(path.join(pkgRoot, "index.d.ts")));
360
- assert.ok(fs.existsSync(path.join(pkgRoot, "README.md")));
361
-
362
- // Verify artifacts were emitted
363
- assert.ok(fs.existsSync(path.join(pkgRoot, "artifacts", "Alpha.abi.json")));
364
- assert.ok(fs.existsSync(path.join(pkgRoot, "artifacts", "Alpha.bin")));
365
- assert.ok(fs.existsSync(path.join(pkgRoot, "artifacts", "Beta.abi.json")));
366
- assert.ok(fs.existsSync(path.join(pkgRoot, "artifacts", "Beta.bin")));
367
-
368
- // Verify tests exist for each contract
369
- assert.ok(fs.existsSync(path.join(pkgRoot, "test", "e2e", "Alpha.e2e.test.js")));
370
- assert.ok(fs.existsSync(path.join(pkgRoot, "test", "e2e", "Beta.e2e.test.js")));
371
-
372
- // Verify Solidity comments were propagated into generated TS
373
- const alphaTs = fs.readFileSync(path.join(pkgRoot, "src", "Alpha.ts"), "utf8");
374
- assert.ok(alphaTs.includes("Alpha contract for multi-contract generator test."));
375
- assert.ok(alphaTs.includes("Set a new value in Alpha."));
376
-
377
- // Install deps + run tests against chain
378
- if (!fs.existsSync(path.join(pkgRoot, "node_modules"))) {
379
- const install = runNpm(["install", "--no-fund", "--no-audit"], pkgRoot, process.env);
380
- assert.equal(install.status, 0, `npm install failed:\n${install.stdout}\n${install.stderr}`);
381
- }
382
-
383
- const env = {
384
- ...process.env,
385
- QC_RPC_URL: rpcUrl,
386
- QC_CHAIN_ID: String(chainId),
387
- };
388
- const testRun = runNpm(["test"], pkgRoot, env);
389
- assert.equal(testRun.status, 0, `generated package tests failed:\n${testRun.stdout}\n${testRun.stderr}`);
390
-
391
- succeeded = true;
392
- } finally {
393
- if (succeeded) {
394
- fs.rmSync(tmpBase, { recursive: true, force: true });
395
- } else {
396
- // eslint-disable-next-line no-console
397
- console.error("Generated package kept at:", pkgRoot);
398
- }
399
- }
400
- }, { timeout: 1_800_000 });
401
- });
402
-
1
+ /**
2
+ * @testCategory e2e
3
+ * @blockchainRequired write
4
+ * @transactional true
5
+ *
6
+ * End-to-end test for the typed contract generator:
7
+ * - Compiles a Solidity contract with a constructor parameter
8
+ * - Generates a standalone typed-contract package into a temp folder
9
+ * - Runs `npm install`
10
+ * - Verifies tests/examples were generated
11
+ * - Runs the generated package tests against the provided JSON-RPC URL
12
+ */
13
+
14
+ const { describe, it } = require("node:test");
15
+ const assert = require("node:assert/strict");
16
+ const fs = require("node:fs");
17
+ const path = require("node:path");
18
+ const os = require("node:os");
19
+ const { execFileSync, spawnSync } = require("node:child_process");
20
+
21
+ const { getRpcUrl, getChainId, getSolcPath, assertSolcExists, logE2eConfig } = require("./helpers");
22
+ const { logSuite, logTest } = require("../verbose-logger");
23
+
24
+ function getNpmCmd() {
25
+ return process.platform === "win32" ? "npm.cmd" : "npm";
26
+ }
27
+
28
+ function run(cmd, args, cwd, env) {
29
+ const res = spawnSync(cmd, args, {
30
+ cwd,
31
+ env,
32
+ encoding: "utf8",
33
+ stdio: "pipe",
34
+ shell: false,
35
+ windowsHide: true,
36
+ });
37
+ if (res.error) throw res.error;
38
+ return res;
39
+ }
40
+
41
+ function _quoteIfNeeded(s) {
42
+ if (typeof s !== "string") return s;
43
+ return /[ \t"]/g.test(s) ? `"${s.replace(/"/g, '\\"')}"` : s;
44
+ }
45
+
46
+ function runNpm(args, cwd, env) {
47
+ if (process.platform === "win32") {
48
+ const cmd = `${getNpmCmd()} ${args.map(_quoteIfNeeded).join(" ")}`;
49
+ return run("cmd.exe", ["/d", "/s", "/c", cmd], cwd, env);
50
+ }
51
+ return run(getNpmCmd(), args, cwd, env);
52
+ }
53
+
54
+ function compileSolidity({ solcPath, solPath, contractName }) {
55
+ const out = execFileSync(solcPath, ["--optimize", "--combined-json", "abi,bin", solPath], { encoding: "utf8" });
56
+ const parsed = JSON.parse(out);
57
+ const key = Object.keys(parsed.contracts || {}).find((k) => k.endsWith(`:${contractName}`));
58
+ if (!key) throw new Error(`Compiled contract ${contractName} not found in solc output`);
59
+ const c = parsed.contracts[key];
60
+ const abi = JSON.parse(c.abi);
61
+ const bin = c.bin || "";
62
+ return { abi, bin };
63
+ }
64
+
65
+ describe("typed contract generator package e2e", () => {
66
+ it("generates a package and runs its transactional tests", async (t) => {
67
+ logSuite("typed contract generator package e2e");
68
+ logTest("generates a package and runs its transactional tests", {});
69
+ const rpcUrl = getRpcUrl();
70
+ if (!rpcUrl) {
71
+ t.skip("QC_RPC_URL not provided");
72
+ return;
73
+ }
74
+ logE2eConfig();
75
+ const chainId = getChainId();
76
+ const solcPath = getSolcPath();
77
+ assertSolcExists(solcPath);
78
+
79
+ const repoRoot = path.resolve(__dirname, "..", "..");
80
+ const fixtureSol = path.join(repoRoot, "test", "fixtures", "ConstructorParam.sol");
81
+ const contractName = "ConstructorParam";
82
+
83
+ const tmpBase = fs.mkdtempSync(path.join(os.tmpdir(), "qcgen-"));
84
+ const pkgName = `qcgen-${Date.now()}`;
85
+ const pkgDir = tmpBase;
86
+ const pkgRoot = path.join(pkgDir, pkgName);
87
+
88
+ let succeeded = false;
89
+ try {
90
+ // 1) Compile fixture to ABI + BIN
91
+ const { abi, bin } = compileSolidity({ solcPath, solPath: fixtureSol, contractName });
92
+
93
+ const abiPath = path.join(tmpBase, `${contractName}.abi.json`);
94
+ const binPath = path.join(tmpBase, `${contractName}.bin`);
95
+ fs.writeFileSync(abiPath, JSON.stringify(abi, null, 2), "utf8");
96
+ fs.writeFileSync(binPath, bin.trim().startsWith("0x") ? bin.trim() : `0x${bin.trim()}`, "utf8");
97
+
98
+ // 2) Generate package
99
+ const generatorCli = path.join(repoRoot, "generate-sdk.js");
100
+ const gen = run(
101
+ process.execPath,
102
+ [
103
+ generatorCli,
104
+ "--abi",
105
+ abiPath,
106
+ "--bin",
107
+ binPath,
108
+ "--name",
109
+ contractName,
110
+ "--create-package",
111
+ "--package-dir",
112
+ pkgDir,
113
+ "--package-name",
114
+ pkgName,
115
+ "--package-description",
116
+ "Temporary typed-contract package generated by quantumcoin.js e2e tests",
117
+ "--package-author",
118
+ "quantumcoin.js test",
119
+ "--package-license",
120
+ "MIT",
121
+ "--package-version",
122
+ "0.0.1",
123
+ "--non-interactive",
124
+ ],
125
+ repoRoot,
126
+ process.env,
127
+ );
128
+ assert.equal(gen.status, 0, `generator failed:\n${gen.stdout}\n${gen.stderr}`);
129
+
130
+ // 3) Verify expected files exist
131
+ assert.ok(fs.existsSync(path.join(pkgRoot, "package.json")));
132
+ assert.ok(fs.existsSync(path.join(pkgRoot, "tsconfig.json")));
133
+ assert.ok(fs.existsSync(path.join(pkgRoot, "src", `${contractName}.ts`)));
134
+ assert.ok(fs.existsSync(path.join(pkgRoot, "src", `${contractName}__factory.ts`)));
135
+ assert.ok(fs.existsSync(path.join(pkgRoot, "test", "e2e", `${contractName}.e2e.test.js`)));
136
+ assert.ok(fs.existsSync(path.join(pkgRoot, "examples", "deploy.js")));
137
+ assert.ok(fs.existsSync(path.join(pkgRoot, "examples", "read-operations.js")));
138
+ assert.ok(fs.existsSync(path.join(pkgRoot, "examples", "write-operations.js")));
139
+ assert.ok(fs.existsSync(path.join(pkgRoot, "examples", "events.js")));
140
+ assert.ok(fs.existsSync(path.join(pkgRoot, "index.js")));
141
+ assert.ok(fs.existsSync(path.join(pkgRoot, "index.d.ts")));
142
+ assert.ok(fs.existsSync(path.join(pkgRoot, "README.md")));
143
+
144
+ // This test uses ABI+BIN input, so Solidity source comments are not available here.
145
+ // Ensure the doc build ran and included the contract name.
146
+ const readme = fs.readFileSync(path.join(pkgRoot, "README.md"), "utf8");
147
+ assert.ok(readme.includes(contractName));
148
+
149
+ // 4) Install deps (generator runs install+build, but keep this idempotent)
150
+ if (!fs.existsSync(path.join(pkgRoot, "node_modules"))) {
151
+ const install = runNpm(["install", "--no-fund", "--no-audit"], pkgRoot, process.env);
152
+ assert.equal(install.status, 0, `npm install failed:\n${install.stdout}\n${install.stderr}`);
153
+ }
154
+
155
+ // 5) Run generated tests against the configured chain
156
+ const env = {
157
+ ...process.env,
158
+ QC_RPC_URL: rpcUrl,
159
+ QC_CHAIN_ID: String(chainId),
160
+ };
161
+ const testRun = runNpm(["test"], pkgRoot, env);
162
+ assert.equal(testRun.status, 0, `generated package tests failed:\n${testRun.stdout}\n${testRun.stderr}`);
163
+
164
+ succeeded = true;
165
+ } finally {
166
+ // Keep artifacts on failure for debugging.
167
+ if (succeeded) {
168
+ fs.rmSync(tmpBase, { recursive: true, force: true });
169
+ } else {
170
+ // eslint-disable-next-line no-console
171
+ console.error("Generated package kept at:", pkgRoot);
172
+ }
173
+ }
174
+ }, { timeout: 1_800_000 });
175
+
176
+ it("generates a package from artifacts JSON input (multiple contracts) and runs its transactional tests", async (t) => {
177
+ const rpcUrl = getRpcUrl();
178
+ if (!rpcUrl) {
179
+ t.skip("QC_RPC_URL not provided");
180
+ return;
181
+ }
182
+
183
+ const chainId = getChainId();
184
+ const solcPath = getSolcPath();
185
+ assertSolcExists(solcPath);
186
+
187
+ const repoRoot = path.resolve(__dirname, "..", "..");
188
+ const fixtureCtorSol = path.join(repoRoot, "test", "fixtures", "ConstructorParam.sol");
189
+ const fixtureMultiSol = path.join(repoRoot, "test", "fixtures", "MultiContracts.sol");
190
+
191
+ const tmpBase = fs.mkdtempSync(path.join(os.tmpdir(), "qcgen-json-"));
192
+ const pkgName = `qcgen-json-${Date.now()}`;
193
+ const pkgDir = tmpBase;
194
+ const pkgRoot = path.join(pkgDir, pkgName);
195
+
196
+ let succeeded = false;
197
+ try {
198
+ // Compile 2 contracts into ABI+BIN pairs
199
+ const ctorName = "ConstructorParam";
200
+ const alphaName = "Alpha";
201
+
202
+ const { abi: ctorAbi, bin: ctorBin } = compileSolidity({ solcPath, solPath: fixtureCtorSol, contractName: ctorName });
203
+ const { abi: alphaAbi, bin: alphaBin } = compileSolidity({ solcPath, solPath: fixtureMultiSol, contractName: alphaName });
204
+
205
+ const ctorAbiPath = path.join(tmpBase, `${ctorName}.abi.json`);
206
+ const ctorBinPath = path.join(tmpBase, `${ctorName}.bin`);
207
+ fs.writeFileSync(ctorAbiPath, JSON.stringify(ctorAbi, null, 2), "utf8");
208
+ fs.writeFileSync(ctorBinPath, ctorBin.trim().startsWith("0x") ? ctorBin.trim() : `0x${ctorBin.trim()}`, "utf8");
209
+
210
+ const alphaAbiPath = path.join(tmpBase, `${alphaName}.abi.json`);
211
+ const alphaBinPath = path.join(tmpBase, `${alphaName}.bin`);
212
+ fs.writeFileSync(alphaAbiPath, JSON.stringify(alphaAbi, null, 2), "utf8");
213
+ fs.writeFileSync(alphaBinPath, alphaBin.trim().startsWith("0x") ? alphaBin.trim() : `0x${alphaBin.trim()}`, "utf8");
214
+
215
+ const artifactsJsonPath = path.join(tmpBase, "artifacts.json");
216
+ fs.writeFileSync(
217
+ artifactsJsonPath,
218
+ JSON.stringify(
219
+ [
220
+ { abi: path.basename(alphaAbiPath), bin: path.basename(alphaBinPath) },
221
+ { abi: path.basename(ctorAbiPath), bin: path.basename(ctorBinPath) },
222
+ ],
223
+ null,
224
+ 2,
225
+ ),
226
+ "utf8",
227
+ );
228
+
229
+ // Generate package using artifacts JSON input
230
+ const generatorCli = path.join(repoRoot, "generate-sdk.js");
231
+ const gen = run(
232
+ process.execPath,
233
+ [
234
+ generatorCli,
235
+ "--artifacts-json",
236
+ artifactsJsonPath,
237
+ "--create-package",
238
+ "--package-dir",
239
+ pkgDir,
240
+ "--package-name",
241
+ pkgName,
242
+ "--package-description",
243
+ "Temporary typed-contract package generated from artifacts JSON input",
244
+ "--package-author",
245
+ "quantumcoin.js test",
246
+ "--package-license",
247
+ "MIT",
248
+ "--package-version",
249
+ "0.0.1",
250
+ "--non-interactive",
251
+ ],
252
+ repoRoot,
253
+ process.env,
254
+ );
255
+ assert.equal(gen.status, 0, `generator failed:\n${gen.stdout}\n${gen.stderr}`);
256
+
257
+ // Verify multiple contracts were generated
258
+ assert.ok(fs.existsSync(path.join(pkgRoot, "src", "Alpha.ts")));
259
+ assert.ok(fs.existsSync(path.join(pkgRoot, "src", "Alpha__factory.ts")));
260
+ assert.ok(fs.existsSync(path.join(pkgRoot, "src", "ConstructorParam.ts")));
261
+ assert.ok(fs.existsSync(path.join(pkgRoot, "src", "ConstructorParam__factory.ts")));
262
+ assert.ok(fs.existsSync(path.join(pkgRoot, "src", "index.ts")));
263
+ assert.ok(fs.existsSync(path.join(pkgRoot, "index.js")));
264
+ assert.ok(fs.existsSync(path.join(pkgRoot, "index.d.ts")));
265
+ assert.ok(fs.existsSync(path.join(pkgRoot, "README.md")));
266
+
267
+ // Verify tests exist for each contract
268
+ assert.ok(fs.existsSync(path.join(pkgRoot, "test", "e2e", "Alpha.e2e.test.js")));
269
+ assert.ok(fs.existsSync(path.join(pkgRoot, "test", "e2e", "ConstructorParam.e2e.test.js")));
270
+ assert.ok(fs.existsSync(path.join(pkgRoot, "test", "e2e", "all-contracts.e2e.test.js")));
271
+
272
+ // Multi-contract packages use per-contract example filenames.
273
+ assert.ok(fs.existsSync(path.join(pkgRoot, "examples", "deploy-Alpha.js")));
274
+ assert.ok(fs.existsSync(path.join(pkgRoot, "examples", "deploy-ConstructorParam.js")));
275
+
276
+ const readme = fs.readFileSync(path.join(pkgRoot, "README.md"), "utf8");
277
+ assert.ok(readme.includes("Alpha"));
278
+ assert.ok(readme.includes("ConstructorParam"));
279
+
280
+ // Install deps + run tests against chain
281
+ if (!fs.existsSync(path.join(pkgRoot, "node_modules"))) {
282
+ const install = runNpm(["install", "--no-fund", "--no-audit"], pkgRoot, process.env);
283
+ assert.equal(install.status, 0, `npm install failed:\n${install.stdout}\n${install.stderr}`);
284
+ }
285
+
286
+ const env = {
287
+ ...process.env,
288
+ QC_RPC_URL: rpcUrl,
289
+ QC_CHAIN_ID: String(chainId),
290
+ };
291
+ const testRun = runNpm(["test"], pkgRoot, env);
292
+ assert.equal(testRun.status, 0, `generated package tests failed:\n${testRun.stdout}\n${testRun.stderr}`);
293
+
294
+ succeeded = true;
295
+ } finally {
296
+ if (succeeded) {
297
+ fs.rmSync(tmpBase, { recursive: true, force: true });
298
+ } else {
299
+ // eslint-disable-next-line no-console
300
+ console.error("Generated package kept at:", pkgRoot);
301
+ }
302
+ }
303
+ }, { timeout: 1_800_000 });
304
+
305
+ it("generates a package from Solidity input (multiple contracts) and runs its transactional tests", async (t) => {
306
+ const rpcUrl = getRpcUrl();
307
+ if (!rpcUrl) {
308
+ t.skip("QC_RPC_URL not provided");
309
+ return;
310
+ }
311
+
312
+ const chainId = getChainId();
313
+ const solcPath = getSolcPath();
314
+ assertSolcExists(solcPath);
315
+
316
+ const repoRoot = path.resolve(__dirname, "..", "..");
317
+ const fixtureSol = path.join(repoRoot, "test", "fixtures", "MultiContracts.sol");
318
+
319
+ const tmpBase = fs.mkdtempSync(path.join(os.tmpdir(), "qcgen-sol-"));
320
+ const pkgName = `qcgen-sol-${Date.now()}`;
321
+ const pkgDir = tmpBase;
322
+ const pkgRoot = path.join(pkgDir, pkgName);
323
+
324
+ let succeeded = false;
325
+ try {
326
+ // Generate package directly from Solidity sources
327
+ const generatorCli = path.join(repoRoot, "generate-sdk.js");
328
+ const gen = run(
329
+ process.execPath,
330
+ [
331
+ generatorCli,
332
+ "--sol",
333
+ fixtureSol,
334
+ "--solc",
335
+ solcPath,
336
+ "--create-package",
337
+ "--package-dir",
338
+ pkgDir,
339
+ "--package-name",
340
+ pkgName,
341
+ "--package-description",
342
+ "Temporary typed-contract package generated from Solidity input",
343
+ "--package-author",
344
+ "quantumcoin.js test",
345
+ "--package-license",
346
+ "MIT",
347
+ "--package-version",
348
+ "0.0.1",
349
+ "--non-interactive",
350
+ ],
351
+ repoRoot,
352
+ process.env,
353
+ );
354
+ assert.equal(gen.status, 0, `generator failed:\n${gen.stdout}\n${gen.stderr}`);
355
+
356
+ // Verify multiple contracts were generated
357
+ assert.ok(fs.existsSync(path.join(pkgRoot, "src", "Alpha.ts")));
358
+ assert.ok(fs.existsSync(path.join(pkgRoot, "src", "Alpha__factory.ts")));
359
+ assert.ok(fs.existsSync(path.join(pkgRoot, "src", "Beta.ts")));
360
+ assert.ok(fs.existsSync(path.join(pkgRoot, "src", "Beta__factory.ts")));
361
+ assert.ok(fs.existsSync(path.join(pkgRoot, "src", "index.ts")));
362
+ assert.ok(fs.existsSync(path.join(pkgRoot, "index.js")));
363
+ assert.ok(fs.existsSync(path.join(pkgRoot, "index.d.ts")));
364
+ assert.ok(fs.existsSync(path.join(pkgRoot, "README.md")));
365
+
366
+ // Verify artifacts were emitted
367
+ assert.ok(fs.existsSync(path.join(pkgRoot, "artifacts", "Alpha.abi.json")));
368
+ assert.ok(fs.existsSync(path.join(pkgRoot, "artifacts", "Alpha.bin")));
369
+ assert.ok(fs.existsSync(path.join(pkgRoot, "artifacts", "Beta.abi.json")));
370
+ assert.ok(fs.existsSync(path.join(pkgRoot, "artifacts", "Beta.bin")));
371
+
372
+ // Verify tests exist for each contract
373
+ assert.ok(fs.existsSync(path.join(pkgRoot, "test", "e2e", "Alpha.e2e.test.js")));
374
+ assert.ok(fs.existsSync(path.join(pkgRoot, "test", "e2e", "Beta.e2e.test.js")));
375
+ assert.ok(fs.existsSync(path.join(pkgRoot, "test", "e2e", "all-contracts.e2e.test.js")));
376
+
377
+ // Verify Solidity comments were propagated into generated TS
378
+ const alphaTs = fs.readFileSync(path.join(pkgRoot, "src", "Alpha.ts"), "utf8");
379
+ assert.ok(alphaTs.includes("Alpha contract for multi-contract generator test."));
380
+ assert.ok(alphaTs.includes("Set a new value in Alpha."));
381
+
382
+ // Install deps + run tests against chain
383
+ if (!fs.existsSync(path.join(pkgRoot, "node_modules"))) {
384
+ const install = runNpm(["install", "--no-fund", "--no-audit"], pkgRoot, process.env);
385
+ assert.equal(install.status, 0, `npm install failed:\n${install.stdout}\n${install.stderr}`);
386
+ }
387
+
388
+ const env = {
389
+ ...process.env,
390
+ QC_RPC_URL: rpcUrl,
391
+ QC_CHAIN_ID: String(chainId),
392
+ };
393
+ const testRun = runNpm(["test"], pkgRoot, env);
394
+ assert.equal(testRun.status, 0, `generated package tests failed:\n${testRun.stdout}\n${testRun.stderr}`);
395
+
396
+ succeeded = true;
397
+ } finally {
398
+ if (succeeded) {
399
+ fs.rmSync(tmpBase, { recursive: true, force: true });
400
+ } else {
401
+ // eslint-disable-next-line no-console
402
+ console.error("Generated package kept at:", pkgRoot);
403
+ }
404
+ }
405
+ }, { timeout: 1_800_000 });
406
+ });
407
+