@@ -21,10 +21,10 @@ import Control.Monad.Trans.Either (EitherT, hoistEither, newEitherT, runEitherT)
2121import Data.Coerce (coerce )
2222import Data.Either.Combinators (rightToMaybe )
2323import Data.Kind (Type )
24- import Data.List (partition , (\\) , find )
24+ import Data.List (find , partition , (\\) )
2525import Data.Map (Map )
2626import Data.Map qualified as Map
27- import Data.Maybe (fromMaybe , mapMaybe , isJust )
27+ import Data.Maybe (fromMaybe , isJust , mapMaybe )
2828import Data.Set (Set )
2929import Data.Set qualified as Set
3030import Data.Text (Text )
@@ -281,22 +281,25 @@ balanceTxIns utxos tx = do
281281 (suboptimally we just pick a random utxo from the tx inputs)
282282-}
283283addTxCollaterals :: Map TxOutRef TxOut -> Tx -> Either Text Tx
284- addTxCollaterals utxos tx = if not $ usesScripts tx then Right tx else do
285- let txIns = mapMaybe (rightToMaybe . txOutToTxIn) $ Map. toList $ filterAdaOnly utxos
286- txIn <- findPubKeyTxIn txIns
287- pure $ tx {txCollateral = Set. singleton txIn}
284+ addTxCollaterals utxos tx =
285+ if not $ usesScripts tx
286+ then Right tx
287+ else do
288+ let txIns = mapMaybe (rightToMaybe . txOutToTxIn) $ Map. toList $ filterAdaOnly utxos
289+ txIn <- findPubKeyTxIn txIns
290+ pure $ tx {txCollateral = Set. singleton txIn}
288291 where
289292 findPubKeyTxIn = \ case
290293 x@ (TxIn _ (Just ConsumePublicKeyAddress )) : _ -> Right x
291294 x@ (TxIn _ Nothing ) : _ -> Right x
292295 _ : xs -> findPubKeyTxIn xs
293296 _ -> Left " There are no utxos to be used as collateral"
294297 filterAdaOnly = Map. filter (isAdaOnly . txOutValue)
295- usesScripts Tx {txInputs, txMintScripts}
296- = not (null txMintScripts)
298+ usesScripts Tx {txInputs, txMintScripts} =
299+ not (null txMintScripts)
297300 || isJust
298- (find (\ TxIn {txInType} -> case txInType of { Just ConsumeScriptAddress {} -> True ; _ -> False })
299- $ Set. toList txInputs
301+ ( find (\ TxIn {txInType} -> case txInType of Just ConsumeScriptAddress {} -> True ; _ -> False ) $
302+ Set. toList txInputs
300303 )
301304
302305-- | Ensures all non ada change goes back to user
0 commit comments