{-# 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 Plutarch.DataRepr (PDataFields)
import Plutarch.Internal.Lift (DeriveDataPLiftable)
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
newtype PTxInInfo (s :: S)
  = PTxInInfo
      ( Term
          s
          ( PDataRecord
              '[ "outRef" ':= V1Tx.PTxOutRef
               , "resolved" ':= 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.1.1
      (forall (s :: S). PTxInInfo s -> Term s (PInner PTxInInfo))
-> (forall (s :: S) (b :: PType).
    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 :: PType).
Term s (PInner PTxInInfo) -> (PTxInInfo s -> Term s b) -> Term s b
forall (a :: PType).
(forall (s :: S). a s -> Term s (PInner a))
-> (forall (s :: S) (b :: PType).
    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 :: PType).
Term s (PInner PTxInInfo) -> (PTxInInfo s -> Term s b) -> Term s b
pmatch' :: forall (s :: S) (b :: PType).
Term s (PInner PTxInInfo) -> (PTxInInfo s -> Term s b) -> Term s b
PlutusType
    , -- | @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 :: PType).
(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 (PDataRecord (PFields PTxInInfo)))
-> PDataFields PTxInInfo
forall (s :: S).
Term s PTxInInfo -> Term s (PDataRecord (PFields PTxInInfo))
forall (a :: PType).
(forall (s :: S). Term s a -> Term s (PDataRecord (PFields a)))
-> PDataFields a
$cptoFields :: forall (s :: S).
Term s PTxInInfo -> Term s (PDataRecord (PFields PTxInInfo))
ptoFields :: forall (s :: S).
Term s PTxInInfo -> Term s (PDataRecord (PFields PTxInInfo))
PDataFields
    , -- | @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 :: PType).
(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 :: PType).
(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
    , -- | @since 3.1.1
      PTryFrom PData
    )

-- | @since 3.1.1
instance DerivePlutusType PTxInInfo where
  type DPTStrat _ = PlutusTypeData

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

-- | @since 3.1.1
instance PTryFrom PData (PAsData PTxInInfo)

-- | @since 3.1.1
newtype PTxInfo (s :: S)
  = PTxInfo
      ( Term
          s
          ( PDataRecord
              '[ "inputs" ':= PBuiltinList (PAsData PTxInInfo)
               , "referenceInputs" ':= PBuiltinList (PAsData PTxInInfo)
               , "outputs" ':= PBuiltinList (PAsData V2Tx.PTxOut)
               , "fee" ':= Value.PValue 'AssocMap.Sorted 'Value.Positive
               , "mint" ':= Value.PValue 'AssocMap.Sorted 'Value.NoGuarantees -- value minted by transaction
               , "dcert" ':= PBuiltinList (PAsData DCert.PDCert)
               , "wdrl" ':= AssocMap.PMap 'AssocMap.Unsorted Credential.PStakingCredential PInteger -- Staking withdrawals
               , "validRange" ':= Interval.PInterval Time.PPosixTime
               , "signatories" ':= PBuiltinList (PAsData Crypto.PPubKeyHash)
               , "redeemers" ':= AssocMap.PMap 'AssocMap.Unsorted Contexts.PScriptPurpose Scripts.PRedeemer
               , "data" ':= AssocMap.PMap 'AssocMap.Unsorted Scripts.PDatumHash Scripts.PDatum
               , "id" ':= 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.1.1
      (forall (s :: S). PTxInfo s -> Term s (PInner PTxInfo))
-> (forall (s :: S) (b :: PType).
    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 :: PType).
Term s (PInner PTxInfo) -> (PTxInfo s -> Term s b) -> Term s b
forall (a :: PType).
(forall (s :: S). a s -> Term s (PInner a))
-> (forall (s :: S) (b :: PType).
    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 :: PType).
Term s (PInner PTxInfo) -> (PTxInfo s -> Term s b) -> Term s b
pmatch' :: forall (s :: S) (b :: PType).
Term s (PInner PTxInfo) -> (PTxInfo s -> Term s b) -> Term s b
PlutusType
    , -- | @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 :: PType).
(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 (PDataRecord (PFields PTxInfo)))
-> PDataFields PTxInfo
forall (s :: S).
Term s PTxInfo -> Term s (PDataRecord (PFields PTxInfo))
forall (a :: PType).
(forall (s :: S). Term s a -> Term s (PDataRecord (PFields a)))
-> PDataFields a
$cptoFields :: forall (s :: S).
Term s PTxInfo -> Term s (PDataRecord (PFields PTxInfo))
ptoFields :: forall (s :: S).
Term s PTxInfo -> Term s (PDataRecord (PFields PTxInfo))
PDataFields
    , -- | @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 :: PType).
(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 :: PType).
(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
    , -- | @since 3.1.1
      PTryFrom PData
    )

-- | @since 3.1.1
instance DerivePlutusType PTxInfo where
  type DPTStrat _ = PlutusTypeData

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

-- | @since 3.1.1
instance PTryFrom PData (PAsData PTxInfo)

-- | @since 3.1.1
newtype PScriptContext (s :: S)
  = PScriptContext (Term s (PDataRecord '["txInfo" ':= PTxInfo, "purpose" ':= 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.1.1
      (forall (s :: S).
 PScriptContext s -> Term s (PInner PScriptContext))
-> (forall (s :: S) (b :: PType).
    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 :: PType).
Term s (PInner PScriptContext)
-> (PScriptContext s -> Term s b) -> Term s b
forall (a :: PType).
(forall (s :: S). a s -> Term s (PInner a))
-> (forall (s :: S) (b :: PType).
    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 :: PType).
Term s (PInner PScriptContext)
-> (PScriptContext s -> Term s b) -> Term s b
pmatch' :: forall (s :: S) (b :: PType).
Term s (PInner PScriptContext)
-> (PScriptContext s -> Term s b) -> Term s b
PlutusType
    , -- | @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 :: PType).
(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 (PDataRecord (PFields PScriptContext)))
-> PDataFields PScriptContext
forall (s :: S).
Term s PScriptContext
-> Term s (PDataRecord (PFields PScriptContext))
forall (a :: PType).
(forall (s :: S). Term s a -> Term s (PDataRecord (PFields a)))
-> PDataFields a
$cptoFields :: forall (s :: S).
Term s PScriptContext
-> Term s (PDataRecord (PFields PScriptContext))
ptoFields :: forall (s :: S).
Term s PScriptContext
-> Term s (PDataRecord (PFields PScriptContext))
PDataFields
    , -- | @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 :: PType).
(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 :: PType).
(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
    , -- | @since 3.1.1
      PTryFrom PData
    )

-- | @since 3.1.1
instance DerivePlutusType PScriptContext where
  type DPTStrat _ = PlutusTypeData

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

-- | @since 3.1.1
instance PTryFrom PData (PAsData PScriptContext)