Skip to content

Commit 0814b7c

Browse files
committed
Add labeled helper, remove withModification
1 parent c8a94c3 commit 0814b7c

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

src/Cooked/MockChain/Staged.hs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ module Cooked.MockChain.Staged
1717
everywhere,
1818
everywhere',
1919
withTweak,
20-
withModification,
2120
there,
2221
there',
22+
labeled,
2323
)
2424
where
2525

2626
import Cardano.Node.Emulator qualified as Emulator
2727
import Control.Applicative
28-
import Control.Monad (MonadPlus (..), msum)
28+
import Control.Monad (MonadPlus (..), guard, msum)
2929
import Control.Monad.Except
3030
import Control.Monad.Reader
3131
import Control.Monad.State
@@ -37,6 +37,7 @@ import Cooked.Pretty.Hashable
3737
import Cooked.Skeleton
3838
import Cooked.Tweak.Common
3939
import Data.Default
40+
import Data.Set qualified as Set
4041
import Ledger.Slot qualified as Ledger
4142
import Ledger.Tx qualified as Ledger
4243
import Plutus.Script.Utils.Address qualified as Script
@@ -191,6 +192,27 @@ everywhere' ltl = modifyLtl (LtlRelease LtlFalsity ltl)
191192
there :: (MonadModalBlockChain m) => Integer -> Tweak InterpMockChain b -> m a -> m a
192193
there 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.
196218
there' :: (MonadModal m) => Integer -> Ltl (Modification m) -> m a -> m a
@@ -210,10 +232,6 @@ there' n ltl = modifyLtl (ltlDelay n ltl)
210232
withTweak :: (MonadModalBlockChain m) => m x -> Tweak InterpMockChain a -> m x
211233
withTweak = 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

219237
singletonBuiltin :: builtin a -> Staged (LtlOp modification builtin) a

0 commit comments

Comments
 (0)