From 21408c8fc897d13e64aedda4a622bd5962d4fe15 Mon Sep 17 00:00:00 2001 From: John Ky Date: Tue, 25 Nov 2025 21:57:23 +1100 Subject: [PATCH] refactor(api): migrate ToJSON instances to use experimental Era type Replaces IsCardanoEra constraint with Exp.IsEra in ToJSON instances for TxOut and UTxO types, adopting the new experimental Era framework for JSON serialization. This change simplifies the era handling by using Exp.useEra instead of cardanoEra and reduces the pattern matching cases in txOutToJsonValue to only handle ConwayEra and DijkstraEra. - Update TxOut ToJSON instance to use Exp.IsEra constraint - Update UTxO ToJSON instance to use Exp.IsEra constraint - Refactor txOutToJsonValue to use Exp.Era type parameter - Remove redundant pattern matches for Byron through Babbage eras - Update test generators to use ShelleyBasedEraConway instead of Babbage The test suite changes ensure compatibility with the new era handling approach by testing against the Conway era, which provides better coverage of the current serialization logic. --- .../src/Cardano/Api/Tx/Internal/Output.hs | 32 ++++--------------- cardano-api/src/Cardano/Api/UTxO.hs | 4 +-- .../cardano-api-test/Test/Cardano/Api/Json.hs | 13 +++++--- 3 files changed, 16 insertions(+), 33 deletions(-) diff --git a/cardano-api/src/Cardano/Api/Tx/Internal/Output.hs b/cardano-api/src/Cardano/Api/Tx/Internal/Output.hs index 62043afb49..92184b820e 100644 --- a/cardano-api/src/Cardano/Api/Tx/Internal/Output.hs +++ b/cardano-api/src/Cardano/Api/Tx/Internal/Output.hs @@ -65,6 +65,7 @@ import Cardano.Api.Era.Internal.Eon.Convert import Cardano.Api.Era.Internal.Eon.ConwayEraOnwards import Cardano.Api.Era.Internal.Eon.ShelleyBasedEra import Cardano.Api.Error (Error (..), displayError) +import Cardano.Api.Experimental.Era qualified as Exp import Cardano.Api.HasTypeProxy qualified as HTP import Cardano.Api.Ledger.Internal.Reexport qualified as Ledger import Cardano.Api.Monad.Error @@ -338,34 +339,13 @@ fromBabbageTxOut w txdatums txout = (fromAlonzoData d) | otherwise = TxOutDatumHash (convert w) (ScriptDataHash dh) -instance IsCardanoEra era => ToJSON (TxOut ctx era) where - toJSON = txOutToJsonValue cardanoEra +instance Exp.IsEra era => ToJSON (TxOut ctx era) where + toJSON = txOutToJsonValue Exp.useEra -txOutToJsonValue :: CardanoEra era -> TxOut ctx era -> Aeson.Value +txOutToJsonValue :: Exp.Era era -> TxOut ctx era -> Aeson.Value txOutToJsonValue era (TxOut addr val dat refScript) = case era of - ByronEra -> object ["address" .= addr, "value" .= val] - ShelleyEra -> object ["address" .= addr, "value" .= val] - AllegraEra -> object ["address" .= addr, "value" .= val] - MaryEra -> object ["address" .= addr, "value" .= val] - AlonzoEra -> - object - [ "address" .= addr - , "value" .= val - , datHashJsonVal dat - , "datum" .= datJsonVal dat - ] - BabbageEra -> - object - [ "address" .= addr - , "value" .= val - , datHashJsonVal dat - , "datum" .= datJsonVal dat - , "inlineDatum" .= inlineDatumJsonVal dat - , "inlineDatumRaw" .= inlineDatumRawJsonCbor dat - , "referenceScript" .= refScriptJsonVal refScript - ] - ConwayEra -> + Exp.ConwayEra -> object [ "address" .= addr , "value" .= val @@ -375,7 +355,7 @@ txOutToJsonValue era (TxOut addr val dat refScript) = , "inlineDatumRaw" .= inlineDatumRawJsonCbor dat , "referenceScript" .= refScriptJsonVal refScript ] - DijkstraEra -> + Exp.DijkstraEra -> object [ "address" .= addr , "value" .= val diff --git a/cardano-api/src/Cardano/Api/UTxO.hs b/cardano-api/src/Cardano/Api/UTxO.hs index 57fecad654..481c66a3c1 100644 --- a/cardano-api/src/Cardano/Api/UTxO.hs +++ b/cardano-api/src/Cardano/Api/UTxO.hs @@ -81,12 +81,12 @@ module Cardano.Api.UTxO ) where -import Cardano.Api.Era.Internal.Core (IsCardanoEra) import Cardano.Api.Era.Internal.Eon.ShelleyBasedEra ( IsShelleyBasedEra , ShelleyBasedEra , ShelleyLedgerEra ) +import Cardano.Api.Experimental.Era qualified as Exp import Cardano.Api.Tx.Internal.Output ( CtxUTxO , TxOut (..) @@ -133,7 +133,7 @@ instance GHC.IsList (UTxO era) where fromList = UTxO . GHC.fromList toList = GHC.toList . unUTxO -instance IsCardanoEra era => ToJSON (UTxO era) where +instance Exp.IsEra era => ToJSON (UTxO era) where toJSON (UTxO m) = toJSON m toEncoding (UTxO m) = toEncoding m diff --git a/cardano-api/test/cardano-api-test/Test/Cardano/Api/Json.hs b/cardano-api/test/cardano-api-test/Test/Cardano/Api/Json.hs index 23883d36d6..bca07fbd01 100644 --- a/cardano-api/test/cardano-api-test/Test/Cardano/Api/Json.hs +++ b/cardano-api/test/cardano-api-test/Test/Cardano/Api/Json.hs @@ -21,6 +21,9 @@ import Hedgehog.Gen qualified as Gen import Test.Tasty (TestTree, testGroup) import Test.Tasty.Hedgehog (testProperty) +testEra :: ShelleyBasedEra ConwayEra +testEra = ShelleyBasedEraConway + prop_json_roundtrip_alonzo_genesis :: Property prop_json_roundtrip_alonzo_genesis = H.property $ do genesis <- forAll genAlonzoGenesis @@ -28,27 +31,27 @@ prop_json_roundtrip_alonzo_genesis = H.property $ do prop_json_roundtrip_utxo :: Property prop_json_roundtrip_utxo = H.property $ do - utxo <- forAll $ genUTxO ShelleyBasedEraBabbage + utxo <- forAll $ genUTxO testEra tripping utxo encode eitherDecode prop_json_roundtrip_reference_scripts :: Property prop_json_roundtrip_reference_scripts = H.property $ do - rScript <- forAll $ genReferenceScript ShelleyBasedEraBabbage + rScript <- forAll $ genReferenceScript testEra tripping rScript encode eitherDecode prop_json_roundtrip_txoutvalue :: Property prop_json_roundtrip_txoutvalue = H.property $ do - oVal <- forAll $ genTxOutValue ShelleyBasedEraBabbage + oVal <- forAll $ genTxOutValue testEra tripping oVal encode eitherDecode prop_json_roundtrip_txout_tx_context :: Property prop_json_roundtrip_txout_tx_context = H.property $ do - txOut <- forAll $ genTxOutTxContext ShelleyBasedEraBabbage + txOut <- forAll $ genTxOutTxContext testEra tripping txOut encode eitherDecode prop_json_roundtrip_txout_utxo_context :: Property prop_json_roundtrip_txout_utxo_context = H.property $ do - txOut <- forAll $ genTxOutUTxOContext ShelleyBasedEraBabbage + txOut <- forAll $ genTxOutUTxOContext testEra tripping txOut encode eitherDecode prop_json_roundtrip_scriptdata_detailed_json :: Property