Skip to content

Commit

Permalink
fix: collateral output min utxo not taken into account
Browse files Browse the repository at this point in the history
  • Loading branch information
joacohoyos committed Oct 17, 2024
1 parent 0321d34 commit fdb02f1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/fifty-cheetahs-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@blaze-cardano/tx": major
---

Fix issue with collateral return belowe min ada utxo
26 changes: 20 additions & 6 deletions packages/blaze-tx/src/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();
Expand All @@ -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();
}
}
Expand Down Expand Up @@ -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
) {
Expand All @@ -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
) {
Expand Down

0 comments on commit fdb02f1

Please sign in to comment.