diff --git a/.changeset/fifty-cheetahs-grin.md b/.changeset/fifty-cheetahs-grin.md new file mode 100644 index 00000000..850014d2 --- /dev/null +++ b/.changeset/fifty-cheetahs-grin.md @@ -0,0 +1,5 @@ +--- +"@blaze-cardano/tx": major +--- + +Fix issue with collateral return belowe min ada utxo diff --git a/packages/blaze-tx/src/tx.ts b/packages/blaze-tx/src/tx.ts index 4aa37484..8f92153d 100644 --- a/packages/blaze-tx/src/tx.ts +++ b/packages/blaze-tx/src/tx.ts @@ -535,6 +535,15 @@ export class TxBuilder { return this; } + /** + * This methods calculates the minimum ada required for a transaction output. + * @param {TransactionOutput} output - The transaction output to calculate the minimum ada for. + * @returns {bigint} The minimum ada required for the output. + */ + private calculateMinAda(output: TransactionOutput): bigint { + const byteLength = BigInt(output.toCbor().length / 2); + return BigInt(this.params.coinsPerUtxoByte) * (byteLength + 160n); + } /** * This method checks and alters the output of a transaction. * It ensures that the output meets the minimum ada requirements and does not exceed the maximum value size. @@ -543,10 +552,10 @@ export class TxBuilder { * @returns {TransactionOutput} The altered transaction output. * @throws {Error} If the output does not meet the minimum ada requirements or exceeds the maximum value size. */ + private checkAndAlterOutput(output: TransactionOutput): TransactionOutput { { - let byteLength = BigInt(output.toCbor().length / 2); - let minAda = BigInt(this.params.coinsPerUtxoByte) * (byteLength + 160n); + let minAda = this.calculateMinAda(output); let coin = output.amount().coin(); while (coin < minAda) { const amount = output.amount(); @@ -560,8 +569,7 @@ export class TxBuilder { if (scriptRef) { output.setScriptRef(scriptRef); } - byteLength = BigInt(output.toCbor().length / 2); - minAda = BigInt(this.params.coinsPerUtxoByte) * (byteLength + 160n); + minAda = this.calculateMinAda(output); coin = output.amount().coin(); } } @@ -1219,8 +1227,11 @@ export class TxBuilder { // if there are provided collateral UTxOs, use them first if (providedCollateral.length > 0) { for (const utxo of providedCollateral) { + const output = utxo.output(); + const outputMinAda = this.calculateMinAda(output); + const coinAmount = output.amount().coin() - outputMinAda; if ( - utxo.output().amount().coin() >= 5_000_000 && + coinAmount >= 5_000_000 && utxo.output().address().getProps().paymentPart?.type == CredentialType.KeyHash ) { @@ -1242,8 +1253,11 @@ export class TxBuilder { if (utxo) { // Check if the UTXO amount is sufficient for collateral. + const output = utxo.output(); + const outputMinAda = this.calculateMinAda(output); + const coinAmount = output.amount().coin() - outputMinAda; if ( - utxo.output().amount().coin() >= 5_000_000 && + coinAmount >= 5_000_000 && utxo.output().address().getProps().paymentPart?.type == CredentialType.KeyHash ) {