Skip to content

Commit c668629

Browse files
committed
refactor(api): migrate JSON instances to use Exp.IsEra constraints
Refactors JSON serialization instances for core types to use the experimental Exp.IsEra constraint system instead of IsShelleyBasedEra/IsCardanoEra. This unifies the constraint approach across the codebase and simplifies era-based type class instances. Key Changes: - AddressInEra: Migrate ToJSON/FromJSON instances to Exp.IsEra - ReferenceScript: Update JSON instances using Exp.convert for era witnesses - TxOut (CtxTx/CtxUTxO): Consolidate era-specific parsing into unified handlers - TxOutValue: Simplify FromJSON with direct era conversion Implementation Details: - Use Exp.useEra for obtaining era witnesses at the type level - Convert era witnesses to required eon types (AlonzoEraOnwards, BabbageEraOnwards, etc.) - Consolidate duplicate reconciliation logic into single reconcileDatums function - Remove redundant era-specific pattern matching in favor of generic handlers This refactoring maintains full backwards compatibility with existing JSON formats while reducing code duplication and improving maintainability. The changes prepare the codebase for easier extension to future eras by centralizing era-handling logic.
1 parent f710b2b commit c668629

File tree

4 files changed

+96
-281
lines changed

4 files changed

+96
-281
lines changed

cardano-api/src/Cardano/Api/Address.hs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ where
9090

9191
import Cardano.Api.Byron.Internal.Key
9292
import Cardano.Api.Era
93+
import Cardano.Api.Experimental.Era qualified as Exp
9394
import Cardano.Api.HasTypeProxy
9495
import Cardano.Api.Key.Internal
9596
import Cardano.Api.Monad.Error
@@ -370,12 +371,14 @@ data AddressInEra era where
370371
instance NFData (AddressInEra era) where
371372
rnf (AddressInEra t a) = deepseq (deepseq t a) ()
372373

373-
instance IsCardanoEra era => ToJSON (AddressInEra era) where
374-
toJSON = Aeson.String . serialiseAddress
374+
instance Exp.IsEra era => ToJSON (AddressInEra era) where
375+
toJSON =
376+
Exp.obtainCommonConstraints (Exp.useEra @era) $
377+
Aeson.String . serialiseAddress
375378

376-
instance IsShelleyBasedEra era => FromJSON (AddressInEra era) where
379+
instance Exp.IsEra era => FromJSON (AddressInEra era) where
377380
parseJSON =
378-
let sbe = shelleyBasedEra @era
381+
let sbe = convert (Exp.useEra @era)
379382
in withText "AddressInEra" $ \txt -> do
380383
addressAny <- P.runParserFail parseAddressAny txt
381384
pure $ anyAddressInShelleyBasedEra sbe addressAny

cardano-api/src/Cardano/Api/Plutus/Internal/Script.hs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ import Cardano.Api.Era.Internal.Core
125125
import Cardano.Api.Era.Internal.Eon.BabbageEraOnwards
126126
import Cardano.Api.Era.Internal.Eon.ShelleyBasedEra
127127
import Cardano.Api.Error
128+
import Cardano.Api.Experimental.Era qualified as Exp
128129
import Cardano.Api.HasTypeProxy
129130
import Cardano.Api.Hash
130131
import Cardano.Api.Key.Internal
@@ -1647,16 +1648,14 @@ deriving instance Eq (ReferenceScript era)
16471648

16481649
deriving instance Show (ReferenceScript era)
16491650

1650-
instance IsCardanoEra era => ToJSON (ReferenceScript era) where
1651+
instance Exp.IsEra era => ToJSON (ReferenceScript era) where
16511652
toJSON (ReferenceScript _ s) = object ["referenceScript" .= s]
16521653
toJSON ReferenceScriptNone = Aeson.Null
16531654

1654-
instance IsCardanoEra era => FromJSON (ReferenceScript era) where
1655+
instance Exp.IsEra era => FromJSON (ReferenceScript era) where
16551656
parseJSON = Aeson.withObject "ReferenceScript" $ \o ->
1656-
caseByronToAlonzoOrBabbageEraOnwards
1657-
(const (pure ReferenceScriptNone))
1658-
(\w -> ReferenceScript w <$> o .: "referenceScript")
1659-
(cardanoEra :: CardanoEra era)
1657+
let w = Exp.convert (Exp.useEra @era)
1658+
in ReferenceScript w <$> o .: "referenceScript"
16601659

16611660
refScriptToShelleyScript
16621661
:: ShelleyBasedEra era

0 commit comments

Comments
 (0)