Skip to content

Commit

Permalink
Merge pull request #4686 from input-output-hk/ch1bo/cardano-api-chain…
Browse files Browse the repository at this point in the history
…-point-json-instances

[cardano-api] Add ToJSON/FromJSON instances for ChainPoint
  • Loading branch information
newhoggy authored Mar 19, 2023
2 parents 8d969ec + 3b01002 commit d677557
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions cardano-api/src/Cardano/Api/Block.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE RankNTypes #-}
Expand Down Expand Up @@ -358,6 +359,24 @@ instance Ord ChainPoint where
compare _ ChainPointAtGenesis = GT
compare (ChainPoint sn _) (ChainPoint sn' _) = compare sn sn'

instance ToJSON ChainPoint where
toJSON = \case
ChainPointAtGenesis -> object ["tag" .= String "ChainPointAtGenesis"]
ChainPoint slot blockHash ->
object
[ "tag" .= String "ChainPoint"
, "slot" .= toJSON slot
, "blockHash" .= toJSON blockHash
]

instance FromJSON ChainPoint where
parseJSON = withObject "ChainPoint" $ \o -> do
tag <- o .: "tag"
case tag :: Text of
"ChainPointAtGenesis" -> pure ChainPointAtGenesis
"ChainPoint" -> ChainPoint <$> o .: "slot" <*> o .: "blockHash"
_ -> fail "Expected tag to be ChainPointAtGenesis | ChainPoint"

toConsensusPointInMode :: ConsensusMode mode
-> ChainPoint
-> Consensus.Point (ConsensusBlockForMode mode)
Expand Down

0 comments on commit d677557

Please sign in to comment.