Skip to content

Commit a869d25

Browse files
committed
use lens in some functions
1 parent 6600f87 commit a869d25

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,5 @@ geth-node/chaindata/history
6565

6666
# debug configs
6767
examples/debug/
68+
69+
visualization/

src/BotPlutusInterface/CoinSelection.hs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,34 @@
33

44
module BotPlutusInterface.CoinSelection (valueToVec, valuesToVecs, selectTxIns) where
55

6-
import Control.Lens (ix, (^?))
6+
import Control.Lens (foldOf, folded, ix, over, to, uncons, (^..), (^?), _Just)
77
import Control.Monad.Freer (Eff, Member)
8-
98
import Control.Monad.Trans.Class (lift)
109
import Control.Monad.Trans.Either (hoistEither, newEitherT, runEitherT)
1110
import Data.Either.Combinators (isRight, maybeToRight)
1211
import Data.Kind (Type)
1312
import Data.List qualified as List
1413
import Data.Map (Map)
1514
import Data.Map qualified as Map
16-
import Data.Maybe (mapMaybe)
1715
import Data.Set (Set)
1816
import Data.Set qualified as Set
1917
import Data.Text (Text, pack)
2018
import Data.Vector (Vector)
2119
import 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)
2427
import Ledger.Value qualified as Value
2528

2629
import Plutus.V1.Ledger.Api (
2730
Credential (PubKeyCredential, ScriptCredential),
2831
)
29-
3032
import BotPlutusInterface.Effects (PABEffect, printBpiLog)
3133
import BotPlutusInterface.Types (LogLevel (Debug), LogType (CoinSelectionLog))
32-
3334
import Prettyprinter (pretty, (<+>))
3435
import 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
238237
valueToVec :: Set AssetClass -> Value -> Either Text (Vector Integer)
239238
valueToVec 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

243242
valuesToVecs :: Set AssetClass -> [Value] -> Vector (Vector Integer)
244243
valuesToVecs 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
258257
txOutToTxIn :: (TxOutRef, TxOut) -> Either Text TxIn
259258
txOutToTxIn (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

Comments
 (0)