33
44module BotPlutusInterface.CoinSelection (valueToVec , valuesToVecs , selectTxIns ) where
55
6- import Control.Lens (ix , (^?) )
6+ import Control.Lens (foldOf , folded , ix , over , to , uncons , (^..) , (^?) , _Just )
77import Control.Monad.Freer (Eff , Member )
8-
98import Control.Monad.Trans.Class (lift )
109import Control.Monad.Trans.Either (hoistEither , newEitherT , runEitherT )
1110import Data.Either.Combinators (isRight , maybeToRight )
1211import Data.Kind (Type )
1312import Data.List qualified as List
1413import Data.Map (Map )
1514import Data.Map qualified as Map
16- import Data.Maybe (mapMaybe )
1715import Data.Set (Set )
1816import Data.Set qualified as Set
1917import Data.Text (Text , pack )
2018import Data.Vector (Vector )
2119import Data.Vector qualified as Vec
22-
23- import Ledger hiding (outValue )
20+ import Ledger qualified
21+ import Ledger.Tx (
22+ TxIn (.. ),
23+ TxOut (.. ),
24+ TxOutRef (.. ),
25+ )
26+ import Ledger.Value (AssetClass , Value )
2427import Ledger.Value qualified as Value
2528
2629import Plutus.V1.Ledger.Api (
2730 Credential (PubKeyCredential , ScriptCredential ),
2831 )
29-
3032import BotPlutusInterface.Effects (PABEffect , printBpiLog )
3133import BotPlutusInterface.Types (LogLevel (Debug ), LogType (CoinSelectionLog ))
32-
3334import Prettyprinter (pretty , (<+>) )
3435import Prelude
3536
@@ -49,14 +50,14 @@ selectTxIns originalTxIns utxosIndex outValue =
4950 runEitherT $ do
5051 let txInsValue :: Value
5152 txInsValue =
52- mconcat $ map txOutValue $ mapMaybe ((`Map.lookup` utxosIndex) . txInRef) $ Set. toList originalTxIns
53+ foldOf (folded . to ((`Map.lookup` utxosIndex) . txInRef) . folded . to txOutValue) originalTxIns
5354
5455 allAssetClasses :: Set AssetClass
5556 allAssetClasses =
56- uniqueAssetClasses $ txInsValue : outValue : map (txOutValue . snd ) ( Map. toList utxosIndex)
57+ uniqueAssetClasses $ txInsValue : outValue : utxosIndex ^.. folded . to txOutValue
5758
5859 txInRefs :: [TxOutRef ]
59- txInRefs = map txInRef $ Set. toList originalTxIns
60+ txInRefs = originalTxIns ^.. folded . to txInRef
6061
6162 remainingUtxos :: [(TxOutRef , TxOut )]
6263 remainingUtxos =
@@ -82,7 +83,7 @@ selectTxIns originalTxIns utxosIndex outValue =
8283 lift $ printBpiLog @ w (Debug [CoinSelectionLog ]) $ " " <+> " Selected UTxOs Index: " <+> pretty selectedUtxosIdxs
8384
8485 let selectedUtxos :: [(TxOutRef , TxOut )]
85- selectedUtxos = mapMaybe (\ idx -> remainingUtxos ^? ix idx) selectedUtxosIdxs
86+ selectedUtxos = selectedUtxosIdxs ^.. folded . to (\ idx -> remainingUtxos ^? ix idx) . folded
8687
8788 selectedTxIns <- hoistEither $ mapM txOutToTxIn selectedUtxos
8889
@@ -141,10 +142,7 @@ greedySearch stopSearch outVec txInsVec utxosVec
141142 then return $ Right mempty
142143 else runEitherT $ do
143144 selectedUtxoVec <-
144- hoistEither $
145- maybeToRight
146- " Out of bounds"
147- (utxosVec ^? ix idx)
145+ hoistEither $ maybeToRight " Out of bounds" (utxosVec ^? ix idx)
148146 newTxInsVec' <- hoistEither $ addVec newTxInsVec selectedUtxoVec
149147
150148 lift $
@@ -175,7 +173,8 @@ greedyPruning stopSearch outVec txInsVec utxosVec
175173 selectedUtxosIdx <- newEitherT $ greedySearch @ w stopSearch outVec txInsVec utxosVec
176174
177175 let revSelectedUtxosVec :: [Vector Integer ]
178- revSelectedUtxosVec = List. reverse $ mapMaybe (\ idx -> utxosVec ^? ix idx) selectedUtxosIdx
176+ revSelectedUtxosVec =
177+ List. reverse $ selectedUtxosIdx ^.. folded . to (\ idx -> utxosVec ^? ix idx) . folded
179178
180179 revSelectedUtxosIdx :: [Int ]
181180 revSelectedUtxosIdx = List. reverse selectedUtxosIdx
@@ -238,7 +237,7 @@ zeroVec n = Vec.fromList $ replicate n 0
238237valueToVec :: Set AssetClass -> Value -> Either Text (Vector Integer )
239238valueToVec allAssetClasses v =
240239 maybeToRight " Error: Not able to uncons from empty vector." $
241- fmap fst $ Vec. uncons $ valuesToVecs allAssetClasses [v]
240+ (over _Just fst . uncons) $ valuesToVecs allAssetClasses [v]
242241
243242valuesToVecs :: Set AssetClass -> [Value ] -> Vector (Vector Integer )
244243valuesToVecs allAssetClasses values = Vec. fromList $ map toVec values
@@ -257,6 +256,6 @@ uniqueAssetClasses = Set.fromList . concatMap valueToAssetClass
257256-- Converting a chain index transaction output to a transaction input type
258257txOutToTxIn :: (TxOutRef , TxOut ) -> Either Text TxIn
259258txOutToTxIn (txOutRef, txOut) =
260- case addressCredential (txOutAddress txOut) of
261- PubKeyCredential _ -> Right $ pubKeyTxIn txOutRef
259+ case Ledger. addressCredential (txOutAddress txOut) of
260+ PubKeyCredential _ -> Right $ Ledger. pubKeyTxIn txOutRef
262261 ScriptCredential _ -> Left " Cannot covert a script output to TxIn"
0 commit comments