@@ -10,7 +10,9 @@ module BotPlutusInterface.Balance (
1010 withFee ,
1111) where
1212
13+ import BotPlutusInterface.BodyBuilder qualified as BodyBuilder
1314import BotPlutusInterface.CardanoCLI qualified as CardanoCLI
15+ import BotPlutusInterface.Collateral (removeCollateralFromMap )
1416import BotPlutusInterface.Effects (
1517 PABEffect ,
1618 createDirectoryIfMissingCLI ,
@@ -21,7 +23,7 @@ import BotPlutusInterface.Effects (
2123import BotPlutusInterface.Files (DummyPrivKey , unDummyPrivateKey )
2224import BotPlutusInterface.Files qualified as Files
2325import BotPlutusInterface.Types (CollateralUtxo , LogLevel (Debug ), PABConfig , collateralTxOutRef )
24- import Cardano.Api (ExecutionUnitPrices (ExecutionUnitPrices ), UTxO ( UTxO ) )
26+ import Cardano.Api (ExecutionUnitPrices (ExecutionUnitPrices ))
2527import Cardano.Api.Shelley (ProtocolParameters (protocolParamPrices ))
2628import Control.Monad (foldM , void , zipWithM )
2729import Control.Monad.Freer (Eff , Member )
@@ -71,10 +73,6 @@ import Plutus.V1.Ledger.Api (
7173 CurrencySymbol (.. ),
7274 TokenName (.. ),
7375 )
74-
75- import BotPlutusInterface.BodyBuilder qualified as BodyBuilder
76-
77- import BotPlutusInterface.Collateral (removeCollateralFromMap )
7876import Prettyprinter (pretty , viaShow , (<+>) )
7977import Prelude
8078
@@ -157,8 +155,7 @@ balanceTxIO' balanceCfg pabConf ownPkh unbalancedTx =
157155 -- the changeAddr.
158156 balancedTxWithChange <-
159157 case adaChange /= 0 of
160- True | bcSeparateChange balanceCfg -> bTx
161- True | not (hasChangeUTxO changeAddr balancedTx) -> bTx
158+ True | bcSeparateChange balanceCfg || not (hasChangeUTxO changeAddr balancedTx) -> bTx
162159 _ -> pure balancedTx
163160
164161 -- Get the updated change, add it to the tx
@@ -189,7 +186,7 @@ balanceTxIO' balanceCfg pabConf ownPkh unbalancedTx =
189186
190187 -- Calculate fees by pre-balancing the tx, building it, and running the CLI on result
191188 txWithoutFees <-
192- hoistEither $ balanceTxStep minUtxos utxoIndex changeAddr $ tx `withFee` 0
189+ hoistEither $ balanceTxStep balanceCfg minUtxos utxoIndex changeAddr $ tx `withFee` 0
193190
194191 exBudget <- newEitherT $ BodyBuilder. buildAndEstimateBudget @ w pabConf privKeys txWithoutFees
195192
@@ -200,7 +197,7 @@ balanceTxIO' balanceCfg pabConf ownPkh unbalancedTx =
200197 lift $ printBpiLog @ w Debug $ " Fees:" <+> pretty fees
201198
202199 -- Rebalance the initial tx with the above fees
203- balancedTx <- hoistEither $ balanceTxStep minUtxos utxoIndex changeAddr $ tx `withFee` fees
200+ balancedTx <- hoistEither $ balanceTxStep balanceCfg minUtxos utxoIndex changeAddr $ tx `withFee` fees
204201
205202 if balancedTx == tx
206203 then pure (balancedTx, minUtxos)
@@ -268,15 +265,16 @@ calculateMinUtxos pabConf datums txOuts =
268265 zipWithM (fmap . (,)) txOuts <$> mapM (CardanoCLI. calculateMinUtxo @ w pabConf datums) txOuts
269266
270267balanceTxStep ::
268+ BalanceConfig ->
271269 [(TxOut , Integer )] ->
272270 Map TxOutRef TxOut ->
273271 Address ->
274272 Tx ->
275273 Either Text Tx
276- balanceTxStep minUtxos utxos changeAddr tx =
274+ balanceTxStep balanceCfg minUtxos utxos changeAddr tx =
277275 Right (addLovelaces minUtxos tx)
278276 >>= balanceTxIns utxos
279- >>= handleNonAdaChange changeAddr utxos
277+ >>= handleNonAdaChange balanceCfg changeAddr utxos
280278
281279-- | Get change value of a transaction, taking inputs, outputs, mint and fees into account
282280getChange :: Map TxOutRef TxOut -> Tx -> Value
@@ -380,9 +378,17 @@ txUsesScripts Tx {txInputs, txMintScripts} =
380378 (Set. toList txInputs)
381379
382380-- | Ensures all non ada change goes back to user
383- handleNonAdaChange :: Address -> Map TxOutRef TxOut -> Tx -> Either Text Tx
384- handleNonAdaChange changeAddr utxos tx =
381+ handleNonAdaChange :: BalanceConfig -> Address -> Map TxOutRef TxOut -> Tx -> Either Text Tx
382+ handleNonAdaChange balanceCfg changeAddr utxos tx =
385383 let nonAdaChange = getNonAdaChange utxos tx
384+ predicate =
385+ if bcSeparateChange balanceCfg
386+ then
387+ ( \ txout ->
388+ Tx. txOutAddress txout == changeAddr
389+ && not (justLovelace $ Tx. txOutValue txout)
390+ )
391+ else (\ txout -> Tx. txOutAddress txout == changeAddr)
386392 newOutput =
387393 TxOut
388394 { txOutAddress = changeAddr
@@ -391,7 +397,7 @@ handleNonAdaChange changeAddr utxos tx =
391397 }
392398 outputs =
393399 modifyFirst
394- ( (==) changeAddr . Tx. txOutAddress)
400+ predicate
395401 (Just . maybe newOutput (addValueToTxOut nonAdaChange))
396402 (txOutputs tx)
397403 in if isValueNat nonAdaChange
0 commit comments