-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObservableStateNoEndpoint.hs
executable file
·105 lines (88 loc) · 4.03 KB
/
ObservableStateNoEndpoint.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env cabal
{- --------------------------------------------------------------- -}
{- How the EmulatorTrace can know the state of a Contract ? -}
{- -}
{- Use the Contract monad writer and `observableState` from -}
{- `Plutus.Trace`. This function gets a handle's contract state -}
{- and reports the observable state field of the instance. -}
{- -}
{- Note that there is no observable state right after calling -}
{- `activateContractWallet` as the instance may not exist yet. -}
{- Avoid name shadowing for contract handles that can hide such -}
{- situation. -}
{- -}
{- A useful type for the Contract writer is `Last a` -}
{- from `Data.Monoid`. -}
{- -}
{- --------------------------------------------------------------- -}
{- cabal:
build-depends:
, base
, data-default
, freer-extras
, plutus-contract
, text
-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE TypeApplications #-}
import Control.Monad ( void )
import Control.Monad.Freer.Extras.Log qualified
as Extras ( logInfo )
import Data.Default ( def )
import Data.Monoid ( Last(..) )
import Data.Text ( Text )
import System.IO ( stdout )
import Prelude ( String, Maybe(..), IO, (.), ($), (<>), (>>), (>>=), show )
import Plutus.Contract ( Contract, EmptySchema, tell )
import Plutus.Contract.Request qualified
as Contract ( waitNSlots )
import Plutus.Contract.Test ( w1 )
import Plutus.Trace
( EmulatorTrace
, TraceConfig(..)
, runEmulatorTraceIO'
, activateContractWallet
, waitNSlots
, observableState
)
import Plutus.Trace.Emulator.Types ( UserThreadMsg(..) )
import Wallet.Emulator.MultiAgent ( EmulatorEvent'(..) )
{- --------------------------------------------------------------- -}
{- Emulator TraceConfig -}
{- --------------------------------------------------------------- -}
simpleTraceConfig :: TraceConfig
simpleTraceConfig =
TraceConfig
{ showEvent = simpleShowEvent
, outputHandle = stdout
}
where
simpleShowEvent :: EmulatorEvent' -> Maybe String
simpleShowEvent = \case
UserThreadEvent (UserLog msg) -> Just $ "*** USER LOG: " <> msg
_ -> Nothing
{- --------------------------------------------------------------- -}
{- Off-chain code -}
{- --------------------------------------------------------------- -}
action :: Contract (Last String) EmptySchema Text ()
action =
do
tell (Last $ Just "Begin")
void (Contract.waitNSlots 3)
tell (Last $ Just "End")
{- --------------------------------------------------------------- -}
{- Main code -}
{- --------------------------------------------------------------- -}
emulatorTrace :: EmulatorTrace ()
emulatorTrace = do
h1 <- activateContractWallet w1 action
waitNSlots 1 >> showObservableState h1 -- wait for contract instance creation
waitNSlots 5 >> showObservableState h1 -- wait past action delay
where
showObservableState h =
observableState h >>= Extras.logInfo @String . show . getLast
main :: IO ()
main = runEmulatorTraceIO' simpleTraceConfig def emulatorTrace