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


module PlutusExample.PlutusVersion2.MintingScript
  ( v2mintingScript
  , v2mintingScriptShortBs
  ) where

import Prelude hiding (($))

import Cardano.Api.Shelley (PlutusScript (..), PlutusScriptV2)
import Prelude hiding (($), (&&))

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

import Plutus.Script.Utils.Typed as Scripts
import Plutus.V2.Ledger.Api qualified as V2
import Plutus.V2.Ledger.Contexts as V2
import PlutusTx qualified
import PlutusTx.Builtins
import PlutusTx.Prelude hiding (Semigroup (..), unless, (.))

{- HLINT ignore "Avoid lambda" -}

{-# INLINABLE mkPolicy #-}
mkPolicy :: BuiltinData -> V2.ScriptContext -> Bool
mkPolicy :: BuiltinData -> ScriptContext -> Bool
mkPolicy BuiltinData
_redeemer ScriptContext
_ctx = Bool
True

policy :: V2.MintingPolicy
policy :: MintingPolicy
policy = CompiledCode (BuiltinData -> BuiltinData -> ()) -> MintingPolicy
V2.mkMintingPolicyScript $$(PlutusTx.compile [|| wrap ||])
 where
  wrap :: BuiltinData -> BuiltinData -> ()
wrap = (BuiltinData -> ScriptContext -> Bool)
-> BuiltinData -> BuiltinData -> ()
forall sc r.
(IsScriptContext sc, UnsafeFromData r) =>
(r -> sc -> Bool) -> BuiltinData -> BuiltinData -> ()
Scripts.mkUntypedMintingPolicy BuiltinData -> ScriptContext -> Bool
mkPolicy

plutusScript :: V2.Script
plutusScript :: Script
plutusScript =
  MintingPolicy -> Script
V2.unMintingPolicyScript MintingPolicy
policy

validator :: V2.Validator
validator :: Validator
validator = Script -> Validator
V2.Validator Script
plutusScript

scriptAsCbor :: LBS.ByteString
scriptAsCbor :: ByteString
scriptAsCbor = Validator -> ByteString
forall a. Serialise a => a -> ByteString
serialise Validator
validator

v2mintingScript :: PlutusScript PlutusScriptV2
v2mintingScript :: PlutusScript PlutusScriptV2
v2mintingScript = ShortByteString -> PlutusScript PlutusScriptV2
forall lang. ShortByteString -> PlutusScript lang
PlutusScriptSerialised (ShortByteString -> PlutusScript PlutusScriptV2)
-> (ByteString -> ShortByteString)
-> ByteString
-> PlutusScript PlutusScriptV2
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ShortByteString
SBS.toShort (ByteString -> PlutusScript PlutusScriptV2)
-> ByteString -> PlutusScript PlutusScriptV2
forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString
LBS.toStrict ByteString
scriptAsCbor

v2mintingScriptShortBs :: SBS.ShortByteString
v2mintingScriptShortBs :: ShortByteString
v2mintingScriptShortBs = 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
$ ByteString
scriptAsCbor