Skip to content
This repository was archived by the owner on May 20, 2024. It is now read-only.

Commit 6972eab

Browse files
committed
Fix the game example
1 parent 38c0f1c commit 6972eab

File tree

4 files changed

+23
-35
lines changed

4 files changed

+23
-35
lines changed

examples/src/Plutus/Contracts/Game.hs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,18 @@ module Plutus.Contracts.Game
3939
, correctGuessTrace
4040
) where
4141

42+
import Cardano.Node.Emulator.Params (testnet)
43+
import Control.Lens (_2, (^?))
4244
import Control.Monad (void)
4345
import qualified Data.ByteString.Char8 as C
4446
import Data.Map (Map)
4547
import qualified Data.Map as Map
4648
import Data.Maybe (catMaybes)
47-
import Plutus.V1.Ledger.Api (Address, ScriptContext, Validator, Value, Datum(Datum))
48-
import qualified Ledger
49+
import Plutus.V2.Ledger.Api (ScriptContext, Validator, Value, Datum(Datum))
50+
import Ledger (CardanoAddress, DecoratedTxOut)
4951
import qualified Ledger.Ada as Ada
5052
import qualified Ledger.Constraints as Constraints
51-
import Ledger.Tx (ChainIndexTxOut (..))
53+
import qualified Ledger
5254
import Playground.Contract
5355
import Plutus.Contract
5456
import Plutus.Contract.Trace as X
@@ -57,7 +59,10 @@ import PlutusTx.Prelude hiding (pure, (<$>))
5759
import qualified Prelude as Haskell
5860
import Plutus.Trace.Emulator (EmulatorTrace)
5961
import qualified Plutus.Trace.Emulator as Trace
60-
import qualified Plutus.Script.Utils.V1.Typed.Scripts as Scripts
62+
import qualified Plutus.Script.Utils.V2.Address as Address
63+
import qualified Plutus.Script.Utils.V2.Typed.Scripts as Scripts
64+
import qualified Plutus.Script.Utils.V1.Typed.Scripts as V1
65+
import qualified Ledger.Typed.Scripts as Script
6166

6267
newtype HashedString = HashedString BuiltinByteString deriving newtype (PlutusTx.ToData, PlutusTx.FromData, PlutusTx.UnsafeFromData)
6368

@@ -80,7 +85,7 @@ gameInstance :: Scripts.TypedValidator Game
8085
gameInstance = Scripts.mkTypedValidator @Game
8186
$$(PlutusTx.compile [|| validateGuess ||])
8287
$$(PlutusTx.compile [|| wrap ||]) where
83-
wrap = Scripts.mkUntypedValidator @HashedString @ClearString
88+
wrap = V1.mkUntypedValidator @ScriptContext @HashedString @ClearString
8489

8590
-- create a data script for the guessing game by hashing the string
8691
-- and lifting the hash to its on-chain representation
@@ -104,13 +109,13 @@ gameValidator :: Validator
104109
gameValidator = Scripts.validatorScript gameInstance
105110

106111
-- | The address of the game (the hash of its validator script)
107-
gameAddress :: Address
108-
gameAddress = Ledger.scriptAddress gameValidator
112+
gameAddress :: CardanoAddress
113+
gameAddress = Address.mkValidatorCardanoAddress testnet $ Script.validatorScript gameInstance
109114

110115
-- | Parameters for the "lock" endpoint
111116
data LockParams = LockParams
112-
{ secretWord :: Haskell.String
113-
, amount :: Value
117+
{ secretWord :: !Haskell.String
118+
, amount :: !Value
114119
}
115120
deriving stock (Haskell.Eq, Haskell.Show, Generic)
116121
deriving anyclass (FromJSON, ToJSON, ToSchema, ToArgument)
@@ -126,7 +131,7 @@ newtype GuessParams = GuessParams
126131
lock :: AsContractError e => Promise () GameSchema e ()
127132
lock = endpoint @"lock" @LockParams $ \(LockParams secret amt) -> do
128133
logInfo @Haskell.String $ "Pay " <> Haskell.show amt <> " to the script"
129-
let tx = Constraints.mustPayToTheScript (hashString secret) amt
134+
let tx = Constraints.mustPayToTheScriptWithDatumInTx (hashString secret) amt
130135
void (submitTxConstraints gameInstance tx)
131136

132137
-- | The "guess" contract endpoint. See note [Contract endpoints]
@@ -137,7 +142,7 @@ guess = endpoint @"guess" @GuessParams $ \(GuessParams theGuess) -> do
137142
utxos <- fundsAtAddressGeq gameAddress (Ada.lovelaceValueOf 1)
138143

139144
let redeemer = clearString theGuess
140-
tx = collectFromScript utxos redeemer
145+
tx = Constraints.collectFromTheScript utxos redeemer
141146

142147
-- Log a message saying if the secret word was correctly guessed
143148
let hashedSecretWord = findSecretWordValue utxos
@@ -153,14 +158,15 @@ guess = endpoint @"guess" @GuessParams $ \(GuessParams theGuess) -> do
153158
void (submitTxConstraintsSpending gameInstance utxos tx)
154159

155160
-- | Find the secret word in the Datum of the UTxOs
156-
findSecretWordValue :: Map TxOutRef ChainIndexTxOut -> Maybe HashedString
161+
-- | Find the secret word in the Datum of the UTxOs
162+
findSecretWordValue :: Map TxOutRef DecoratedTxOut -> Maybe HashedString
157163
findSecretWordValue =
158164
listToMaybe . catMaybes . Map.elems . Map.map secretWordValue
159165

160166
-- | Extract the secret word in the Datum of a given transaction output is possible
161-
secretWordValue :: ChainIndexTxOut -> Maybe HashedString
167+
secretWordValue :: DecoratedTxOut -> Maybe HashedString
162168
secretWordValue o = do
163-
Datum d <- either (const Nothing) Just (_ciTxOutDatum o)
169+
Datum d <- o ^? Ledger.decoratedTxOutDatum . _2 . Ledger.datumInDatumFromQuery
164170
PlutusTx.fromBuiltinData d
165171

166172
game :: AsContractError e => Contract () GameSchema e ()

examples/test/Spec/Game.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ tests = testGroup "game"
3737
$ void $ Trace.activateContractWallet w1 (lock @ContractError)
3838

3939
, checkPredicate "'lock' endpoint submits a transaction"
40-
(anyTx theContract t1)
40+
(anyUnbalancedTx theContract t1)
4141
$ do
4242
hdl <- Trace.activateContractWallet w1 theContract
43+
void $ Trace.waitNSlots 1
4344
Trace.callEndpoint @"lock" hdl (LockParams "secret" (Ada.adaValueOf 10))
4445

4546
, checkPredicate "'guess' endpoint is available after locking funds"

pab/Main.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import qualified Plutus.PAB.Webserver.Server as PAB.Server
2828
import Plutus.Contracts.Game as Game
2929
import Plutus.Trace.Emulator.Extract (writeScriptsTo, ScriptsConfig (..), Command (..))
3030
import Prettyprinter (Pretty (..), viaShow)
31-
import Ledger.Index (ValidatorMode(..))
3231
import qualified Wallet.Emulator.Wallet as Wallet
3332

3433
main :: IO ()

plutus-starter.cabal

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ library
4747
base >= 4.9 && < 5,
4848
aeson -any,
4949
bytestring -any,
50+
cardano-node-emulator -any,
5051
containers -any,
5152
freer-extras -any,
5253
playground-common -any,
@@ -82,22 +83,3 @@ test-suite plutus-example-projects-test
8283
text -any,
8384
tasty-hedgehog >=0.2.0.0
8485

85-
86-
executable plutus-starter-pab
87-
import: lang
88-
main-is: Main.hs
89-
hs-source-dirs: pab
90-
ghc-options:
91-
-threaded
92-
build-depends:
93-
base >= 4.9 && < 5,
94-
data-default -any,
95-
plutus-contract -any,
96-
plutus-pab -any,
97-
plutus-starter -any,
98-
aeson -any,
99-
freer-simple -any,
100-
prettyprinter -any,
101-
freer-extras -any,
102-
plutus-ledger -any,
103-
openapi3 -any,

0 commit comments

Comments
 (0)