{-# OPTIONS_GHC -Wno-orphans #-}

module Plutarch.LedgerApi.V1 (
  -- * 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 (..),
  Tx.PTxId (..),
  PTxOut (..),
  PTxInInfo (..),
  Tx.PTxOutRef (..),
  Crypto.PPubKeyHash (..),
  PTxInfo (..),

  -- * 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 Tx
import Plutarch.LedgerApi.Value qualified as Value
import Plutarch.Prelude
import PlutusLedgerApi.V1 qualified as Plutus

-- | @since 3.1.1
newtype PTxOut (s :: S)
  = PTxOut
      ( Term
          s
          ( PDataRecord
              '[ "address" ':= Address.PAddress
               , "value" ':= Value.PValue 'AssocMap.Sorted 'Value.Positive
               , "datumHash" ':= Scripts.PDatumHash
               ]
          )
      )
  deriving stock
    ( -- | @since 3.1.1
      (forall x. PTxOut s -> Rep (PTxOut s) x)
-> (forall x. Rep (PTxOut s) x -> PTxOut s) -> Generic (PTxOut s)
forall x. Rep (PTxOut s) x -> PTxOut s
forall x. PTxOut s -> Rep (PTxOut s) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (s :: S) x. Rep (PTxOut s) x -> PTxOut s
forall (s :: S) x. PTxOut s -> Rep (PTxOut s) x
$cfrom :: forall (s :: S) x. PTxOut s -> Rep (PTxOut s) x
from :: forall x. PTxOut s -> Rep (PTxOut s) x
$cto :: forall (s :: S) x. Rep (PTxOut s) x -> PTxOut s
to :: forall x. Rep (PTxOut s) x -> PTxOut s
Generic
    )
  deriving anyclass
    ( -- | @since 3.1.1
      (forall (s :: S). PTxOut s -> Term s (PInner PTxOut))
-> (forall (s :: S) (b :: PType).
    Term s (PInner PTxOut) -> (PTxOut s -> Term s b) -> Term s b)
-> PlutusType PTxOut
forall (s :: S). PTxOut s -> Term s (PInner PTxOut)
forall (s :: S) (b :: PType).
Term s (PInner PTxOut) -> (PTxOut 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). PTxOut s -> Term s (PInner PTxOut)
pcon' :: forall (s :: S). PTxOut s -> Term s (PInner PTxOut)
$cpmatch' :: forall (s :: S) (b :: PType).
Term s (PInner PTxOut) -> (PTxOut s -> Term s b) -> Term s b
pmatch' :: forall (s :: S) (b :: PType).
Term s (PInner PTxOut) -> (PTxOut s -> Term s b) -> Term s b
PlutusType
    , -- | @since 3.1.1
      (forall (s :: S). Term s (PAsData PTxOut) -> Term s PTxOut)
-> (forall (s :: S). Term s PTxOut -> Term s PData)
-> PIsData PTxOut
forall (s :: S). Term s (PAsData PTxOut) -> Term s PTxOut
forall (s :: S). Term s PTxOut -> 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 PTxOut) -> Term s PTxOut
pfromDataImpl :: forall (s :: S). Term s (PAsData PTxOut) -> Term s PTxOut
$cpdataImpl :: forall (s :: S). Term s PTxOut -> Term s PData
pdataImpl :: forall (s :: S). Term s PTxOut -> Term s PData
PIsData
    , -- | @since 3.1.1
      (forall (s :: S). Term s PTxOut -> Term s PTxOut -> Term s PBool)
-> PEq PTxOut
forall (s :: S). Term s PTxOut -> Term s PTxOut -> 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 PTxOut -> Term s PTxOut -> Term s PBool
#== :: forall (s :: S). Term s PTxOut -> Term s PTxOut -> Term s PBool
PEq
    , -- | @since 3.1.1
      (forall (s :: S). Bool -> Term s PTxOut -> Term s PString)
-> PShow PTxOut
forall (s :: S). Bool -> Term s PTxOut -> 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 PTxOut -> Term s PString
pshow' :: forall (s :: S). Bool -> Term s PTxOut -> Term s PString
PShow
    , -- | @since 3.1.1
      PTryFrom PData
    )

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

-- | @since WIP
deriving via
  DeriveDataPLiftable PTxOut Plutus.TxOut
  instance
    PLiftable PTxOut

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

-- | @since 3.1.1
newtype PTxInInfo (s :: S)
  = PTxInInfo
      ( Term
          s
          ( PDataRecord
              '[ "outRef" ':= Tx.PTxOutRef
               , "resolved" ':= 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 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 WIP
newtype PTxInfo (s :: S)
  = PTxInfo
      ( Term
          s
          ( PDataRecord
              '[ "inputs" ':= PBuiltinList (PAsData PTxInInfo)
               , "outputs" ':= PBuiltinList (PAsData 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" ':= PBuiltinList (PAsData (PBuiltinPair (PAsData Credential.PStakingCredential) (PAsData PInteger))) -- Staking withdrawals
               , "validRange" ':= Interval.PInterval Time.PPosixTime
               , "signatories" ':= PBuiltinList (PAsData Crypto.PPubKeyHash)
               , "data" ':= PBuiltinList (PAsData (PBuiltinPair (PAsData Scripts.PDatumHash) (PAsData Scripts.PDatum)))
               , "id" ':= Tx.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)