@@ -6,9 +6,10 @@ import Control.Lens (Cons, cons, ix, uncons, (^?))
66import Control.Monad.Freer (Eff , Member )
77
88import Control.Monad.Trans.Class (lift )
9- import Control.Monad.Trans.Either (hoistEither , runEitherT )
9+ import Control.Monad.Trans.Either (hoistEither , runEitherT , newEitherT )
1010import Data.Either.Combinators (isRight , maybeToRight )
1111import Data.Kind (Type )
12+ import Data.List qualified as List
1213import Data.Map (Map )
1314import Data.Map qualified as Map
1415import Data.Maybe (mapMaybe )
@@ -26,7 +27,7 @@ import Plutus.V1.Ledger.Api (
2627 )
2728
2829import BotPlutusInterface.Effects (PABEffect , printBpiLog )
29- import BotPlutusInterface.Types (LogLevel (Notice ))
30+ import BotPlutusInterface.Types (LogLevel (Debug ))
3031
3132import Prettyprinter (pretty , (<+>) )
3233import Prelude
@@ -43,8 +44,7 @@ selectTxIns ::
4344 Eff effs (Either Text (Set TxIn ))
4445selectTxIns originalTxIns utxosIndex outValue =
4546 runEitherT $ do
46- lift $ printBpiLog @ w Notice $ pretty (Map. toList utxosIndex)
47-
47+
4848 let txInsValue :: Value
4949 txInsValue =
5050 mconcat $ map txOutValue $ mapMaybe ((`Map.lookup` utxosIndex) . txInRef) $ Set. toList originalTxIns
@@ -63,7 +63,7 @@ selectTxIns originalTxIns utxosIndex outValue =
6363 (\ k v -> k `notElem` txInRefs && isRight (txOutToTxIn (k, v)))
6464 utxosIndex
6565
66- lift $ printBpiLog @ w Notice $ " \n\n Remaining UTxOs: " <+> pretty remainingUtxos <+> " \n\n "
66+ lift $ printBpiLog @ w Debug $ " Remaining UTxOs: " <+> pretty remainingUtxos <+> " \n\n "
6767
6868 txInsVec <-
6969 hoistEither $
@@ -73,20 +73,18 @@ selectTxIns originalTxIns utxosIndex outValue =
7373
7474 outVec <- hoistEither $ valueToVec allAssetClasses outValue
7575
76- lift $ printBpiLog @ w Notice $ " IsSufficient: " <+> pretty (isSufficient outVec txInsVec) <+> " \n\n "
77-
7876 remainingUtxosVec <- hoistEither $ mapM (valueToVec allAssetClasses . txOutValue . snd ) remainingUtxos
7977
80- selectedUtxosIdxs <- hoistEither $ selectTxIns' Greedy (isSufficient outVec) outVec txInsVec remainingUtxosVec
78+ selectedUtxosIdxs <- newEitherT $ selectTxIns' @ w Greedy (isSufficient outVec) outVec txInsVec remainingUtxosVec
8179
82- lift $ printBpiLog @ w Notice $ " \n\n " <+> " Selected UTxOs Index: " <+> pretty selectedUtxosIdxs <+> " \n\n "
80+ lift $ printBpiLog @ w Debug $ " " <+> " Selected UTxOs Index: " <+> pretty selectedUtxosIdxs <+> " \n\n "
8381
8482 let selectedUtxos :: [(TxOutRef , TxOut )]
8583 selectedUtxos = mapMaybe (\ idx -> remainingUtxos ^? ix (fromInteger idx)) selectedUtxosIdxs
8684
8785 selectedTxIns <- hoistEither $ mapM txOutToTxIn selectedUtxos
8886
89- lift $ printBpiLog @ w Notice $ " Selected TxIns: " <+> pretty selectedTxIns <+> " \n\n "
87+ lift $ printBpiLog @ w Debug $ " Selected TxIns: " <+> pretty selectedTxIns <+> " \n\n "
9088
9189 return $ originalTxIns <> Set. fromList selectedTxIns
9290 where
@@ -95,26 +93,51 @@ selectTxIns originalTxIns utxosIndex outValue =
9593 && txInsVec /= zeroVec (toInteger $ length txInsVec)
9694
9795selectTxIns' ::
96+ forall (w :: Type ) (effs :: [Type -> Type ]).
97+ Member (PABEffect w ) effs =>
9898 Search ->
9999 (Vector Integer -> Bool ) ->
100100 Vector Integer ->
101101 Vector Integer ->
102102 [Vector Integer ] ->
103- Either Text [Integer ]
103+ Eff effs ( Either Text [Integer ])
104104selectTxIns' Greedy stopSearch outVec txInsVec utxosVec
105- | null utxosVec || stopSearch txInsVec = Right mempty
105+ | null utxosVec = printBpiLog @ w Debug " The list of remanining UTxO vectors in null.\n\n "
106+ >> return (Right mempty )
107+
108+ | stopSearch txInsVec = printBpiLog @ w Debug " Stopping search early.\n\n " >>
109+ return (Right mempty )
106110 | otherwise =
107- do
108- utxosDist <- Vec. fromList . map (l2norm outVec) <$> mapM (addVec txInsVec) utxosVec
109- let minIndex = toInteger $ Vec. minIndex utxosDist
110-
111- (selectedUtxoVec, remainingUtxosVec) <- pop utxosVec minIndex
111+ runEitherT $ do
112+
113+ x <- hoistEither $ mapM (addVec txInsVec) utxosVec
114+ utxosDist <- hoistEither $ mapM (l2norm outVec) x
115+
116+ let sortedDist :: [(Integer , Float )]
117+ sortedDist = List. sortBy (\ a b -> compare (snd a) (snd b))
118+ $ zip [0 .. toInteger (length utxosVec) - 1 ] utxosDist
112119
113- newTxInsVec <- addVec txInsVec selectedUtxoVec
120+ newEitherT $ loop sortedDist txInsVec
114121
122+ where
123+
124+ loop :: [(Integer , Float )] -> Vector Integer -> Eff effs (Either Text [Integer ])
125+ loop [] _ = return $ Right mempty
126+ loop ((idx,_): remSortedDist) newTxInsVec =
115127 if stopSearch newTxInsVec
116- then return [minIndex]
117- else (minIndex : ) <$> selectTxIns' Greedy stopSearch outVec newTxInsVec remainingUtxosVec
128+ then return $ Right mempty
129+ else
130+ runEitherT $ do
131+ selectedUtxoVec <- hoistEither $ maybeToRight " Out of bounds"
132+ (utxosVec ^? ix (fromInteger idx))
133+ newTxInsVec' <- hoistEither $ addVec newTxInsVec selectedUtxoVec
134+
135+ lift $ printBpiLog @ w Debug
136+ $ " Loop Info: Stop search -> " <+> pretty (stopSearch newTxInsVec')
137+ <+> " Selected UTxo Idx : " <+> pretty idx
138+ <+> " \n\n "
139+
140+ (idx: ) <$> newEitherT (loop remSortedDist newTxInsVec')
118141
119142l2norm :: Vector Integer -> Vector Integer -> Either Text Float
120143l2norm v1 v2
0 commit comments