HeliosLang / compiler

Helios is a DSL for writing Cardano smart contracts. This library lets you compile Helios scripts and build Cardano transactions.
https://www.hyperion-bt.org/helios-book
BSD 3-Clause "New" or "Revised" License
142 stars 31 forks source link

balanceCollateral should not make use of 'clean' script inputs #85

Closed gavinharris-dev closed 1 year ago

gavinharris-dev commented 1 year ago

Hi! πŸ‘‹

Firstly, thanks for your work on this project! πŸ™‚

Today I used patch-package to patch @hyperionbt/helios@0.13.38 for the project I'm working on.

As discussed in the Discord, the method balanceCollateral makes use of any 'clean' Transaction Input, however if you are using Script Inputs that are for Lovelace only then this can cause issues.

The below patch fixes the issue for me (it removes our the Script Inputs) but I'm not sure if this will be the best solution overall,

Here is the diff that solved my problem:

diff --git a/node_modules/@hyperionbt/helios/helios.js b/node_modules/@hyperionbt/helios/helios.js
index 9ef16f8..23e8c0a 100644
--- a/node_modules/@hyperionbt/helios/helios.js
+++ b/node_modules/@hyperionbt/helios/helios.js
@@ -37835,7 +37835,7 @@ export class Tx extends CborData {
         */
        function addCollateralInputs(inputs) {
            // first try using the UTxOs that already form the inputs
-           const cleanInputs = inputs.filter(utxo => utxo.value.assets.isZero()).sort((a, b) => Number(a.value.lovelace - b.value.lovelace));
+           const cleanInputs = inputs.filter(utxo => !utxo.address.validatorHash && utxo.value.assets.isZero()).sort((a, b) => Number(a.value.lovelace - b.value.lovelace));

            for (let input of cleanInputs) {
                if (collateral > minCollateral) {
diff --git a/node_modules/@hyperionbt/helios/src/tx-builder.js b/node_modules/@hyperionbt/helios/src/tx-builder.js
index a6cdfc3..e80310a 100644
--- a/node_modules/@hyperionbt/helios/src/tx-builder.js
+++ b/node_modules/@hyperionbt/helios/src/tx-builder.js
@@ -625,7 +625,7 @@ export class Tx extends CborData {
         */
        function addCollateralInputs(inputs) {
            // first try using the UTxOs that already form the inputs
-           const cleanInputs = inputs.filter(utxo => utxo.value.assets.isZero()).sort((a, b) => Number(a.value.lovelace - b.value.lovelace));
+           const cleanInputs = inputs.filter(utxo => !utxo.address.validatorHash && utxo.value.assets.isZero()).sort((a, b) => Number(a.value.lovelace - b.value.lovelace));

            for (let input of cleanInputs) {
                if (collateral > minCollateral) {

This issue body was partially generated by patch-package.

christianschmitz commented 1 year ago

This is probably the best solution, I've added it to the main code-base