-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpabtest.seq.hs
268 lines (215 loc) · 9.02 KB
/
pabtest.seq.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
{-# 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.GuessGameIndexed
import Control.Concurrent.STM as STM
import Control.Monad.Freer
import qualified Control.Monad.Freer.Error as Error
import Control.Monad.Freer.Reader hiding (local)
import Control.Monad.IO.Class
import Control.Monad
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
import Transient.Internals
import Transient.Move.Internals
import Transient.Move.Utils
import Control.Applicative
import Data.ByteString.Lazy.Char8 as BS (unpack)
type PABC contract a = Eff (PABEffects (Builtin contract) (Simulator.SimulatorState (Builtin contract))) a
type PAB a = PABC GuessGameContracts a
runPAB :: MonadIO m => PAB a -> m a
runPAB = liftIO . runPABC
main3 = Simulator.runSimulationWith handlers $ do
cid <- Simulator.activateContract (Wallet 1) Lock
cid2 <- Simulator.activateContract (Wallet 2) Lock
cid3 <- Simulator.activateContract (Wallet 3) Guess
cid4 <- Simulator.activateContract (Wallet 4) Guess
waitNSlots 3
callEndpointOnInstance cid "lock" LockParams{secretWord="world", amount= Ada.adaValueOf 200,lockIndex=1}
waitNSlots 3
callEndpointOnInstance cid2 "lock" LockParams{secretWord="world2", amount= Ada.adaValueOf 200,lockIndex=2}
waitNSlots 3
callEndpointOnInstance cid3 "guess" GuessParams{guessWord="world",guessIndex=1}
waitNSlots 3
Simulator.logString @(Builtin GuessGameContracts) "SENDING SECOND GUESS"
callEndpointOnInstance cid3 "guess" GuessParams{guessWord="world2",guessIndex=1}
Simulator.logString @(Builtin GuessGameContracts) "END SENDING SECOND GUESS"
waitNSlots 10
Simulator.logString @(Builtin GuessGameContracts) "Balances at the end of the simulation"
b <- Simulator.currentBalances
Simulator.logBalances @(Builtin GuessGameContracts) b
where
handlers :: SimulatorEffectHandlers (Builtin GuessGameContracts)
handlers =
Simulator.mkSimulatorHandlers def def
$ interpret (contractHandler (Builtin.handleBuiltin @GuessGameContracts))
locks = unsafePerformIO $ newIORef ([] :: [InputData])
main= keep $ initNode $ do
local $ initPAB simulatorHandlers
firstCont
wallet <- minput "wallet" enter "your wallet id"
local $ setState wallet
gameSequence <|> availableOptions <|> balances
where
gameSequence = do
(amo ::Int,word, hint) <- minput "lock" "enter lock amount, the key and a hint. Example: 100 myKey \"number between 0..1000\""
local $ do
wallet <- getState
runPAB $ do
cid <- runPAB $ Simulator.activateContract (Wallet wallet) Lock
callEndpointOnInstance cid "lock" LockParams{secretWord=BS.unpack word, amount= Ada.adaValueOf $ fromIntegral amo,lockIndex=ind}
return()
minput "guess" ("guess a lock for " ++ hint) <|> addToOptions :: Cloud ()
local $ do
wallet <- getState
runPAB $ do
cid3 <- Simulator.activateContract (Wallet wallet) Guess
callEndpointOnInstance cid3 "guess" GuessParams{guessWord=guessw,guessIndex=index}
waitNSlots 3
availableOptions = local $ do
inputdatas <- liftIO $ readIORef locks -- `onNothing` return [] -- do minput "" "no lock has been done yet. Do it yourself!" ; empty
foldr (<|>) empty $ map (\(InputData msg url) -> sendURL msg url) inputdatas
balances= do
minput "bal" "display account balances" :: Cloud ()
local $ runPAB $ do
Simulator.logString @(Builtin GuessGameContracts) "Balances at the end of the simulation"
b <- Simulator.currentBalances
Simulator.logBalances @(Builtin GuessGameContracts) b
return()
addToOptions :: Loggable a => Cloud a
addToOptions= local $ do
idata <- getState
liftIO $ atomicModifyIORef' locks (\idatas -> (idata:idatas,()))
empty
simulatorHandlers :: EffectHandlers (Builtin GuessGameContracts) (SimulatorState (Builtin GuessGameContracts))
simulatorHandlers = mkSimulatorHandlers def def handler
where
handler :: SimulatorContractHandler (Builtin GuessGameContracts)
handler = interpret (contractHandler handleBuiltin)
data GuessGameContracts = Lock
| Guess
deriving (Eq, Ord, Show, Generic, FromJSON, ToJSON)
instance Pretty GuessGameContracts where
pretty = viaShow
instance HasDefinitions GuessGameContracts where
getDefinitions = [Lock, Guess]
getContract = getGuessGameContracts
getSchema = getGuessGameSchema
getGuessGameSchema :: GuessGameContracts -> [FunctionSchema FormSchema]
getGuessGameSchema = \case
Lock -> Builtin.endpointsToSchemas @GameSchema
Guess -> Builtin.endpointsToSchemas @GameSchema
getGuessGameContracts :: GuessGameContracts -> SomeBuiltin
getGuessGameContracts = \case
Lock -> SomeBuiltin $ (lock :: Promise () GameSchema T.Text ())
Guess -> SomeBuiltin $ (guess :: Promise () GameSchema T.Text ())
{-
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 :: MonadIO m => EffectHandlers t env -> m ()
initPAB effectHandlers = liftIO $ 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