Skip to content

Commit 1d42199

Browse files
committed
Add a warning when we filter some collateral utxos
1 parent cbbab61 commit 1d42199

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/Internal/BalanceTx/BalanceTx.purs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Ctl.Internal.BalanceTx
44

55
import Prelude
66

7+
import Contract.Log (logWarn')
78
import Control.Monad.Error.Class (liftMaybe)
89
import Control.Monad.Except.Trans (ExceptT(ExceptT), except, runExceptT)
910
import 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+
)
9599
import 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, (??))
119123
import Ctl.Internal.Partition (equipartition, partition)
120124
import Ctl.Internal.Plutus.Conversion (fromPlutusUtxoMap)
121125
import 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

src/Internal/Cardano/Types/TransactionUnspentOutput.purs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
module Ctl.Internal.Cardano.Types.TransactionUnspentOutput
22
( TransactionUnspentOutput(TransactionUnspentOutput)
3+
, transactionUnspentOutputsToUtxoMap
34
) where
45

56
import Prelude
67

78
import Aeson (class EncodeAeson)
8-
import Ctl.Internal.Cardano.Types.Transaction (TransactionOutput)
9+
import Ctl.Internal.Cardano.Types.Transaction (TransactionOutput, UtxoMap)
910
import Ctl.Internal.Types.Transaction (TransactionInput)
1011
import Data.Generic.Rep (class Generic)
12+
import Data.Map as Map
1113
import Data.Newtype (class Newtype)
1214
import Data.Show.Generic (genericShow)
15+
import Data.Tuple (Tuple(Tuple))
1316

1417
newtype TransactionUnspentOutput = TransactionUnspentOutput
1518
{ input :: TransactionInput
@@ -23,3 +26,7 @@ derive newtype instance EncodeAeson TransactionUnspentOutput
2326

2427
instance Show TransactionUnspentOutput where
2528
show = genericShow
29+
30+
transactionUnspentOutputsToUtxoMap :: Array TransactionUnspentOutput -> UtxoMap
31+
transactionUnspentOutputsToUtxoMap = Map.fromFoldable <<< map
32+
\(TransactionUnspentOutput { input, output }) -> Tuple input output

0 commit comments

Comments
 (0)