11{-# LANGUAGE AllowAmbiguousTypes #-}
2+ {-# LANGUAGE NamedFieldPuns #-}
23
34module BotPlutusInterface.Balance (
45 balanceTxStep ,
@@ -20,10 +21,10 @@ import Control.Monad.Trans.Either (EitherT, hoistEither, newEitherT, runEitherT)
2021import Data.Coerce (coerce )
2122import Data.Either.Combinators (rightToMaybe )
2223import Data.Kind (Type )
23- import Data.List (partition , (\\) )
24+ import Data.List (partition , (\\) , find )
2425import Data.Map (Map )
2526import Data.Map qualified as Map
26- import Data.Maybe (fromMaybe , mapMaybe )
27+ import Data.Maybe (fromMaybe , mapMaybe , isJust )
2728import Data.Set (Set )
2829import Data.Set qualified as Set
2930import Data.Text (Text )
@@ -280,7 +281,7 @@ balanceTxIns utxos tx = do
280281 (suboptimally we just pick a random utxo from the tx inputs)
281282-}
282283addTxCollaterals :: Map TxOutRef TxOut -> Tx -> Either Text Tx
283- addTxCollaterals utxos tx = do
284+ addTxCollaterals utxos tx = if not $ usesScripts tx then Right tx else do
284285 let txIns = mapMaybe (rightToMaybe . txOutToTxIn) $ Map. toList $ filterAdaOnly utxos
285286 txIn <- findPubKeyTxIn txIns
286287 pure $ tx {txCollateral = Set. singleton txIn}
@@ -291,6 +292,12 @@ addTxCollaterals utxos tx = do
291292 _ : xs -> findPubKeyTxIn xs
292293 _ -> Left " There are no utxos to be used as collateral"
293294 filterAdaOnly = Map. filter (isAdaOnly . txOutValue)
295+ usesScripts Tx {txInputs, txMintScripts}
296+ = not (null txMintScripts)
297+ || isJust
298+ (find (\ TxIn {txInType} -> case txInType of { Just ConsumeScriptAddress {} -> True ; _ -> False })
299+ $ Set. toList txInputs
300+ )
294301
295302-- | Ensures all non ada change goes back to user
296303handleNonAdaChange :: Address -> Map TxOutRef TxOut -> Tx -> Either Text Tx
0 commit comments