From 05c66618673ebbb7928786c13f3c44bbfb9452c9 Mon Sep 17 00:00:00 2001 From: Joaquin Hoyos Date: Tue, 17 Sep 2024 14:17:41 +0200 Subject: [PATCH] fix: evaluator and changeAddress overriding on preCompleteHook --- .changeset/pretty-rocks-wait.md | 6 ++++++ packages/blaze-sdk/src/blaze.ts | 5 +++-- packages/blaze-tx/src/tx.ts | 14 ++++++++++---- 3 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 .changeset/pretty-rocks-wait.md diff --git a/.changeset/pretty-rocks-wait.md b/.changeset/pretty-rocks-wait.md new file mode 100644 index 00000000..1a6b2c34 --- /dev/null +++ b/.changeset/pretty-rocks-wait.md @@ -0,0 +1,6 @@ +--- +"@blaze-cardano/sdk": patch +"@blaze-cardano/tx": patch +--- + +avoid preCompleteHook overriding user selections diff --git a/packages/blaze-sdk/src/blaze.ts b/packages/blaze-sdk/src/blaze.ts index f301f6d9..f9935f00 100644 --- a/packages/blaze-sdk/src/blaze.ts +++ b/packages/blaze-sdk/src/blaze.ts @@ -51,8 +51,9 @@ export class Blaze { const changeAddress = await this.wallet.getChangeAddress(); tx.setNetworkId(await this.wallet.getNetworkId()) .addUnspentOutputs(myUtxos) - .setChangeAddress(changeAddress) - .useEvaluator((x, y) => this.provider.evaluateTransaction(x, y)); + // We do not want to override the change address if it was already set + .setChangeAddress(changeAddress, false) + .useEvaluator((x, y) => this.provider.evaluateTransaction(x, y), false); }); } diff --git a/packages/blaze-tx/src/tx.ts b/packages/blaze-tx/src/tx.ts index c39faf3e..d36fa79c 100644 --- a/packages/blaze-tx/src/tx.ts +++ b/packages/blaze-tx/src/tx.ts @@ -165,10 +165,13 @@ export class TxBuilder { * This address will receive any remaining funds not allocated to outputs or fees. * * @param {Address} address - The address to receive the change. + * @param {boolean} [override=true] - Whether to override the change address if one is already set. * @returns {TxBuilder} The same transaction builder */ - setChangeAddress(address: Address): TxBuilder { - this.changeAddress = address; + setChangeAddress(address: Address, override = true): TxBuilder { + if (override || !this.changeAddress) { + this.changeAddress = address; + } return this; } @@ -189,10 +192,13 @@ export class TxBuilder { * The evaluator is used to execute Plutus scripts during transaction building. * * @param {Evaluator} evaluator - The evaluator to be used for script execution. + * @param {boolean} [override=true] - Whether to override the evaluator if one is already set. * @returns {TxBuilder} The same transaction builder */ - useEvaluator(evaluator: Evaluator): TxBuilder { - this.evaluator = evaluator; + useEvaluator(evaluator: Evaluator, override = true): TxBuilder { + if (override || !this.evaluator) { + this.evaluator = evaluator; + } return this; }