content: --  This Source Code Form is subject to the terms of the Mozilla Public
  --  License, v. 2.0. If a copy of the MPL was not distributed with this
  --  file, You can obtain one at http://mozilla.org/MPL/2.0/.
  
  module Ogmios.Data.Ledger.PredicateFailure.Allegra where
  
  import Ogmios.Prelude
  
  import Ogmios.Data.Ledger.PredicateFailure
      ( DiscriminatedEntities (..)
      , MultiEraPredicateFailure (..)
      , TxOutInAnyEra (..)
      , ValueInAnyEra (..)
      )
  import Ogmios.Data.Ledger.PredicateFailure.Shelley
      ( encodeDelegsFailure
      , encodeUtxowFailure
      )
  
  import qualified Cardano.Ledger.Allegra.Rules as Al
  import Cardano.Ledger.BaseTypes
      ( Mismatch (..)
      )
  import qualified Cardano.Ledger.Shelley.Rules as Sh
  
  encodeLedgerFailure
      :: Sh.ShelleyLedgerPredFailure AllegraEra
      -> MultiEraPredicateFailure
  encodeLedgerFailure = \case
      Sh.UtxowFailure e  ->
          encodeUtxowFailure (encodeUtxoFailure ShelleyBasedEraAllegra) e
      Sh.DelegsFailure e ->
          encodeDelegsFailure e
  
  encodeUtxoFailure
      :: forall era.
          ( Era era
          )
      => ShelleyBasedEra era
      -> Al.AllegraUtxoPredFailure era
      -> MultiEraPredicateFailure
  encodeUtxoFailure era = \case
      Al.BadInputsUTxO inputs ->
          UnknownUtxoReference inputs
      Al.OutsideValidityIntervalUTxO validityInterval currentSlot ->
          TransactionOutsideValidityInterval { validityInterval, currentSlot }
      Al.OutputTooBigUTxO outs ->
          let culpritOutputs = (\out -> TxOutInAnyEra (era, out)) <$> outs in
          ValueSizeAboveLimit culpritOutputs
      Al.MaxTxSizeUTxO (Mismatch measuredSize maximumSize) ->
          TransactionTooLarge { measuredSize, maximumSize }
      Al.InputSetEmptyUTxO ->
          EmptyInputSet
      Al.FeeTooSmallUTxO (Mismatch suppliedFee minimumRequiredFee) ->
          TransactionFeeTooSmall { minimumRequiredFee, suppliedFee }
      Al.ValueNotConservedUTxO (Mismatch consumed produced) ->
          let valueConsumed = ValueInAnyEra (era, consumed) in
          let valueProduced = ValueInAnyEra (era, produced) in
          ValueNotConserved { valueConsumed, valueProduced }
      Al.WrongNetwork expectedNetwork invalidAddrs ->
          let invalidEntities = DiscriminatedAddresses invalidAddrs in
          NetworkMismatch { expectedNetwork, invalidEntities }
      Al.WrongNetworkWithdrawal expectedNetwork invalidAccts ->
          let invalidEntities = DiscriminatedRewardAccounts invalidAccts in
          NetworkMismatch { expectedNetwork, invalidEntities }
      Al.OutputTooSmallUTxO outs ->
          let insufficientlyFundedOutputs =
                  (\out -> (TxOutInAnyEra (era, out), Nothing)) <$> outs
           in InsufficientAdaInOutput { insufficientlyFundedOutputs }
      Al.OutputBootAddrAttrsTooBig outs ->
          let culpritOutputs = (\out -> TxOutInAnyEra (era, out)) <$> outs in
          BootstrapAddressAttributesTooLarge { culpritOutputs }
      Al.TriesToForgeADA ->
          MintingOrBurningAda
      Al.UpdateFailure{} ->
          InvalidProtocolParametersUpdate
  