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 15, 2024
1 parent 6dd96c6 commit 49d47fa
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 0 deletions.
19 changes: 19 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 @@ -109,3 +110,21 @@ 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 @@ -117,3 +118,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 @@ -111,3 +112,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
7 changes: 7 additions & 0 deletions cardano-api/internal/Cardano/Api/Eon/ConwayEraOnwards.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Cardano.Api.Eon.ConwayEraOnwards
, conwayEraOnwardsConstraints
, conwayEraOnwardsToShelleyBasedEra
, ConwayEraOnwardsConstraints
, IsConwayBasedEra (..)
)
where

Expand Down Expand Up @@ -112,3 +113,9 @@ conwayEraOnwardsConstraints = \case
conwayEraOnwardsToShelleyBasedEra :: ConwayEraOnwards era -> ShelleyBasedEra era
conwayEraOnwardsToShelleyBasedEra = \case
ConwayEraOnwardsConway -> ShelleyBasedEraConway

class IsConwayBasedEra era where
conwayBasedEra :: ConwayEraOnwards era

instance IsConwayBasedEra ConwayEra where
conwayBasedEra = ConwayEraOnwardsConway
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 @@ -109,3 +110,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 @@ -1021,6 +1024,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
7 changes: 7 additions & 0 deletions cardano-api/src/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -79,26 +79,31 @@ 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 (..)
, conwayEraOnwardsConstraints
, conwayEraOnwardsToShelleyBasedEra
, IsConwayBasedEra (..)

-- * Era case handling

Expand Down Expand Up @@ -501,6 +506,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 49d47fa

Please sign in to comment.