Skip to content

Commit 4c21458

Browse files
committed
refactor(api): migrate JSON instances to Exp.IsEra constraints
Refactors ToJSON/FromJSON instances for key types to use the new Exp.IsEra constraint system instead of the legacy IsShelleyBasedEra and IsCardanoEra constraints. This modernizes the codebase to use the experimental era infrastructure while maintaining backwards compatibility. - Migrate AddressInEra JSON instances to use Exp.IsEra - Update ReferenceScript serialization to use new constraints - Refactor TxOut (CtxTx and CtxUTxO) JSON parsing to use Exp.convert - Simplify TxOutValue JSON handling with unified era conversion - Consolidate duplicate parsing logic for Babbage/Conway/Dijkstra eras - Remove era-specific case matching in favor of generic implementations The refactoring eliminates significant code duplication by using the convert function to obtain appropriate era witnesses, making the code more maintainable while preserving the exact JSON format.
1 parent e789c02 commit 4c21458

File tree

4 files changed

+97
-283
lines changed

4 files changed

+97
-283
lines changed

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

Lines changed: 6 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,13 @@ 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 = Exp.obtainCommonConstraints (Exp.useEra @era) $
376+
Aeson.String . serialiseAddress
375377

376-
instance IsShelleyBasedEra era => FromJSON (AddressInEra era) where
378+
instance Exp.IsEra era => FromJSON (AddressInEra era) where
377379
parseJSON =
378-
let sbe = shelleyBasedEra @era
380+
let sbe = convert (Exp.useEra @era)
379381
in withText "AddressInEra" $ \txt -> do
380382
addressAny <- P.runParserFail parseAddressAny txt
381383
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)