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


module PlutusExample.PlutusVersion2.StakeScript
  ( v2StakeScript
  , v2StakeScriptShortBs
  ) 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.StakeValidator
policy :: StakeValidator
policy = CompiledCode (BuiltinData -> BuiltinData -> ()) -> StakeValidator
V2.mkStakeValidatorScript $$(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.mkUntypedStakeValidator BuiltinData -> ScriptContext -> Bool
mkPolicy

plutusScript :: V2.Script
plutusScript :: Script
plutusScript =
  StakeValidator -> Script
V2.unStakeValidatorScript StakeValidator
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

v2StakeScript :: PlutusScript PlutusScriptV2
v2StakeScript :: PlutusScript PlutusScriptV2
v2StakeScript = 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

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