{- |
Module     : Agora.ScriptInfo
Maintainer : emi@haskell.fyi
Description: Exportable script bundles for off-chain consumption.

Exportable script bundles for off-chain consumption.
-}
module Agora.ScriptInfo (
  -- * Types
  PolicyInfo (..),
  ValidatorInfo (..),

  -- * Introduction functions
  mkValidatorInfo,
  mkPolicyInfo,
) where

import Agora.Aeson.Orphans ()
import Data.Aeson qualified as Aeson
import GHC.Generics qualified as GHC
import Plutarch.Api.V1 (PMintingPolicy, PValidator, mintingPolicySymbol, mkMintingPolicy, mkValidator, validatorHash)
import PlutusLedgerApi.V1 (MintingPolicy, Validator, ValidatorHash)
import PlutusLedgerApi.V1.Value (CurrencySymbol)

{- | Bundle containing a 'Validator' and its hash.

     @since 0.1.0
-}
data ValidatorInfo = ValidatorInfo
  { ValidatorInfo -> Validator
script :: Validator
  -- ^ The validator script.
  , ValidatorInfo -> ValidatorHash
hash :: ValidatorHash
  -- ^ Hash of the validator.
  }
  deriving stock
    ( -- | @since 0.1.0
      Int -> ValidatorInfo -> ShowS
[ValidatorInfo] -> ShowS
ValidatorInfo -> String
(Int -> ValidatorInfo -> ShowS)
-> (ValidatorInfo -> String)
-> ([ValidatorInfo] -> ShowS)
-> Show ValidatorInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ValidatorInfo] -> ShowS
$cshowList :: [ValidatorInfo] -> ShowS
show :: ValidatorInfo -> String
$cshow :: ValidatorInfo -> String
showsPrec :: Int -> ValidatorInfo -> ShowS
$cshowsPrec :: Int -> ValidatorInfo -> ShowS
Show
    , -- | @since 0.1.0
      ValidatorInfo -> ValidatorInfo -> Bool
(ValidatorInfo -> ValidatorInfo -> Bool)
-> (ValidatorInfo -> ValidatorInfo -> Bool) -> Eq ValidatorInfo
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ValidatorInfo -> ValidatorInfo -> Bool
$c/= :: ValidatorInfo -> ValidatorInfo -> Bool
== :: ValidatorInfo -> ValidatorInfo -> Bool
$c== :: ValidatorInfo -> ValidatorInfo -> Bool
Eq
    , -- | @since 0.1.0
      (forall x. ValidatorInfo -> Rep ValidatorInfo x)
-> (forall x. Rep ValidatorInfo x -> ValidatorInfo)
-> Generic ValidatorInfo
forall x. Rep ValidatorInfo x -> ValidatorInfo
forall x. ValidatorInfo -> Rep ValidatorInfo x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep ValidatorInfo x -> ValidatorInfo
$cfrom :: forall x. ValidatorInfo -> Rep ValidatorInfo x
GHC.Generic
    )
  deriving anyclass
    ( -- | @since 0.1.0
      [ValidatorInfo] -> Encoding
[ValidatorInfo] -> Value
ValidatorInfo -> Encoding
ValidatorInfo -> Value
(ValidatorInfo -> Value)
-> (ValidatorInfo -> Encoding)
-> ([ValidatorInfo] -> Value)
-> ([ValidatorInfo] -> Encoding)
-> ToJSON ValidatorInfo
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [ValidatorInfo] -> Encoding
$ctoEncodingList :: [ValidatorInfo] -> Encoding
toJSONList :: [ValidatorInfo] -> Value
$ctoJSONList :: [ValidatorInfo] -> Value
toEncoding :: ValidatorInfo -> Encoding
$ctoEncoding :: ValidatorInfo -> Encoding
toJSON :: ValidatorInfo -> Value
$ctoJSON :: ValidatorInfo -> Value
Aeson.ToJSON
    , -- | @since 0.1.0
      Value -> Parser [ValidatorInfo]
Value -> Parser ValidatorInfo
(Value -> Parser ValidatorInfo)
-> (Value -> Parser [ValidatorInfo]) -> FromJSON ValidatorInfo
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [ValidatorInfo]
$cparseJSONList :: Value -> Parser [ValidatorInfo]
parseJSON :: Value -> Parser ValidatorInfo
$cparseJSON :: Value -> Parser ValidatorInfo
Aeson.FromJSON
    )

{- | Create a 'ValidatorInfo' given a Plutarch term.

     @since 0.1.0
-}
mkValidatorInfo :: ClosedTerm PValidator -> ValidatorInfo
mkValidatorInfo :: ClosedTerm PValidator -> ValidatorInfo
mkValidatorInfo ClosedTerm PValidator
term =
  ValidatorInfo
    { script :: Validator
script = Validator
validator
    , hash :: ValidatorHash
hash = Validator -> ValidatorHash
validatorHash Validator
validator
    }
  where
    validator :: Validator
validator = ClosedTerm PValidator -> Validator
mkValidator ClosedTerm PValidator
term

{- | Bundle containing a 'MintingPolicy' and its symbol.

     @since 0.1.0
-}
data PolicyInfo = PolicyInfo
  { PolicyInfo -> MintingPolicy
policy :: MintingPolicy
  -- ^ The minting policy.
  , PolicyInfo -> CurrencySymbol
currencySymbol :: CurrencySymbol
  -- ^ The symbol given by the minting policy.
  }
  deriving stock
    ( -- | @since 0.1.0
      Int -> PolicyInfo -> ShowS
[PolicyInfo] -> ShowS
PolicyInfo -> String
(Int -> PolicyInfo -> ShowS)
-> (PolicyInfo -> String)
-> ([PolicyInfo] -> ShowS)
-> Show PolicyInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PolicyInfo] -> ShowS
$cshowList :: [PolicyInfo] -> ShowS
show :: PolicyInfo -> String
$cshow :: PolicyInfo -> String
showsPrec :: Int -> PolicyInfo -> ShowS
$cshowsPrec :: Int -> PolicyInfo -> ShowS
Show
    , -- | @since 0.1.0
      PolicyInfo -> PolicyInfo -> Bool
(PolicyInfo -> PolicyInfo -> Bool)
-> (PolicyInfo -> PolicyInfo -> Bool) -> Eq PolicyInfo
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PolicyInfo -> PolicyInfo -> Bool
$c/= :: PolicyInfo -> PolicyInfo -> Bool
== :: PolicyInfo -> PolicyInfo -> Bool
$c== :: PolicyInfo -> PolicyInfo -> Bool
Eq
    , -- | @since 0.1.0
      (forall x. PolicyInfo -> Rep PolicyInfo x)
-> (forall x. Rep PolicyInfo x -> PolicyInfo) -> Generic PolicyInfo
forall x. Rep PolicyInfo x -> PolicyInfo
forall x. PolicyInfo -> Rep PolicyInfo x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep PolicyInfo x -> PolicyInfo
$cfrom :: forall x. PolicyInfo -> Rep PolicyInfo x
GHC.Generic
    )
  deriving anyclass
    ( -- | @since 0.1.0
      [PolicyInfo] -> Encoding
[PolicyInfo] -> Value
PolicyInfo -> Encoding
PolicyInfo -> Value
(PolicyInfo -> Value)
-> (PolicyInfo -> Encoding)
-> ([PolicyInfo] -> Value)
-> ([PolicyInfo] -> Encoding)
-> ToJSON PolicyInfo
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [PolicyInfo] -> Encoding
$ctoEncodingList :: [PolicyInfo] -> Encoding
toJSONList :: [PolicyInfo] -> Value
$ctoJSONList :: [PolicyInfo] -> Value
toEncoding :: PolicyInfo -> Encoding
$ctoEncoding :: PolicyInfo -> Encoding
toJSON :: PolicyInfo -> Value
$ctoJSON :: PolicyInfo -> Value
Aeson.ToJSON
    , -- | @since 0.1.0
      Value -> Parser [PolicyInfo]
Value -> Parser PolicyInfo
(Value -> Parser PolicyInfo)
-> (Value -> Parser [PolicyInfo]) -> FromJSON PolicyInfo
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [PolicyInfo]
$cparseJSONList :: Value -> Parser [PolicyInfo]
parseJSON :: Value -> Parser PolicyInfo
$cparseJSON :: Value -> Parser PolicyInfo
Aeson.FromJSON
    )

{- | Create a 'PolicyInfo' given a Plutarch term.

     @since 0.1.0
-}
mkPolicyInfo :: ClosedTerm PMintingPolicy -> PolicyInfo
mkPolicyInfo :: ClosedTerm PMintingPolicy -> PolicyInfo
mkPolicyInfo ClosedTerm PMintingPolicy
term =
  PolicyInfo
    { policy :: MintingPolicy
policy = MintingPolicy
policy
    , currencySymbol :: CurrencySymbol
currencySymbol = MintingPolicy -> CurrencySymbol
mintingPolicySymbol MintingPolicy
policy
    }
  where
    policy :: MintingPolicy
policy = ClosedTerm PMintingPolicy -> MintingPolicy
mkMintingPolicy ClosedTerm PMintingPolicy
term