@hardkas/sdk 0.7.11-alpha → 0.7.13-alpha

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/dist/index.d.ts CHANGED
@@ -287,11 +287,15 @@ declare class HardkasArtifactsManager {
287
287
  /**
288
288
  * Reads an artifact by path or ID/hash from the workspace.
289
289
  */
290
- read(id: string): Promise<any>;
290
+ read(id: string, options?: {
291
+ expectedSchema?: string;
292
+ }): Promise<any>;
291
293
  /**
292
294
  * Alias for read().
293
295
  */
294
- get(id: string): Promise<any>;
296
+ get(id: string, options?: {
297
+ expectedSchema?: string;
298
+ }): Promise<any>;
295
299
  /**
296
300
  * Lists all artifacts in the workspace.
297
301
  */
package/dist/index.js CHANGED
@@ -107,6 +107,37 @@ import {
107
107
  import { coreEvents } from "@hardkas/core";
108
108
  import { signTxPlanArtifact } from "@hardkas/accounts";
109
109
  import { parseKasToSompi } from "@hardkas/core";
110
+ function normalizeSimulatedPlanInput(target, fallbackId) {
111
+ if (target.schema === ARTIFACT_SCHEMAS.TX_PLAN && Array.isArray(target.inputs)) {
112
+ return target;
113
+ }
114
+ if (target.from && target.to && target.amountSompi) {
115
+ if (target.mode !== "simulated") {
116
+ throw new Error("Cannot simulate real signed artifact without parent plan. Missing plan inputs data.");
117
+ }
118
+ return {
119
+ schema: ARTIFACT_SCHEMAS.TX_PLAN,
120
+ planId: target.planId || target.sourcePlanId || fallbackId,
121
+ networkId: target.networkId || "simnet",
122
+ mode: "simulated",
123
+ from: target.from,
124
+ to: target.to,
125
+ amountSompi: target.amountSompi,
126
+ estimatedFeeSompi: "0",
127
+ estimatedMass: "0",
128
+ inputs: [],
129
+ outputs: [{ address: target.to.address, amountSompi: target.amountSompi || "0" }],
130
+ plan: {
131
+ inputs: [],
132
+ outputs: [{ address: target.to.address, amountSompi: BigInt(target.amountSompi || 0) }],
133
+ feeSompi: 0n,
134
+ mass: 0n,
135
+ changeSompi: 0n
136
+ }
137
+ };
138
+ }
139
+ throw new Error("Cannot simulate signed artifact without parent plan or embedded plan data.");
140
+ }
110
141
  var HardkasTx = class {
111
142
  constructor(sdk) {
112
143
  this.sdk = sdk;
@@ -428,7 +459,7 @@ var HardkasTx = class {
428
459
  let targetObj = target;
429
460
  if (typeof target === "string") {
430
461
  try {
431
- targetObj = await this.sdk.artifacts.read(target);
462
+ targetObj = await this.sdk.artifacts.read(target, { expectedSchema: ARTIFACT_SCHEMAS.TX_PLAN });
432
463
  } catch (e) {
433
464
  throw new Error(`Artifact '${target}' not found. If you already have an in-memory artifact, pass the object directly to tx.simulate(artifact).`);
434
465
  }
@@ -438,30 +469,9 @@ var HardkasTx = class {
438
469
  sourcePlanId = targetObj.sourcePlanId || "unknown";
439
470
  txId = targetObj.txId || `simulated-${sourcePlanId}-tx`;
440
471
  try {
441
- planArtifact = await this.sdk.artifacts.read(sourcePlanId);
472
+ planArtifact = await this.sdk.artifacts.read(sourcePlanId, { expectedSchema: ARTIFACT_SCHEMAS.TX_PLAN });
442
473
  } catch (e) {
443
- if (targetObj.from && targetObj.to && targetObj.amountSompi) {
444
- planArtifact = {
445
- schema: "hardkas.txPlan",
446
- planId: sourcePlanId,
447
- from: targetObj.from,
448
- to: targetObj.to,
449
- amountSompi: targetObj.amountSompi,
450
- estimatedFeeSompi: "0",
451
- estimatedMass: "0",
452
- inputs: [],
453
- outputs: [{ address: targetObj.to.address, amountSompi: targetObj.amountSompi || "0" }],
454
- plan: {
455
- inputs: [],
456
- outputs: [{ address: targetObj.to.address, amountSompi: BigInt(targetObj.amountSompi || 0) }],
457
- feeSompi: 0n,
458
- mass: 0n,
459
- changeSompi: 0n
460
- }
461
- };
462
- } else {
463
- throw new Error(`Cannot simulate signed artifact: source plan '${sourcePlanId}' not found in workspace and artifact lacks details.`);
464
- }
474
+ planArtifact = targetObj;
465
475
  }
466
476
  } else {
467
477
  planArtifact = targetObj;
@@ -472,9 +482,10 @@ var HardkasTx = class {
472
482
  sourcePlanId = planArtifact.planId || "unknown";
473
483
  }
474
484
  }
485
+ const normalizedPlan = normalizeSimulatedPlanInput(planArtifact, sourcePlanId);
475
486
  const simResult = applySimulatedPlan(
476
487
  state,
477
- planArtifact,
488
+ normalizedPlan,
478
489
  systemRuntimeContext,
479
490
  { txId }
480
491
  );
@@ -596,7 +607,34 @@ var HardkasTx = class {
596
607
  if (isSimulated) {
597
608
  const persistOpt = typeof urlOrOptions === "object" ? urlOrOptions.persist : true;
598
609
  const simOpts = persistOpt !== void 0 ? { persist: persistOpt } : {};
599
- const simResult = await this.simulate(signedArtifact, simOpts);
610
+ let simResult;
611
+ try {
612
+ simResult = await this.simulate(signedArtifact, simOpts);
613
+ } catch (e) {
614
+ if (e.message && e.message.includes("invalid simulated input")) {
615
+ try {
616
+ const { loadSimulatedReceipt, getReceiptPath } = await import("@hardkas/localnet");
617
+ const txIdToLoad = signedArtifact.txId || `simulated-${signedArtifact.sourcePlanId}-tx`;
618
+ const existingReceipt = await loadSimulatedReceipt(txIdToLoad, { cwd: this.sdk.workspace.root });
619
+ if (existingReceipt) {
620
+ if (existingReceipt.schema === ARTIFACT_SCHEMAS.TX_RECEIPT && (existingReceipt.status === "confirmed" || existingReceipt.status === "accepted")) {
621
+ return {
622
+ mode: "simulated",
623
+ simulated: true,
624
+ submitted: false,
625
+ txId: existingReceipt.txId,
626
+ artifactId: existingReceipt.txId,
627
+ // simulated receipts use txId as artifactId
628
+ receipt: existingReceipt,
629
+ receiptPath: getReceiptPath(existingReceipt.txId, this.sdk.workspace.root)
630
+ };
631
+ }
632
+ }
633
+ } catch (err) {
634
+ }
635
+ }
636
+ throw e;
637
+ }
600
638
  const result2 = {
601
639
  mode: "simulated",
602
640
  simulated: true,
@@ -1256,9 +1294,13 @@ var HardkasArtifactsManager = class {
1256
1294
  /**
1257
1295
  * Reads an artifact by path or ID/hash from the workspace.
1258
1296
  */
1259
- async read(id) {
1297
+ async read(id, options) {
1260
1298
  if (this.cache.has(id)) {
1261
- return this.cache.get(id);
1299
+ const cached = this.cache.get(id);
1300
+ if (options?.expectedSchema && cached.schema !== options.expectedSchema) {
1301
+ throw new Error(`Artifact ${id} has schema '${cached.schema}' but expected '${options.expectedSchema}'`);
1302
+ }
1303
+ return cached;
1262
1304
  }
1263
1305
  const { readArtifact } = await import("@hardkas/artifacts");
1264
1306
  let filePath = id;
@@ -1276,7 +1318,7 @@ var HardkasArtifactsManager = class {
1276
1318
  if (!fs3.existsSync(filePath)) {
1277
1319
  if (fs3.existsSync(this.workspace.artifactsDir)) {
1278
1320
  const files = fs3.readdirSync(this.workspace.artifactsDir);
1279
- const found = files.find((f) => f.includes(id) || f.endsWith(`${id}.json`));
1321
+ const found = files.find((f) => f === `${id}.json` || f.startsWith(`${id}-`) || f.startsWith(`${id}.`));
1280
1322
  if (found) {
1281
1323
  filePath = path3.join(this.workspace.artifactsDir, found);
1282
1324
  } else {
@@ -1287,13 +1329,17 @@ var HardkasArtifactsManager = class {
1287
1329
  }
1288
1330
  }
1289
1331
  }
1290
- return readArtifact(filePath);
1332
+ const artifact = await readArtifact(filePath);
1333
+ if (options?.expectedSchema && artifact.schema !== options.expectedSchema) {
1334
+ throw new Error(`Artifact ${id} has schema '${artifact.schema}' but expected '${options.expectedSchema}'`);
1335
+ }
1336
+ return artifact;
1291
1337
  }
1292
1338
  /**
1293
1339
  * Alias for read().
1294
1340
  */
1295
- async get(id) {
1296
- return this.read(id);
1341
+ async get(id, options) {
1342
+ return this.read(id, options);
1297
1343
  }
1298
1344
  /**
1299
1345
  * Lists all artifacts in the workspace.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hardkas/sdk",
3
- "version": "0.7.11-alpha",
3
+ "version": "0.7.13-alpha",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",
@@ -23,18 +23,18 @@
23
23
  }
24
24
  },
25
25
  "dependencies": {
26
- "@hardkas/accounts": "0.7.11-alpha",
27
- "@hardkas/artifacts": "0.7.11-alpha",
28
- "@hardkas/core": "0.7.11-alpha",
29
- "@hardkas/config": "0.7.11-alpha",
30
- "@hardkas/kaspa-rpc": "0.7.11-alpha",
31
- "@hardkas/localnet": "0.7.11-alpha",
32
- "@hardkas/l2": "0.7.11-alpha",
33
- "@hardkas/simulator": "0.7.11-alpha",
34
- "@hardkas/query": "0.7.11-alpha",
35
- "@hardkas/tx-builder": "0.7.11-alpha",
36
- "@hardkas/wallet-adapter": "0.7.11-alpha",
37
- "@hardkas/query-store": "0.7.11-alpha"
26
+ "@hardkas/accounts": "0.7.13-alpha",
27
+ "@hardkas/core": "0.7.13-alpha",
28
+ "@hardkas/artifacts": "0.7.13-alpha",
29
+ "@hardkas/config": "0.7.13-alpha",
30
+ "@hardkas/l2": "0.7.13-alpha",
31
+ "@hardkas/kaspa-rpc": "0.7.13-alpha",
32
+ "@hardkas/query": "0.7.13-alpha",
33
+ "@hardkas/simulator": "0.7.13-alpha",
34
+ "@hardkas/tx-builder": "0.7.13-alpha",
35
+ "@hardkas/wallet-adapter": "0.7.13-alpha",
36
+ "@hardkas/localnet": "0.7.13-alpha",
37
+ "@hardkas/query-store": "0.7.13-alpha"
38
38
  },
39
39
  "devDependencies": {
40
40
  "tsup": "^8.3.5",