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

module PlutusExample.PlutusVersion1.CustomDatumRedeemerGuess
  ( MyCustomDatum(..)
  , MyCustomRedeemer(..)
  , customGuessScript
  , customDatumRedeemerGuessScriptAsShortBs
  ) 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.Script.Utils.Typed qualified as Scripts
import Plutus.V1.Ledger.Api qualified as Plutus
import Plutus.V1.Ledger.Contexts (ScriptContext)
import PlutusTx qualified
import PlutusTx.Prelude hiding (Semigroup ((<>)), unless, (.))

newtype MyCustomDatum = MyCustomDatum Integer
newtype MyCustomRedeemer = MyCustomRedeemer Integer

PlutusTx.unstableMakeIsData ''MyCustomDatum
PlutusTx.unstableMakeIsData ''MyCustomRedeemer

{-# INLINABLE mkValidator #-}
mkValidator :: MyCustomDatum -> MyCustomRedeemer -> ScriptContext -> Bool
mkValidator :: MyCustomDatum -> MyCustomRedeemer -> ScriptContext -> Bool
mkValidator (MyCustomDatum Integer
d) (MyCustomRedeemer Integer
r) ScriptContext
_ =
  Integer
d Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
42 Bool -> Bool -> Bool
&& Integer
r Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
42

validator :: Plutus.Validator
validator :: Validator
validator = CompiledCode (BuiltinData -> BuiltinData -> BuiltinData -> ())
-> Validator
Plutus.mkValidatorScript
    $$(PlutusTx.compile [|| wrap ||])
 where
     wrap :: BuiltinData -> BuiltinData -> BuiltinData -> ()
wrap = (MyCustomDatum -> MyCustomRedeemer -> ScriptContext -> Bool)
-> BuiltinData -> BuiltinData -> BuiltinData -> ()
forall sc d r.
(IsScriptContext sc, UnsafeFromData d, UnsafeFromData r) =>
(d -> r -> sc -> Bool)
-> BuiltinData -> BuiltinData -> BuiltinData -> ()
Scripts.mkUntypedValidator MyCustomDatum -> MyCustomRedeemer -> ScriptContext -> Bool
mkValidator

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

customDatumRedeemerGuessScriptAsShortBs :: SBS.ShortByteString
customDatumRedeemerGuessScriptAsShortBs :: ShortByteString
customDatumRedeemerGuessScriptAsShortBs = 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

customGuessScript :: PlutusScript PlutusScriptV1
customGuessScript :: PlutusScript PlutusScriptV1
customGuessScript = ShortByteString -> PlutusScript PlutusScriptV1
forall lang. ShortByteString -> PlutusScript lang
PlutusScriptSerialised ShortByteString
customDatumRedeemerGuessScriptAsShortBs