@@ -48,7 +48,7 @@ import Data.Kind (Type)
4848import Data.List qualified as List
4949import Data.Map (Map )
5050import Data.Map qualified as Map
51- import Data.Maybe (fromMaybe , mapMaybe )
51+ import Data.Maybe (fromMaybe , isJust , mapMaybe )
5252import Data.Set qualified as Set
5353import Data.Text (Text )
5454import Data.Text qualified as Text
@@ -322,6 +322,12 @@ getAdaChange utxos = lovelaceValue . getChange utxos
322322getNonAdaChange :: Map TxOutRef TxOut -> Tx -> Value
323323getNonAdaChange utxos = Ledger. noAdaValue . getChange utxos
324324
325+ hasDatum :: TxOut -> Bool
326+ hasDatum = isJust . txOutDatumHash
327+
328+ hasNoDatum :: TxOut -> Bool
329+ hasNoDatum = not . hasDatum
330+
325331balanceTxIns ::
326332 forall (w :: Type ) (effs :: [Type -> Type ]).
327333 Member (PABEffect w ) effs =>
@@ -377,8 +383,9 @@ handleNonAdaChange balanceCfg changeAddr utxos tx = runEitherT $ do
377383 ( \ txout ->
378384 Tx. txOutAddress txout == changeAddr
379385 && not (justLovelace $ Tx. txOutValue txout)
386+ && hasNoDatum txout
380387 )
381- else (\ txout -> Tx. txOutAddress txout == changeAddr)
388+ else (\ txout -> Tx. txOutAddress txout == changeAddr && hasNoDatum txout )
382389
383390 newOutput :: TxOut
384391 newOutput =
@@ -415,15 +422,19 @@ addAdaChange balanceCfg changeAddr change tx
415422 { txOutputs =
416423 List. reverse $
417424 modifyFirst
418- (\ txout -> Tx. txOutAddress txout == changeAddr && justLovelace (txOutValue txout))
425+ ( \ txout ->
426+ Tx. txOutAddress txout == changeAddr
427+ && justLovelace (txOutValue txout)
428+ && hasNoDatum txout
429+ )
419430 (fmap $ addValueToTxOut $ Ada. lovelaceValueOf change)
420431 (List. reverse $ txOutputs tx)
421432 }
422433 | otherwise =
423434 tx
424435 { txOutputs =
425436 modifyFirst
426- (( == changeAddr) . Tx. txOutAddress )
437+ (\ txout -> Tx. txOutAddress txout == changeAddr && hasNoDatum txout )
427438 (fmap $ addValueToTxOut $ Ada. lovelaceValueOf change)
428439 (txOutputs tx)
429440 }
0 commit comments