@@ -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 , (^?) )
4244import Control.Monad (void )
4345import qualified Data.ByteString.Char8 as C
4446import Data.Map (Map )
4547import qualified Data.Map as Map
4648import 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 )
4951import qualified Ledger.Ada as Ada
5052import qualified Ledger.Constraints as Constraints
51- import Ledger.Tx ( ChainIndexTxOut ( .. ))
53+ import qualified Ledger
5254import Playground.Contract
5355import Plutus.Contract
5456import Plutus.Contract.Trace as X
@@ -57,7 +59,10 @@ import PlutusTx.Prelude hiding (pure, (<$>))
5759import qualified Prelude as Haskell
5860import Plutus.Trace.Emulator (EmulatorTrace )
5961import 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
6267newtype HashedString = HashedString BuiltinByteString deriving newtype (PlutusTx.ToData , PlutusTx.FromData , PlutusTx.UnsafeFromData )
6368
@@ -80,7 +85,7 @@ gameInstance :: Scripts.TypedValidator Game
8085gameInstance = 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
104109gameValidator = 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
111116data 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
126131lock :: AsContractError e => Promise () GameSchema e ()
127132lock = 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
157163findSecretWordValue =
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
162168secretWordValue 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
166172game :: AsContractError e => Contract () GameSchema e ()
0 commit comments