Skip to content

Commit

Permalink
New functions anyShelleyBasedEra, inAnyCardanoEra and inAnyShelleyBas…
Browse files Browse the repository at this point in the history
…edEra
  • Loading branch information
newhoggy committed Oct 18, 2023
1 parent 99f27e8 commit d1d6341
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 13 deletions.
6 changes: 2 additions & 4 deletions cardano-api/internal/Cardano/Api/Convenience/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ queryStateForBalancedTx era allTxIns certs = runExceptT $ do
& onNothing (left ByronEraNotSupported)

qeInMode <- pure (toEraInMode era CardanoMode)
& onNothing (left (EraConsensusModeMismatch (AnyConsensusMode CardanoMode) (cardanoEraConstraints era $ AnyCardanoEra era)))
& onNothing (left (EraConsensusModeMismatch (AnyConsensusMode CardanoMode) (anyCardanoEra era)))

let stakeCreds = Set.fromList $ mapMaybe filterUnRegCreds certs
drepCreds = Set.fromList $ mapMaybe filterUnRegDRepCreds certs
Expand Down Expand Up @@ -175,9 +175,7 @@ executeQueryAnyMode era localNodeConnInfo q = runExceptT $ do
let cMode = consensusModeOnly $ localConsensusModeParams localNodeConnInfo

eraInMode <- pure (toEraInMode era cMode)
& onNothing (left $ EraConsensusModeMismatch
(AnyConsensusMode CardanoMode)
(cardanoEraConstraints era $ AnyCardanoEra era))
& onNothing (left $ EraConsensusModeMismatch (AnyConsensusMode CardanoMode) (anyCardanoEra era))

case eraInMode of
ByronEraInByronMode -> left ByronEraNotSupported
Expand Down
13 changes: 13 additions & 0 deletions cardano-api/internal/Cardano/Api/Eon/ShelleyBasedEra.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ module Cardano.Api.Eon.ShelleyBasedEra
ShelleyBasedEra(..)
, IsShelleyBasedEra(..)
, AnyShelleyBasedEra(..)
, anyShelleyBasedEra
, InAnyShelleyBasedEra(..)
, inAnyShelleyBasedEra
, shelleyBasedToCardanoEra
, inEonForShelleyBasedEra
, inEonForShelleyBasedEraMaybe
Expand Down Expand Up @@ -281,6 +283,11 @@ instance FromJSON AnyShelleyBasedEra where
"Conway" -> pure $ AnyShelleyBasedEra ShelleyBasedEraConway
wrong -> fail $ "Failed to parse unknown shelley-based era: " <> Text.unpack wrong

anyShelleyBasedEra :: ()
=> ShelleyBasedEra era
-> AnyShelleyBasedEra
anyShelleyBasedEra sbe =
shelleyBasedEraConstraints sbe $ AnyShelleyBasedEra sbe

-- | This pairs up some era-dependent type with a 'ShelleyBasedEra' value that
-- tells us what era it is, but hides the era type. This is useful when the era
Expand All @@ -292,6 +299,12 @@ data InAnyShelleyBasedEra thing where
-> thing era
-> InAnyShelleyBasedEra thing

inAnyShelleyBasedEra :: ()
=> ShelleyBasedEra era
-> thing era
-> InAnyShelleyBasedEra thing
inAnyShelleyBasedEra sbe a =
shelleyBasedEraConstraints sbe $ InAnyShelleyBasedEra sbe a

-- | Converts a 'ShelleyBasedEra' to the broader 'CardanoEra'.
shelleyBasedToCardanoEra :: ShelleyBasedEra era -> CardanoEra era
Expand Down
3 changes: 2 additions & 1 deletion cardano-api/internal/Cardano/Api/Eras.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ module Cardano.Api.Eras
, IsCardanoEra(..)
, AnyCardanoEra(..)
, anyCardanoEra
, cardanoEraConstraints
, InAnyCardanoEra(..)
, inAnyCardanoEra
, cardanoEraConstraints
, CardanoLedgerEra
, ToCardanoEra(..)

Expand Down
24 changes: 17 additions & 7 deletions cardano-api/internal/Cardano/Api/Eras/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module Cardano.Api.Eras.Core
, AnyCardanoEra(..)
, anyCardanoEra
, InAnyCardanoEra(..)
, inAnyCardanoEra
, CardanoLedgerEra
, ToCardanoEra(..)

Expand Down Expand Up @@ -292,9 +293,10 @@ cardanoEraConstraints = \case
ConwayEra -> id

data AnyCardanoEra where
AnyCardanoEra :: IsCardanoEra era -- Provide class constraint
=> CardanoEra era -- and explicit value.
-> AnyCardanoEra
AnyCardanoEra
:: IsCardanoEra era
=> CardanoEra era
-> AnyCardanoEra

deriving instance Show AnyCardanoEra

Expand Down Expand Up @@ -368,10 +370,18 @@ anyCardanoEra = \case
-- not statically known, for example when deserialising from a file.
--
data InAnyCardanoEra thing where
InAnyCardanoEra :: IsCardanoEra era -- Provide class constraint
=> CardanoEra era -- and explicit value.
-> thing era
-> InAnyCardanoEra thing
InAnyCardanoEra
:: IsCardanoEra era
=> CardanoEra era
-> thing era
-> InAnyCardanoEra thing

inAnyCardanoEra :: ()
=> CardanoEra era
-> thing era
-> InAnyCardanoEra thing
inAnyCardanoEra era a =
cardanoEraConstraints era $ InAnyCardanoEra era a

-- ----------------------------------------------------------------------------
-- Conversion to ledger library types
Expand Down
5 changes: 4 additions & 1 deletion cardano-api/src/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ module Cardano.Api (
IsCardanoEra(..),
AnyCardanoEra(..),
anyCardanoEra,
cardanoEraConstraints,
InAnyCardanoEra(..),
inAnyCardanoEra,
cardanoEraConstraints,
ToCardanoEra(..),

-- * Eon support
Expand Down Expand Up @@ -101,7 +102,9 @@ module Cardano.Api (
ShelleyBasedEra(..),
IsShelleyBasedEra(..),
AnyShelleyBasedEra(..),
anyShelleyBasedEra,
InAnyShelleyBasedEra(..),
inAnyShelleyBasedEra,
CardanoEraStyle(..),
cardanoEraStyle,
shelleyBasedToCardanoEra,
Expand Down

0 comments on commit d1d6341

Please sign in to comment.