Skip to content

Commit

Permalink
Add ToJSON/FromJSON instances for ChainPoint
Browse files Browse the repository at this point in the history
There is already a ToJSON instance for ChainTip so we figured we could
upstream this from hydra-cardano-api.
  • Loading branch information
ch1bo authored and newhoggy committed Mar 19, 2023
1 parent 8d969ec commit e910b83
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions cardano-api/src/Cardano/Api/Block.hs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,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 e910b83

Please sign in to comment.