{-# OPTIONS_GHC -Wno-deprecations #-}
{-# OPTIONS_GHC -Wno-orphans #-}

-- Mirrors the equivalent V4 module in plutus-ledger-api
module Plutarch.LedgerApi.V4.Address (
  PAccountId (..),
  PAddress (..),
) where

import GHC.Generics (Generic)
import Generics.SOP qualified as SOP
import Plutarch.Internal.Lift (
  LiftError (CouldNotDecodeData),
  punsafeCoercePLifted,
 )
import Plutarch.LedgerApi.Utils (PMaybeData)
import Plutarch.LedgerApi.V1.Credential (PCredential)
import Plutarch.Prelude
import PlutusLedgerApi.V4 qualified as Plutus

-- | @since 3.8.0
newtype PAccountId (s :: S) = PAccountId (Term s PCredential)
  deriving stock
    ( -- | @since 3.8.0
      (forall x. PAccountId s -> Rep (PAccountId s) x)
-> (forall x. Rep (PAccountId s) x -> PAccountId s)
-> Generic (PAccountId s)
forall x. Rep (PAccountId s) x -> PAccountId s
forall x. PAccountId s -> Rep (PAccountId s) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (s :: S) x. Rep (PAccountId s) x -> PAccountId s
forall (s :: S) x. PAccountId s -> Rep (PAccountId s) x
$cfrom :: forall (s :: S) x. PAccountId s -> Rep (PAccountId s) x
from :: forall x. PAccountId s -> Rep (PAccountId s) x
$cto :: forall (s :: S) x. Rep (PAccountId s) x -> PAccountId s
to :: forall x. Rep (PAccountId s) x -> PAccountId s
Generic
    )
  deriving anyclass
    ( -- | @since 3.8.0
      All SListI (Code (PAccountId s))
All SListI (Code (PAccountId s)) =>
(PAccountId s -> Rep (PAccountId s))
-> (Rep (PAccountId s) -> PAccountId s) -> Generic (PAccountId s)
Rep (PAccountId s) -> PAccountId s
PAccountId s -> Rep (PAccountId s)
forall a.
All SListI (Code a) =>
(a -> Rep a) -> (Rep a -> a) -> Generic a
forall (s :: S). All SListI (Code (PAccountId s))
forall (s :: S). Rep (PAccountId s) -> PAccountId s
forall (s :: S). PAccountId s -> Rep (PAccountId s)
$cfrom :: forall (s :: S). PAccountId s -> Rep (PAccountId s)
from :: PAccountId s -> Rep (PAccountId s)
$cto :: forall (s :: S). Rep (PAccountId s) -> PAccountId s
to :: Rep (PAccountId s) -> PAccountId s
SOP.Generic
    , -- | @since 3.8.0
      (forall (s :: S). Term s (PAsData PAccountId) -> Term s PAccountId)
-> (forall (s :: S). Term s PAccountId -> Term s PData)
-> PIsData PAccountId
forall (s :: S). Term s (PAsData PAccountId) -> Term s PAccountId
forall (s :: S). Term s PAccountId -> 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 PAccountId) -> Term s PAccountId
pfromDataImpl :: forall (s :: S). Term s (PAsData PAccountId) -> Term s PAccountId
$cpdataImpl :: forall (s :: S). Term s PAccountId -> Term s PData
pdataImpl :: forall (s :: S). Term s PAccountId -> Term s PData
PIsData
    , -- | @since 3.8.0
      (forall (s :: S).
 Term s PAccountId -> Term s PAccountId -> Term s PBool)
-> PEq PAccountId
forall (s :: S).
Term s PAccountId -> Term s PAccountId -> 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 PAccountId -> Term s PAccountId -> Term s PBool
#== :: forall (s :: S).
Term s PAccountId -> Term s PAccountId -> Term s PBool
PEq
    , -- | @since 3.8.0
      (forall (s :: S). Bool -> Term s PAccountId -> Term s PString)
-> PShow PAccountId
forall (s :: S). Bool -> Term s PAccountId -> 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 PAccountId -> Term s PString
pshow' :: forall (s :: S). Bool -> Term s PAccountId -> Term s PString
PShow
    )
  deriving
    ( -- | @since 3.8.0
      (forall (s :: S). PAccountId s -> Term s (PInner PAccountId))
-> (forall (s :: S) (b :: S -> Type).
    Term s (PInner PAccountId)
    -> (PAccountId s -> Term s b) -> Term s b)
-> PlutusType PAccountId
forall (s :: S). PAccountId s -> Term s (PInner PAccountId)
forall (s :: S) (b :: S -> Type).
Term s (PInner PAccountId)
-> (PAccountId 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). PAccountId s -> Term s (PInner PAccountId)
pcon' :: forall (s :: S). PAccountId s -> Term s (PInner PAccountId)
$cpmatch' :: forall (s :: S) (b :: S -> Type).
Term s (PInner PAccountId)
-> (PAccountId s -> Term s b) -> Term s b
pmatch' :: forall (s :: S) (b :: S -> Type).
Term s (PInner PAccountId)
-> (PAccountId s -> Term s b) -> Term s b
PlutusType
    )
    via (DeriveNewtypePlutusType PAccountId)
  deriving
    ( -- | @since 3.8.0
      (forall (s :: S).
 Term s PData -> forall (r :: S -> Type). Term s r -> Term s r)
-> PValidateData PAccountId
forall (s :: S).
Term s PData -> forall (r :: S -> Type). Term s r -> Term s r
forall (a :: S -> Type).
(forall (s :: S).
 Term s PData -> forall (r :: S -> Type). Term s r -> Term s r)
-> PValidateData a
$cpwithValidated :: forall (s :: S).
Term s PData -> forall (r :: S -> Type). Term s r -> Term s r
pwithValidated :: forall (s :: S).
Term s PData -> forall (r :: S -> Type). Term s r -> Term s r
PValidateData
    )
    via (DeriveNewtypePValidateData PAccountId PCredential)

-- | @since 3.8.0
deriving via
  DeriveDataPLiftable PAccountId Plutus.AccountId
  instance
    PLiftable PAccountId

-- | @since 3.8.0
instance PTryFrom PData (PAsData PAccountId)

-- | @since 3.8.0
data PAddress (s :: S) = PAddress
  { forall (s :: S). PAddress s -> Term s (PAsData PCredential)
paddress'credential :: Term s (PAsData PCredential)
  , forall (s :: S). PAddress s -> Term s (PMaybeData PAccountId)
paddress'stakingAccountId :: Term s (PMaybeData PAccountId)
  }
  deriving stock
    ( -- | @since 3.8.0
      (forall x. PAddress s -> Rep (PAddress s) x)
-> (forall x. Rep (PAddress s) x -> PAddress s)
-> Generic (PAddress s)
forall x. Rep (PAddress s) x -> PAddress s
forall x. PAddress s -> Rep (PAddress s) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (s :: S) x. Rep (PAddress s) x -> PAddress s
forall (s :: S) x. PAddress s -> Rep (PAddress s) x
$cfrom :: forall (s :: S) x. PAddress s -> Rep (PAddress s) x
from :: forall x. PAddress s -> Rep (PAddress s) x
$cto :: forall (s :: S) x. Rep (PAddress s) x -> PAddress s
to :: forall x. Rep (PAddress s) x -> PAddress s
Generic
    )
  deriving anyclass
    ( -- | @since 3.8.0
      All SListI (Code (PAddress s))
All SListI (Code (PAddress s)) =>
(PAddress s -> Rep (PAddress s))
-> (Rep (PAddress s) -> PAddress s) -> Generic (PAddress s)
Rep (PAddress s) -> PAddress s
PAddress s -> Rep (PAddress s)
forall a.
All SListI (Code a) =>
(a -> Rep a) -> (Rep a -> a) -> Generic a
forall (s :: S). All SListI (Code (PAddress s))
forall (s :: S). Rep (PAddress s) -> PAddress s
forall (s :: S). PAddress s -> Rep (PAddress s)
$cfrom :: forall (s :: S). PAddress s -> Rep (PAddress s)
from :: PAddress s -> Rep (PAddress s)
$cto :: forall (s :: S). Rep (PAddress s) -> PAddress s
to :: Rep (PAddress s) -> PAddress s
SOP.Generic
    , -- | @since 3.8.0
      (forall (s :: S). Term s (PAsData PAddress) -> Term s PAddress)
-> (forall (s :: S). Term s PAddress -> Term s PData)
-> PIsData PAddress
forall (s :: S). Term s (PAsData PAddress) -> Term s PAddress
forall (s :: S). Term s PAddress -> 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 PAddress) -> Term s PAddress
pfromDataImpl :: forall (s :: S). Term s (PAsData PAddress) -> Term s PAddress
$cpdataImpl :: forall (s :: S). Term s PAddress -> Term s PData
pdataImpl :: forall (s :: S). Term s PAddress -> Term s PData
PIsData
    , -- | @since 3.8.0
      (forall (s :: S).
 Term s PAddress -> Term s PAddress -> Term s PBool)
-> PEq PAddress
forall (s :: S). Term s PAddress -> Term s PAddress -> 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 PAddress -> Term s PAddress -> Term s PBool
#== :: forall (s :: S). Term s PAddress -> Term s PAddress -> Term s PBool
PEq
    , -- | @since 3.8.0
      (forall (s :: S). Bool -> Term s PAddress -> Term s PString)
-> PShow PAddress
forall (s :: S). Bool -> Term s PAddress -> 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 PAddress -> Term s PString
pshow' :: forall (s :: S). Bool -> Term s PAddress -> Term s PString
PShow
    )
  deriving
    ( -- | @since 3.8.0
      (forall (s :: S). PAddress s -> Term s (PInner PAddress))
-> (forall (s :: S) (b :: S -> Type).
    Term s (PInner PAddress) -> (PAddress s -> Term s b) -> Term s b)
-> PlutusType PAddress
forall (s :: S). PAddress s -> Term s (PInner PAddress)
forall (s :: S) (b :: S -> Type).
Term s (PInner PAddress) -> (PAddress 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). PAddress s -> Term s (PInner PAddress)
pcon' :: forall (s :: S). PAddress s -> Term s (PInner PAddress)
$cpmatch' :: forall (s :: S) (b :: S -> Type).
Term s (PInner PAddress) -> (PAddress s -> Term s b) -> Term s b
pmatch' :: forall (s :: S) (b :: S -> Type).
Term s (PInner PAddress) -> (PAddress s -> Term s b) -> Term s b
PlutusType
    , -- | @since 3.8.0
      (forall (s :: S).
 Term s PData -> forall (r :: S -> Type). Term s r -> Term s r)
-> PValidateData PAddress
forall (s :: S).
Term s PData -> forall (r :: S -> Type). Term s r -> Term s r
forall (a :: S -> Type).
(forall (s :: S).
 Term s PData -> forall (r :: S -> Type). Term s r -> Term s r)
-> PValidateData a
$cpwithValidated :: forall (s :: S).
Term s PData -> forall (r :: S -> Type). Term s r -> Term s r
pwithValidated :: forall (s :: S).
Term s PData -> forall (r :: S -> Type). Term s r -> Term s r
PValidateData
    )
    via (DeriveAsDataRec PAddress)

-- | @since 3.8.0
instance PLiftable PAddress where
  type AsHaskell PAddress = Plutus.Address
  type PlutusRepr PAddress = Plutus.Data
  haskToRepr :: AsHaskell PAddress -> PlutusRepr PAddress
haskToRepr = AsHaskell PAddress -> PlutusRepr PAddress
Address -> Data
forall a. ToData a => a -> Data
Plutus.toData
  reprToHask :: PlutusRepr PAddress -> Either LiftError (AsHaskell PAddress)
reprToHask = Either LiftError Address
-> (Address -> Either LiftError Address)
-> Maybe Address
-> Either LiftError Address
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (LiftError -> Either LiftError Address
forall a b. a -> Either a b
Left LiftError
CouldNotDecodeData) Address -> Either LiftError Address
forall a b. b -> Either a b
Right (Maybe Address -> Either LiftError Address)
-> (Data -> Maybe Address) -> Data -> Either LiftError Address
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Data -> Maybe Address
forall a. FromData a => Data -> Maybe a
Plutus.fromData
  plutToRepr :: (forall (s :: S). PLifted s PAddress)
-> Either LiftError (PlutusRepr PAddress)
plutToRepr forall (s :: S). PLifted s PAddress
t = ([Data] -> PlutusRepr PAddress)
-> Either LiftError [Data]
-> Either LiftError (PlutusRepr PAddress)
forall a b. (a -> b) -> Either LiftError a -> Either LiftError b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap [Data] -> PlutusRepr PAddress
[Data] -> Data
Plutus.List (Either LiftError [Data] -> Either LiftError (PlutusRepr PAddress))
-> Either LiftError [Data]
-> Either LiftError (PlutusRepr PAddress)
forall a b. (a -> b) -> a -> b
$ forall (a :: S -> Type).
KnownBuiltinType
  (Term DeBruijn DefaultUni DefaultFun ()) (PlutusRepr a) =>
(forall (s :: S). PLifted s a) -> Either LiftError (PlutusRepr a)
plutToReprUni @(PBuiltinList PData) ((forall (s :: S). PLifted s (PBuiltinList PData))
 -> Either LiftError (PlutusRepr (PBuiltinList PData)))
-> (forall (s :: S). PLifted s (PBuiltinList PData))
-> Either LiftError (PlutusRepr (PBuiltinList PData))
forall a b. (a -> b) -> a -> b
$ PLifted s PAddress -> PLifted s (PBuiltinList PData)
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
PLifted s a -> PLifted s b
punsafeCoercePLifted PLifted s PAddress
forall (s :: S). PLifted s PAddress
t
  reprToPlut :: forall (s :: S). PlutusRepr PAddress -> PLifted s PAddress
reprToPlut = \case
    Plutus.List [Data]
ell -> PLifted s (PBuiltinList PData) -> PLifted s PAddress
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
PLifted s a -> PLifted s b
punsafeCoercePLifted (forall (a :: S -> Type) (s :: S).
Includes DefaultUni (PlutusRepr a) =>
PlutusRepr a -> PLifted s a
reprToPlutUni @(PBuiltinList PData) [Data]
PlutusRepr (PBuiltinList PData)
ell)
    PlutusRepr PAddress
_ -> [Char] -> PLifted s PAddress
forall a. HasCallStack => [Char] -> a
error [Char]
"Impossible: PAddress is list-represented."

-- | @since 3.8.0
instance PTryFrom PData (PAsData PAddress)