Skip to content

Commit 60df9ca

Browse files
Ignore collateral handling for simple transactions
1 parent 629023e commit 60df9ca

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/BotPlutusInterface/Balance.hs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE AllowAmbiguousTypes #-}
2+
{-# LANGUAGE NamedFieldPuns #-}
23

34
module BotPlutusInterface.Balance (
45
balanceTxStep,
@@ -20,10 +21,10 @@ import Control.Monad.Trans.Either (EitherT, hoistEither, newEitherT, runEitherT)
2021
import Data.Coerce (coerce)
2122
import Data.Either.Combinators (rightToMaybe)
2223
import Data.Kind (Type)
23-
import Data.List (partition, (\\))
24+
import Data.List (partition, (\\), find)
2425
import Data.Map (Map)
2526
import Data.Map qualified as Map
26-
import Data.Maybe (fromMaybe, mapMaybe)
27+
import Data.Maybe (fromMaybe, mapMaybe, isJust)
2728
import Data.Set (Set)
2829
import Data.Set qualified as Set
2930
import Data.Text (Text)
@@ -280,7 +281,7 @@ balanceTxIns utxos tx = do
280281
(suboptimally we just pick a random utxo from the tx inputs)
281282
-}
282283
addTxCollaterals :: 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
296303
handleNonAdaChange :: Address -> Map TxOutRef TxOut -> Tx -> Either Text Tx

0 commit comments

Comments
 (0)