@harmoniclabs/buildooor 0.1.1 → 0.1.3

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.
@@ -1,7 +1,7 @@
1
1
  import { GenesisInfos, NormalizedGenesisInfos } from "./GenesisInfos.js";
2
2
  import { CostModelsToLanguageViewCborOpts } from "@harmoniclabs/cardano-costmodels-ts";
3
3
  import { Tx, Value, ValueUnits, TxOut, TxRedeemerTag, ScriptType, UTxO, TxRedeemer, TxWitnessSet, ScriptDataHash, ITxOut } from "@harmoniclabs/cardano-ledger-ts";
4
- import { CborString, CanBeCborString } from "@harmoniclabs/cbor";
4
+ import { CanBeCborString } from "@harmoniclabs/cbor";
5
5
  import { Data } from "@harmoniclabs/plutus-data";
6
6
  import { ITxBuildArgs, ITxBuildOptions, ITxBuildInput, ITxBuildSyncOptions } from "../txBuild/index.js";
7
7
  import { CanBeUInteger } from "../utils/ints.js";
@@ -16,9 +16,10 @@ export declare class TxBuilder {
16
16
  runWithProvider(provider: Partial<ITxRunnerProvider>): TxBuilderRunner;
17
17
  constructor(protocolParamters?: Readonly<TxBuilderProtocolParams>, genesisInfos?: GenesisInfos);
18
18
  keepRelevant(requestedOutputSet: Value | ValueUnits, initialUTxOSet: ITxBuildInput[], minimumLovelaceRequired?: CanBeUInteger): ITxBuildInput[];
19
- calcLinearFee(tx: Tx | CborString): bigint;
19
+ private calcLinearFee;
20
20
  calcMinFee(tx: Tx): bigint;
21
21
  getMinimumOutputLovelaces(tx_out: TxOut | CanBeCborString): bigint;
22
+ addMinLovelacesIfMissing(txOut: TxOut): TxOut;
22
23
  minimizeLovelaces(out: ITxOut): TxOut;
23
24
  /**
24
25
  *
@@ -168,7 +168,9 @@ var TxBuilder = /** @class */ (function () {
168
168
  };
169
169
  TxBuilder.prototype.calcLinearFee = function (tx) {
170
170
  return ((0, ints_1.forceBigUInt)(this.protocolParamters.txFeePerByte) *
171
- BigInt((tx instanceof cardano_ledger_ts_1.Tx ? tx.toCbor() : tx).toBuffer().length) +
171
+ BigInt((tx instanceof cardano_ledger_ts_1.Tx ? tx.toCbor() : tx).toBuffer().length
172
+ + 2 // for good measure
173
+ ) +
172
174
  (0, ints_1.forceBigUInt)(this.protocolParamters.txFeeFixed));
173
175
  };
174
176
  TxBuilder.prototype.calcMinFee = function (tx) {
@@ -192,9 +194,10 @@ var TxBuilder = /** @class */ (function () {
192
194
  // each vkey witness has fixed size of 102 cbor bytes
193
195
  // (1 bytes cbor array tag (length 2)) + (34 cbor bytes of length 32) + (67 cbor bytes of length 64)
194
196
  // for a fixed length of 102
195
- BigInt(102) * nVkeyWits * minFeeMultiplier +
197
+ // we also add 2 for possible unwanted encoding
198
+ BigInt(104) * nVkeyWits * minFeeMultiplier +
196
199
  // we add some more bytes for the array tag
197
- BigInt(nVkeyWits < 24 ? 1 : (nVkeyWits < 256 ? 2 : 3)) * minFeeMultiplier;
200
+ BigInt(nVkeyWits < 24 ? 1 : (nVkeyWits < 256 ? 2 : 4)) * minFeeMultiplier;
198
201
  return minFee;
199
202
  };
200
203
  TxBuilder.prototype.getMinimumOutputLovelaces = function (tx_out) {
@@ -215,6 +218,16 @@ var TxBuilder = /** @class */ (function () {
215
218
  size = BigInt(tx_out.length);
216
219
  return BigInt(this.protocolParamters.utxoCostPerByte) * size;
217
220
  };
221
+ TxBuilder.prototype.addMinLovelacesIfMissing = function (txOut) {
222
+ if (txOut.value.lovelaces > 0)
223
+ return txOut.clone();
224
+ return this.minimizeLovelaces({
225
+ address: txOut.address,
226
+ value: cardano_ledger_ts_1.Value.add(cardano_ledger_ts_1.Value.lovelaces(this.getMinimumOutputLovelaces(txOut) + BigInt(1000000)), txOut.value),
227
+ datum: txOut.datum,
228
+ refScript: txOut.refScript
229
+ });
230
+ };
218
231
  TxBuilder.prototype.minimizeLovelaces = function (out) {
219
232
  var o = out instanceof cardano_ledger_ts_1.TxOut ? out : (0, txBuild_1.txBuildOutToTxOut)(out);
220
233
  var minLovelaces = this.getMinimumOutputLovelaces(o);
@@ -411,7 +424,7 @@ var TxBuilder = /** @class */ (function () {
411
424
  if (round === maxRound - 1)
412
425
  return "break";
413
426
  for (var i = 0; i < outs.length; i++) {
414
- txOuts[i] = outs[i].clone();
427
+ txOuts[i] = this_1.addMinLovelacesIfMissing(outs[i]);
415
428
  }
416
429
  txOuts[txOuts.length - 1] = (new cardano_ledger_ts_1.TxOut({
417
430
  address: change.address,
@@ -456,6 +469,7 @@ var TxBuilder = /** @class */ (function () {
456
469
  * this is all that either `build` or `buildSync` needs to do
457
470
  **/
458
471
  TxBuilder.prototype.initTxBuild = function (buildArgs) {
472
+ var _this = this;
459
473
  var _a = (0, txBuild_1.normalizeITxBuildArgs)(buildArgs), outputs = _a.outputs, requiredSigners = _a.requiredSigners, mints = _a.mints, invalidAfter = _a.invalidAfter, certificates = _a.certificates, withdrawals = _a.withdrawals, memo = _a.memo, metadata = _a.metadata, votingProcedures = _a.votingProcedures, proposalProcedures = _a.proposalProcedures, currentTreasuryValue = _a.currentTreasuryValue, paymentToTreasury = _a.paymentToTreasury, args = __rest(_a, ["outputs", "requiredSigners", "mints", "invalidAfter", "certificates", "withdrawals", "memo", "metadata", "votingProcedures", "proposalProcedures", "currentTreasuryValue", "paymentToTreasury"]);
460
474
  // mutable args
461
475
  var inputs = args.inputs, changeAddress = args.changeAddress, change = args.change, invalidBefore = args.invalidBefore, readonlyRefInputs = args.readonlyRefInputs, collaterals = args.collaterals, collateralReturn = args.collateralReturn;
@@ -932,8 +946,10 @@ var TxBuilder = /** @class */ (function () {
932
946
  isScriptValid: isScriptValid
933
947
  });
934
948
  var minFee = this.calcMinFee(dummyTx);
949
+ if (typeof args.fee === "bigint" && args.fee > minFee)
950
+ minFee = args.fee;
935
951
  var txOuts = new Array(outs.length + 1);
936
- outs.forEach(function (txO, i) { return txOuts[i] = txO.clone(); });
952
+ outs.forEach(function (txO, i) { return txOuts[i] = _this.addMinLovelacesIfMissing(txO); });
937
953
  var changeOutput = new cardano_ledger_ts_1.TxOut({
938
954
  address: change.address,
939
955
  value: cardano_ledger_ts_1.Value.sub(totInputValue, cardano_ledger_ts_1.Value.add(requiredOutputValue, cardano_ledger_ts_1.Value.lovelaces(minFee))),
@@ -31,6 +31,8 @@ export interface ITxBuildArgs {
31
31
  invalidAfter?: CanBeUInteger;
32
32
  certificates?: ITxBuildCert[];
33
33
  withdrawals?: ITxBuildWithdrawal[];
34
+ /** explicitly sets the fee (if higher than calculated minFee) */
35
+ fee?: CanBeUInteger;
34
36
  /**
35
37
  * # metadata message following cip20
36
38
  *
@@ -57,6 +59,7 @@ export interface NormalizedITxBuildArgs extends ITxBuildArgs {
57
59
  invalidAfter?: CanBeUInteger;
58
60
  certificates?: NormalizedITxBuildCert[];
59
61
  withdrawals?: NormalizedITxBuildWithdrawal[];
62
+ fee?: bigint;
60
63
  /**
61
64
  * # metadata message following cip20
62
65
  *
@@ -69,6 +72,6 @@ export interface NormalizedITxBuildArgs extends ITxBuildArgs {
69
72
  currentTreasuryValue?: bigint;
70
73
  paymentToTreasury?: bigint;
71
74
  }
72
- export declare function normalizeITxBuildArgs({ inputs, change, changeAddress, outputs, readonlyRefInputs, requiredSigners, collaterals, collateralReturn, mints, invalidBefore, invalidAfter, certificates, withdrawals, memo, metadata, votingProcedures, proposalProcedures, currentTreasuryValue, paymentToTreasury }: ITxBuildArgs): NormalizedITxBuildArgs;
75
+ export declare function normalizeITxBuildArgs({ inputs, change, changeAddress, outputs, readonlyRefInputs, requiredSigners, collaterals, collateralReturn, mints, invalidBefore, invalidAfter, certificates, withdrawals, fee, memo, metadata, votingProcedures, proposalProcedures, currentTreasuryValue, paymentToTreasury }: ITxBuildArgs): NormalizedITxBuildArgs;
73
76
  /** @deprecated use `normalizeITxBuildArgs` instead */
74
77
  export declare function cloneITxBuildArgs(args: ITxBuildArgs): ITxBuildArgs;
@@ -7,11 +7,12 @@ var ITxBuildInput_1 = require("./ITxBuildInput/ITxBuildInput.js");
7
7
  var ITxBuildMint_1 = require("./ITxBuildMint.js");
8
8
  var ITxBuildOutput_1 = require("./ITxBuildOutput.js");
9
9
  var ITxBuildWithdrawal_1 = require("./ITxBuildWithdrawal.js");
10
+ var ints_1 = require("../utils/ints.js");
10
11
  var ChangeInfos_1 = require("./ChangeInfos/ChangeInfos.js");
11
12
  var ITxBuildVotingProcedure_1 = require("./ITxBuildVotingProcedure.js");
12
13
  var ITxBuildProposalProcedure_1 = require("./ITxBuildProposalProcedure.js");
13
14
  function normalizeITxBuildArgs(_a) {
14
- var inputs = _a.inputs, change = _a.change, changeAddress = _a.changeAddress, outputs = _a.outputs, readonlyRefInputs = _a.readonlyRefInputs, requiredSigners = _a.requiredSigners, collaterals = _a.collaterals, collateralReturn = _a.collateralReturn, mints = _a.mints, invalidBefore = _a.invalidBefore, invalidAfter = _a.invalidAfter, certificates = _a.certificates, withdrawals = _a.withdrawals, memo = _a.memo, metadata = _a.metadata, votingProcedures = _a.votingProcedures, proposalProcedures = _a.proposalProcedures, currentTreasuryValue = _a.currentTreasuryValue, paymentToTreasury = _a.paymentToTreasury;
15
+ var inputs = _a.inputs, change = _a.change, changeAddress = _a.changeAddress, outputs = _a.outputs, readonlyRefInputs = _a.readonlyRefInputs, requiredSigners = _a.requiredSigners, collaterals = _a.collaterals, collateralReturn = _a.collateralReturn, mints = _a.mints, invalidBefore = _a.invalidBefore, invalidAfter = _a.invalidAfter, certificates = _a.certificates, withdrawals = _a.withdrawals, fee = _a.fee, memo = _a.memo, metadata = _a.metadata, votingProcedures = _a.votingProcedures, proposalProcedures = _a.proposalProcedures, currentTreasuryValue = _a.currentTreasuryValue, paymentToTreasury = _a.paymentToTreasury;
15
16
  return {
16
17
  inputs: inputs.map(normalizeITxBuildArgsInputs),
17
18
  change: change ? (0, ChangeInfos_1.normalizeChangeInfos)(change) : undefined,
@@ -28,6 +29,7 @@ function normalizeITxBuildArgs(_a) {
28
29
  invalidAfter: invalidAfter === undefined ? undefined : BigInt(invalidAfter),
29
30
  certificates: certificates === null || certificates === void 0 ? void 0 : certificates.map(ITxBuildCert_1.normalizeITxBuildCert),
30
31
  withdrawals: withdrawals === null || withdrawals === void 0 ? void 0 : withdrawals.map(ITxBuildWithdrawal_1.normalizeITxBuildWithdrawal),
32
+ fee: (0, ints_1.canBeUInteger)(fee) ? BigInt(fee) : undefined,
31
33
  memo: memo ? String(memo) : undefined,
32
34
  metadata: metadata,
33
35
  votingProcedures: Array.isArray(votingProcedures) ?
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@harmoniclabs/buildooor",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Cardano transaction builder in typescript",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",