Skip to content

Commit c7b644a

Browse files
committed
Added FromJSON instance for PParamUpdates
1 parent 4a9e8a4 commit c7b644a

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

libs/cardano-ledger-core/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 1.18.0.0
44

5+
* Add `FromJSON` instance for `PParamUpdate`
56
* Changed the type of `AtMostEra` and `AtLeastEra` to accept a type level string instead of an actual era type.
67
* Add `EraName` type family to the `Era` type class and use it for default implementation of
78
`eraName` type class function.

libs/cardano-ledger-core/src/Cardano/Ledger/Core/PParams.hs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ import Cardano.Ledger.HKD (HKD, HKDApplicative, HKDFunctor (..), NoUpdate (..))
108108
import Cardano.Ledger.Plutus.ToPlutusData (ToPlutusData (..))
109109
import Control.DeepSeq (NFData)
110110
import Control.Monad.Identity (Identity)
111-
import Data.Aeson (FromJSON (..), ToJSON (..), withObject, (.:), (.=))
111+
import Data.Aeson (FromJSON (..), ToJSON (..), withObject, (.:), (.:!), (.=))
112112
import qualified Data.Aeson.Key as Aeson (fromText)
113113
import Data.Default (Default (..))
114114
import qualified Data.Foldable as F (foldMap', foldl', foldlM)
@@ -249,6 +249,18 @@ deriving via
249249
instance
250250
EraPParams era => ToJSON (PParamsUpdate era)
251251

252+
instance EraPParams era => FromJSON (PParamsUpdate era) where
253+
parseJSON =
254+
withObject (show . typeRep $ Proxy @(PParamsUpdate era)) $ \obj ->
255+
let go acc PParam {ppName, ppUpdate} =
256+
maybe id setPpu <$> obj .:! Aeson.fromText ppName <*> pure acc
257+
where
258+
setPpu = maybe (const id) (\PParamUpdate {ppuLens} -> set ppuLens . SJust) ppUpdate
259+
in F.foldlM
260+
go
261+
(emptyPParamsUpdate @era)
262+
(eraPParams @era)
263+
252264
deriving instance Generic (PParamsUpdate era)
253265

254266
-- Generic derivation of `applyPPUpdates`

libs/cardano-ledger-core/testlib/Test/Cardano/Ledger/Core/JSON.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ roundTripJsonEraSpec =
6262
describe (eraName @era) $ do
6363
describe "RoundTrip JSON" $ do
6464
roundTripJsonSpec @(PParams era)
65+
roundTripJsonSpec @(PParamsUpdate era)
6566
roundTripJsonSpec @(TranslationContext era)
6667

6768
goldenJsonPParamsSpec ::

0 commit comments

Comments
 (0)