Skip to content

Commit

Permalink
Add Classy versions of Era witness functions
Browse files Browse the repository at this point in the history
  • Loading branch information
locallycompact committed Jul 10, 2024
1 parent 19da63d commit 556176b
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 0 deletions.
20 changes: 20 additions & 0 deletions cardano-api/internal/Cardano/Api/Eon/AllegraEraOnwards.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Cardano.Api.Eon.AllegraEraOnwards
, allegraEraOnwardsConstraints
, allegraEraOnwardsToShelleyBasedEra
, AllegraEraOnwardsConstraints
, IsAllegraBasedEra(..)
)
where

Expand Down Expand Up @@ -107,3 +108,22 @@ allegraEraOnwardsToShelleyBasedEra = \case
AllegraEraOnwardsAlonzo -> ShelleyBasedEraAlonzo
AllegraEraOnwardsBabbage -> ShelleyBasedEraBabbage
AllegraEraOnwardsConway -> ShelleyBasedEraConway

class IsAllegraBasedEra era where
allegraBasedEra :: AllegraEraOnwards era

instance IsAllegraBasedEra AllegraEra where
allegraBasedEra = AllegraEraOnwardsAllegra

instance IsAllegraBasedEra MaryEra where
allegraBasedEra = AllegraEraOnwardsMary

instance IsAllegraBasedEra AlonzoEra where
allegraBasedEra = AllegraEraOnwardsAlonzo

instance IsAllegraBasedEra BabbageEra where
allegraBasedEra = AllegraEraOnwardsBabbage

instance IsAllegraBasedEra ConwayEra where
allegraBasedEra = AllegraEraOnwardsConway

13 changes: 13 additions & 0 deletions cardano-api/internal/Cardano/Api/Eon/AlonzoEraOnwards.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Cardano.Api.Eon.AlonzoEraOnwards
, alonzoEraOnwardsConstraints
, alonzoEraOnwardsToShelleyBasedEra
, AlonzoEraOnwardsConstraints
, IsAlonzoBasedEra (..)
)
where

Expand Down Expand Up @@ -115,3 +116,15 @@ alonzoEraOnwardsToShelleyBasedEra = \case
AlonzoEraOnwardsAlonzo -> ShelleyBasedEraAlonzo
AlonzoEraOnwardsBabbage -> ShelleyBasedEraBabbage
AlonzoEraOnwardsConway -> ShelleyBasedEraConway

class IsAlonzoBasedEra era where
alonzoBasedEra :: AlonzoEraOnwards era

instance IsAlonzoBasedEra AlonzoEra where
alonzoBasedEra = AlonzoEraOnwardsAlonzo

instance IsAlonzoBasedEra BabbageEra where
alonzoBasedEra = AlonzoEraOnwardsBabbage

instance IsAlonzoBasedEra ConwayEra where
alonzoBasedEra = AlonzoEraOnwardsConway
10 changes: 10 additions & 0 deletions cardano-api/internal/Cardano/Api/Eon/BabbageEraOnwards.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Cardano.Api.Eon.BabbageEraOnwards
, babbageEraOnwardsConstraints
, babbageEraOnwardsToShelleyBasedEra
, BabbageEraOnwardsConstraints
, IsBabbageBasedEra (..)
)
where

Expand Down Expand Up @@ -109,3 +110,12 @@ babbageEraOnwardsToShelleyBasedEra :: BabbageEraOnwards era -> ShelleyBasedEra e
babbageEraOnwardsToShelleyBasedEra = \case
BabbageEraOnwardsBabbage -> ShelleyBasedEraBabbage
BabbageEraOnwardsConway -> ShelleyBasedEraConway

class IsBabbageBasedEra era where
babbageBasedEra :: BabbageEraOnwards era

instance IsBabbageBasedEra BabbageEra where
babbageBasedEra = BabbageEraOnwardsBabbage

instance IsBabbageBasedEra ConwayEra where
babbageBasedEra = BabbageEraOnwardsConway
16 changes: 16 additions & 0 deletions cardano-api/internal/Cardano/Api/Eon/MaryEraOnwards.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Cardano.Api.Eon.MaryEraOnwards
, maryEraOnwardsConstraints
, maryEraOnwardsToShelleyBasedEra
, MaryEraOnwardsConstraints
, IsMaryBasedEra (..)
)
where

Expand Down Expand Up @@ -107,3 +108,18 @@ maryEraOnwardsToShelleyBasedEra = \case
MaryEraOnwardsAlonzo -> ShelleyBasedEraAlonzo
MaryEraOnwardsBabbage -> ShelleyBasedEraBabbage
MaryEraOnwardsConway -> ShelleyBasedEraConway

class IsMaryBasedEra era where
maryBasedEra :: MaryEraOnwards era

instance IsMaryBasedEra MaryEra where
maryBasedEra = MaryEraOnwardsMary

instance IsMaryBasedEra AlonzoEra where
maryBasedEra = MaryEraOnwardsAlonzo

instance IsMaryBasedEra BabbageEra where
maryBasedEra = MaryEraOnwardsBabbage

instance IsMaryBasedEra ConwayEra where
maryBasedEra = MaryEraOnwardsConway
51 changes: 51 additions & 0 deletions cardano-api/internal/Cardano/Api/Script.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE RankNTypes #-}
Expand Down Expand Up @@ -40,6 +41,8 @@ module Cardano.Api.Script
, ScriptInEra (..)
, toScriptInEra
, eraOfScriptInEra
, HasScriptLanguageInEra (..)
, ToAlonzoScript (..)

-- * Reference scripts
, ReferenceScript (..)
Expand Down Expand Up @@ -1019,6 +1022,54 @@ instance IsPlutusScriptLanguage lang => HasTextEnvelope (PlutusScript lang) wher
PlutusScriptV2 -> "PlutusScriptV2"
PlutusScriptV3 -> "PlutusScriptV3"

-- | Smart-constructor for 'ScriptLanguageInEra' to write functions
-- manipulating scripts that do not commit to a particular era.
class HasScriptLanguageInEra lang era where
scriptLanguageInEra :: ScriptLanguageInEra lang era

instance HasScriptLanguageInEra PlutusScriptV1 AlonzoEra where
scriptLanguageInEra = PlutusScriptV1InAlonzo

instance HasScriptLanguageInEra PlutusScriptV1 BabbageEra where
scriptLanguageInEra = PlutusScriptV1InBabbage

instance HasScriptLanguageInEra PlutusScriptV2 BabbageEra where
scriptLanguageInEra = PlutusScriptV2InBabbage

instance HasScriptLanguageInEra PlutusScriptV1 ConwayEra where
scriptLanguageInEra = PlutusScriptV1InConway

instance HasScriptLanguageInEra PlutusScriptV2 ConwayEra where
scriptLanguageInEra = PlutusScriptV2InConway

instance HasScriptLanguageInEra PlutusScriptV3 ConwayEra where
scriptLanguageInEra = PlutusScriptV3InConway

class ToAlonzoScript lang era where
toLedgerScript ::
PlutusScript lang ->
Conway.AlonzoScript (ShelleyLedgerEra era)

instance ToAlonzoScript PlutusScriptV1 BabbageEra where
toLedgerScript (PlutusScriptSerialised bytes) =
Conway.PlutusScript $ Conway.BabbagePlutusV1 $ Plutus.Plutus $ Plutus.PlutusBinary bytes

instance ToAlonzoScript PlutusScriptV2 BabbageEra where
toLedgerScript (PlutusScriptSerialised bytes) =
Conway.PlutusScript $ Conway.BabbagePlutusV2 $ Plutus.Plutus $ Plutus.PlutusBinary bytes

instance ToAlonzoScript PlutusScriptV1 ConwayEra where
toLedgerScript (PlutusScriptSerialised bytes) =
Conway.PlutusScript $ Conway.ConwayPlutusV1 $ Plutus.Plutus $ Plutus.PlutusBinary bytes

instance ToAlonzoScript PlutusScriptV2 ConwayEra where
toLedgerScript (PlutusScriptSerialised bytes) =
Conway.PlutusScript $ Conway.ConwayPlutusV2 $ Plutus.Plutus $ Plutus.PlutusBinary bytes

instance ToAlonzoScript PlutusScriptV3 ConwayEra where
toLedgerScript (PlutusScriptSerialised bytes) =
Conway.PlutusScript $ Conway.ConwayPlutusV3 $ Plutus.Plutus $ Plutus.PlutusBinary bytes

-- | An example Plutus script that always succeeds, irrespective of inputs.
--
-- For example, if one were to use this for a payment address then it would
Expand Down
6 changes: 6 additions & 0 deletions cardano-api/src/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,25 @@ module Cardano.Api

-- ** From Allegra
, AllegraEraOnwards (..)
, IsAllegraBasedEra (..)

-- ** From Mary
, MaryEraOnwards (..)
, maryEraOnwardsConstraints
, maryEraOnwardsToShelleyBasedEra
, IsMaryBasedEra (..)

-- ** From Alonzo
, AlonzoEraOnwards (..)
, alonzoEraOnwardsConstraints
, alonzoEraOnwardsToShelleyBasedEra
, IsAlonzoBasedEra (..)

-- ** From Babbage
, BabbageEraOnwards (..)
, babbageEraOnwardsConstraints
, babbageEraOnwardsToShelleyBasedEra
, IsBabbageBasedEra (..)

-- ** From Conway
, ConwayEraOnwards (..)
Expand Down Expand Up @@ -501,6 +505,8 @@ module Cardano.Api
, ScriptInEra (..)
, toScriptInEra
, eraOfScriptInEra
, HasScriptLanguageInEra (..)
, ToAlonzoScript (..)

-- ** Use of a script in an era as a witness
, WitCtxTxIn
Expand Down

0 comments on commit 556176b

Please sign in to comment.