{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}

-- | Convenience query functions
--
module Cardano.Api.Convenience.Query (
    QueryConvenienceError(..),
    determineEra,
    -- * Simplest query related
    executeQueryCardanoMode,

    queryStateForBalancedTx,
    renderQueryConvenienceError,
  ) where

import           Prelude

import           Data.Bifunctor (first)
import           Data.Set (Set)
import qualified Data.Set as Set
import           Data.Text (Text)

import           Ouroboros.Consensus.HardFork.Combinator.AcrossEras (EraMismatch (..))

import           Cardano.Api.Certificate
import           Cardano.Api.Convenience.Constraints
import           Cardano.Api.Environment
import           Cardano.Api.Eras
import           Cardano.Api.IPC
import           Cardano.Api.Modes
import           Cardano.Api.NetworkId
import           Cardano.Api.ProtocolParameters
import           Cardano.Api.Query
import           Cardano.Api.TxBody
import           Cardano.Api.Utils

data QueryConvenienceError
  = AcqFailure AcquiringFailure
  | SockErr EnvSocketError
  | QueryEraMismatch EraMismatch
  | ByronEraNotSupported
  | EraConsensusModeMismatch !AnyConsensusMode !AnyCardanoEra

renderQueryConvenienceError :: QueryConvenienceError -> Text
renderQueryConvenienceError :: QueryConvenienceError -> Text
renderQueryConvenienceError (AcqFailure AcquiringFailure
e) =
  Text
"Acquiring failure: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> AcquiringFailure -> Text
forall a. Show a => a -> Text
textShow AcquiringFailure
e
renderQueryConvenienceError (SockErr EnvSocketError
e) =
  EnvSocketError -> Text
renderEnvSocketError EnvSocketError
e
renderQueryConvenienceError (QueryEraMismatch (EraMismatch Text
ledgerEraName' Text
otherEraName')) =
  Text
"The era of the node and the tx do not match. " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>
  Text
"The node is running in the " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
ledgerEraName' Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>
  Text
" era, but the transaction is for the " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
otherEraName' Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" era."
renderQueryConvenienceError QueryConvenienceError
ByronEraNotSupported =
  Text
"Byron era not supported"
renderQueryConvenienceError (EraConsensusModeMismatch AnyConsensusMode
cMode AnyCardanoEra
anyCEra) =
  Text
"Consensus mode and era mismatch. Consensus mode: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> AnyConsensusMode -> Text
forall a. Show a => a -> Text
textShow AnyConsensusMode
cMode Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>
  Text
" Era: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> AnyCardanoEra -> Text
forall a. Show a => a -> Text
textShow AnyCardanoEra
anyCEra

-- | A convenience function to query the relevant information, from
-- the local node, for Cardano.Api.Convenience.Construction.constructBalancedTx
queryStateForBalancedTx
  :: CardanoEra era
  -> NetworkId
  -> [TxIn]
  -> IO (Either QueryConvenienceError (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart, Set PoolId))
queryStateForBalancedTx :: CardanoEra era
-> NetworkId
-> [TxIn]
-> IO
     (Either
        QueryConvenienceError
        (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart,
         Set PoolId))
queryStateForBalancedTx CardanoEra era
era NetworkId
networkId [TxIn]
allTxIns = do
 Either QueryConvenienceError SocketPath
eSocketPath <- (EnvSocketError -> QueryConvenienceError)
-> Either EnvSocketError SocketPath
-> Either QueryConvenienceError SocketPath
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first EnvSocketError -> QueryConvenienceError
SockErr (Either EnvSocketError SocketPath
 -> Either QueryConvenienceError SocketPath)
-> IO (Either EnvSocketError SocketPath)
-> IO (Either QueryConvenienceError SocketPath)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO (Either EnvSocketError SocketPath)
readEnvSocketPath
 case Either QueryConvenienceError SocketPath
eSocketPath of
  Left QueryConvenienceError
e -> Either
  QueryConvenienceError
  (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart,
   Set PoolId)
-> IO
     (Either
        QueryConvenienceError
        (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart,
         Set PoolId))
forall (m :: * -> *) a. Monad m => a -> m a
return (Either
   QueryConvenienceError
   (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart,
    Set PoolId)
 -> IO
      (Either
         QueryConvenienceError
         (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart,
          Set PoolId)))
-> Either
     QueryConvenienceError
     (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart,
      Set PoolId)
-> IO
     (Either
        QueryConvenienceError
        (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart,
         Set PoolId))
forall a b. (a -> b) -> a -> b
$ QueryConvenienceError
-> Either
     QueryConvenienceError
     (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart,
      Set PoolId)
forall a b. a -> Either a b
Left QueryConvenienceError
e
  Right (SocketPath FilePath
sockPath) -> do
    let cModeParams :: ConsensusModeParams CardanoMode
cModeParams = EpochSlots -> ConsensusModeParams CardanoMode
CardanoModeParams (EpochSlots -> ConsensusModeParams CardanoMode)
-> EpochSlots -> ConsensusModeParams CardanoMode
forall a b. (a -> b) -> a -> b
$ Word64 -> EpochSlots
EpochSlots Word64
21600
        localNodeConnInfo :: LocalNodeConnectInfo CardanoMode
localNodeConnInfo = ConsensusModeParams CardanoMode
-> NetworkId -> FilePath -> LocalNodeConnectInfo CardanoMode
forall mode.
ConsensusModeParams mode
-> NetworkId -> FilePath -> LocalNodeConnectInfo mode
LocalNodeConnectInfo
                              ConsensusModeParams CardanoMode
cModeParams
                              NetworkId
networkId
                              FilePath
sockPath
    Either QueryConvenienceError (ShelleyBasedEra era)
eSbe <- Either QueryConvenienceError (ShelleyBasedEra era)
-> IO (Either QueryConvenienceError (ShelleyBasedEra era))
forall (m :: * -> *) a. Monad m => a -> m a
return (Either QueryConvenienceError (ShelleyBasedEra era)
 -> IO (Either QueryConvenienceError (ShelleyBasedEra era)))
-> (CardanoEraStyle era
    -> Either QueryConvenienceError (ShelleyBasedEra era))
-> CardanoEraStyle era
-> IO (Either QueryConvenienceError (ShelleyBasedEra era))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CardanoEraStyle era
-> Either QueryConvenienceError (ShelleyBasedEra era)
forall era.
CardanoEraStyle era
-> Either QueryConvenienceError (ShelleyBasedEra era)
getSbe (CardanoEraStyle era
 -> IO (Either QueryConvenienceError (ShelleyBasedEra era)))
-> CardanoEraStyle era
-> IO (Either QueryConvenienceError (ShelleyBasedEra era))
forall a b. (a -> b) -> a -> b
$ CardanoEra era -> CardanoEraStyle era
forall era. CardanoEra era -> CardanoEraStyle era
cardanoEraStyle CardanoEra era
era
    case Either QueryConvenienceError (ShelleyBasedEra era)
eSbe of
      Left QueryConvenienceError
e -> Either
  QueryConvenienceError
  (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart,
   Set PoolId)
-> IO
     (Either
        QueryConvenienceError
        (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart,
         Set PoolId))
forall (m :: * -> *) a. Monad m => a -> m a
return (Either
   QueryConvenienceError
   (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart,
    Set PoolId)
 -> IO
      (Either
         QueryConvenienceError
         (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart,
          Set PoolId)))
-> Either
     QueryConvenienceError
     (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart,
      Set PoolId)
-> IO
     (Either
        QueryConvenienceError
        (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart,
         Set PoolId))
forall a b. (a -> b) -> a -> b
$ QueryConvenienceError
-> Either
     QueryConvenienceError
     (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart,
      Set PoolId)
forall a b. a -> Either a b
Left QueryConvenienceError
e
      Right ShelleyBasedEra era
qSbe -> do
        case CardanoEra era
-> ConsensusMode CardanoMode -> Maybe (EraInMode era CardanoMode)
forall era mode.
CardanoEra era -> ConsensusMode mode -> Maybe (EraInMode era mode)
toEraInMode CardanoEra era
era ConsensusMode CardanoMode
CardanoMode of
          Just EraInMode era CardanoMode
qeInMode -> do

            -- Queries
            let utxoQuery :: QueryInMode CardanoMode (Either EraMismatch (UTxO era))
utxoQuery = EraInMode era CardanoMode
-> QueryInEra era (UTxO era)
-> QueryInMode CardanoMode (Either EraMismatch (UTxO era))
forall era mode result.
EraInMode era mode
-> QueryInEra era result
-> QueryInMode mode (Either EraMismatch result)
QueryInEra EraInMode era CardanoMode
qeInMode (QueryInEra era (UTxO era)
 -> QueryInMode CardanoMode (Either EraMismatch (UTxO era)))
-> QueryInEra era (UTxO era)
-> QueryInMode CardanoMode (Either EraMismatch (UTxO era))
forall a b. (a -> b) -> a -> b
$ ShelleyBasedEra era
-> QueryInShelleyBasedEra era (UTxO era)
-> QueryInEra era (UTxO era)
forall era result.
ShelleyBasedEra era
-> QueryInShelleyBasedEra era result -> QueryInEra era result
QueryInShelleyBasedEra ShelleyBasedEra era
qSbe
                              (QueryInShelleyBasedEra era (UTxO era)
 -> QueryInEra era (UTxO era))
-> QueryInShelleyBasedEra era (UTxO era)
-> QueryInEra era (UTxO era)
forall a b. (a -> b) -> a -> b
$ QueryUTxOFilter -> QueryInShelleyBasedEra era (UTxO era)
forall era.
QueryUTxOFilter -> QueryInShelleyBasedEra era (UTxO era)
QueryUTxO (Set TxIn -> QueryUTxOFilter
QueryUTxOByTxIn ([TxIn] -> Set TxIn
forall a. Ord a => [a] -> Set a
Set.fromList [TxIn]
allTxIns))
                pparamsQuery :: QueryInMode CardanoMode (Either EraMismatch ProtocolParameters)
pparamsQuery = EraInMode era CardanoMode
-> QueryInEra era ProtocolParameters
-> QueryInMode CardanoMode (Either EraMismatch ProtocolParameters)
forall era mode result.
EraInMode era mode
-> QueryInEra era result
-> QueryInMode mode (Either EraMismatch result)
QueryInEra EraInMode era CardanoMode
qeInMode
                                 (QueryInEra era ProtocolParameters
 -> QueryInMode CardanoMode (Either EraMismatch ProtocolParameters))
-> QueryInEra era ProtocolParameters
-> QueryInMode CardanoMode (Either EraMismatch ProtocolParameters)
forall a b. (a -> b) -> a -> b
$ ShelleyBasedEra era
-> QueryInShelleyBasedEra era ProtocolParameters
-> QueryInEra era ProtocolParameters
forall era result.
ShelleyBasedEra era
-> QueryInShelleyBasedEra era result -> QueryInEra era result
QueryInShelleyBasedEra ShelleyBasedEra era
qSbe QueryInShelleyBasedEra era ProtocolParameters
forall era. QueryInShelleyBasedEra era ProtocolParameters
QueryProtocolParameters
                eraHistoryQuery :: QueryInMode CardanoMode (EraHistory CardanoMode)
eraHistoryQuery = ConsensusModeIsMultiEra CardanoMode
-> QueryInMode CardanoMode (EraHistory CardanoMode)
forall mode.
ConsensusModeIsMultiEra mode -> QueryInMode mode (EraHistory mode)
QueryEraHistory ConsensusModeIsMultiEra CardanoMode
CardanoModeIsMultiEra
                systemStartQuery :: QueryInMode mode SystemStart
systemStartQuery = QueryInMode mode SystemStart
forall mode. QueryInMode mode SystemStart
QuerySystemStart
                stakePoolsQuery :: QueryInMode CardanoMode (Either EraMismatch (Set PoolId))
stakePoolsQuery = EraInMode era CardanoMode
-> QueryInEra era (Set PoolId)
-> QueryInMode CardanoMode (Either EraMismatch (Set PoolId))
forall era mode result.
EraInMode era mode
-> QueryInEra era result
-> QueryInMode mode (Either EraMismatch result)
QueryInEra EraInMode era CardanoMode
qeInMode (QueryInEra era (Set PoolId)
 -> QueryInMode CardanoMode (Either EraMismatch (Set PoolId)))
-> (QueryInShelleyBasedEra era (Set PoolId)
    -> QueryInEra era (Set PoolId))
-> QueryInShelleyBasedEra era (Set PoolId)
-> QueryInMode CardanoMode (Either EraMismatch (Set PoolId))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShelleyBasedEra era
-> QueryInShelleyBasedEra era (Set PoolId)
-> QueryInEra era (Set PoolId)
forall era result.
ShelleyBasedEra era
-> QueryInShelleyBasedEra era result -> QueryInEra era result
QueryInShelleyBasedEra ShelleyBasedEra era
qSbe (QueryInShelleyBasedEra era (Set PoolId)
 -> QueryInMode CardanoMode (Either EraMismatch (Set PoolId)))
-> QueryInShelleyBasedEra era (Set PoolId)
-> QueryInMode CardanoMode (Either EraMismatch (Set PoolId))
forall a b. (a -> b) -> a -> b
$ QueryInShelleyBasedEra era (Set PoolId)
forall era. QueryInShelleyBasedEra era (Set PoolId)
QueryStakePools

            -- Query execution
            Either QueryConvenienceError (UTxO era)
eUtxo <- CardanoEra era
-> NetworkId
-> QueryInMode CardanoMode (Either EraMismatch (UTxO era))
-> IO (Either QueryConvenienceError (UTxO era))
forall era result.
CardanoEra era
-> NetworkId
-> QueryInMode CardanoMode (Either EraMismatch result)
-> IO (Either QueryConvenienceError result)
executeQueryCardanoMode CardanoEra era
era NetworkId
networkId QueryInMode CardanoMode (Either EraMismatch (UTxO era))
utxoQuery
            Either QueryConvenienceError ProtocolParameters
ePparams <- CardanoEra era
-> NetworkId
-> QueryInMode CardanoMode (Either EraMismatch ProtocolParameters)
-> IO (Either QueryConvenienceError ProtocolParameters)
forall era result.
CardanoEra era
-> NetworkId
-> QueryInMode CardanoMode (Either EraMismatch result)
-> IO (Either QueryConvenienceError result)
executeQueryCardanoMode CardanoEra era
era NetworkId
networkId QueryInMode CardanoMode (Either EraMismatch ProtocolParameters)
pparamsQuery
            Either AcquiringFailure (EraHistory CardanoMode)
eEraHistory <- LocalNodeConnectInfo CardanoMode
-> Maybe ChainPoint
-> QueryInMode CardanoMode (EraHistory CardanoMode)
-> IO (Either AcquiringFailure (EraHistory CardanoMode))
forall mode result.
LocalNodeConnectInfo mode
-> Maybe ChainPoint
-> QueryInMode mode result
-> IO (Either AcquiringFailure result)
queryNodeLocalState LocalNodeConnectInfo CardanoMode
localNodeConnInfo Maybe ChainPoint
forall a. Maybe a
Nothing QueryInMode CardanoMode (EraHistory CardanoMode)
eraHistoryQuery
            Either AcquiringFailure SystemStart
eSystemStart <- LocalNodeConnectInfo CardanoMode
-> Maybe ChainPoint
-> QueryInMode CardanoMode SystemStart
-> IO (Either AcquiringFailure SystemStart)
forall mode result.
LocalNodeConnectInfo mode
-> Maybe ChainPoint
-> QueryInMode mode result
-> IO (Either AcquiringFailure result)
queryNodeLocalState LocalNodeConnectInfo CardanoMode
localNodeConnInfo Maybe ChainPoint
forall a. Maybe a
Nothing QueryInMode CardanoMode SystemStart
forall mode. QueryInMode mode SystemStart
systemStartQuery
            Either QueryConvenienceError (Set PoolId)
eStakePools <- CardanoEra era
-> NetworkId
-> QueryInMode CardanoMode (Either EraMismatch (Set PoolId))
-> IO (Either QueryConvenienceError (Set PoolId))
forall era result.
CardanoEra era
-> NetworkId
-> QueryInMode CardanoMode (Either EraMismatch result)
-> IO (Either QueryConvenienceError result)
executeQueryCardanoMode CardanoEra era
era NetworkId
networkId QueryInMode CardanoMode (Either EraMismatch (Set PoolId))
stakePoolsQuery
            Either
  QueryConvenienceError
  (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart,
   Set PoolId)
-> IO
     (Either
        QueryConvenienceError
        (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart,
         Set PoolId))
forall (m :: * -> *) a. Monad m => a -> m a
return (Either
   QueryConvenienceError
   (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart,
    Set PoolId)
 -> IO
      (Either
         QueryConvenienceError
         (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart,
          Set PoolId)))
-> Either
     QueryConvenienceError
     (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart,
      Set PoolId)
-> IO
     (Either
        QueryConvenienceError
        (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart,
         Set PoolId))
forall a b. (a -> b) -> a -> b
$ do
              UTxO era
utxo <- Either QueryConvenienceError (UTxO era)
eUtxo
              ProtocolParameters
pparams <- Either QueryConvenienceError ProtocolParameters
ePparams
              EraHistory CardanoMode
eraHistory <- (AcquiringFailure -> QueryConvenienceError)
-> Either AcquiringFailure (EraHistory CardanoMode)
-> Either QueryConvenienceError (EraHistory CardanoMode)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first AcquiringFailure -> QueryConvenienceError
AcqFailure Either AcquiringFailure (EraHistory CardanoMode)
eEraHistory
              SystemStart
systemStart <- (AcquiringFailure -> QueryConvenienceError)
-> Either AcquiringFailure SystemStart
-> Either QueryConvenienceError SystemStart
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first AcquiringFailure -> QueryConvenienceError
AcqFailure Either AcquiringFailure SystemStart
eSystemStart
              Set PoolId
stakePools <- Either QueryConvenienceError (Set PoolId)
eStakePools
              (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart,
 Set PoolId)
-> Either
     QueryConvenienceError
     (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart,
      Set PoolId)
forall a b. b -> Either a b
Right (UTxO era
utxo, ProtocolParameters
pparams, EraHistory CardanoMode
eraHistory, SystemStart
systemStart, Set PoolId
stakePools)
          Maybe (EraInMode era CardanoMode)
Nothing -> Either
  QueryConvenienceError
  (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart,
   Set PoolId)
-> IO
     (Either
        QueryConvenienceError
        (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart,
         Set PoolId))
forall (m :: * -> *) a. Monad m => a -> m a
return (Either
   QueryConvenienceError
   (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart,
    Set PoolId)
 -> IO
      (Either
         QueryConvenienceError
         (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart,
          Set PoolId)))
-> Either
     QueryConvenienceError
     (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart,
      Set PoolId)
-> IO
     (Either
        QueryConvenienceError
        (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart,
         Set PoolId))
forall a b. (a -> b) -> a -> b
$ QueryConvenienceError
-> Either
     QueryConvenienceError
     (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart,
      Set PoolId)
forall a b. a -> Either a b
Left (QueryConvenienceError
 -> Either
      QueryConvenienceError
      (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart,
       Set PoolId))
-> QueryConvenienceError
-> Either
     QueryConvenienceError
     (UTxO era, ProtocolParameters, EraHistory CardanoMode, SystemStart,
      Set PoolId)
forall a b. (a -> b) -> a -> b
$ AnyConsensusMode -> AnyCardanoEra -> QueryConvenienceError
EraConsensusModeMismatch
                                      (ConsensusMode CardanoMode -> AnyConsensusMode
forall mode. ConsensusMode mode -> AnyConsensusMode
AnyConsensusMode ConsensusMode CardanoMode
CardanoMode)
                                      (CardanoEra era
-> (IsCardanoEra era => AnyCardanoEra) -> AnyCardanoEra
forall era a. CardanoEra era -> (IsCardanoEra era => a) -> a
getIsCardanoEraConstraint CardanoEra era
era ((IsCardanoEra era => AnyCardanoEra) -> AnyCardanoEra)
-> (IsCardanoEra era => AnyCardanoEra) -> AnyCardanoEra
forall a b. (a -> b) -> a -> b
$ CardanoEra era -> AnyCardanoEra
forall era. IsCardanoEra era => CardanoEra era -> AnyCardanoEra
AnyCardanoEra CardanoEra era
era)

-- | Query the node to determine which era it is in.
determineEra
  :: ConsensusModeParams mode
  -> LocalNodeConnectInfo mode
  -> IO (Either AcquiringFailure AnyCardanoEra)
determineEra :: ConsensusModeParams mode
-> LocalNodeConnectInfo mode
-> IO (Either AcquiringFailure AnyCardanoEra)
determineEra ConsensusModeParams mode
cModeParams LocalNodeConnectInfo mode
localNodeConnInfo =
  case ConsensusModeParams mode -> ConsensusMode mode
forall mode. ConsensusModeParams mode -> ConsensusMode mode
consensusModeOnly ConsensusModeParams mode
cModeParams of
    ConsensusMode mode
ByronMode -> Either AcquiringFailure AnyCardanoEra
-> IO (Either AcquiringFailure AnyCardanoEra)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either AcquiringFailure AnyCardanoEra
 -> IO (Either AcquiringFailure AnyCardanoEra))
-> (AnyCardanoEra -> Either AcquiringFailure AnyCardanoEra)
-> AnyCardanoEra
-> IO (Either AcquiringFailure AnyCardanoEra)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnyCardanoEra -> Either AcquiringFailure AnyCardanoEra
forall a b. b -> Either a b
Right (AnyCardanoEra -> IO (Either AcquiringFailure AnyCardanoEra))
-> AnyCardanoEra -> IO (Either AcquiringFailure AnyCardanoEra)
forall a b. (a -> b) -> a -> b
$ CardanoEra ByronEra -> AnyCardanoEra
forall era. IsCardanoEra era => CardanoEra era -> AnyCardanoEra
AnyCardanoEra CardanoEra ByronEra
ByronEra
    ConsensusMode mode
ShelleyMode -> Either AcquiringFailure AnyCardanoEra
-> IO (Either AcquiringFailure AnyCardanoEra)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either AcquiringFailure AnyCardanoEra
 -> IO (Either AcquiringFailure AnyCardanoEra))
-> (AnyCardanoEra -> Either AcquiringFailure AnyCardanoEra)
-> AnyCardanoEra
-> IO (Either AcquiringFailure AnyCardanoEra)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnyCardanoEra -> Either AcquiringFailure AnyCardanoEra
forall a b. b -> Either a b
Right (AnyCardanoEra -> IO (Either AcquiringFailure AnyCardanoEra))
-> AnyCardanoEra -> IO (Either AcquiringFailure AnyCardanoEra)
forall a b. (a -> b) -> a -> b
$ CardanoEra ShelleyEra -> AnyCardanoEra
forall era. IsCardanoEra era => CardanoEra era -> AnyCardanoEra
AnyCardanoEra CardanoEra ShelleyEra
ShelleyEra
    ConsensusMode mode
CardanoMode ->
      LocalNodeConnectInfo mode
-> Maybe ChainPoint
-> QueryInMode mode AnyCardanoEra
-> IO (Either AcquiringFailure AnyCardanoEra)
forall mode result.
LocalNodeConnectInfo mode
-> Maybe ChainPoint
-> QueryInMode mode result
-> IO (Either AcquiringFailure result)
queryNodeLocalState LocalNodeConnectInfo mode
localNodeConnInfo Maybe ChainPoint
forall a. Maybe a
Nothing
        (QueryInMode mode AnyCardanoEra
 -> IO (Either AcquiringFailure AnyCardanoEra))
-> QueryInMode mode AnyCardanoEra
-> IO (Either AcquiringFailure AnyCardanoEra)
forall a b. (a -> b) -> a -> b
$ ConsensusModeIsMultiEra CardanoMode
-> QueryInMode CardanoMode AnyCardanoEra
forall mode.
ConsensusModeIsMultiEra mode -> QueryInMode mode AnyCardanoEra
QueryCurrentEra ConsensusModeIsMultiEra CardanoMode
CardanoModeIsMultiEra

getSbe :: CardanoEraStyle era -> Either QueryConvenienceError (ShelleyBasedEra era)
getSbe :: CardanoEraStyle era
-> Either QueryConvenienceError (ShelleyBasedEra era)
getSbe CardanoEraStyle era
LegacyByronEra = QueryConvenienceError
-> Either QueryConvenienceError (ShelleyBasedEra era)
forall a b. a -> Either a b
Left QueryConvenienceError
ByronEraNotSupported
getSbe (ShelleyBasedEra ShelleyBasedEra era
sbe) = ShelleyBasedEra era
-> Either QueryConvenienceError (ShelleyBasedEra era)
forall (m :: * -> *) a. Monad m => a -> m a
return ShelleyBasedEra era
sbe

-- | Execute a query against the local node. The local
-- node must be in CardanoMode.
executeQueryCardanoMode
  :: CardanoEra era
  -> NetworkId
  -> QueryInMode CardanoMode (Either EraMismatch result)
  -> IO (Either QueryConvenienceError result)
executeQueryCardanoMode :: CardanoEra era
-> NetworkId
-> QueryInMode CardanoMode (Either EraMismatch result)
-> IO (Either QueryConvenienceError result)
executeQueryCardanoMode CardanoEra era
era NetworkId
nid QueryInMode CardanoMode (Either EraMismatch result)
q = do
  Either QueryConvenienceError SocketPath
eSocketPath <- (EnvSocketError -> QueryConvenienceError)
-> Either EnvSocketError SocketPath
-> Either QueryConvenienceError SocketPath
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first EnvSocketError -> QueryConvenienceError
SockErr (Either EnvSocketError SocketPath
 -> Either QueryConvenienceError SocketPath)
-> IO (Either EnvSocketError SocketPath)
-> IO (Either QueryConvenienceError SocketPath)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO (Either EnvSocketError SocketPath)
readEnvSocketPath
  case Either QueryConvenienceError SocketPath
eSocketPath of
   Left QueryConvenienceError
e -> Either QueryConvenienceError result
-> IO (Either QueryConvenienceError result)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either QueryConvenienceError result
 -> IO (Either QueryConvenienceError result))
-> Either QueryConvenienceError result
-> IO (Either QueryConvenienceError result)
forall a b. (a -> b) -> a -> b
$ QueryConvenienceError -> Either QueryConvenienceError result
forall a b. a -> Either a b
Left QueryConvenienceError
e
   Right (SocketPath FilePath
sockPath) -> do
     let localConnectInfo :: LocalNodeConnectInfo CardanoMode
localConnectInfo =
           LocalNodeConnectInfo :: forall mode.
ConsensusModeParams mode
-> NetworkId -> FilePath -> LocalNodeConnectInfo mode
LocalNodeConnectInfo
             { localConsensusModeParams :: ConsensusModeParams CardanoMode
localConsensusModeParams = EpochSlots -> ConsensusModeParams CardanoMode
CardanoModeParams (Word64 -> EpochSlots
EpochSlots Word64
21600)
             , localNodeNetworkId :: NetworkId
localNodeNetworkId = NetworkId
nid
             , localNodeSocketPath :: FilePath
localNodeSocketPath = FilePath
sockPath
             }
     CardanoEra era
-> LocalNodeConnectInfo CardanoMode
-> QueryInMode CardanoMode (Either EraMismatch result)
-> IO (Either QueryConvenienceError result)
forall result era mode.
CardanoEra era
-> LocalNodeConnectInfo mode
-> QueryInMode mode (Either EraMismatch result)
-> IO (Either QueryConvenienceError result)
executeQueryAnyMode CardanoEra era
era LocalNodeConnectInfo CardanoMode
localConnectInfo QueryInMode CardanoMode (Either EraMismatch result)
q

-- | Execute a query against the local node in any mode.
executeQueryAnyMode
  :: forall result era mode. CardanoEra era
  -> LocalNodeConnectInfo mode
  -> QueryInMode mode (Either EraMismatch result)
  -> IO (Either QueryConvenienceError result)
executeQueryAnyMode :: CardanoEra era
-> LocalNodeConnectInfo mode
-> QueryInMode mode (Either EraMismatch result)
-> IO (Either QueryConvenienceError result)
executeQueryAnyMode CardanoEra era
era LocalNodeConnectInfo mode
localNodeConnInfo QueryInMode mode (Either EraMismatch result)
q = do
  let cMode :: ConsensusMode mode
cMode = ConsensusModeParams mode -> ConsensusMode mode
forall mode. ConsensusModeParams mode -> ConsensusMode mode
consensusModeOnly (ConsensusModeParams mode -> ConsensusMode mode)
-> ConsensusModeParams mode -> ConsensusMode mode
forall a b. (a -> b) -> a -> b
$ LocalNodeConnectInfo mode -> ConsensusModeParams mode
forall mode. LocalNodeConnectInfo mode -> ConsensusModeParams mode
localConsensusModeParams LocalNodeConnectInfo mode
localNodeConnInfo
  case CardanoEra era -> ConsensusMode mode -> Maybe (EraInMode era mode)
forall era mode.
CardanoEra era -> ConsensusMode mode -> Maybe (EraInMode era mode)
toEraInMode CardanoEra era
era ConsensusMode mode
cMode of
    Just EraInMode era mode
eraInMode ->
      case EraInMode era mode
eraInMode of
        EraInMode era mode
ByronEraInByronMode -> Either QueryConvenienceError result
-> IO (Either QueryConvenienceError result)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either QueryConvenienceError result
 -> IO (Either QueryConvenienceError result))
-> Either QueryConvenienceError result
-> IO (Either QueryConvenienceError result)
forall a b. (a -> b) -> a -> b
$ QueryConvenienceError -> Either QueryConvenienceError result
forall a b. a -> Either a b
Left QueryConvenienceError
ByronEraNotSupported
        EraInMode era mode
_ -> IO (Either QueryConvenienceError result)
execQuery
    Maybe (EraInMode era mode)
Nothing -> Either QueryConvenienceError result
-> IO (Either QueryConvenienceError result)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either QueryConvenienceError result
 -> IO (Either QueryConvenienceError result))
-> Either QueryConvenienceError result
-> IO (Either QueryConvenienceError result)
forall a b. (a -> b) -> a -> b
$ QueryConvenienceError -> Either QueryConvenienceError result
forall a b. a -> Either a b
Left (QueryConvenienceError -> Either QueryConvenienceError result)
-> QueryConvenienceError -> Either QueryConvenienceError result
forall a b. (a -> b) -> a -> b
$ AnyConsensusMode -> AnyCardanoEra -> QueryConvenienceError
EraConsensusModeMismatch
                                      (ConsensusMode CardanoMode -> AnyConsensusMode
forall mode. ConsensusMode mode -> AnyConsensusMode
AnyConsensusMode ConsensusMode CardanoMode
CardanoMode)
                                      (CardanoEra era
-> (IsCardanoEra era => AnyCardanoEra) -> AnyCardanoEra
forall era a. CardanoEra era -> (IsCardanoEra era => a) -> a
getIsCardanoEraConstraint CardanoEra era
era ((IsCardanoEra era => AnyCardanoEra) -> AnyCardanoEra)
-> (IsCardanoEra era => AnyCardanoEra) -> AnyCardanoEra
forall a b. (a -> b) -> a -> b
$ CardanoEra era -> AnyCardanoEra
forall era. IsCardanoEra era => CardanoEra era -> AnyCardanoEra
AnyCardanoEra CardanoEra era
era)
 where
  execQuery :: IO (Either QueryConvenienceError result)
  execQuery :: IO (Either QueryConvenienceError result)
execQuery = Either AcquiringFailure (Either EraMismatch result)
-> Either QueryConvenienceError result
forall a.
Either AcquiringFailure (Either EraMismatch a)
-> Either QueryConvenienceError a
collapse (Either AcquiringFailure (Either EraMismatch result)
 -> Either QueryConvenienceError result)
-> IO (Either AcquiringFailure (Either EraMismatch result))
-> IO (Either QueryConvenienceError result)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LocalNodeConnectInfo mode
-> Maybe ChainPoint
-> QueryInMode mode (Either EraMismatch result)
-> IO (Either AcquiringFailure (Either EraMismatch result))
forall mode result.
LocalNodeConnectInfo mode
-> Maybe ChainPoint
-> QueryInMode mode result
-> IO (Either AcquiringFailure result)
queryNodeLocalState LocalNodeConnectInfo mode
localNodeConnInfo Maybe ChainPoint
forall a. Maybe a
Nothing QueryInMode mode (Either EraMismatch result)
q

collapse
  :: Either AcquiringFailure (Either EraMismatch a)
  -> Either QueryConvenienceError a
collapse :: Either AcquiringFailure (Either EraMismatch a)
-> Either QueryConvenienceError a
collapse Either AcquiringFailure (Either EraMismatch a)
res = do
 Either EraMismatch a
innerRes <- (AcquiringFailure -> QueryConvenienceError)
-> Either AcquiringFailure (Either EraMismatch a)
-> Either QueryConvenienceError (Either EraMismatch a)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first AcquiringFailure -> QueryConvenienceError
AcqFailure Either AcquiringFailure (Either EraMismatch a)
res
 (EraMismatch -> QueryConvenienceError)
-> Either EraMismatch a -> Either QueryConvenienceError a
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first EraMismatch -> QueryConvenienceError
QueryEraMismatch Either EraMismatch a
innerRes