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


module PlutusExample.PlutusVersion1.MintingScript
  ( apiExamplePlutusMintingScript
  , mintingScriptShortBs
  ) where

import Prelude hiding (($))

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

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

import Plutus.Script.Utils.Typed qualified as Scripts
import Plutus.V1.Ledger.Api (MintingPolicy, Script, ScriptContext, Validator (Validator), mkMintingPolicyScript,
                             unMintingPolicyScript)
import PlutusTx qualified
import PlutusTx.Prelude hiding (Semigroup (..), unless, (.))

{- HLINT ignore "Avoid lambda" -}

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

policy :: MintingPolicy
policy :: MintingPolicy
policy = CompiledCode (BuiltinData -> BuiltinData -> ()) -> MintingPolicy
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 :: Script
plutusScript :: Script
plutusScript =
  MintingPolicy -> Script
unMintingPolicyScript MintingPolicy
policy

validator :: Validator
validator :: Validator
validator =
  Script -> Validator
Validator (Script -> Validator) -> Script -> Validator
forall a b. (a -> b) -> a -> b
$ MintingPolicy -> Script
unMintingPolicyScript MintingPolicy
policy

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

apiExamplePlutusMintingScript :: PlutusScript PlutusScriptV1
apiExamplePlutusMintingScript :: PlutusScript PlutusScriptV1
apiExamplePlutusMintingScript = ShortByteString -> PlutusScript PlutusScriptV1
forall lang. ShortByteString -> PlutusScript lang
PlutusScriptSerialised (ShortByteString -> PlutusScript PlutusScriptV1)
-> (ByteString -> ShortByteString)
-> ByteString
-> PlutusScript PlutusScriptV1
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ShortByteString
SBS.toShort (ByteString -> PlutusScript PlutusScriptV1)
-> ByteString -> PlutusScript PlutusScriptV1
forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString
LB.toStrict ByteString
scriptAsCbor

mintingScriptShortBs :: SBS.ShortByteString
mintingScriptShortBs :: ShortByteString
mintingScriptShortBs = ByteString -> ShortByteString
SBS.toShort (ByteString -> ShortByteString)
-> (ByteString -> ByteString) -> ByteString -> ShortByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
LB.toStrict (ByteString -> ShortByteString) -> ByteString -> ShortByteString
forall a b. (a -> b) -> a -> b
$ ByteString
scriptAsCbor