-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ADP-3359] Move
ApiEra
type and related functions to separate modul…
…e. (#4605) This PR moves the `ApiEra` type and related functions to a separate module `Api.Types.Era`. As a result: - the `ApiEra` type is co-located only with its related functions; - the `Api.Types` [megamodule](https://www.parsonsmatt.org/2019/11/27/keeping_compilation_fast.html) is now ever-so-slightly smaller. Additionally: - functions relating to `ApiEra` are now imported via the `ApiEra` qualifier; - the `Api.Types` module does **_not_** re-export symbols from `Api.Types.Era`. ## Issue ADP-3359 Follow-on from #4595.
- Loading branch information
Showing
19 changed files
with
188 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
{-# LANGUAGE DeriveAnyClass #-} | ||
{-# LANGUAGE DeriveDataTypeable #-} | ||
{-# LANGUAGE DeriveGeneric #-} | ||
{-# LANGUAGE DerivingStrategies #-} | ||
{-# LANGUAGE GADTs #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
|
||
-- | | ||
-- Copyright: | ||
-- © 2018-2023 IOHK | ||
-- © 2023-2024 Cardano Foundation | ||
-- License: | ||
-- Apache-2.0 | ||
-- | ||
-- This module provides API types and functions relating to eras. | ||
-- | ||
module Cardano.Wallet.Api.Types.Era | ||
( ApiEra (..) | ||
, fromAnyCardanoEra | ||
, toAnyCardanoEra | ||
, allRecentEras | ||
) | ||
where | ||
|
||
import Cardano.Api | ||
( AnyCardanoEra (AnyCardanoEra) | ||
, CardanoEra (..) | ||
) | ||
import Control.DeepSeq | ||
( NFData | ||
) | ||
import Data.Aeson | ||
( FromJSON (parseJSON) | ||
, Options (constructorTagModifier) | ||
, ToJSON (toJSON) | ||
, camelTo2 | ||
, genericParseJSON | ||
, genericToJSON | ||
) | ||
import Data.Data | ||
( Data | ||
) | ||
import Data.Eq | ||
( Eq | ||
) | ||
import Data.Function | ||
( ($) | ||
, (.) | ||
) | ||
import Data.List | ||
( drop | ||
) | ||
import Data.Ord | ||
( Ord | ||
) | ||
import Data.Set | ||
( Set | ||
) | ||
import GHC.Generics | ||
( Generic | ||
) | ||
import Prelude | ||
( Bounded | ||
, Enum | ||
) | ||
import Text.Show | ||
( Show | ||
) | ||
|
||
import qualified Data.Aeson as Aeson | ||
import qualified Data.Set as Set | ||
import qualified Internal.Cardano.Write.Tx as Write | ||
( allRecentEras | ||
, toAnyCardanoEra | ||
) | ||
|
||
data ApiEra | ||
= ApiByron | ||
| ApiShelley | ||
| ApiAllegra | ||
| ApiMary | ||
| ApiAlonzo | ||
| ApiBabbage | ||
| ApiConway | ||
deriving (Data, Show, Eq, Generic, Enum, Ord, Bounded) | ||
deriving anyclass NFData | ||
|
||
instance FromJSON ApiEra where | ||
parseJSON = genericParseJSON $ Aeson.defaultOptions | ||
{ constructorTagModifier = drop 4 . camelTo2 '_' } | ||
|
||
instance ToJSON ApiEra where | ||
toJSON = genericToJSON $ Aeson.defaultOptions | ||
{ constructorTagModifier = drop 4 . camelTo2 '_' } | ||
|
||
fromAnyCardanoEra :: AnyCardanoEra -> ApiEra | ||
fromAnyCardanoEra = \case | ||
AnyCardanoEra ByronEra -> ApiByron | ||
AnyCardanoEra ShelleyEra -> ApiShelley | ||
AnyCardanoEra AllegraEra -> ApiAllegra | ||
AnyCardanoEra MaryEra -> ApiMary | ||
AnyCardanoEra AlonzoEra -> ApiAlonzo | ||
AnyCardanoEra BabbageEra -> ApiBabbage | ||
AnyCardanoEra ConwayEra -> ApiConway | ||
|
||
toAnyCardanoEra :: ApiEra -> AnyCardanoEra | ||
toAnyCardanoEra = \case | ||
ApiByron -> AnyCardanoEra ByronEra | ||
ApiShelley -> AnyCardanoEra ShelleyEra | ||
ApiAllegra -> AnyCardanoEra AllegraEra | ||
ApiMary -> AnyCardanoEra MaryEra | ||
ApiAlonzo -> AnyCardanoEra AlonzoEra | ||
ApiBabbage -> AnyCardanoEra BabbageEra | ||
ApiConway -> AnyCardanoEra ConwayEra | ||
|
||
-- | The complete set of recent eras. | ||
-- | ||
allRecentEras :: Set ApiEra | ||
allRecentEras = | ||
Set.map (fromAnyCardanoEra . Write.toAnyCardanoEra) Write.allRecentEras |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.