Skip to content

Commit

Permalink
Use ToJSON instance for getting era in query tip command
Browse files Browse the repository at this point in the history
  • Loading branch information
newhoggy committed Mar 31, 2021
1 parent f1bbbc9 commit 389fd4b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 12 deletions.
3 changes: 3 additions & 0 deletions cardano-api/src/Cardano/Api/Eras.hs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ instance Bounded AnyCardanoEra where
minBound = AnyCardanoEra ByronEra
maxBound = AnyCardanoEra MaryEra

instance ToJSON AnyCardanoEra where
toJSON (AnyCardanoEra era) = toJSON era

-- | Like the 'AnyCardanoEra' constructor but does not demand a 'IsCardanoEra'
-- class constraint.
--
Expand Down
14 changes: 2 additions & 12 deletions cardano-cli/src/Cardano/CLI/Shelley/Run/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Prelude (String)

import Data.Aeson (ToJSON (..), (.=))
import qualified Data.Aeson as Aeson
import qualified Data.Aeson as J
import Data.Aeson.Encode.Pretty (encodePretty)
import qualified Data.ByteString.Lazy.Char8 as LBS
import qualified Data.HashMap.Strict as HMS
Expand Down Expand Up @@ -156,24 +157,13 @@ runQueryTip (AnyConsensusModeParams cModeParams) network mOutFile = do
mEpoch <- mEpochQuery anyEra consensusMode localNodeConnInfo
tip <- liftIO $ getLocalChainTip localNodeConnInfo
let output = encodePretty
. toObject "era" (Just (eraString anyEra))
. toObject "era" (Just (J.toJSON anyEra))
. toObject "epoch" mEpoch
$ toJSON tip
case mOutFile of
Just (OutputFile fpath) -> liftIO $ LBS.writeFile fpath output
Nothing -> liftIO $ LBS.putStrLn output
where
eraString
:: AnyCardanoEra
-> String
eraString (AnyCardanoEra era) =
case cardanoEraStyle era of
LegacyByronEra -> "byron"
ShelleyBasedEra sbe -> case sbe of
ShelleyBasedEraShelley -> "shelley"
ShelleyBasedEraAllegra -> "allegra"
ShelleyBasedEraMary -> "mary"

mEpochQuery
:: AnyCardanoEra
-> ConsensusMode mode
Expand Down

0 comments on commit 389fd4b

Please sign in to comment.