{-# OPTIONS_GHC -Wno-orphans #-}

module Plutarch.LedgerApi.V2 (
  -- * Contexts
  Contexts.PScriptPurpose (..),
  PScriptContext (..),

  -- * Certificates
  DCert.PDCert (..),

  -- * Credentials
  Credential.PCredential (..),
  Credential.PStakingCredential (..),

  -- * Value
  Value.PValue (..),
  Value.AmountGuarantees (..),
  Value.PLovelace (..),
  Value.PTokenName (..),
  Value.PCurrencySymbol (..),

  -- * Time
  Time.PPosixTime (..),
  Time.pposixTime,
  Time.unPPosixTime,

  -- * Intervals
  Interval.PExtended (..),
  Interval.PLowerBound (..),
  Interval.PUpperBound (..),
  Interval.PInterval (..),

  -- * Script stuff
  Scripts.PDatum (..),
  Scripts.PRedeemer (..),
  Scripts.PDatumHash (..),
  Scripts.PRedeemerHash (..),
  Scripts.PScriptHash (..),

  -- * Transactions
  Address.PAddress (..),
  Crypto.PPubKeyHash (..),
  V1Tx.PTxId (..),
  PTxInfo (..),
  V2Tx.PTxOut (..),
  V1Tx.PTxOutRef (..),
  PTxInInfo (..),
  V2Tx.POutputDatum (..),

  -- * Helpers
  AssocMap.PMap (..),
  AssocMap.KeyGuarantees (..),
  AssocMap.Commutativity (..),

  -- * Utilities

  -- ** Types
  Utils.PMaybeData (..),
  Utils.PRationalData (..),

  -- ** Utilities
  Utils.pfromDJust,
  Utils.pisDJust,
  Utils.pmaybeData,
  Utils.pdjust,
  Utils.pdnothing,
  Utils.pmaybeToMaybeData,
  Utils.passertPDJust,
  Utils.prationalFromData,
) where

import GHC.Generics (Generic)
import Generics.SOP qualified as SOP
import Plutarch.LedgerApi.AssocMap qualified as AssocMap
import Plutarch.LedgerApi.Interval qualified as Interval
import Plutarch.LedgerApi.Utils qualified as Utils
import Plutarch.LedgerApi.V1.Address qualified as Address
import Plutarch.LedgerApi.V1.Contexts qualified as Contexts
import Plutarch.LedgerApi.V1.Credential qualified as Credential
import Plutarch.LedgerApi.V1.Crypto qualified as Crypto
import Plutarch.LedgerApi.V1.DCert qualified as DCert
import Plutarch.LedgerApi.V1.Scripts qualified as Scripts
import Plutarch.LedgerApi.V1.Time qualified as Time
import Plutarch.LedgerApi.V1.Tx qualified as V1Tx
import Plutarch.LedgerApi.V2.Tx qualified as V2Tx
import Plutarch.LedgerApi.Value qualified as Value
import Plutarch.Prelude
import PlutusLedgerApi.V2 qualified as Plutus

-- | @since 3.1.1
data PTxInInfo (s :: S) = PTxInInfo
  { forall (s :: S). PTxInInfo s -> Term s PTxOutRef
ptxInInfo'outRef :: Term s V1Tx.PTxOutRef
  , forall (s :: S). PTxInInfo s -> Term s PTxOut
ptxInInfo'resolved :: Term s V2Tx.PTxOut
  }
  deriving stock
    ( -- | @since 3.1.1
      (forall x. PTxInInfo s -> Rep (PTxInInfo s) x)
-> (forall x. Rep (PTxInInfo s) x -> PTxInInfo s)
-> Generic (PTxInInfo s)
forall x. Rep (PTxInInfo s) x -> PTxInInfo s
forall x. PTxInInfo s -> Rep (PTxInInfo s) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (s :: S) x. Rep (PTxInInfo s) x -> PTxInInfo s
forall (s :: S) x. PTxInInfo s -> Rep (PTxInInfo s) x
$cfrom :: forall (s :: S) x. PTxInInfo s -> Rep (PTxInInfo s) x
from :: forall x. PTxInInfo s -> Rep (PTxInInfo s) x
$cto :: forall (s :: S) x. Rep (PTxInInfo s) x -> PTxInInfo s
to :: forall x. Rep (PTxInInfo s) x -> PTxInInfo s
Generic
    )
  deriving anyclass
    ( -- | @since 3.3.0
      All SListI (Code (PTxInInfo s))
All SListI (Code (PTxInInfo s)) =>
(PTxInInfo s -> Rep (PTxInInfo s))
-> (Rep (PTxInInfo s) -> PTxInInfo s) -> Generic (PTxInInfo s)
Rep (PTxInInfo s) -> PTxInInfo s
PTxInInfo s -> Rep (PTxInInfo s)
forall a.
All SListI (Code a) =>
(a -> Rep a) -> (Rep a -> a) -> Generic a
forall (s :: S). All SListI (Code (PTxInInfo s))
forall (s :: S). Rep (PTxInInfo s) -> PTxInInfo s
forall (s :: S). PTxInInfo s -> Rep (PTxInInfo s)
$cfrom :: forall (s :: S). PTxInInfo s -> Rep (PTxInInfo s)
from :: PTxInInfo s -> Rep (PTxInInfo s)
$cto :: forall (s :: S). Rep (PTxInInfo s) -> PTxInInfo s
to :: Rep (PTxInInfo s) -> PTxInInfo s
SOP.Generic
    , -- | @since 3.1.1
      (forall (s :: S). Term s (PAsData PTxInInfo) -> Term s PTxInInfo)
-> (forall (s :: S). Term s PTxInInfo -> Term s PData)
-> PIsData PTxInInfo
forall (s :: S). Term s (PAsData PTxInInfo) -> Term s PTxInInfo
forall (s :: S). Term s PTxInInfo -> Term s PData
forall (a :: S -> Type).
(forall (s :: S). Term s (PAsData a) -> Term s a)
-> (forall (s :: S). Term s a -> Term s PData) -> PIsData a
$cpfromDataImpl :: forall (s :: S). Term s (PAsData PTxInInfo) -> Term s PTxInInfo
pfromDataImpl :: forall (s :: S). Term s (PAsData PTxInInfo) -> Term s PTxInInfo
$cpdataImpl :: forall (s :: S). Term s PTxInInfo -> Term s PData
pdataImpl :: forall (s :: S). Term s PTxInInfo -> Term s PData
PIsData
    , -- | @since 3.1.1
      (forall (s :: S).
 Term s PTxInInfo -> Term s PTxInInfo -> Term s PBool)
-> PEq PTxInInfo
forall (s :: S).
Term s PTxInInfo -> Term s PTxInInfo -> Term s PBool
forall (t :: S -> Type).
(forall (s :: S). Term s t -> Term s t -> Term s PBool) -> PEq t
$c#== :: forall (s :: S).
Term s PTxInInfo -> Term s PTxInInfo -> Term s PBool
#== :: forall (s :: S).
Term s PTxInInfo -> Term s PTxInInfo -> Term s PBool
PEq
    , -- | @since 3.1.1
      (forall (s :: S). Bool -> Term s PTxInInfo -> Term s PString)
-> PShow PTxInInfo
forall (s :: S). Bool -> Term s PTxInInfo -> Term s PString
forall (t :: S -> Type).
(forall (s :: S). Bool -> Term s t -> Term s PString) -> PShow t
$cpshow' :: forall (s :: S). Bool -> Term s PTxInInfo -> Term s PString
pshow' :: forall (s :: S). Bool -> Term s PTxInInfo -> Term s PString
PShow
    )
  deriving
    ( -- | @since 3.3.0
      (forall (s :: S). PTxInInfo s -> Term s (PInner PTxInInfo))
-> (forall (s :: S) (b :: S -> Type).
    Term s (PInner PTxInInfo) -> (PTxInInfo s -> Term s b) -> Term s b)
-> PlutusType PTxInInfo
forall (s :: S). PTxInInfo s -> Term s (PInner PTxInInfo)
forall (s :: S) (b :: S -> Type).
Term s (PInner PTxInInfo) -> (PTxInInfo s -> Term s b) -> Term s b
forall (a :: S -> Type).
(forall (s :: S). a s -> Term s (PInner a))
-> (forall (s :: S) (b :: S -> Type).
    Term s (PInner a) -> (a s -> Term s b) -> Term s b)
-> PlutusType a
$cpcon' :: forall (s :: S). PTxInInfo s -> Term s (PInner PTxInInfo)
pcon' :: forall (s :: S). PTxInInfo s -> Term s (PInner PTxInInfo)
$cpmatch' :: forall (s :: S) (b :: S -> Type).
Term s (PInner PTxInInfo) -> (PTxInInfo s -> Term s b) -> Term s b
pmatch' :: forall (s :: S) (b :: S -> Type).
Term s (PInner PTxInInfo) -> (PTxInInfo s -> Term s b) -> Term s b
PlutusType
    )
    via (DeriveAsDataStruct PTxInInfo)

-- | @since 3.3.0
deriving via
  DeriveDataPLiftable PTxInInfo Plutus.TxInInfo
  instance
    PLiftable PTxInInfo

-- | @since 3.1.1
data PTxInfo (s :: S) = PTxInfo
  { forall (s :: S).
PTxInfo s -> Term s (PAsData (PBuiltinList (PAsData PTxInInfo)))
ptxInfo'inputs :: Term s (PAsData (PBuiltinList (PAsData PTxInInfo)))
  , forall (s :: S).
PTxInfo s -> Term s (PAsData (PBuiltinList (PAsData PTxInInfo)))
ptxInfo'referenceInputs :: Term s (PAsData (PBuiltinList (PAsData PTxInInfo)))
  , forall (s :: S).
PTxInfo s -> Term s (PAsData (PBuiltinList (PAsData PTxOut)))
ptxInfo'outputs :: Term s (PAsData (PBuiltinList (PAsData V2Tx.PTxOut)))
  , forall (s :: S).
PTxInfo s -> Term s (PAsData (PValue 'Sorted 'Positive))
ptxInfo'fee :: Term s (PAsData (Value.PValue 'AssocMap.Sorted 'Value.Positive))
  , forall (s :: S).
PTxInfo s -> Term s (PAsData (PValue 'Sorted 'NoGuarantees))
ptxInfo'mint :: Term s (PAsData (Value.PValue 'AssocMap.Sorted 'Value.NoGuarantees)) -- value minted by transaction
  , forall (s :: S).
PTxInfo s -> Term s (PAsData (PBuiltinList (PAsData PDCert)))
ptxInfo'dcert :: Term s (PAsData (PBuiltinList (PAsData DCert.PDCert)))
  , forall (s :: S).
PTxInfo s
-> Term s (PAsData (PMap 'Unsorted PStakingCredential PInteger))
ptxInfo'wdrl :: Term s (PAsData (AssocMap.PMap 'AssocMap.Unsorted Credential.PStakingCredential PInteger)) -- Staking withdrawals
  , forall (s :: S). PTxInfo s -> Term s (PInterval PPosixTime)
ptxInfo'validRange :: Term s (Interval.PInterval Time.PPosixTime)
  , forall (s :: S).
PTxInfo s -> Term s (PAsData (PBuiltinList (PAsData PPubKeyHash)))
ptxInfo'signatories :: Term s (PAsData (PBuiltinList (PAsData Crypto.PPubKeyHash)))
  , forall (s :: S).
PTxInfo s
-> Term s (PAsData (PMap 'Unsorted PScriptPurpose PRedeemer))
ptxInfo'redeemers :: Term s (PAsData (AssocMap.PMap 'AssocMap.Unsorted Contexts.PScriptPurpose Scripts.PRedeemer))
  , forall (s :: S).
PTxInfo s -> Term s (PAsData (PMap 'Unsorted PDatumHash PDatum))
ptxInfo'data :: Term s (PAsData (AssocMap.PMap 'AssocMap.Unsorted Scripts.PDatumHash Scripts.PDatum))
  , forall (s :: S). PTxInfo s -> Term s PTxId
ptxInfo'id :: Term s V1Tx.PTxId -- hash of the pending transaction
  }
  deriving stock
    ( -- | @since 3.1.1
      (forall x. PTxInfo s -> Rep (PTxInfo s) x)
-> (forall x. Rep (PTxInfo s) x -> PTxInfo s)
-> Generic (PTxInfo s)
forall x. Rep (PTxInfo s) x -> PTxInfo s
forall x. PTxInfo s -> Rep (PTxInfo s) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (s :: S) x. Rep (PTxInfo s) x -> PTxInfo s
forall (s :: S) x. PTxInfo s -> Rep (PTxInfo s) x
$cfrom :: forall (s :: S) x. PTxInfo s -> Rep (PTxInfo s) x
from :: forall x. PTxInfo s -> Rep (PTxInfo s) x
$cto :: forall (s :: S) x. Rep (PTxInfo s) x -> PTxInfo s
to :: forall x. Rep (PTxInfo s) x -> PTxInfo s
Generic
    )
  deriving anyclass
    ( -- | @since 3.3.0
      All SListI (Code (PTxInfo s))
All SListI (Code (PTxInfo s)) =>
(PTxInfo s -> Rep (PTxInfo s))
-> (Rep (PTxInfo s) -> PTxInfo s) -> Generic (PTxInfo s)
Rep (PTxInfo s) -> PTxInfo s
PTxInfo s -> Rep (PTxInfo s)
forall a.
All SListI (Code a) =>
(a -> Rep a) -> (Rep a -> a) -> Generic a
forall (s :: S). All SListI (Code (PTxInfo s))
forall (s :: S). Rep (PTxInfo s) -> PTxInfo s
forall (s :: S). PTxInfo s -> Rep (PTxInfo s)
$cfrom :: forall (s :: S). PTxInfo s -> Rep (PTxInfo s)
from :: PTxInfo s -> Rep (PTxInfo s)
$cto :: forall (s :: S). Rep (PTxInfo s) -> PTxInfo s
to :: Rep (PTxInfo s) -> PTxInfo s
SOP.Generic
    , -- | @since 3.1.1
      (forall (s :: S). Term s (PAsData PTxInfo) -> Term s PTxInfo)
-> (forall (s :: S). Term s PTxInfo -> Term s PData)
-> PIsData PTxInfo
forall (s :: S). Term s (PAsData PTxInfo) -> Term s PTxInfo
forall (s :: S). Term s PTxInfo -> Term s PData
forall (a :: S -> Type).
(forall (s :: S). Term s (PAsData a) -> Term s a)
-> (forall (s :: S). Term s a -> Term s PData) -> PIsData a
$cpfromDataImpl :: forall (s :: S). Term s (PAsData PTxInfo) -> Term s PTxInfo
pfromDataImpl :: forall (s :: S). Term s (PAsData PTxInfo) -> Term s PTxInfo
$cpdataImpl :: forall (s :: S). Term s PTxInfo -> Term s PData
pdataImpl :: forall (s :: S). Term s PTxInfo -> Term s PData
PIsData
    , -- | @since 3.1.1
      (forall (s :: S). Term s PTxInfo -> Term s PTxInfo -> Term s PBool)
-> PEq PTxInfo
forall (s :: S). Term s PTxInfo -> Term s PTxInfo -> Term s PBool
forall (t :: S -> Type).
(forall (s :: S). Term s t -> Term s t -> Term s PBool) -> PEq t
$c#== :: forall (s :: S). Term s PTxInfo -> Term s PTxInfo -> Term s PBool
#== :: forall (s :: S). Term s PTxInfo -> Term s PTxInfo -> Term s PBool
PEq
    , -- | @since 3.1.1
      (forall (s :: S). Bool -> Term s PTxInfo -> Term s PString)
-> PShow PTxInfo
forall (s :: S). Bool -> Term s PTxInfo -> Term s PString
forall (t :: S -> Type).
(forall (s :: S). Bool -> Term s t -> Term s PString) -> PShow t
$cpshow' :: forall (s :: S). Bool -> Term s PTxInfo -> Term s PString
pshow' :: forall (s :: S). Bool -> Term s PTxInfo -> Term s PString
PShow
    )
  deriving
    ( -- | @since 3.3.0
      (forall (s :: S). PTxInfo s -> Term s (PInner PTxInfo))
-> (forall (s :: S) (b :: S -> Type).
    Term s (PInner PTxInfo) -> (PTxInfo s -> Term s b) -> Term s b)
-> PlutusType PTxInfo
forall (s :: S). PTxInfo s -> Term s (PInner PTxInfo)
forall (s :: S) (b :: S -> Type).
Term s (PInner PTxInfo) -> (PTxInfo s -> Term s b) -> Term s b
forall (a :: S -> Type).
(forall (s :: S). a s -> Term s (PInner a))
-> (forall (s :: S) (b :: S -> Type).
    Term s (PInner a) -> (a s -> Term s b) -> Term s b)
-> PlutusType a
$cpcon' :: forall (s :: S). PTxInfo s -> Term s (PInner PTxInfo)
pcon' :: forall (s :: S). PTxInfo s -> Term s (PInner PTxInfo)
$cpmatch' :: forall (s :: S) (b :: S -> Type).
Term s (PInner PTxInfo) -> (PTxInfo s -> Term s b) -> Term s b
pmatch' :: forall (s :: S) (b :: S -> Type).
Term s (PInner PTxInfo) -> (PTxInfo s -> Term s b) -> Term s b
PlutusType
    )
    via (DeriveAsDataStruct PTxInfo)

-- | @since 3.3.0
deriving via
  DeriveDataPLiftable PTxInfo Plutus.TxInfo
  instance
    PLiftable PTxInfo

-- | @since 3.1.1
data PScriptContext (s :: S) = PScriptContext
  { forall (s :: S). PScriptContext s -> Term s PTxInfo
pscriptContext'txInfo :: Term s PTxInfo
  , forall (s :: S). PScriptContext s -> Term s PScriptPurpose
pscriptContext'purpose :: Term s Contexts.PScriptPurpose
  }
  deriving stock
    ( -- | @since 3.1.1
      (forall x. PScriptContext s -> Rep (PScriptContext s) x)
-> (forall x. Rep (PScriptContext s) x -> PScriptContext s)
-> Generic (PScriptContext s)
forall x. Rep (PScriptContext s) x -> PScriptContext s
forall x. PScriptContext s -> Rep (PScriptContext s) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (s :: S) x. Rep (PScriptContext s) x -> PScriptContext s
forall (s :: S) x. PScriptContext s -> Rep (PScriptContext s) x
$cfrom :: forall (s :: S) x. PScriptContext s -> Rep (PScriptContext s) x
from :: forall x. PScriptContext s -> Rep (PScriptContext s) x
$cto :: forall (s :: S) x. Rep (PScriptContext s) x -> PScriptContext s
to :: forall x. Rep (PScriptContext s) x -> PScriptContext s
Generic
    )
  deriving anyclass
    ( -- | @since 3.3.0
      All SListI (Code (PScriptContext s))
All SListI (Code (PScriptContext s)) =>
(PScriptContext s -> Rep (PScriptContext s))
-> (Rep (PScriptContext s) -> PScriptContext s)
-> Generic (PScriptContext s)
Rep (PScriptContext s) -> PScriptContext s
PScriptContext s -> Rep (PScriptContext s)
forall a.
All SListI (Code a) =>
(a -> Rep a) -> (Rep a -> a) -> Generic a
forall (s :: S). All SListI (Code (PScriptContext s))
forall (s :: S). Rep (PScriptContext s) -> PScriptContext s
forall (s :: S). PScriptContext s -> Rep (PScriptContext s)
$cfrom :: forall (s :: S). PScriptContext s -> Rep (PScriptContext s)
from :: PScriptContext s -> Rep (PScriptContext s)
$cto :: forall (s :: S). Rep (PScriptContext s) -> PScriptContext s
to :: Rep (PScriptContext s) -> PScriptContext s
SOP.Generic
    , -- | @since 3.1.1
      (forall (s :: S).
 Term s (PAsData PScriptContext) -> Term s PScriptContext)
-> (forall (s :: S). Term s PScriptContext -> Term s PData)
-> PIsData PScriptContext
forall (s :: S).
Term s (PAsData PScriptContext) -> Term s PScriptContext
forall (s :: S). Term s PScriptContext -> Term s PData
forall (a :: S -> Type).
(forall (s :: S). Term s (PAsData a) -> Term s a)
-> (forall (s :: S). Term s a -> Term s PData) -> PIsData a
$cpfromDataImpl :: forall (s :: S).
Term s (PAsData PScriptContext) -> Term s PScriptContext
pfromDataImpl :: forall (s :: S).
Term s (PAsData PScriptContext) -> Term s PScriptContext
$cpdataImpl :: forall (s :: S). Term s PScriptContext -> Term s PData
pdataImpl :: forall (s :: S). Term s PScriptContext -> Term s PData
PIsData
    , -- | @since 3.1.1
      (forall (s :: S).
 Term s PScriptContext -> Term s PScriptContext -> Term s PBool)
-> PEq PScriptContext
forall (s :: S).
Term s PScriptContext -> Term s PScriptContext -> Term s PBool
forall (t :: S -> Type).
(forall (s :: S). Term s t -> Term s t -> Term s PBool) -> PEq t
$c#== :: forall (s :: S).
Term s PScriptContext -> Term s PScriptContext -> Term s PBool
#== :: forall (s :: S).
Term s PScriptContext -> Term s PScriptContext -> Term s PBool
PEq
    , -- | @since 3.1.1
      (forall (s :: S). Bool -> Term s PScriptContext -> Term s PString)
-> PShow PScriptContext
forall (s :: S). Bool -> Term s PScriptContext -> Term s PString
forall (t :: S -> Type).
(forall (s :: S). Bool -> Term s t -> Term s PString) -> PShow t
$cpshow' :: forall (s :: S). Bool -> Term s PScriptContext -> Term s PString
pshow' :: forall (s :: S). Bool -> Term s PScriptContext -> Term s PString
PShow
    )
  deriving
    ( -- | @since 3.3.0
      (forall (s :: S).
 PScriptContext s -> Term s (PInner PScriptContext))
-> (forall (s :: S) (b :: S -> Type).
    Term s (PInner PScriptContext)
    -> (PScriptContext s -> Term s b) -> Term s b)
-> PlutusType PScriptContext
forall (s :: S). PScriptContext s -> Term s (PInner PScriptContext)
forall (s :: S) (b :: S -> Type).
Term s (PInner PScriptContext)
-> (PScriptContext s -> Term s b) -> Term s b
forall (a :: S -> Type).
(forall (s :: S). a s -> Term s (PInner a))
-> (forall (s :: S) (b :: S -> Type).
    Term s (PInner a) -> (a s -> Term s b) -> Term s b)
-> PlutusType a
$cpcon' :: forall (s :: S). PScriptContext s -> Term s (PInner PScriptContext)
pcon' :: forall (s :: S). PScriptContext s -> Term s (PInner PScriptContext)
$cpmatch' :: forall (s :: S) (b :: S -> Type).
Term s (PInner PScriptContext)
-> (PScriptContext s -> Term s b) -> Term s b
pmatch' :: forall (s :: S) (b :: S -> Type).
Term s (PInner PScriptContext)
-> (PScriptContext s -> Term s b) -> Term s b
PlutusType
    )
    via (DeriveAsDataStruct PScriptContext)

-- | @since 3.3.0
deriving via
  DeriveDataPLiftable PScriptContext Plutus.ScriptContext
  instance
    PLiftable PScriptContext