content: {-# LANGUAGE DataKinds #-}
  {-# LANGUAGE FlexibleContexts #-}
  {-# LANGUAGE ScopedTypeVariables #-}
  {-# LANGUAGE TypeApplications #-}
  {-# LANGUAGE TypeFamilies #-}
  {-# LANGUAGE TypeOperators #-}
  
  module Test.Cardano.Ledger.Babbage.Examples (
    ledgerExamples,
    exampleBabbageNewEpochState,
    exampleCollateralOutput,
  ) where
  
  import Cardano.Ledger.Alonzo.Scripts (AlonzoPlutusPurpose (..))
  import Cardano.Ledger.Alonzo.Translation ()
  import Cardano.Ledger.Babbage (BabbageEra)
  import Cardano.Ledger.Babbage.Core
  import Cardano.Ledger.Babbage.TxBody (BabbageTxOut (..), TxBody (..))
  import Cardano.Ledger.BaseTypes (StrictMaybe (..))
  import Cardano.Ledger.Binary (mkSized)
  import Cardano.Ledger.Coin (Coin (..))
  import Cardano.Ledger.Genesis (NoGenesis (..))
  import Cardano.Ledger.Mary.Value (MaryValue (..))
  import Cardano.Ledger.Plutus.Data (
    Datum (..),
    dataToBinaryData,
   )
  import Cardano.Ledger.Plutus.Language (Language (..))
  import Cardano.Ledger.Shelley.API (
    ApplyTxError (..),
    Network (..),
    NewEpochState (..),
    ProposedPPUpdates (..),
    RewardAccount (..),
    TxId (..),
    Update (..),
   )
  import Cardano.Ledger.Shelley.Rules (ShelleyDelegsPredFailure (..), ShelleyLedgerPredFailure (..))
  import Cardano.Ledger.Shelley.Scripts
  import Cardano.Ledger.TxIn (mkTxInPartial)
  import Cardano.Slotting.Slot (EpochNo (..), SlotNo (..))
  import qualified Data.Map.Strict as Map
  import qualified Data.Sequence.Strict as StrictSeq
  import qualified Data.Set as Set
  import Lens.Micro
  import Test.Cardano.Ledger.Alonzo.Arbitrary (alwaysSucceeds)
  import Test.Cardano.Ledger.Alonzo.Examples (exampleDatum, exampleTx, mkLedgerExamples)
  import Test.Cardano.Ledger.Babbage.Era
  import Test.Cardano.Ledger.Core.KeyPair (mkAddr)
  import Test.Cardano.Ledger.Core.Utils (mkDummySafeHash)
  import Test.Cardano.Ledger.Mary.Examples (exampleMultiAssetValue)
  import Test.Cardano.Ledger.Shelley.Examples (
    LedgerExamples (..),
    exampleCerts,
    exampleNewEpochState,
    examplePayKey,
    exampleStakeKey,
    keyToCredential,
    mkKeyHash,
   )
  
  ledgerExamples :: LedgerExamples BabbageEra
  ledgerExamples =
    mkLedgerExamples
      ( ApplyTxError $
          pure $
            DelegsFailure $
              DelegateeNotRegisteredDELEG @BabbageEra (mkKeyHash 1)
      )
      exampleBabbageNewEpochState
      exampleTxBabbage
      NoGenesis
  
  exampleBabbageNewEpochState ::
    ( BabbageEraTest era
    , Value era ~ MaryValue
    ) =>
    NewEpochState era
  exampleBabbageNewEpochState =
    exampleNewEpochState
      (exampleMultiAssetValue 1)
      emptyPParams
      (emptyPParams & ppCoinsPerUTxOByteL .~ CoinPerByte (Coin 1))
  
  exampleTxBabbage :: Tx BabbageEra
  exampleTxBabbage =
    exampleTx
      exampleTxBodyBabbage
      (AlonzoSpending $ AsIx 0)
      (RequireAllOf @BabbageEra mempty)
  
  exampleTxBodyBabbage :: TxBody BabbageEra
  exampleTxBodyBabbage =
    BabbageTxBody
      (Set.fromList [mkTxInPartial (TxId (mkDummySafeHash 1)) 0]) -- spending inputs
      (Set.fromList [mkTxInPartial (TxId (mkDummySafeHash 2)) 1]) -- collateral inputs
      (Set.fromList [mkTxInPartial (TxId (mkDummySafeHash 1)) 3]) -- reference inputs
      ( StrictSeq.fromList
          [ mkSized (eraProtVerHigh @BabbageEra) $
              BabbageTxOut
                (mkAddr examplePayKey exampleStakeKey)
                (exampleMultiAssetValue 2)
                (Datum $ dataToBinaryData exampleDatum) -- inline datum
                (SJust $ alwaysSucceeds @'PlutusV2 3) -- reference script
          ]
      )
      (SJust $ mkSized (eraProtVerHigh @BabbageEra) exampleCollateralOutput) -- collateral return
      (SJust $ Coin 8675309) -- collateral tot
      exampleCerts
      ( Withdrawals $
          Map.singleton
            (RewardAccount Testnet (keyToCredential exampleStakeKey))
            (Coin 100)
      )
      (Coin 999) -- txfee
      (ValidityInterval (SJust (SlotNo 2)) (SJust (SlotNo 4))) -- txvldt
      ( SJust $
          Update
            ( ProposedPPUpdates $
                Map.singleton
                  (mkKeyHash 1)
                  (emptyPParamsUpdate & ppuMaxBHSizeL .~ SJust 4000)
            )
            (EpochNo 0)
      ) -- txUpdates
      (Set.singleton $ mkKeyHash 212) -- reqSignerHashes
      exampleMultiAsset -- mint
      (SJust $ mkDummySafeHash 42) -- scriptIntegrityHash
      (SJust . TxAuxDataHash $ mkDummySafeHash 42) -- adHash
      (SJust Mainnet) -- txnetworkid
    where
      MaryValue _ exampleMultiAsset = exampleMultiAssetValue 3
  
  exampleCollateralOutput ::
    ( BabbageEraTxOut era
    , Value era ~ MaryValue
    ) =>
    BabbageTxOut era
  exampleCollateralOutput =
    BabbageTxOut
      (mkAddr examplePayKey exampleStakeKey)
      (MaryValue (Coin 8675309) mempty)
      NoDatum
      SNothing
  