{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -Wno-orphans #-}

module Cardano.Ledger.Mary.TxCert () where

import Cardano.Ledger.Crypto (Crypto, StandardCrypto)
import Cardano.Ledger.Mary.Era (MaryEra)
import Cardano.Ledger.Mary.PParams ()
import Cardano.Ledger.Shelley.TxCert (
  EraTxCert (..),
  PoolCert (..),
  ShelleyDelegCert (..),
  ShelleyEraTxCert (..),
  ShelleyTxCert (..),
  getScriptWitnessShelleyTxCert,
  getVKeyWitnessShelleyTxCert,
  shelleyTotalDepositsTxCerts,
  shelleyTotalRefundsTxCerts,
  upgradeShelleyTxCert,
  pattern RegTxCert,
  pattern UnRegTxCert,
 )

instance Crypto c => EraTxCert (MaryEra c) where
  {-# SPECIALIZE instance EraTxCert (MaryEra StandardCrypto) #-}

  type TxCert (MaryEra c) = ShelleyTxCert (MaryEra c)

  upgradeTxCert :: EraTxCert (PreviousEra (MaryEra c)) =>
TxCert (PreviousEra (MaryEra c))
-> Either (TxCertUpgradeError (MaryEra c)) (TxCert (MaryEra c))
upgradeTxCert = forall a b. b -> Either a b
Right forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall era1 era2.
(EraCrypto era1 ~ EraCrypto era2) =>
ShelleyTxCert era1 -> ShelleyTxCert era2
upgradeShelleyTxCert

  getVKeyWitnessTxCert :: TxCert (MaryEra c)
-> Maybe (KeyHash 'Witness (EraCrypto (MaryEra c)))
getVKeyWitnessTxCert = forall era.
ShelleyTxCert era -> Maybe (KeyHash 'Witness (EraCrypto era))
getVKeyWitnessShelleyTxCert

  getScriptWitnessTxCert :: TxCert (MaryEra c) -> Maybe (ScriptHash (EraCrypto (MaryEra c)))
getScriptWitnessTxCert = forall era. ShelleyTxCert era -> Maybe (ScriptHash (EraCrypto era))
getScriptWitnessShelleyTxCert

  mkRegPoolTxCert :: PoolParams (EraCrypto (MaryEra c)) -> TxCert (MaryEra c)
mkRegPoolTxCert = forall era. PoolCert (EraCrypto era) -> ShelleyTxCert era
ShelleyTxCertPool forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. PoolParams c -> PoolCert c
RegPool

  getRegPoolTxCert :: TxCert (MaryEra c) -> Maybe (PoolParams (EraCrypto (MaryEra c)))
getRegPoolTxCert (ShelleyTxCertPool (RegPool PoolParams (EraCrypto (MaryEra c))
poolParams)) = forall a. a -> Maybe a
Just PoolParams (EraCrypto (MaryEra c))
poolParams
  getRegPoolTxCert TxCert (MaryEra c)
_ = forall a. Maybe a
Nothing

  mkRetirePoolTxCert :: KeyHash 'StakePool (EraCrypto (MaryEra c))
-> EpochNo -> TxCert (MaryEra c)
mkRetirePoolTxCert KeyHash 'StakePool (EraCrypto (MaryEra c))
poolId EpochNo
epochNo = forall era. PoolCert (EraCrypto era) -> ShelleyTxCert era
ShelleyTxCertPool forall a b. (a -> b) -> a -> b
$ forall c. KeyHash 'StakePool c -> EpochNo -> PoolCert c
RetirePool KeyHash 'StakePool (EraCrypto (MaryEra c))
poolId EpochNo
epochNo

  getRetirePoolTxCert :: TxCert (MaryEra c)
-> Maybe (KeyHash 'StakePool (EraCrypto (MaryEra c)), EpochNo)
getRetirePoolTxCert (ShelleyTxCertPool (RetirePool KeyHash 'StakePool (EraCrypto (MaryEra c))
poolId EpochNo
epochNo)) = forall a. a -> Maybe a
Just (KeyHash 'StakePool (EraCrypto (MaryEra c))
poolId, EpochNo
epochNo)
  getRetirePoolTxCert TxCert (MaryEra c)
_ = forall a. Maybe a
Nothing

  lookupRegStakeTxCert :: TxCert (MaryEra c)
-> Maybe (Credential 'Staking (EraCrypto (MaryEra c)))
lookupRegStakeTxCert = \case
    RegTxCert Credential 'Staking (EraCrypto (MaryEra c))
c -> forall a. a -> Maybe a
Just Credential 'Staking (EraCrypto (MaryEra c))
c
    TxCert (MaryEra c)
_ -> forall a. Maybe a
Nothing
  lookupUnRegStakeTxCert :: TxCert (MaryEra c)
-> Maybe (Credential 'Staking (EraCrypto (MaryEra c)))
lookupUnRegStakeTxCert = \case
    UnRegTxCert Credential 'Staking (EraCrypto (MaryEra c))
c -> forall a. a -> Maybe a
Just Credential 'Staking (EraCrypto (MaryEra c))
c
    TxCert (MaryEra c)
_ -> forall a. Maybe a
Nothing

  getTotalDepositsTxCerts :: forall (f :: * -> *).
Foldable f =>
PParams (MaryEra c)
-> (KeyHash 'StakePool (EraCrypto (MaryEra c)) -> Bool)
-> f (TxCert (MaryEra c))
-> Coin
getTotalDepositsTxCerts = forall era (f :: * -> *).
(EraPParams era, Foldable f, EraTxCert era) =>
PParams era
-> (KeyHash 'StakePool (EraCrypto era) -> Bool)
-> f (TxCert era)
-> Coin
shelleyTotalDepositsTxCerts

  getTotalRefundsTxCerts :: forall (f :: * -> *).
Foldable f =>
PParams (MaryEra c)
-> (Credential 'Staking (EraCrypto (MaryEra c)) -> Maybe Coin)
-> (Credential 'DRepRole (EraCrypto (MaryEra c)) -> Maybe Coin)
-> f (TxCert (MaryEra c))
-> Coin
getTotalRefundsTxCerts PParams (MaryEra c)
pp Credential 'Staking (EraCrypto (MaryEra c)) -> Maybe Coin
lookupStakeDeposit Credential 'DRepRole (EraCrypto (MaryEra c)) -> Maybe Coin
_ = forall era (f :: * -> *).
(EraPParams era, Foldable f, EraTxCert era) =>
PParams era
-> (StakeCredential (EraCrypto era) -> Maybe Coin)
-> f (TxCert era)
-> Coin
shelleyTotalRefundsTxCerts PParams (MaryEra c)
pp Credential 'Staking (EraCrypto (MaryEra c)) -> Maybe Coin
lookupStakeDeposit

instance Crypto c => ShelleyEraTxCert (MaryEra c) where
  {-# SPECIALIZE instance ShelleyEraTxCert (MaryEra StandardCrypto) #-}

  mkRegTxCert :: StakeCredential (EraCrypto (MaryEra c)) -> TxCert (MaryEra c)
mkRegTxCert = forall era. ShelleyDelegCert (EraCrypto era) -> ShelleyTxCert era
ShelleyTxCertDelegCert forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. StakeCredential c -> ShelleyDelegCert c
ShelleyRegCert

  getRegTxCert :: TxCert (MaryEra c)
-> Maybe (StakeCredential (EraCrypto (MaryEra c)))
getRegTxCert (ShelleyTxCertDelegCert (ShelleyRegCert StakeCredential (EraCrypto (MaryEra c))
c)) = forall a. a -> Maybe a
Just StakeCredential (EraCrypto (MaryEra c))
c
  getRegTxCert TxCert (MaryEra c)
_ = forall a. Maybe a
Nothing

  mkUnRegTxCert :: StakeCredential (EraCrypto (MaryEra c)) -> TxCert (MaryEra c)
mkUnRegTxCert = forall era. ShelleyDelegCert (EraCrypto era) -> ShelleyTxCert era
ShelleyTxCertDelegCert forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. StakeCredential c -> ShelleyDelegCert c
ShelleyUnRegCert

  getUnRegTxCert :: TxCert (MaryEra c)
-> Maybe (StakeCredential (EraCrypto (MaryEra c)))
getUnRegTxCert (ShelleyTxCertDelegCert (ShelleyUnRegCert StakeCredential (EraCrypto (MaryEra c))
c)) = forall a. a -> Maybe a
Just StakeCredential (EraCrypto (MaryEra c))
c
  getUnRegTxCert TxCert (MaryEra c)
_ = forall a. Maybe a
Nothing

  mkDelegStakeTxCert :: StakeCredential (EraCrypto (MaryEra c))
-> KeyHash 'StakePool (EraCrypto (MaryEra c)) -> TxCert (MaryEra c)
mkDelegStakeTxCert StakeCredential (EraCrypto (MaryEra c))
c KeyHash 'StakePool (EraCrypto (MaryEra c))
kh = forall era. ShelleyDelegCert (EraCrypto era) -> ShelleyTxCert era
ShelleyTxCertDelegCert forall a b. (a -> b) -> a -> b
$ forall c.
StakeCredential c -> KeyHash 'StakePool c -> ShelleyDelegCert c
ShelleyDelegCert StakeCredential (EraCrypto (MaryEra c))
c KeyHash 'StakePool (EraCrypto (MaryEra c))
kh

  getDelegStakeTxCert :: TxCert (MaryEra c)
-> Maybe
     (StakeCredential (EraCrypto (MaryEra c)),
      KeyHash 'StakePool (EraCrypto (MaryEra c)))
getDelegStakeTxCert (ShelleyTxCertDelegCert (ShelleyDelegCert StakeCredential (EraCrypto (MaryEra c))
c KeyHash 'StakePool (EraCrypto (MaryEra c))
kh)) = forall a. a -> Maybe a
Just (StakeCredential (EraCrypto (MaryEra c))
c, KeyHash 'StakePool (EraCrypto (MaryEra c))
kh)
  getDelegStakeTxCert TxCert (MaryEra c)
_ = forall a. Maybe a
Nothing

  mkGenesisDelegTxCert :: ProtVerAtMost (MaryEra c) 8 =>
GenesisDelegCert (EraCrypto (MaryEra c)) -> TxCert (MaryEra c)
mkGenesisDelegTxCert = forall era. GenesisDelegCert (EraCrypto era) -> ShelleyTxCert era
ShelleyTxCertGenesisDeleg

  getGenesisDelegTxCert :: ProtVerAtMost (MaryEra c) 8 =>
TxCert (MaryEra c)
-> Maybe (GenesisDelegCert (EraCrypto (MaryEra c)))
getGenesisDelegTxCert (ShelleyTxCertGenesisDeleg GenesisDelegCert (EraCrypto (MaryEra c))
c) = forall a. a -> Maybe a
Just GenesisDelegCert (EraCrypto (MaryEra c))
c
  getGenesisDelegTxCert TxCert (MaryEra c)
_ = forall a. Maybe a
Nothing

  mkMirTxCert :: ProtVerAtMost (MaryEra c) 8 =>
MIRCert (EraCrypto (MaryEra c)) -> TxCert (MaryEra c)
mkMirTxCert = forall era. MIRCert (EraCrypto era) -> ShelleyTxCert era
ShelleyTxCertMir

  getMirTxCert :: ProtVerAtMost (MaryEra c) 8 =>
TxCert (MaryEra c) -> Maybe (MIRCert (EraCrypto (MaryEra c)))
getMirTxCert (ShelleyTxCertMir MIRCert (EraCrypto (MaryEra c))
c) = forall a. a -> Maybe a
Just MIRCert (EraCrypto (MaryEra c))
c
  getMirTxCert TxCert (MaryEra c)
_ = forall a. Maybe a
Nothing