{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}

module Cardano.Api.LedgerEvents.Rule.BBODY.UTXOW
  ( handleAlonzoUTxOWEvent
  , handleAllegraMaryUTxOWEvent
  , handleConwayUTxOWEvent
  , handlePreAlonzoUTxOWEvent
  )
where

import           Cardano.Api.LedgerEvents.LedgerEvent

import qualified Cardano.Ledger.Allegra.Rules as Allegra
import           Cardano.Ledger.Alonzo.Rules (AlonzoUtxoEvent (..), AlonzoUtxosEvent (..),
                   AlonzoUtxowEvent (..))
import qualified Cardano.Ledger.Alonzo.Rules as Alonzo
import           Cardano.Ledger.Conway
import qualified Cardano.Ledger.Conway.Rules as Conway
import qualified Cardano.Ledger.Core as Ledger.Core
import qualified Cardano.Ledger.Crypto as Crypto
import qualified Cardano.Ledger.Shelley.Rules as Shelley

import           Control.State.Transition.Extended

handleConwayUTxOWEvent
  :: AlonzoUtxowEvent (ConwayEra Crypto.StandardCrypto) -> Maybe LedgerEvent
handleConwayUTxOWEvent :: AlonzoUtxowEvent (ConwayEra StandardCrypto) -> Maybe LedgerEvent
handleConwayUTxOWEvent (Alonzo.WrappedShelleyEraEvent (Shelley.UtxoEvent (Alonzo.UtxosEvent Event (EraRule "UTXOS" (ConwayEra StandardCrypto))
conwayUTxOsEvent))) =
  case Event (EraRule "UTXOS" (ConwayEra StandardCrypto))
conwayUTxOsEvent of
    Conway.TotalDeposits{} -> Maybe LedgerEvent
forall a. Maybe a
Nothing
    Conway.SuccessfulPlutusScriptsEvent NonEmpty (PlutusWithContext (EraCrypto (ConwayEra StandardCrypto)))
e -> LedgerEvent -> Maybe LedgerEvent
forall a. a -> Maybe a
Just (LedgerEvent -> Maybe LedgerEvent)
-> LedgerEvent -> Maybe LedgerEvent
forall a b. (a -> b) -> a -> b
$ NonEmpty (PlutusWithContext StandardCrypto) -> LedgerEvent
SuccessfulPlutusScript NonEmpty (PlutusWithContext (EraCrypto (ConwayEra StandardCrypto)))
NonEmpty (PlutusWithContext StandardCrypto)
e
    Conway.FailedPlutusScriptsEvent NonEmpty (PlutusWithContext (EraCrypto (ConwayEra StandardCrypto)))
e -> LedgerEvent -> Maybe LedgerEvent
forall a. a -> Maybe a
Just (LedgerEvent -> Maybe LedgerEvent)
-> LedgerEvent -> Maybe LedgerEvent
forall a b. (a -> b) -> a -> b
$ NonEmpty (PlutusWithContext StandardCrypto) -> LedgerEvent
FailedPlutusScript NonEmpty (PlutusWithContext (EraCrypto (ConwayEra StandardCrypto)))
NonEmpty (PlutusWithContext StandardCrypto)
e
    Conway.TxUTxODiff UTxO (ConwayEra StandardCrypto)
_ UTxO (ConwayEra StandardCrypto)
_ -> Maybe LedgerEvent
forall a. Maybe a
Nothing

handleAlonzoUTxOWEvent
  :: Event (Ledger.Core.EraRule "UTXO" ledgerera) ~ AlonzoUtxoEvent ledgerera
  => Event (Ledger.Core.EraRule "UTXOS" ledgerera) ~ AlonzoUtxosEvent ledgerera
  => Ledger.Core.EraCrypto ledgerera ~ Crypto.StandardCrypto
  => AlonzoUtxowEvent ledgerera -> Maybe LedgerEvent
handleAlonzoUTxOWEvent :: forall ledgerera.
(Event (EraRule "UTXO" ledgerera) ~ AlonzoUtxoEvent ledgerera,
 Event (EraRule "UTXOS" ledgerera) ~ AlonzoUtxosEvent ledgerera,
 EraCrypto ledgerera ~ StandardCrypto) =>
AlonzoUtxowEvent ledgerera -> Maybe LedgerEvent
handleAlonzoUTxOWEvent (WrappedShelleyEraEvent (Shelley.UtxoEvent (UtxosEvent Event (EraRule "UTXOS" ledgerera)
utxoEvent))) =
  case Event (EraRule "UTXOS" ledgerera)
utxoEvent of
    Alonzo.AlonzoPpupToUtxosEvent{} -> Maybe LedgerEvent
forall a. Maybe a
Nothing
    Alonzo.TotalDeposits{} -> Maybe LedgerEvent
forall a. Maybe a
Nothing
    Alonzo.SuccessfulPlutusScriptsEvent NonEmpty (PlutusWithContext (EraCrypto ledgerera))
e -> LedgerEvent -> Maybe LedgerEvent
forall a. a -> Maybe a
Just (LedgerEvent -> Maybe LedgerEvent)
-> LedgerEvent -> Maybe LedgerEvent
forall a b. (a -> b) -> a -> b
$ NonEmpty (PlutusWithContext StandardCrypto) -> LedgerEvent
SuccessfulPlutusScript NonEmpty (PlutusWithContext (EraCrypto ledgerera))
NonEmpty (PlutusWithContext StandardCrypto)
e
    Alonzo.FailedPlutusScriptsEvent NonEmpty (PlutusWithContext (EraCrypto ledgerera))
e -> LedgerEvent -> Maybe LedgerEvent
forall a. a -> Maybe a
Just (LedgerEvent -> Maybe LedgerEvent)
-> LedgerEvent -> Maybe LedgerEvent
forall a b. (a -> b) -> a -> b
$ NonEmpty (PlutusWithContext StandardCrypto) -> LedgerEvent
FailedPlutusScript NonEmpty (PlutusWithContext (EraCrypto ledgerera))
NonEmpty (PlutusWithContext StandardCrypto)
e
    Alonzo.TxUTxODiff UTxO ledgerera
_ UTxO ledgerera
_ -> Maybe LedgerEvent
forall a. Maybe a
Nothing

handlePreAlonzoUTxOWEvent
  :: Event (Ledger.Core.EraRule "UTXO" ledgerera) ~ Shelley.UtxoEvent ledgerera
  => Event (Ledger.Core.EraRule "PPUP" ledgerera) ~ Shelley.PpupEvent ledgerera
  => Shelley.ShelleyUtxowEvent ledgerera -> Maybe LedgerEvent
handlePreAlonzoUTxOWEvent :: forall ledgerera.
(Event (EraRule "UTXO" ledgerera) ~ UtxoEvent ledgerera,
 Event (EraRule "PPUP" ledgerera) ~ PpupEvent ledgerera) =>
ShelleyUtxowEvent ledgerera -> Maybe LedgerEvent
handlePreAlonzoUTxOWEvent (Shelley.UtxoEvent Event (EraRule "UTXO" ledgerera)
e) =
  case Event (EraRule "UTXO" ledgerera)
e of
    Shelley.TotalDeposits{} -> Maybe LedgerEvent
forall a. Maybe a
Nothing
    Shelley.UpdateEvent (Shelley.PpupNewEpoch EpochNo
_) -> Maybe LedgerEvent
forall a. Maybe a
Nothing
    Shelley.TxUTxODiff UTxO ledgerera
_ UTxO ledgerera
_ -> Maybe LedgerEvent
forall a. Maybe a
Nothing

handleAllegraMaryUTxOWEvent
  :: Event (Ledger.Core.EraRule "UTXO" ledgerera) ~ Allegra.AllegraUtxoEvent ledgerera
  => Event (Ledger.Core.EraRule "PPUP" ledgerera) ~ Shelley.PpupEvent ledgerera
  => Shelley.ShelleyUtxowEvent ledgerera -> Maybe LedgerEvent
handleAllegraMaryUTxOWEvent :: forall ledgerera.
(Event (EraRule "UTXO" ledgerera) ~ AllegraUtxoEvent ledgerera,
 Event (EraRule "PPUP" ledgerera) ~ PpupEvent ledgerera) =>
ShelleyUtxowEvent ledgerera -> Maybe LedgerEvent
handleAllegraMaryUTxOWEvent (Shelley.UtxoEvent Event (EraRule "UTXO" ledgerera)
e) =
  case Event (EraRule "UTXO" ledgerera)
e of
    Allegra.TotalDeposits{} -> Maybe LedgerEvent
forall a. Maybe a
Nothing
    Allegra.UpdateEvent (Shelley.PpupNewEpoch EpochNo
_) -> Maybe LedgerEvent
forall a. Maybe a
Nothing
    Allegra.TxUTxODiff UTxO ledgerera
_ UTxO ledgerera
_ -> Maybe LedgerEvent
forall a. Maybe a
Nothing