Skip to content

Commit

Permalink
Import cardano-ledger-conway and add ToAlonzoScript typeclass
Browse files Browse the repository at this point in the history
  • Loading branch information
locallycompact committed Mar 5, 2024
1 parent 6fcc456 commit 695e9d7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
1 change: 1 addition & 0 deletions hydra-cardano-api/hydra-cardano-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ library
, cardano-ledger-alonzo >=1.6 && <1.7
, cardano-ledger-api >=1.8 && <1.9
, cardano-ledger-babbage >=1.6 && <1.7
, cardano-ledger-conway >=1.12 && <1.13
, cardano-ledger-binary >=1.3 && <1.4
, cardano-ledger-byron >=1.0.0 && <1.1
, cardano-ledger-core >=1.10 && <1.11
Expand Down
38 changes: 26 additions & 12 deletions hydra-cardano-api/src/Hydra/Cardano/Api/PlutusScript.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Hydra.Cardano.Api.PlutusScript where
import Hydra.Cardano.Api.Prelude

import Cardano.Ledger.Alonzo.Scripts qualified as Ledger
import Cardano.Ledger.Babbage.Scripts qualified as Ledger
import Cardano.Ledger.Conway.Scripts qualified as Ledger
import Cardano.Ledger.Plutus.Language qualified as Ledger
import Data.ByteString.Short qualified as SBS
import PlutusLedgerApi.Common qualified as Plutus
Expand All @@ -29,17 +29,31 @@ fromLedgerScript = \case
Ledger.TimelockScript{} -> error "fromLedgerScript: TimelockScript"
Ledger.PlutusScript x -> Ledger.withPlutusScript x (\(Ledger.Plutus (Ledger.PlutusBinary bytes)) -> PlutusScriptSerialised bytes)

-- | Convert a cardano-api 'PlutusScript' into a cardano-ledger 'Script'.
toLedgerScript ::
forall lang.
IsPlutusScriptLanguage lang =>
PlutusScript lang ->
Ledger.AlonzoScript (ShelleyLedgerEra Era)
toLedgerScript (PlutusScriptSerialised bytes) =
Ledger.PlutusScript $ case plutusScriptVersion @lang of
PlutusScriptV1 -> Ledger.BabbagePlutusV1 $ Ledger.Plutus (Ledger.PlutusBinary bytes)
PlutusScriptV2 -> Ledger.BabbagePlutusV2 $ Ledger.Plutus (Ledger.PlutusBinary bytes)
PlutusScriptV3 -> error "toLedgerScript: PlutusV3 not supported in Babbage"
class ToAlonzoScript lang era where
toLedgerScript ::
IsPlutusScriptLanguage lang =>
PlutusScript lang ->
Ledger.AlonzoScript (ShelleyLedgerEra era)

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

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

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

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

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

-- | Convert a serialized plutus script into a cardano-api 'PlutusScript'.
fromPlutusScript :: Plutus.SerialisedScript -> PlutusScript lang
Expand Down

0 comments on commit 695e9d7

Please sign in to comment.