{-# LANGUAGE DataKinds         #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE TemplateHaskell   #-}
{-# LANGUAGE TypeApplications  #-}
{-# LANGUAGE TypeFamilies      #-}

module PlutusExample.PlutusVersion1.DatumRedeemerGuess
  ( guessScript
  , guessScriptStake
  , datumRedeemerGuessScriptShortBs
  ) where

import Prelude hiding (($), (&&), (==))

import Cardano.Api.Shelley (PlutusScript (..), PlutusScriptV1)

import Codec.Serialise
import Data.ByteString.Lazy qualified as LBS
import Data.ByteString.Short qualified as SBS

import Plutus.V1.Ledger.Api qualified as Plutus
import PlutusTx (toBuiltinData)
import PlutusTx qualified
import PlutusTx.Prelude hiding (Semigroup (..), unless, (.))

{-# INLINABLE mkValidator #-}
mkValidator :: BuiltinData -> BuiltinData -> BuiltinData  -> ()
mkValidator :: BuiltinData -> BuiltinData -> BuiltinData -> ()
mkValidator BuiltinData
datum BuiltinData
redeemer BuiltinData
_txContext
  |    BuiltinData
datum    BuiltinData -> BuiltinData -> Bool
forall a. Eq a => a -> a -> Bool
== Integer -> BuiltinData
forall a. ToData a => a -> BuiltinData
toBuiltinData (Integer
42 :: Integer)
    Bool -> Bool -> Bool
&& BuiltinData
redeemer BuiltinData -> BuiltinData -> Bool
forall a. Eq a => a -> a -> Bool
== Integer -> BuiltinData
forall a. ToData a => a -> BuiltinData
toBuiltinData (Integer
42 :: Integer) = ()
  | Bool
otherwise = BuiltinString -> ()
forall a. BuiltinString -> a
traceError BuiltinString
"Incorrect datum. Expected 42."

validator :: Plutus.Validator
validator :: Validator
validator = CompiledCode (BuiltinData -> BuiltinData -> BuiltinData -> ())
-> Validator
Plutus.mkValidatorScript $$(PlutusTx.compile [|| mkValidator ||])

script :: Plutus.Script
script :: Script
script = Validator -> Script
Plutus.unValidatorScript Validator
validator

datumRedeemerGuessScriptShortBs :: SBS.ShortByteString
datumRedeemerGuessScriptShortBs :: ShortByteString
datumRedeemerGuessScriptShortBs = ByteString -> ShortByteString
SBS.toShort (ByteString -> ShortByteString)
-> (ByteString -> ByteString) -> ByteString -> ShortByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
LBS.toStrict (ByteString -> ShortByteString) -> ByteString -> ShortByteString
forall a b. (a -> b) -> a -> b
$ Script -> ByteString
forall a. Serialise a => a -> ByteString
serialise Script
script

guessScript :: PlutusScript PlutusScriptV1
guessScript :: PlutusScript PlutusScriptV1
guessScript = ShortByteString -> PlutusScript PlutusScriptV1
forall lang. ShortByteString -> PlutusScript lang
PlutusScriptSerialised ShortByteString
datumRedeemerGuessScriptShortBs

{-# INLINEABLE mkValidatorStake #-}
mkValidatorStake :: BuiltinData -> BuiltinData -> ()
mkValidatorStake :: BuiltinData -> BuiltinData -> ()
mkValidatorStake BuiltinData
redeemer BuiltinData
_txContext
  | BuiltinData
redeemer BuiltinData -> BuiltinData -> Bool
forall a. Eq a => a -> a -> Bool
== Integer -> BuiltinData
forall a. ToData a => a -> BuiltinData
toBuiltinData (Integer
42 :: Integer) = ()
  | Bool
otherwise = BuiltinString -> ()
forall a. BuiltinString -> a
traceError BuiltinString
"Incorrect datum. Expected 42."

validatorStake :: Plutus.StakeValidator
validatorStake :: StakeValidator
validatorStake = CompiledCode (BuiltinData -> BuiltinData -> ()) -> StakeValidator
Plutus.mkStakeValidatorScript $$(PlutusTx.compile [||mkValidatorStake||])

scriptStake :: Plutus.Script
scriptStake :: Script
scriptStake = StakeValidator -> Script
Plutus.unStakeValidatorScript StakeValidator
validatorStake

datumRedeemerGuessScriptStakeShortBs :: SBS.ShortByteString
datumRedeemerGuessScriptStakeShortBs :: ShortByteString
datumRedeemerGuessScriptStakeShortBs = ByteString -> ShortByteString
SBS.toShort (ByteString -> ShortByteString)
-> (ByteString -> ByteString) -> ByteString -> ShortByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
LBS.toStrict (ByteString -> ShortByteString) -> ByteString -> ShortByteString
forall a b. (a -> b) -> a -> b
$ Script -> ByteString
forall a. Serialise a => a -> ByteString
serialise Script
scriptStake

guessScriptStake :: PlutusScript PlutusScriptV1
guessScriptStake :: PlutusScript PlutusScriptV1
guessScriptStake = ShortByteString -> PlutusScript PlutusScriptV1
forall lang. ShortByteString -> PlutusScript lang
PlutusScriptSerialised ShortByteString
datumRedeemerGuessScriptStakeShortBs