@meshsdk/transaction 1.9.0-beta.0 → 1.9.0-beta.10

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.cjs CHANGED
@@ -40,6 +40,7 @@ __export(index_exports, {
40
40
  module.exports = __toCommonJS(index_exports);
41
41
 
42
42
  // src/mesh-tx-builder/index.ts
43
+ var import_common2 = require("@meshsdk/common");
43
44
  var import_core_cst = require("@meshsdk/core-cst");
44
45
 
45
46
  // src/mesh-tx-builder/tx-builder-core.ts
@@ -1256,7 +1257,8 @@ var MeshTxBuilderCore = class {
1256
1257
  txHash,
1257
1258
  txIndex,
1258
1259
  scriptHash,
1259
- scriptSize
1260
+ scriptSize,
1261
+ version
1260
1262
  },
1261
1263
  redeemer: currentCert.type === "ScriptCertificate" ? currentCert.redeemer : void 0
1262
1264
  });
@@ -1383,6 +1385,41 @@ var MeshTxBuilderCore = class {
1383
1385
  this.meshTxBuilderBody.network = network;
1384
1386
  return this;
1385
1387
  };
1388
+ /**
1389
+ * Add a transaction that is used as input, but not yet reflected on the global blockchain
1390
+ * @param txHex The transaction hex of chained transaction
1391
+ * @returns The MeshTxBuilderCore instance
1392
+ */
1393
+ chainTx(txHex) {
1394
+ this.meshTxBuilderBody.chainedTxs.push(txHex);
1395
+ return this;
1396
+ }
1397
+ /**
1398
+ * Add a transaction input to provide information for offline evaluation
1399
+ * @param input The input to be added
1400
+ * @returns The MeshTxBuilderCore instance
1401
+ */
1402
+ inputForEvaluation(input) {
1403
+ const utxoId = `${input.input.txHash}${input.input.outputIndex}`;
1404
+ const currentUtxo = this.meshTxBuilderBody.inputsForEvaluation[utxoId];
1405
+ if (currentUtxo) {
1406
+ const {
1407
+ dataHash: currentDataHash,
1408
+ plutusData: currentPlutusData,
1409
+ scriptRef: currentScriptRef,
1410
+ scriptHash: currentScriptHash
1411
+ } = currentUtxo.output;
1412
+ const updatedUtxo = { ...currentUtxo };
1413
+ if (currentDataHash) updatedUtxo.output.dataHash = currentDataHash;
1414
+ if (currentPlutusData) updatedUtxo.output.plutusData = currentPlutusData;
1415
+ if (currentScriptRef) updatedUtxo.output.scriptRef = currentScriptRef;
1416
+ if (currentScriptHash) updatedUtxo.output.scriptHash = currentScriptHash;
1417
+ this.meshTxBuilderBody.inputsForEvaluation[utxoId] = updatedUtxo;
1418
+ } else {
1419
+ this.meshTxBuilderBody.inputsForEvaluation[utxoId] = input;
1420
+ }
1421
+ return this;
1422
+ }
1386
1423
  queueAllLastItem = () => {
1387
1424
  if (this.txOutput) {
1388
1425
  this.meshTxBuilderBody.outputs.push(this.txOutput);
@@ -1648,6 +1685,7 @@ var MeshTxBuilderCore = class {
1648
1685
  scriptSize: input.output.scriptRef.length / 2
1649
1686
  });
1650
1687
  }
1688
+ this.inputForEvaluation(input);
1651
1689
  });
1652
1690
  };
1653
1691
  removeDuplicateInputs = () => {
@@ -1722,16 +1760,16 @@ var MeshTxBuilder = class extends MeshTxBuilderCore {
1722
1760
  verbose = false
1723
1761
  } = {}) {
1724
1762
  super();
1763
+ if (fetcher) this.fetcher = fetcher;
1764
+ if (submitter) this.submitter = submitter;
1765
+ if (evaluator) this.evaluator = evaluator;
1766
+ if (params) this.protocolParams(params);
1725
1767
  if (serializer) {
1726
1768
  this.serializer = serializer;
1727
1769
  } else {
1728
- this.serializer = new import_core_cst.CardanoSDKSerializer();
1770
+ this.serializer = new import_core_cst.CardanoSDKSerializer(this._protocolParams);
1729
1771
  }
1730
1772
  this.serializer.verbose = verbose;
1731
- if (fetcher) this.fetcher = fetcher;
1732
- if (submitter) this.submitter = submitter;
1733
- if (evaluator) this.evaluator = evaluator;
1734
- if (params) this.protocolParams(params);
1735
1773
  if (isHydra)
1736
1774
  this.protocolParams({
1737
1775
  minFeeA: 0,
@@ -1747,7 +1785,7 @@ var MeshTxBuilder = class extends MeshTxBuilderCore {
1747
1785
  * @param customizedTx The optional customized transaction body
1748
1786
  * @returns The signed transaction in hex ready to submit / signed by client
1749
1787
  */
1750
- complete = async (customizedTx) => {
1788
+ complete = async (customizedTx, balanced = true) => {
1751
1789
  if (customizedTx) {
1752
1790
  this.meshTxBuilderBody = { ...this.meshTxBuilderBody, ...customizedTx };
1753
1791
  } else {
@@ -1757,25 +1795,93 @@ var MeshTxBuilder = class extends MeshTxBuilderCore {
1757
1795
  for (let collateral of this.meshTxBuilderBody.collaterals) {
1758
1796
  collateral.txIn.scriptSize = 0;
1759
1797
  }
1760
- const { inputs, collaterals, mints } = this.meshTxBuilderBody;
1798
+ const { inputs, collaterals, mints, withdrawals, votes, certificates } = this.meshTxBuilderBody;
1761
1799
  const incompleteTxIns = [...inputs, ...collaterals].filter(
1762
1800
  (txIn) => !this.isInputComplete(txIn)
1763
1801
  );
1764
- const incompleteMints = mints.filter((mint) => !this.isMintComplete(mint));
1765
- await this.queryAllTxInfo(incompleteTxIns, incompleteMints);
1766
- incompleteTxIns.forEach((txIn) => {
1767
- this.completeTxInformation(txIn);
1802
+ let incompleteScriptSources = [];
1803
+ let incompleteSimpleScriptSources = [];
1804
+ inputs.forEach((txIn) => {
1805
+ if (txIn.type === "Script" && txIn.scriptTxIn.scriptSource?.type === "Inline") {
1806
+ if (!this.isRefScriptInfoComplete(txIn.scriptTxIn.scriptSource)) {
1807
+ incompleteScriptSources.push(txIn.scriptTxIn.scriptSource);
1808
+ }
1809
+ } else if (txIn.type === "SimpleScript" && txIn.simpleScriptTxIn?.scriptSource?.type === "Inline") {
1810
+ if (!this.isSimpleRefScriptInfoComplete(
1811
+ txIn.simpleScriptTxIn.scriptSource
1812
+ )) {
1813
+ incompleteSimpleScriptSources.push(
1814
+ txIn.simpleScriptTxIn.scriptSource
1815
+ );
1816
+ }
1817
+ }
1768
1818
  });
1769
- incompleteMints.forEach((mint) => {
1819
+ mints.forEach((mint) => {
1770
1820
  if (mint.type === "Plutus") {
1771
1821
  const scriptSource = mint.scriptSource;
1772
- this.completeScriptInfo(scriptSource);
1773
- }
1774
- if (mint.type === "Native") {
1822
+ if (!this.isRefScriptInfoComplete(scriptSource)) {
1823
+ incompleteScriptSources.push(scriptSource);
1824
+ }
1825
+ } else if (mint.type === "Native") {
1775
1826
  const scriptSource = mint.scriptSource;
1776
- this.completeSimpleScriptInfo(scriptSource);
1827
+ if (!this.isSimpleRefScriptInfoComplete(scriptSource)) {
1828
+ incompleteSimpleScriptSources.push(scriptSource);
1829
+ }
1830
+ }
1831
+ });
1832
+ withdrawals.forEach((withdrawal) => {
1833
+ if (withdrawal.type === "ScriptWithdrawal") {
1834
+ const scriptSource = withdrawal.scriptSource;
1835
+ if (!this.isRefScriptInfoComplete(scriptSource)) {
1836
+ incompleteScriptSources.push(scriptSource);
1837
+ }
1838
+ } else if (withdrawal.type === "SimpleScriptWithdrawal") {
1839
+ const scriptSource = withdrawal.scriptSource;
1840
+ if (!this.isSimpleRefScriptInfoComplete(scriptSource)) {
1841
+ incompleteSimpleScriptSources.push(scriptSource);
1842
+ }
1843
+ }
1844
+ });
1845
+ votes.forEach((vote) => {
1846
+ if (vote.type === "ScriptVote") {
1847
+ const scriptSource = vote.scriptSource;
1848
+ if (!this.isRefScriptInfoComplete(scriptSource)) {
1849
+ incompleteScriptSources.push(scriptSource);
1850
+ }
1851
+ } else if (vote.type === "SimpleScriptVote") {
1852
+ const scriptSource = vote.simpleScriptSource;
1853
+ if (!this.isSimpleRefScriptInfoComplete(scriptSource)) {
1854
+ incompleteSimpleScriptSources.push(scriptSource);
1855
+ }
1777
1856
  }
1778
1857
  });
1858
+ certificates.forEach((certificate) => {
1859
+ if (certificate.type === "ScriptCertificate") {
1860
+ const scriptSource = certificate.scriptSource;
1861
+ if (!this.isRefScriptInfoComplete(scriptSource)) {
1862
+ incompleteScriptSources.push(scriptSource);
1863
+ }
1864
+ } else if (certificate.type === "SimpleScriptCertificate") {
1865
+ const scriptSource = certificate.simpleScriptSource;
1866
+ if (!this.isSimpleRefScriptInfoComplete(scriptSource)) {
1867
+ incompleteSimpleScriptSources.push(scriptSource);
1868
+ }
1869
+ }
1870
+ });
1871
+ await this.queryAllTxInfo(
1872
+ incompleteTxIns,
1873
+ incompleteScriptSources,
1874
+ incompleteSimpleScriptSources
1875
+ );
1876
+ incompleteTxIns.forEach((txIn) => {
1877
+ this.completeTxInformation(txIn);
1878
+ });
1879
+ incompleteScriptSources.forEach((scriptSource) => {
1880
+ this.completeScriptInfo(scriptSource);
1881
+ });
1882
+ incompleteSimpleScriptSources.forEach((simpleScriptSource) => {
1883
+ this.completeSimpleScriptInfo(simpleScriptSource);
1884
+ });
1779
1885
  this.meshTxBuilderBody.inputs.forEach((input) => {
1780
1886
  if (input.txIn.scriptSize && input.txIn.scriptSize > 0) {
1781
1887
  if (this.meshTxBuilderBody.referenceInputs.find((refTxIn) => {
@@ -1806,17 +1912,30 @@ var MeshTxBuilder = class extends MeshTxBuilderCore {
1806
1912
  });
1807
1913
  let txHex = this.serializer.serializeTxBody(
1808
1914
  this.meshTxBuilderBody,
1809
- this._protocolParams
1915
+ this._protocolParams,
1916
+ balanced
1810
1917
  );
1811
1918
  if (this.evaluator) {
1812
- const txEvaluation = await this.evaluator.evaluateTx(txHex).catch((error) => {
1813
- throw Error(`Tx evaluation failed: ${error}
1814
- For txHex: ${txHex}`);
1919
+ const txEvaluation = await this.evaluator.evaluateTx(
1920
+ txHex,
1921
+ Object.values(this.meshTxBuilderBody.inputsForEvaluation).concat(
1922
+ this.meshTxBuilderBody.inputs.map((val) => (0, import_common2.txInToUtxo)(val.txIn)),
1923
+ this.meshTxBuilderBody.collaterals.map(
1924
+ (val) => (0, import_common2.txInToUtxo)(val.txIn)
1925
+ )
1926
+ ),
1927
+ this.meshTxBuilderBody.chainedTxs
1928
+ ).catch((error) => {
1929
+ throw new Error(
1930
+ `Tx evaluation failed: ${JSON.stringify(error)}
1931
+ For txHex: ${txHex}`
1932
+ );
1815
1933
  });
1816
1934
  this.updateRedeemer(this.meshTxBuilderBody, txEvaluation);
1817
1935
  txHex = this.serializer.serializeTxBody(
1818
1936
  this.meshTxBuilderBody,
1819
- this._protocolParams
1937
+ this._protocolParams,
1938
+ balanced
1820
1939
  );
1821
1940
  }
1822
1941
  this.txHex = txHex;
@@ -1827,7 +1946,7 @@ var MeshTxBuilder = class extends MeshTxBuilderCore {
1827
1946
  * @param customizedTx The optional customized transaction body
1828
1947
  * @returns The signed transaction in hex ready to submit / signed by client
1829
1948
  */
1830
- completeSync = (customizedTx) => {
1949
+ completeSync = (customizedTx, balanced = true) => {
1831
1950
  if (customizedTx) {
1832
1951
  this.meshTxBuilderBody = customizedTx;
1833
1952
  } else {
@@ -1836,7 +1955,8 @@ var MeshTxBuilder = class extends MeshTxBuilderCore {
1836
1955
  this.addUtxosFromSelection();
1837
1956
  return this.serializer.serializeTxBody(
1838
1957
  this.meshTxBuilderBody,
1839
- this._protocolParams
1958
+ this._protocolParams,
1959
+ balanced
1840
1960
  );
1841
1961
  };
1842
1962
  /**
@@ -1872,9 +1992,9 @@ var MeshTxBuilder = class extends MeshTxBuilderCore {
1872
1992
  this.queriedUTxOs[txHash] = utxos;
1873
1993
  }
1874
1994
  };
1875
- queryAllTxInfo = (incompleteTxIns, incompleteMints) => {
1995
+ queryAllTxInfo = (incompleteTxIns, incompleteScriptSources, incompleteSimpleScriptSources) => {
1876
1996
  const queryUTxOPromises = [];
1877
- if ((incompleteTxIns.length > 0 || incompleteMints.length > 0) && !this.fetcher)
1997
+ if ((incompleteTxIns.length > 0 || incompleteScriptSources.length > 0 || incompleteSimpleScriptSources.length) && !this.fetcher)
1878
1998
  throw Error(
1879
1999
  "Transaction information is incomplete while no fetcher instance is provided. Provide a `fetcher`."
1880
2000
  );
@@ -1883,21 +2003,11 @@ var MeshTxBuilder = class extends MeshTxBuilderCore {
1883
2003
  if (!this.isInputInfoComplete(currentTxIn)) {
1884
2004
  queryUTxOPromises.push(this.getUTxOInfo(currentTxIn.txIn.txHash));
1885
2005
  }
1886
- if (currentTxIn.type === "Script" && currentTxIn.scriptTxIn.scriptSource?.type === "Inline" && !this.isRefScriptInfoComplete(currentTxIn.scriptTxIn.scriptSource)) {
1887
- queryUTxOPromises.push(
1888
- this.getUTxOInfo(currentTxIn.scriptTxIn.scriptSource.txHash)
1889
- );
1890
- }
1891
2006
  }
1892
- for (let i = 0; i < incompleteMints.length; i++) {
1893
- const currentMint = incompleteMints[i];
1894
- if (currentMint.type === "Plutus") {
1895
- const scriptSource = currentMint.scriptSource;
1896
- if (scriptSource.type === "Inline") {
1897
- if (!this.isRefScriptInfoComplete(scriptSource)) {
1898
- queryUTxOPromises.push(this.getUTxOInfo(scriptSource.txHash));
1899
- }
1900
- }
2007
+ for (let i = 0; i < incompleteScriptSources.length; i++) {
2008
+ const scriptSource = incompleteScriptSources[i];
2009
+ if (scriptSource.type === "Inline") {
2010
+ queryUTxOPromises.push(this.getUTxOInfo(scriptSource.txHash));
1901
2011
  }
1902
2012
  }
1903
2013
  return Promise.all(queryUTxOPromises);
@@ -1991,6 +2101,13 @@ var MeshTxBuilder = class extends MeshTxBuilderCore {
1991
2101
  }
1992
2102
  return true;
1993
2103
  };
2104
+ isSimpleRefScriptInfoComplete = (simpleScriptSource) => {
2105
+ if (simpleScriptSource.type === "Inline") {
2106
+ if (!simpleScriptSource.simpleScriptHash || !simpleScriptSource.scriptSize)
2107
+ return false;
2108
+ }
2109
+ return true;
2110
+ };
1994
2111
  };
1995
2112
 
1996
2113
  // src/scripts/forge.script.ts
@@ -2041,7 +2158,7 @@ var ForgeScript = class {
2041
2158
  };
2042
2159
 
2043
2160
  // src/transaction/index.ts
2044
- var import_common2 = require("@meshsdk/common");
2161
+ var import_common3 = require("@meshsdk/common");
2045
2162
  var import_core_cst3 = require("@meshsdk/core-cst");
2046
2163
  var Transaction = class {
2047
2164
  txBuilder;
@@ -2165,7 +2282,7 @@ var Transaction = class {
2165
2282
  * @see {@link https://meshjs.dev/apis/transaction#sendToken}
2166
2283
  */
2167
2284
  sendToken(recipient, ticker, amount) {
2168
- const assets = [{ unit: import_common2.SUPPORTED_TOKENS[ticker], quantity: amount }];
2285
+ const assets = [{ unit: import_common3.SUPPORTED_TOKENS[ticker], quantity: amount }];
2169
2286
  return this.sendAssets(recipient, assets);
2170
2287
  }
2171
2288
  /**
@@ -2242,7 +2359,7 @@ var Transaction = class {
2242
2359
  const { value, script, datum, redeemer } = options;
2243
2360
  const red = redeemer || {
2244
2361
  data: { alternative: 0, fields: ["mesh"] },
2245
- budget: import_common2.DEFAULT_REDEEMER_BUDGET
2362
+ budget: import_common3.DEFAULT_REDEEMER_BUDGET
2246
2363
  };
2247
2364
  if ("code" in script) {
2248
2365
  this.isCollateralNeeded = true;
@@ -2250,7 +2367,8 @@ var Transaction = class {
2250
2367
  value.input.txHash,
2251
2368
  value.input.outputIndex,
2252
2369
  value.output.amount,
2253
- value.output.address
2370
+ value.output.address,
2371
+ value.output.scriptRef ? value.output.scriptRef.length / 2 : 0
2254
2372
  ).txInScript(script.code).txInRedeemerValue(red.data, "Mesh", red.budget);
2255
2373
  }
2256
2374
  if ("output" in script) {
@@ -2284,10 +2402,10 @@ var Transaction = class {
2284
2402
  // TODO: nuke this probably as the input type is too confusing
2285
2403
  mintAsset(forgeScript, mint, redeemer) {
2286
2404
  const assetQuantity = mint.assetQuantity;
2287
- let assetNameHex = (0, import_common2.stringToHex)(mint.assetName);
2288
- const referenceAssetNameHex = (0, import_common2.CIP68_100)(assetNameHex);
2405
+ let assetNameHex = (0, import_common3.stringToHex)(mint.assetName);
2406
+ const referenceAssetNameHex = (0, import_common3.CIP68_100)(assetNameHex);
2289
2407
  if (mint.cip68ScriptAddress) {
2290
- assetNameHex = (0, import_common2.CIP68_222)(assetNameHex);
2408
+ assetNameHex = (0, import_common3.CIP68_222)(assetNameHex);
2291
2409
  }
2292
2410
  let policyId = "";
2293
2411
  switch (typeof forgeScript) {
@@ -2359,7 +2477,7 @@ var Transaction = class {
2359
2477
  this.sendAssets(
2360
2478
  {
2361
2479
  address: mint.cip68ScriptAddress,
2362
- datum: { inline: true, value: (0, import_common2.metadataToCip68)(mint.metadata) }
2480
+ datum: { inline: true, value: (0, import_common3.metadataToCip68)(mint.metadata) }
2363
2481
  },
2364
2482
  [
2365
2483
  {
@@ -2398,7 +2516,7 @@ var Transaction = class {
2398
2516
  burnAsset(forgeScript, asset, redeemer) {
2399
2517
  const assetQuantity = "-" + asset.quantity;
2400
2518
  const mint = {
2401
- assetName: (0, import_common2.hexToString)(asset.unit.slice(import_common2.POLICY_ID_LENGTH)),
2519
+ assetName: (0, import_common3.hexToString)(asset.unit.slice(import_common3.POLICY_ID_LENGTH)),
2402
2520
  assetQuantity
2403
2521
  };
2404
2522
  try {
@@ -2498,7 +2616,7 @@ var Transaction = class {
2498
2616
  delegateStake(rewardAddress, poolId) {
2499
2617
  this.txBuilder.delegateStakeCertificate(
2500
2618
  rewardAddress,
2501
- this.txBuilder.serializer.resolver.keys.resolveEd25519KeyHash(poolId)
2619
+ this.txBuilder.serializer.deserializer.cert.deserializePoolId(poolId)
2502
2620
  );
2503
2621
  return this;
2504
2622
  }
@@ -2520,12 +2638,12 @@ var Transaction = class {
2520
2638
  this.txBuilder.retirePoolCertificate(poolId, epochNo);
2521
2639
  return this;
2522
2640
  }
2523
- async build() {
2641
+ async build(balanced = true) {
2524
2642
  try {
2525
2643
  await this.addCollateralIfNeeded();
2526
2644
  await this.addTxInputsAsNeeded();
2527
2645
  await this.addChangeAddress();
2528
- return this.txBuilder.complete();
2646
+ return this.txBuilder.complete(void 0, balanced);
2529
2647
  } catch (error) {
2530
2648
  throw new Error(
2531
2649
  `[Transaction] An error occurred during build: ${error}.`
package/dist/index.d.cts CHANGED
@@ -484,6 +484,18 @@ declare class MeshTxBuilderCore {
484
484
  * @returns The MeshTxBuilder instance
485
485
  */
486
486
  setNetwork: (network: Network | number[][]) => this;
487
+ /**
488
+ * Add a transaction that is used as input, but not yet reflected on the global blockchain
489
+ * @param txHex The transaction hex of chained transaction
490
+ * @returns The MeshTxBuilderCore instance
491
+ */
492
+ chainTx(txHex: string): this;
493
+ /**
494
+ * Add a transaction input to provide information for offline evaluation
495
+ * @param input The input to be added
496
+ * @returns The MeshTxBuilderCore instance
497
+ */
498
+ inputForEvaluation(input: UTxO): this;
487
499
  protected queueAllLastItem: () => void;
488
500
  private queueInput;
489
501
  private queueMint;
@@ -534,13 +546,13 @@ declare class MeshTxBuilder extends MeshTxBuilderCore {
534
546
  * @param customizedTx The optional customized transaction body
535
547
  * @returns The signed transaction in hex ready to submit / signed by client
536
548
  */
537
- complete: (customizedTx?: Partial<MeshTxBuilderBody>) => Promise<string>;
549
+ complete: (customizedTx?: Partial<MeshTxBuilderBody>, balanced?: Boolean) => Promise<string>;
538
550
  /**
539
551
  * It builds the transaction without dependencies
540
552
  * @param customizedTx The optional customized transaction body
541
553
  * @returns The signed transaction in hex ready to submit / signed by client
542
554
  */
543
- completeSync: (customizedTx?: MeshTxBuilderBody) => string;
555
+ completeSync: (customizedTx?: MeshTxBuilderBody, balanced?: Boolean) => string;
544
556
  /**
545
557
  * Complete the signing process
546
558
  * @returns The signed transaction in hex
@@ -557,7 +569,7 @@ declare class MeshTxBuilder extends MeshTxBuilderCore {
557
569
  * @param txHash The TxIn object that contains the txHash and txIndex, while missing amount and address information
558
570
  */
559
571
  protected getUTxOInfo: (txHash: string) => Promise<void>;
560
- protected queryAllTxInfo: (incompleteTxIns: TxIn[], incompleteMints: MintItem[]) => Promise<void[]>;
572
+ protected queryAllTxInfo: (incompleteTxIns: TxIn[], incompleteScriptSources: ScriptSource[], incompleteSimpleScriptSources: SimpleScriptSourceInfo[]) => Promise<void[]>;
561
573
  protected completeTxInformation: (input: TxIn) => void;
562
574
  protected completeInputInfo: (input: TxIn) => void;
563
575
  protected completeScriptInfo: (scriptSource: ScriptSource) => void;
@@ -566,6 +578,7 @@ declare class MeshTxBuilder extends MeshTxBuilderCore {
566
578
  protected isInputInfoComplete: (txIn: TxIn) => boolean;
567
579
  protected isMintComplete: (mint: MintItem) => boolean;
568
580
  protected isRefScriptInfoComplete: (scriptSource: ScriptSource) => boolean;
581
+ protected isSimpleRefScriptInfoComplete: (simpleScriptSource: SimpleScriptSourceInfo) => boolean;
569
582
  }
570
583
 
571
584
  declare class ForgeScript {
@@ -735,7 +748,7 @@ declare class Transaction {
735
748
  registerStake(rewardAddress: string): Transaction;
736
749
  registerPool(params: PoolParams): Transaction;
737
750
  retirePool(poolId: string, epochNo: number): Transaction;
738
- build(): Promise<string>;
751
+ build(balanced?: Boolean): Promise<string>;
739
752
  protected mintPlutusScript(script: PlutusScript): MeshTxBuilder;
740
753
  protected spendingPlutusScript(script: PlutusScript): MeshTxBuilder;
741
754
  private addCollateralIfNeeded;
package/dist/index.d.ts CHANGED
@@ -484,6 +484,18 @@ declare class MeshTxBuilderCore {
484
484
  * @returns The MeshTxBuilder instance
485
485
  */
486
486
  setNetwork: (network: Network | number[][]) => this;
487
+ /**
488
+ * Add a transaction that is used as input, but not yet reflected on the global blockchain
489
+ * @param txHex The transaction hex of chained transaction
490
+ * @returns The MeshTxBuilderCore instance
491
+ */
492
+ chainTx(txHex: string): this;
493
+ /**
494
+ * Add a transaction input to provide information for offline evaluation
495
+ * @param input The input to be added
496
+ * @returns The MeshTxBuilderCore instance
497
+ */
498
+ inputForEvaluation(input: UTxO): this;
487
499
  protected queueAllLastItem: () => void;
488
500
  private queueInput;
489
501
  private queueMint;
@@ -534,13 +546,13 @@ declare class MeshTxBuilder extends MeshTxBuilderCore {
534
546
  * @param customizedTx The optional customized transaction body
535
547
  * @returns The signed transaction in hex ready to submit / signed by client
536
548
  */
537
- complete: (customizedTx?: Partial<MeshTxBuilderBody>) => Promise<string>;
549
+ complete: (customizedTx?: Partial<MeshTxBuilderBody>, balanced?: Boolean) => Promise<string>;
538
550
  /**
539
551
  * It builds the transaction without dependencies
540
552
  * @param customizedTx The optional customized transaction body
541
553
  * @returns The signed transaction in hex ready to submit / signed by client
542
554
  */
543
- completeSync: (customizedTx?: MeshTxBuilderBody) => string;
555
+ completeSync: (customizedTx?: MeshTxBuilderBody, balanced?: Boolean) => string;
544
556
  /**
545
557
  * Complete the signing process
546
558
  * @returns The signed transaction in hex
@@ -557,7 +569,7 @@ declare class MeshTxBuilder extends MeshTxBuilderCore {
557
569
  * @param txHash The TxIn object that contains the txHash and txIndex, while missing amount and address information
558
570
  */
559
571
  protected getUTxOInfo: (txHash: string) => Promise<void>;
560
- protected queryAllTxInfo: (incompleteTxIns: TxIn[], incompleteMints: MintItem[]) => Promise<void[]>;
572
+ protected queryAllTxInfo: (incompleteTxIns: TxIn[], incompleteScriptSources: ScriptSource[], incompleteSimpleScriptSources: SimpleScriptSourceInfo[]) => Promise<void[]>;
561
573
  protected completeTxInformation: (input: TxIn) => void;
562
574
  protected completeInputInfo: (input: TxIn) => void;
563
575
  protected completeScriptInfo: (scriptSource: ScriptSource) => void;
@@ -566,6 +578,7 @@ declare class MeshTxBuilder extends MeshTxBuilderCore {
566
578
  protected isInputInfoComplete: (txIn: TxIn) => boolean;
567
579
  protected isMintComplete: (mint: MintItem) => boolean;
568
580
  protected isRefScriptInfoComplete: (scriptSource: ScriptSource) => boolean;
581
+ protected isSimpleRefScriptInfoComplete: (simpleScriptSource: SimpleScriptSourceInfo) => boolean;
569
582
  }
570
583
 
571
584
  declare class ForgeScript {
@@ -735,7 +748,7 @@ declare class Transaction {
735
748
  registerStake(rewardAddress: string): Transaction;
736
749
  registerPool(params: PoolParams): Transaction;
737
750
  retirePool(poolId: string, epochNo: number): Transaction;
738
- build(): Promise<string>;
751
+ build(balanced?: Boolean): Promise<string>;
739
752
  protected mintPlutusScript(script: PlutusScript): MeshTxBuilder;
740
753
  protected spendingPlutusScript(script: PlutusScript): MeshTxBuilder;
741
754
  private addCollateralIfNeeded;
package/dist/index.js CHANGED
@@ -1,4 +1,7 @@
1
1
  // src/mesh-tx-builder/index.ts
2
+ import {
3
+ txInToUtxo
4
+ } from "@meshsdk/common";
2
5
  import { CardanoSDKSerializer } from "@meshsdk/core-cst";
3
6
 
4
7
  // src/mesh-tx-builder/tx-builder-core.ts
@@ -1221,7 +1224,8 @@ var MeshTxBuilderCore = class {
1221
1224
  txHash,
1222
1225
  txIndex,
1223
1226
  scriptHash,
1224
- scriptSize
1227
+ scriptSize,
1228
+ version
1225
1229
  },
1226
1230
  redeemer: currentCert.type === "ScriptCertificate" ? currentCert.redeemer : void 0
1227
1231
  });
@@ -1348,6 +1352,41 @@ var MeshTxBuilderCore = class {
1348
1352
  this.meshTxBuilderBody.network = network;
1349
1353
  return this;
1350
1354
  };
1355
+ /**
1356
+ * Add a transaction that is used as input, but not yet reflected on the global blockchain
1357
+ * @param txHex The transaction hex of chained transaction
1358
+ * @returns The MeshTxBuilderCore instance
1359
+ */
1360
+ chainTx(txHex) {
1361
+ this.meshTxBuilderBody.chainedTxs.push(txHex);
1362
+ return this;
1363
+ }
1364
+ /**
1365
+ * Add a transaction input to provide information for offline evaluation
1366
+ * @param input The input to be added
1367
+ * @returns The MeshTxBuilderCore instance
1368
+ */
1369
+ inputForEvaluation(input) {
1370
+ const utxoId = `${input.input.txHash}${input.input.outputIndex}`;
1371
+ const currentUtxo = this.meshTxBuilderBody.inputsForEvaluation[utxoId];
1372
+ if (currentUtxo) {
1373
+ const {
1374
+ dataHash: currentDataHash,
1375
+ plutusData: currentPlutusData,
1376
+ scriptRef: currentScriptRef,
1377
+ scriptHash: currentScriptHash
1378
+ } = currentUtxo.output;
1379
+ const updatedUtxo = { ...currentUtxo };
1380
+ if (currentDataHash) updatedUtxo.output.dataHash = currentDataHash;
1381
+ if (currentPlutusData) updatedUtxo.output.plutusData = currentPlutusData;
1382
+ if (currentScriptRef) updatedUtxo.output.scriptRef = currentScriptRef;
1383
+ if (currentScriptHash) updatedUtxo.output.scriptHash = currentScriptHash;
1384
+ this.meshTxBuilderBody.inputsForEvaluation[utxoId] = updatedUtxo;
1385
+ } else {
1386
+ this.meshTxBuilderBody.inputsForEvaluation[utxoId] = input;
1387
+ }
1388
+ return this;
1389
+ }
1351
1390
  queueAllLastItem = () => {
1352
1391
  if (this.txOutput) {
1353
1392
  this.meshTxBuilderBody.outputs.push(this.txOutput);
@@ -1613,6 +1652,7 @@ var MeshTxBuilderCore = class {
1613
1652
  scriptSize: input.output.scriptRef.length / 2
1614
1653
  });
1615
1654
  }
1655
+ this.inputForEvaluation(input);
1616
1656
  });
1617
1657
  };
1618
1658
  removeDuplicateInputs = () => {
@@ -1687,16 +1727,16 @@ var MeshTxBuilder = class extends MeshTxBuilderCore {
1687
1727
  verbose = false
1688
1728
  } = {}) {
1689
1729
  super();
1730
+ if (fetcher) this.fetcher = fetcher;
1731
+ if (submitter) this.submitter = submitter;
1732
+ if (evaluator) this.evaluator = evaluator;
1733
+ if (params) this.protocolParams(params);
1690
1734
  if (serializer) {
1691
1735
  this.serializer = serializer;
1692
1736
  } else {
1693
- this.serializer = new CardanoSDKSerializer();
1737
+ this.serializer = new CardanoSDKSerializer(this._protocolParams);
1694
1738
  }
1695
1739
  this.serializer.verbose = verbose;
1696
- if (fetcher) this.fetcher = fetcher;
1697
- if (submitter) this.submitter = submitter;
1698
- if (evaluator) this.evaluator = evaluator;
1699
- if (params) this.protocolParams(params);
1700
1740
  if (isHydra)
1701
1741
  this.protocolParams({
1702
1742
  minFeeA: 0,
@@ -1712,7 +1752,7 @@ var MeshTxBuilder = class extends MeshTxBuilderCore {
1712
1752
  * @param customizedTx The optional customized transaction body
1713
1753
  * @returns The signed transaction in hex ready to submit / signed by client
1714
1754
  */
1715
- complete = async (customizedTx) => {
1755
+ complete = async (customizedTx, balanced = true) => {
1716
1756
  if (customizedTx) {
1717
1757
  this.meshTxBuilderBody = { ...this.meshTxBuilderBody, ...customizedTx };
1718
1758
  } else {
@@ -1722,25 +1762,93 @@ var MeshTxBuilder = class extends MeshTxBuilderCore {
1722
1762
  for (let collateral of this.meshTxBuilderBody.collaterals) {
1723
1763
  collateral.txIn.scriptSize = 0;
1724
1764
  }
1725
- const { inputs, collaterals, mints } = this.meshTxBuilderBody;
1765
+ const { inputs, collaterals, mints, withdrawals, votes, certificates } = this.meshTxBuilderBody;
1726
1766
  const incompleteTxIns = [...inputs, ...collaterals].filter(
1727
1767
  (txIn) => !this.isInputComplete(txIn)
1728
1768
  );
1729
- const incompleteMints = mints.filter((mint) => !this.isMintComplete(mint));
1730
- await this.queryAllTxInfo(incompleteTxIns, incompleteMints);
1731
- incompleteTxIns.forEach((txIn) => {
1732
- this.completeTxInformation(txIn);
1769
+ let incompleteScriptSources = [];
1770
+ let incompleteSimpleScriptSources = [];
1771
+ inputs.forEach((txIn) => {
1772
+ if (txIn.type === "Script" && txIn.scriptTxIn.scriptSource?.type === "Inline") {
1773
+ if (!this.isRefScriptInfoComplete(txIn.scriptTxIn.scriptSource)) {
1774
+ incompleteScriptSources.push(txIn.scriptTxIn.scriptSource);
1775
+ }
1776
+ } else if (txIn.type === "SimpleScript" && txIn.simpleScriptTxIn?.scriptSource?.type === "Inline") {
1777
+ if (!this.isSimpleRefScriptInfoComplete(
1778
+ txIn.simpleScriptTxIn.scriptSource
1779
+ )) {
1780
+ incompleteSimpleScriptSources.push(
1781
+ txIn.simpleScriptTxIn.scriptSource
1782
+ );
1783
+ }
1784
+ }
1733
1785
  });
1734
- incompleteMints.forEach((mint) => {
1786
+ mints.forEach((mint) => {
1735
1787
  if (mint.type === "Plutus") {
1736
1788
  const scriptSource = mint.scriptSource;
1737
- this.completeScriptInfo(scriptSource);
1738
- }
1739
- if (mint.type === "Native") {
1789
+ if (!this.isRefScriptInfoComplete(scriptSource)) {
1790
+ incompleteScriptSources.push(scriptSource);
1791
+ }
1792
+ } else if (mint.type === "Native") {
1740
1793
  const scriptSource = mint.scriptSource;
1741
- this.completeSimpleScriptInfo(scriptSource);
1794
+ if (!this.isSimpleRefScriptInfoComplete(scriptSource)) {
1795
+ incompleteSimpleScriptSources.push(scriptSource);
1796
+ }
1742
1797
  }
1743
1798
  });
1799
+ withdrawals.forEach((withdrawal) => {
1800
+ if (withdrawal.type === "ScriptWithdrawal") {
1801
+ const scriptSource = withdrawal.scriptSource;
1802
+ if (!this.isRefScriptInfoComplete(scriptSource)) {
1803
+ incompleteScriptSources.push(scriptSource);
1804
+ }
1805
+ } else if (withdrawal.type === "SimpleScriptWithdrawal") {
1806
+ const scriptSource = withdrawal.scriptSource;
1807
+ if (!this.isSimpleRefScriptInfoComplete(scriptSource)) {
1808
+ incompleteSimpleScriptSources.push(scriptSource);
1809
+ }
1810
+ }
1811
+ });
1812
+ votes.forEach((vote) => {
1813
+ if (vote.type === "ScriptVote") {
1814
+ const scriptSource = vote.scriptSource;
1815
+ if (!this.isRefScriptInfoComplete(scriptSource)) {
1816
+ incompleteScriptSources.push(scriptSource);
1817
+ }
1818
+ } else if (vote.type === "SimpleScriptVote") {
1819
+ const scriptSource = vote.simpleScriptSource;
1820
+ if (!this.isSimpleRefScriptInfoComplete(scriptSource)) {
1821
+ incompleteSimpleScriptSources.push(scriptSource);
1822
+ }
1823
+ }
1824
+ });
1825
+ certificates.forEach((certificate) => {
1826
+ if (certificate.type === "ScriptCertificate") {
1827
+ const scriptSource = certificate.scriptSource;
1828
+ if (!this.isRefScriptInfoComplete(scriptSource)) {
1829
+ incompleteScriptSources.push(scriptSource);
1830
+ }
1831
+ } else if (certificate.type === "SimpleScriptCertificate") {
1832
+ const scriptSource = certificate.simpleScriptSource;
1833
+ if (!this.isSimpleRefScriptInfoComplete(scriptSource)) {
1834
+ incompleteSimpleScriptSources.push(scriptSource);
1835
+ }
1836
+ }
1837
+ });
1838
+ await this.queryAllTxInfo(
1839
+ incompleteTxIns,
1840
+ incompleteScriptSources,
1841
+ incompleteSimpleScriptSources
1842
+ );
1843
+ incompleteTxIns.forEach((txIn) => {
1844
+ this.completeTxInformation(txIn);
1845
+ });
1846
+ incompleteScriptSources.forEach((scriptSource) => {
1847
+ this.completeScriptInfo(scriptSource);
1848
+ });
1849
+ incompleteSimpleScriptSources.forEach((simpleScriptSource) => {
1850
+ this.completeSimpleScriptInfo(simpleScriptSource);
1851
+ });
1744
1852
  this.meshTxBuilderBody.inputs.forEach((input) => {
1745
1853
  if (input.txIn.scriptSize && input.txIn.scriptSize > 0) {
1746
1854
  if (this.meshTxBuilderBody.referenceInputs.find((refTxIn) => {
@@ -1771,17 +1879,30 @@ var MeshTxBuilder = class extends MeshTxBuilderCore {
1771
1879
  });
1772
1880
  let txHex = this.serializer.serializeTxBody(
1773
1881
  this.meshTxBuilderBody,
1774
- this._protocolParams
1882
+ this._protocolParams,
1883
+ balanced
1775
1884
  );
1776
1885
  if (this.evaluator) {
1777
- const txEvaluation = await this.evaluator.evaluateTx(txHex).catch((error) => {
1778
- throw Error(`Tx evaluation failed: ${error}
1779
- For txHex: ${txHex}`);
1886
+ const txEvaluation = await this.evaluator.evaluateTx(
1887
+ txHex,
1888
+ Object.values(this.meshTxBuilderBody.inputsForEvaluation).concat(
1889
+ this.meshTxBuilderBody.inputs.map((val) => txInToUtxo(val.txIn)),
1890
+ this.meshTxBuilderBody.collaterals.map(
1891
+ (val) => txInToUtxo(val.txIn)
1892
+ )
1893
+ ),
1894
+ this.meshTxBuilderBody.chainedTxs
1895
+ ).catch((error) => {
1896
+ throw new Error(
1897
+ `Tx evaluation failed: ${JSON.stringify(error)}
1898
+ For txHex: ${txHex}`
1899
+ );
1780
1900
  });
1781
1901
  this.updateRedeemer(this.meshTxBuilderBody, txEvaluation);
1782
1902
  txHex = this.serializer.serializeTxBody(
1783
1903
  this.meshTxBuilderBody,
1784
- this._protocolParams
1904
+ this._protocolParams,
1905
+ balanced
1785
1906
  );
1786
1907
  }
1787
1908
  this.txHex = txHex;
@@ -1792,7 +1913,7 @@ var MeshTxBuilder = class extends MeshTxBuilderCore {
1792
1913
  * @param customizedTx The optional customized transaction body
1793
1914
  * @returns The signed transaction in hex ready to submit / signed by client
1794
1915
  */
1795
- completeSync = (customizedTx) => {
1916
+ completeSync = (customizedTx, balanced = true) => {
1796
1917
  if (customizedTx) {
1797
1918
  this.meshTxBuilderBody = customizedTx;
1798
1919
  } else {
@@ -1801,7 +1922,8 @@ var MeshTxBuilder = class extends MeshTxBuilderCore {
1801
1922
  this.addUtxosFromSelection();
1802
1923
  return this.serializer.serializeTxBody(
1803
1924
  this.meshTxBuilderBody,
1804
- this._protocolParams
1925
+ this._protocolParams,
1926
+ balanced
1805
1927
  );
1806
1928
  };
1807
1929
  /**
@@ -1837,9 +1959,9 @@ var MeshTxBuilder = class extends MeshTxBuilderCore {
1837
1959
  this.queriedUTxOs[txHash] = utxos;
1838
1960
  }
1839
1961
  };
1840
- queryAllTxInfo = (incompleteTxIns, incompleteMints) => {
1962
+ queryAllTxInfo = (incompleteTxIns, incompleteScriptSources, incompleteSimpleScriptSources) => {
1841
1963
  const queryUTxOPromises = [];
1842
- if ((incompleteTxIns.length > 0 || incompleteMints.length > 0) && !this.fetcher)
1964
+ if ((incompleteTxIns.length > 0 || incompleteScriptSources.length > 0 || incompleteSimpleScriptSources.length) && !this.fetcher)
1843
1965
  throw Error(
1844
1966
  "Transaction information is incomplete while no fetcher instance is provided. Provide a `fetcher`."
1845
1967
  );
@@ -1848,21 +1970,11 @@ var MeshTxBuilder = class extends MeshTxBuilderCore {
1848
1970
  if (!this.isInputInfoComplete(currentTxIn)) {
1849
1971
  queryUTxOPromises.push(this.getUTxOInfo(currentTxIn.txIn.txHash));
1850
1972
  }
1851
- if (currentTxIn.type === "Script" && currentTxIn.scriptTxIn.scriptSource?.type === "Inline" && !this.isRefScriptInfoComplete(currentTxIn.scriptTxIn.scriptSource)) {
1852
- queryUTxOPromises.push(
1853
- this.getUTxOInfo(currentTxIn.scriptTxIn.scriptSource.txHash)
1854
- );
1855
- }
1856
1973
  }
1857
- for (let i = 0; i < incompleteMints.length; i++) {
1858
- const currentMint = incompleteMints[i];
1859
- if (currentMint.type === "Plutus") {
1860
- const scriptSource = currentMint.scriptSource;
1861
- if (scriptSource.type === "Inline") {
1862
- if (!this.isRefScriptInfoComplete(scriptSource)) {
1863
- queryUTxOPromises.push(this.getUTxOInfo(scriptSource.txHash));
1864
- }
1865
- }
1974
+ for (let i = 0; i < incompleteScriptSources.length; i++) {
1975
+ const scriptSource = incompleteScriptSources[i];
1976
+ if (scriptSource.type === "Inline") {
1977
+ queryUTxOPromises.push(this.getUTxOInfo(scriptSource.txHash));
1866
1978
  }
1867
1979
  }
1868
1980
  return Promise.all(queryUTxOPromises);
@@ -1956,6 +2068,13 @@ var MeshTxBuilder = class extends MeshTxBuilderCore {
1956
2068
  }
1957
2069
  return true;
1958
2070
  };
2071
+ isSimpleRefScriptInfoComplete = (simpleScriptSource) => {
2072
+ if (simpleScriptSource.type === "Inline") {
2073
+ if (!simpleScriptSource.simpleScriptHash || !simpleScriptSource.scriptSize)
2074
+ return false;
2075
+ }
2076
+ return true;
2077
+ };
1959
2078
  };
1960
2079
 
1961
2080
  // src/scripts/forge.script.ts
@@ -2238,7 +2357,8 @@ var Transaction = class {
2238
2357
  value.input.txHash,
2239
2358
  value.input.outputIndex,
2240
2359
  value.output.amount,
2241
- value.output.address
2360
+ value.output.address,
2361
+ value.output.scriptRef ? value.output.scriptRef.length / 2 : 0
2242
2362
  ).txInScript(script.code).txInRedeemerValue(red.data, "Mesh", red.budget);
2243
2363
  }
2244
2364
  if ("output" in script) {
@@ -2486,7 +2606,7 @@ var Transaction = class {
2486
2606
  delegateStake(rewardAddress, poolId) {
2487
2607
  this.txBuilder.delegateStakeCertificate(
2488
2608
  rewardAddress,
2489
- this.txBuilder.serializer.resolver.keys.resolveEd25519KeyHash(poolId)
2609
+ this.txBuilder.serializer.deserializer.cert.deserializePoolId(poolId)
2490
2610
  );
2491
2611
  return this;
2492
2612
  }
@@ -2508,12 +2628,12 @@ var Transaction = class {
2508
2628
  this.txBuilder.retirePoolCertificate(poolId, epochNo);
2509
2629
  return this;
2510
2630
  }
2511
- async build() {
2631
+ async build(balanced = true) {
2512
2632
  try {
2513
2633
  await this.addCollateralIfNeeded();
2514
2634
  await this.addTxInputsAsNeeded();
2515
2635
  await this.addChangeAddress();
2516
- return this.txBuilder.complete();
2636
+ return this.txBuilder.complete(void 0, balanced);
2517
2637
  } catch (error) {
2518
2638
  throw new Error(
2519
2639
  `[Transaction] An error occurred during build: ${error}.`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meshsdk/transaction",
3
- "version": "1.9.0-beta.0",
3
+ "version": "1.9.0-beta.10",
4
4
  "description": "Transactions - https://meshjs.dev/apis/transaction",
5
5
  "main": "./dist/index.cjs",
6
6
  "browser": "./dist/index.js",
@@ -35,8 +35,8 @@
35
35
  "typescript": "^5.3.3"
36
36
  },
37
37
  "dependencies": {
38
- "@meshsdk/common": "1.9.0-beta.0",
39
- "@meshsdk/core-cst": "1.9.0-beta.0",
38
+ "@meshsdk/common": "1.9.0-beta.10",
39
+ "@meshsdk/core-cst": "1.9.0-beta.10",
40
40
  "json-bigint": "^1.0.0"
41
41
  },
42
42
  "prettier": "@meshsdk/configs/prettier",