-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpabtest.GuessGame.hs
204 lines (156 loc) · 6.25 KB
/
pabtest.GuessGame.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
import ContractExample.GuessGame
import Control.Concurrent.STM as STM
import Control.Monad.Freer
import qualified Control.Monad.Freer.Error as Error
import Control.Monad.Freer.Reader
import Control.Monad.IO.Class
import Data.Aeson (FromJSON, ToJSON)
import Data.Default (Default (def))
import Data.IORef
-- (Builtin, BuiltinHandler, contractHandler, handleBuiltin)
import Data.Text.Prettyprint.Doc
import GHC.Generics (Generic)
import Ledger (Value, pubKeyHash, txId)
import qualified Ledger.Ada as Ada
-- import Ledger.Constraints (mustPayToPubKey)
import Playground.Types (FunctionSchema)
-- import Plutus.Contract.Request (Endpoint, awaitTxConfirmed, endpoint, submitTx)
import Plutus.Contract.Types (Promise (..),Contract)
import Plutus.PAB.Core (EffectHandlers (..), PABAction, PABEffects, PABEnvironment (..), handleContractDefinitionEffect, handleContractEffect, handleContractStoreEffect, handleLogMessages)
import qualified Plutus.PAB.Core.ContractInstance.STM as Instances
import Plutus.PAB.Effects.Contract.Builtin
import qualified Plutus.PAB.Effects.Contract.Builtin as Builtin
import Plutus.PAB.Effects.TimeEffect (TimeEffect (..))
import Plutus.PAB.Simulator as Simulator
( SimulatorContractHandler,
logBalances,
currentBalances,
logString,
SimulatorEffectHandlers,
runSimulationWith,
SimulatorState,
activateContract,
callEndpointOnInstance,
waitForEndpoint,
mkSimulatorHandlers,
waitNSlots
)
import Plutus.PAB.Types (PABError)
import Schema (FormSchema)
import System.IO.Unsafe
import Wallet.Emulator.Types (Wallet (..))
import qualified Data.Text as T
import qualified Data.List as Ada
type PABC contract a = Eff (PABEffects (Builtin contract) (Simulator.SimulatorState (Builtin contract))) a
type PAB a = PABC TestContracts a
runPAB :: PAB a -> IO a
runPAB = runPABC
main1 = Simulator.runSimulationWith handlers $ do
cid <- Simulator.activateContract (Wallet 1) GuessGame
waitNSlots 3
callEndpointOnInstance cid "lock" LockParams{secretWord="world", amount= Ada.adaValueOf 200}
waitNSlots 3
callEndpointOnInstance cid "guess" GuessParams{guessWord="world"}
waitNSlots 3
Simulator.logString @(Builtin TestContracts) "Balances at the end of the simulation"
b <- Simulator.currentBalances
Simulator.logBalances @(Builtin TestContracts) b
where
handlers :: SimulatorEffectHandlers (Builtin TestContracts)
handlers =
Simulator.mkSimulatorHandlers def def
$ interpret (contractHandler (Builtin.handleBuiltin @TestContracts))
main = do
initPAB simulatorHandlers
cid <- runPAB $ Simulator.activateContract (Wallet 1) GuessGame
cid2 <- runPAB $ Simulator.activateContract (Wallet 2) GuessGame
runPAB $ waitNSlots 3
runPAB $ callEndpointOnInstance cid "lock" LockParams{secretWord="world", amount= Ada.adaValueOf 200}
runPAB $ waitNSlots 3
runPAB $ callEndpointOnInstance cid2 "guess" GuessParams{guessWord="world"}
runPAB $ waitNSlots 3
runPAB $ do
Simulator.logString @(Builtin TestContracts) "Balances at the end of the simulation"
b <- Simulator.currentBalances
Simulator.logBalances @(Builtin TestContracts) b
simulatorHandlers :: EffectHandlers (Builtin TestContracts) (SimulatorState (Builtin TestContracts))
simulatorHandlers = mkSimulatorHandlers def def handler
where
handler :: SimulatorContractHandler (Builtin TestContracts)
handler = interpret (contractHandler handleBuiltin)
data TestContracts
= GuessGame
deriving (Eq, Ord, Show, Generic)
deriving anyclass (FromJSON, ToJSON)
instance Pretty TestContracts where
pretty = viaShow
instance HasDefinitions TestContracts where
getDefinitions = [GuessGame]
getSchema = \case
GuessGame -> Builtin.endpointsToSchemas @GameSchema
getContract _ = SomeBuiltin endpoints'
endpoints' :: Contract () GameSchema T.Text ()
endpoints'= endpoints >> endpoints'
{-# NOINLINE state #-}
state = unsafePerformIO $ newIORef undefined
initPAB :: EffectHandlers t env -> IO ()
initPAB effectHandlers = do
let EffectHandlers
{ initialiseEnvironment,
onStartup
} = effectHandlers
let endpointTimeout = def
(instancesState, blockchainEnv, appEnv) <- runM . Main.handleError @PABError $ initialiseEnvironment
writeIORef state $ PABEnvironment {instancesState, blockchainEnv, appEnv, effectHandlers, endpointTimeout}
runPABC onStartup
handleError :: forall e effs a. (Show e) => Eff (Error.Error e ': effs) a -> Eff effs a
handleError = interpret $ \case
Error.Error e -> error $ show e
-- | Get a 'PABRunner' that uses the current environment.
runPABC :: forall t env a. PABAction t env a -> IO a -- (Either PABError a)
runPABC action = do
h@PABEnvironment {effectHandlers = EffectHandlers {handleLogMessages, handleContractStoreEffect, handleContractEffect, handleContractDefinitionEffect}} <- readIORef state -- ask @(PABEnvironment t env)
r <-
runM $
Error.runError $
runReader h $
interpret (handleTimeEffect @t @env) $
handleLogMessages $
handleContractDefinitionEffect $
handleContractEffect $
handleContractStoreEffect action
case r of
Left x -> error $ show x
Right x -> return x
-- From Core
-- | Handle the 'TimeEffect' by reading the current slot number from
-- the blockchain env.
handleTimeEffect ::
forall t env m effs.
( Member (Reader (PABEnvironment t env)) effs,
LastMember m effs,
MonadIO m
) =>
TimeEffect
~> Eff effs
handleTimeEffect = \case
SystemTime -> do
Instances.BlockchainEnv {Instances.beCurrentSlot} <- liftIO $ readIORef state -- asks @(PABEnvironment t env) blockchainEnv
liftIO $ STM.readTVarIO beCurrentSlot