@@ -17,15 +17,15 @@ module Cooked.MockChain.Staged
1717 everywhere ,
1818 everywhere' ,
1919 withTweak ,
20- withModification ,
2120 there ,
2221 there' ,
22+ labeled ,
2323 )
2424where
2525
2626import Cardano.Node.Emulator qualified as Emulator
2727import Control.Applicative
28- import Control.Monad (MonadPlus (.. ), msum )
28+ import Control.Monad (MonadPlus (.. ), guard , msum )
2929import Control.Monad.Except
3030import Control.Monad.Reader
3131import Control.Monad.State
@@ -37,6 +37,7 @@ import Cooked.Pretty.Hashable
3737import Cooked.Skeleton
3838import Cooked.Tweak.Common
3939import Data.Default
40+ import Data.Set qualified as Set
4041import Ledger.Slot qualified as Ledger
4142import Ledger.Tx qualified as Ledger
4243import Plutus.Script.Utils.Address qualified as Script
@@ -191,6 +192,27 @@ everywhere' ltl = modifyLtl (LtlRelease LtlFalsity ltl)
191192there :: (MonadModalBlockChain m ) => Integer -> Tweak InterpMockChain b -> m a -> m a
192193there n = there' n . LtlAtom . UntypedTweak
193194
195+ -- | Apply a tweak to every transaction which has a specific label. This can
196+ -- be useful to apply a tweak to every transaction in a set of associated
197+ -- transactions.
198+ --
199+ -- >
200+ -- > someEndpoint = do
201+ -- > ..
202+ -- > validateTxSkel' txSkelTemplate
203+ -- > { txSkelLabels =
204+ -- > [TxSkelLabel @Text "InitialMinting", TxSkelLabel @Text "AuctionWorkflow"]
205+ -- > }
206+ -- >
207+ -- > someTest = someEndpoint & labled @Text "InitialMinting" someTweak
208+ labeled :: (LabelConstrs lbl , MonadModalBlockChain m ) => lbl -> Tweak InterpMockChain b -> m a -> m a
209+ labeled lbl tweak = everywhere (hasLabel >> tweak)
210+ where
211+ hasLabel = do
212+ -- using 'hasLabelTweak' causes a cyclic dependency
213+ lbls <- viewTweak txSkelLabelL
214+ guard (Set. member (TxSkelLabel lbl) lbls)
215+
194216-- | Apply an Ltl modification to the (0-indexed) nth transaction in a
195217-- given trace. Successful when this transaction exists and can be modified.
196218there' :: (MonadModal m ) => Integer -> Ltl (Modification m ) -> m a -> m a
@@ -210,10 +232,6 @@ there' n ltl = modifyLtl (ltlDelay n ltl)
210232withTweak :: (MonadModalBlockChain m ) => m x -> Tweak InterpMockChain a -> m x
211233withTweak = flip (there 0 )
212234
213- -- | Apply modification to the next transaction in a given trace. See 'withTweak'
214- withModification :: (MonadModal m ) => m a -> Ltl (Modification m ) -> m a
215- withModification = flip (there' 0 )
216-
217235-- * 'MonadBlockChain' and 'MonadMockChain' instances
218236
219237singletonBuiltin :: builtin a -> Staged (LtlOp modification builtin ) a
0 commit comments