@@ -4,6 +4,7 @@ module Ctl.Internal.BalanceTx
44
55import Prelude
66
7+ import Contract.Log (logWarn' )
78import Control.Monad.Error.Class (liftMaybe )
89import Control.Monad.Except.Trans (ExceptT (ExceptT), except , runExceptT )
910import Control.Monad.Logger.Class (info ) as Logger
@@ -92,6 +93,9 @@ import Ctl.Internal.Cardano.Types.Transaction
9293 , _witnessSet
9394 , pprintUtxoMap
9495 )
96+ import Ctl.Internal.Cardano.Types.TransactionUnspentOutput
97+ ( transactionUnspentOutputsToUtxoMap
98+ )
9599import Ctl.Internal.Cardano.Types.Value
96100 ( AssetClass
97101 , Coin (Coin)
@@ -115,7 +119,7 @@ import Ctl.Internal.Contract.Wallet
115119 , getWalletCollateral
116120 , getWalletUtxos
117121 ) as Wallet
118- import Ctl.Internal.Helpers (liftEither , (??))
122+ import Ctl.Internal.Helpers (liftEither , pprintTagSet , (??))
119123import Ctl.Internal.Partition (equipartition , partition )
120124import Ctl.Internal.Plutus.Conversion (fromPlutusUtxoMap )
121125import Ctl.Internal.Serialization.Address (Address )
@@ -272,10 +276,19 @@ setTransactionCollateral changeAddr transaction = do
272276 let
273277 isSpendable = not <<< flip Set .member nonSpendableSet
274278 collateral <- case mbCollateralUtxos of
275- -- if no collateral utxos are specified, use the wallet
276- Nothing -> Array .filter (isSpendable <<< _.input <<< unwrap) <$> do
277- liftEitherContract $ note CouldNotGetCollateral <$>
278- Wallet .getWalletCollateral
279+ -- if no collateral utxos are specified, use the wallet, but filter
280+ -- the unspendable ones
281+ Nothing -> do
282+ let isSpendableUtxo = isSpendable <<< _.input <<< unwrap
283+ { yes: spendableUtxos, no: filteredUtxos } <-
284+ Array .partition isSpendableUtxo <$> do
285+ liftEitherContract $ note CouldNotGetCollateral <$>
286+ Wallet .getWalletCollateral
287+ when (not $ Array .null filteredUtxos) do
288+ logWarn' $ pprintTagSet
289+ " Some of the collateral UTxOs returned by the wallet were marked as non-spendable and ignored"
290+ (pprintUtxoMap (transactionUnspentOutputsToUtxoMap filteredUtxos))
291+ pure spendableUtxos
279292 -- otherwise, get all the utxos, filter out unspendable, and select
280293 -- collateral using internal algo, that is also used in KeyWallet
281294 Just utxoMap -> do
0 commit comments