{-# LANGUAGE DataKinds        #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MonoLocalBinds   #-}
{-# LANGUAGE RankNTypes       #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators    #-}
module Plutus.ChainIndex.Server(
    serveChainIndexQueryServer,
    serveChainIndex) where

import Control.Monad ((>=>))
import Control.Monad.Except qualified as E
import Control.Monad.Freer (Eff, Member, type (~>))
import Control.Monad.Freer.Error (Error, runError, throwError)
import Control.Monad.Freer.Extras.Modify (raiseEnd)
import Control.Monad.IO.Class (MonadIO (liftIO))
import Data.ByteString.Lazy qualified as BSL
import Data.Default (Default (def))
import Data.Maybe (fromMaybe)
import Data.Proxy (Proxy (..))
import Data.Text qualified as Text
import Data.Text.Encoding qualified as Text
import Network.Wai.Handler.Warp qualified as Warp
import Plutus.ChainIndex (RunRequirements, runChainIndexEffects)
import Plutus.ChainIndex.Api (API, FromHashAPI, FullAPI, QueryAtAddressRequest (QueryAtAddressRequest),
                              TxoAtAddressRequest (TxoAtAddressRequest), UtxoAtAddressRequest (UtxoAtAddressRequest),
                              UtxoWithCurrencyRequest (UtxoWithCurrencyRequest), swagger)
import Plutus.ChainIndex.Effects (ChainIndexControlEffect, ChainIndexQueryEffect)
import Plutus.ChainIndex.Effects qualified as E
import Servant.API ((:<|>) (..))
import Servant.API.ContentTypes (NoContent (..))
import Servant.Server (Handler, ServerError, ServerT, err404, err500, errBody, hoistServer, serve)

serveChainIndexQueryServer ::
    Int -- ^ Port
    -> RunRequirements
    -> IO ()
serveChainIndexQueryServer :: Int -> RunRequirements -> IO ()
serveChainIndexQueryServer Int
port RunRequirements
runReq = do
    let server :: ServerT API Handler
server = Proxy API
-> (forall x.
    Eff
      '[Error ServerError, ChainIndexQueryEffect,
        ChainIndexControlEffect]
      x
    -> Handler x)
-> ServerT
     API
     (Eff
        '[Error ServerError, ChainIndexQueryEffect,
          ChainIndexControlEffect])
-> ServerT API Handler
forall api (m :: * -> *) (n :: * -> *).
HasServer api '[] =>
Proxy api
-> (forall x. m x -> n x) -> ServerT api m -> ServerT api n
hoistServer (Proxy API
forall k (t :: k). Proxy t
Proxy @API) (RunRequirements
-> forall x.
   Eff
     '[Error ServerError, ChainIndexQueryEffect,
       ChainIndexControlEffect]
     x
   -> Handler x
runChainIndexQuery RunRequirements
runReq) ServerT
  API
  (Eff
     '[Error ServerError, ChainIndexQueryEffect,
       ChainIndexControlEffect])
forall (effs :: [* -> *]).
(Member (Error ServerError) effs,
 Member ChainIndexQueryEffect effs,
 Member ChainIndexControlEffect effs) =>
ServerT API (Eff effs)
serveChainIndex
    Int -> Application -> IO ()
Warp.run Int
port (Proxy FullAPI -> Server FullAPI -> Application
forall api.
HasServer api '[] =>
Proxy api -> Server api -> Application
serve (Proxy FullAPI
forall k (t :: k). Proxy t
Proxy @FullAPI) (Handler NoContent
:<|> (((DatumHash -> Handler Datum)
       :<|> ((ValidatorHash -> Handler (Versioned Validator))
             :<|> ((MintingPolicyHash -> Handler (Versioned MintingPolicy))
                   :<|> ((StakeValidatorHash -> Handler (Versioned StakeValidator))
                         :<|> (RedeemerHash -> Handler Redeemer)))))
      :<|> ((TxOutRef -> Handler DecoratedTxOut)
            :<|> ((TxOutRef -> Handler DecoratedTxOut)
                  :<|> ((TxId -> Handler ChainIndexTx)
                        :<|> ((TxOutRef -> Handler IsUtxoResponse)
                              :<|> ((UtxoAtAddressRequest -> Handler UtxosResponse)
                                    :<|> ((QueryAtAddressRequest
                                           -> Handler (QueryResponse [(TxOutRef, DecoratedTxOut)]))
                                          :<|> ((QueryAtAddressRequest
                                                 -> Handler (QueryResponse [Datum]))
                                                :<|> ((UtxoWithCurrencyRequest
                                                       -> Handler UtxosResponse)
                                                      :<|> (([TxId] -> Handler [ChainIndexTx])
                                                            :<|> ((TxoAtAddressRequest
                                                                   -> Handler TxosResponse)
                                                                  :<|> (Handler Tip
                                                                        :<|> (Handler NoContent
                                                                              :<|> Handler
                                                                                     Diagnostics)))))))))))))
ServerT API Handler
server (Handler NoContent
 :<|> (((DatumHash -> Handler Datum)
        :<|> ((ValidatorHash -> Handler (Versioned Validator))
              :<|> ((MintingPolicyHash -> Handler (Versioned MintingPolicy))
                    :<|> ((StakeValidatorHash -> Handler (Versioned StakeValidator))
                          :<|> (RedeemerHash -> Handler Redeemer)))))
       :<|> ((TxOutRef -> Handler DecoratedTxOut)
             :<|> ((TxOutRef -> Handler DecoratedTxOut)
                   :<|> ((TxId -> Handler ChainIndexTx)
                         :<|> ((TxOutRef -> Handler IsUtxoResponse)
                               :<|> ((UtxoAtAddressRequest -> Handler UtxosResponse)
                                     :<|> ((QueryAtAddressRequest
                                            -> Handler (QueryResponse [(TxOutRef, DecoratedTxOut)]))
                                           :<|> ((QueryAtAddressRequest
                                                  -> Handler (QueryResponse [Datum]))
                                                 :<|> ((UtxoWithCurrencyRequest
                                                        -> Handler UtxosResponse)
                                                       :<|> (([TxId] -> Handler [ChainIndexTx])
                                                             :<|> ((TxoAtAddressRequest
                                                                    -> Handler TxosResponse)
                                                                   :<|> (Handler Tip
                                                                         :<|> (Handler NoContent
                                                                               :<|> Handler
                                                                                      Diagnostics))))))))))))))
-> (Handler Value
    :<|> (Handler
            (SwaggerUiHtml "swagger-ui" ("swagger.json" :> Get '[JSON] Value))
          :<|> (Handler
                  (SwaggerUiHtml "swagger-ui" ("swagger.json" :> Get '[JSON] Value))
                :<|> Tagged Handler Application)))
-> (Handler NoContent
    :<|> (((DatumHash -> Handler Datum)
           :<|> ((ValidatorHash -> Handler (Versioned Validator))
                 :<|> ((MintingPolicyHash -> Handler (Versioned MintingPolicy))
                       :<|> ((StakeValidatorHash -> Handler (Versioned StakeValidator))
                             :<|> (RedeemerHash -> Handler Redeemer)))))
          :<|> ((TxOutRef -> Handler DecoratedTxOut)
                :<|> ((TxOutRef -> Handler DecoratedTxOut)
                      :<|> ((TxId -> Handler ChainIndexTx)
                            :<|> ((TxOutRef -> Handler IsUtxoResponse)
                                  :<|> ((UtxoAtAddressRequest -> Handler UtxosResponse)
                                        :<|> ((QueryAtAddressRequest
                                               -> Handler
                                                    (QueryResponse [(TxOutRef, DecoratedTxOut)]))
                                              :<|> ((QueryAtAddressRequest
                                                     -> Handler (QueryResponse [Datum]))
                                                    :<|> ((UtxoWithCurrencyRequest
                                                           -> Handler UtxosResponse)
                                                          :<|> (([TxId] -> Handler [ChainIndexTx])
                                                                :<|> ((TxoAtAddressRequest
                                                                       -> Handler TxosResponse)
                                                                      :<|> (Handler Tip
                                                                            :<|> (Handler NoContent
                                                                                  :<|> Handler
                                                                                         Diagnostics))))))))))))))
   :<|> (Handler Value
         :<|> (Handler
                 (SwaggerUiHtml "swagger-ui" ("swagger.json" :> Get '[JSON] Value))
               :<|> (Handler
                       (SwaggerUiHtml "swagger-ui" ("swagger.json" :> Get '[JSON] Value))
                     :<|> Tagged Handler Application)))
forall a b. a -> b -> a :<|> b
:<|> Handler Value
:<|> (Handler
        (SwaggerUiHtml "swagger-ui" ("swagger.json" :> Get '[JSON] Value))
      :<|> (Handler
              (SwaggerUiHtml "swagger-ui" ("swagger.json" :> Get '[JSON] Value))
            :<|> Tagged Handler Application))
forall (dir :: Symbol) api.
(Server api ~ Handler Value) =>
Server (SwaggerSchemaUI' dir api)
swagger))

runChainIndexQuery ::
    RunRequirements
    -> Eff '[Error ServerError, ChainIndexQueryEffect, ChainIndexControlEffect] ~> Handler
runChainIndexQuery :: RunRequirements
-> forall x.
   Eff
     '[Error ServerError, ChainIndexQueryEffect,
       ChainIndexControlEffect]
     x
   -> Handler x
runChainIndexQuery RunRequirements
runReq Eff
  '[Error ServerError, ChainIndexQueryEffect,
    ChainIndexControlEffect]
  x
action = do
    Either ChainIndexError (Either ServerError x)
result <- IO (Either ChainIndexError (Either ServerError x))
-> Handler (Either ChainIndexError (Either ServerError x))
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Either ChainIndexError (Either ServerError x))
 -> Handler (Either ChainIndexError (Either ServerError x)))
-> IO (Either ChainIndexError (Either ServerError x))
-> Handler (Either ChainIndexError (Either ServerError x))
forall a b. (a -> b) -> a -> b
$ RunRequirements
-> Eff
     '[ChainIndexQueryEffect, ChainIndexControlEffect,
       BeamEffect Sqlite]
     (Either ServerError x)
-> IO (Either ChainIndexError (Either ServerError x))
forall a.
RunRequirements
-> Eff
     '[ChainIndexQueryEffect, ChainIndexControlEffect,
       BeamEffect Sqlite]
     a
-> IO (Either ChainIndexError a)
runChainIndexEffects RunRequirements
runReq (Eff
   '[ChainIndexQueryEffect, ChainIndexControlEffect,
     BeamEffect Sqlite]
   (Either ServerError x)
 -> IO (Either ChainIndexError (Either ServerError x)))
-> Eff
     '[ChainIndexQueryEffect, ChainIndexControlEffect,
       BeamEffect Sqlite]
     (Either ServerError x)
-> IO (Either ChainIndexError (Either ServerError x))
forall a b. (a -> b) -> a -> b
$ Eff
  '[Error ServerError, ChainIndexQueryEffect,
    ChainIndexControlEffect, BeamEffect Sqlite]
  x
-> Eff
     '[ChainIndexQueryEffect, ChainIndexControlEffect,
       BeamEffect Sqlite]
     (Either ServerError x)
forall e (effs :: [* -> *]) a.
Eff (Error e : effs) a -> Eff effs (Either e a)
runError (Eff
   '[Error ServerError, ChainIndexQueryEffect,
     ChainIndexControlEffect, BeamEffect Sqlite]
   x
 -> Eff
      '[ChainIndexQueryEffect, ChainIndexControlEffect,
        BeamEffect Sqlite]
      (Either ServerError x))
-> Eff
     '[Error ServerError, ChainIndexQueryEffect,
       ChainIndexControlEffect, BeamEffect Sqlite]
     x
-> Eff
     '[ChainIndexQueryEffect, ChainIndexControlEffect,
       BeamEffect Sqlite]
     (Either ServerError x)
forall a b. (a -> b) -> a -> b
$ Eff
  '[Error ServerError, ChainIndexQueryEffect,
    ChainIndexControlEffect]
  x
-> Eff
     '[Error ServerError, ChainIndexQueryEffect,
       ChainIndexControlEffect, BeamEffect Sqlite]
     x
forall (effs :: [* -> *]) (as :: [* -> *]).
CanWeakenEnd as effs =>
Eff as ~> Eff effs
raiseEnd Eff
  '[Error ServerError, ChainIndexQueryEffect,
    ChainIndexControlEffect]
  x
action
    case Either ChainIndexError (Either ServerError x)
result of
        Right (Right x
a) -> x -> Handler x
forall (f :: * -> *) a. Applicative f => a -> f a
pure x
a
        Right (Left ServerError
e) -> ServerError -> Handler x
forall e (m :: * -> *) a. MonadError e m => e -> m a
E.throwError ServerError
e
        Left ChainIndexError
e' ->
            let err :: ServerError
err = ServerError
err500 { errBody :: ByteString
errBody = ByteString -> ByteString
BSL.fromStrict (ByteString -> ByteString) -> ByteString -> ByteString
forall a b. (a -> b) -> a -> b
$ Text -> ByteString
Text.encodeUtf8 (Text -> ByteString) -> Text -> ByteString
forall a b. (a -> b) -> a -> b
$ String -> Text
Text.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ ChainIndexError -> String
forall a. Show a => a -> String
show ChainIndexError
e' } in
            ServerError -> Handler x
forall e (m :: * -> *) a. MonadError e m => e -> m a
E.throwError ServerError
err

serveChainIndex ::
    forall effs.
    ( Member (Error ServerError) effs
    , Member ChainIndexQueryEffect effs
    , Member ChainIndexControlEffect effs
    )
    => ServerT API (Eff effs)
serveChainIndex :: ServerT API (Eff effs)
serveChainIndex =
    NoContent -> Eff effs NoContent
forall (f :: * -> *) a. Applicative f => a -> f a
pure NoContent
NoContent
    Eff effs NoContent
-> (((DatumHash -> Eff effs Datum)
     :<|> ((ValidatorHash -> Eff effs (Versioned Validator))
           :<|> ((MintingPolicyHash -> Eff effs (Versioned MintingPolicy))
                 :<|> ((StakeValidatorHash -> Eff effs (Versioned StakeValidator))
                       :<|> (RedeemerHash -> Eff effs Redeemer)))))
    :<|> ((TxOutRef -> Eff effs DecoratedTxOut)
          :<|> ((TxOutRef -> Eff effs DecoratedTxOut)
                :<|> ((TxId -> Eff effs ChainIndexTx)
                      :<|> ((TxOutRef -> Eff effs IsUtxoResponse)
                            :<|> ((UtxoAtAddressRequest -> Eff effs UtxosResponse)
                                  :<|> ((QueryAtAddressRequest
                                         -> Eff effs (QueryResponse [(TxOutRef, DecoratedTxOut)]))
                                        :<|> ((QueryAtAddressRequest
                                               -> Eff effs (QueryResponse [Datum]))
                                              :<|> ((UtxoWithCurrencyRequest
                                                     -> Eff effs UtxosResponse)
                                                    :<|> (([TxId] -> Eff effs [ChainIndexTx])
                                                          :<|> ((TxoAtAddressRequest
                                                                 -> Eff effs TxosResponse)
                                                                :<|> (Eff effs Tip
                                                                      :<|> (Eff effs NoContent
                                                                            :<|> Eff
                                                                                   effs
                                                                                   Diagnostics)))))))))))))
-> Eff effs NoContent
   :<|> (((DatumHash -> Eff effs Datum)
          :<|> ((ValidatorHash -> Eff effs (Versioned Validator))
                :<|> ((MintingPolicyHash -> Eff effs (Versioned MintingPolicy))
                      :<|> ((StakeValidatorHash -> Eff effs (Versioned StakeValidator))
                            :<|> (RedeemerHash -> Eff effs Redeemer)))))
         :<|> ((TxOutRef -> Eff effs DecoratedTxOut)
               :<|> ((TxOutRef -> Eff effs DecoratedTxOut)
                     :<|> ((TxId -> Eff effs ChainIndexTx)
                           :<|> ((TxOutRef -> Eff effs IsUtxoResponse)
                                 :<|> ((UtxoAtAddressRequest -> Eff effs UtxosResponse)
                                       :<|> ((QueryAtAddressRequest
                                              -> Eff
                                                   effs
                                                   (QueryResponse [(TxOutRef, DecoratedTxOut)]))
                                             :<|> ((QueryAtAddressRequest
                                                    -> Eff effs (QueryResponse [Datum]))
                                                   :<|> ((UtxoWithCurrencyRequest
                                                          -> Eff effs UtxosResponse)
                                                         :<|> (([TxId] -> Eff effs [ChainIndexTx])
                                                               :<|> ((TxoAtAddressRequest
                                                                      -> Eff effs TxosResponse)
                                                                     :<|> (Eff effs Tip
                                                                           :<|> (Eff effs NoContent
                                                                                 :<|> Eff
                                                                                        effs
                                                                                        Diagnostics)))))))))))))
forall a b. a -> b -> a :<|> b
:<|> (DatumHash -> Eff effs Datum)
:<|> ((ValidatorHash -> Eff effs (Versioned Validator))
      :<|> ((MintingPolicyHash -> Eff effs (Versioned MintingPolicy))
            :<|> ((StakeValidatorHash -> Eff effs (Versioned StakeValidator))
                  :<|> (RedeemerHash -> Eff effs Redeemer))))
forall (effs :: [* -> *]).
(Member (Error ServerError) effs,
 Member ChainIndexQueryEffect effs) =>
ServerT FromHashAPI (Eff effs)
serveFromHashApi
    ((DatumHash -> Eff effs Datum)
 :<|> ((ValidatorHash -> Eff effs (Versioned Validator))
       :<|> ((MintingPolicyHash -> Eff effs (Versioned MintingPolicy))
             :<|> ((StakeValidatorHash -> Eff effs (Versioned StakeValidator))
                   :<|> (RedeemerHash -> Eff effs Redeemer)))))
-> ((TxOutRef -> Eff effs DecoratedTxOut)
    :<|> ((TxOutRef -> Eff effs DecoratedTxOut)
          :<|> ((TxId -> Eff effs ChainIndexTx)
                :<|> ((TxOutRef -> Eff effs IsUtxoResponse)
                      :<|> ((UtxoAtAddressRequest -> Eff effs UtxosResponse)
                            :<|> ((QueryAtAddressRequest
                                   -> Eff effs (QueryResponse [(TxOutRef, DecoratedTxOut)]))
                                  :<|> ((QueryAtAddressRequest -> Eff effs (QueryResponse [Datum]))
                                        :<|> ((UtxoWithCurrencyRequest -> Eff effs UtxosResponse)
                                              :<|> (([TxId] -> Eff effs [ChainIndexTx])
                                                    :<|> ((TxoAtAddressRequest
                                                           -> Eff effs TxosResponse)
                                                          :<|> (Eff effs Tip
                                                                :<|> (Eff effs NoContent
                                                                      :<|> Eff
                                                                             effs
                                                                             Diagnostics))))))))))))
-> ((DatumHash -> Eff effs Datum)
    :<|> ((ValidatorHash -> Eff effs (Versioned Validator))
          :<|> ((MintingPolicyHash -> Eff effs (Versioned MintingPolicy))
                :<|> ((StakeValidatorHash -> Eff effs (Versioned StakeValidator))
                      :<|> (RedeemerHash -> Eff effs Redeemer)))))
   :<|> ((TxOutRef -> Eff effs DecoratedTxOut)
         :<|> ((TxOutRef -> Eff effs DecoratedTxOut)
               :<|> ((TxId -> Eff effs ChainIndexTx)
                     :<|> ((TxOutRef -> Eff effs IsUtxoResponse)
                           :<|> ((UtxoAtAddressRequest -> Eff effs UtxosResponse)
                                 :<|> ((QueryAtAddressRequest
                                        -> Eff effs (QueryResponse [(TxOutRef, DecoratedTxOut)]))
                                       :<|> ((QueryAtAddressRequest
                                              -> Eff effs (QueryResponse [Datum]))
                                             :<|> ((UtxoWithCurrencyRequest
                                                    -> Eff effs UtxosResponse)
                                                   :<|> (([TxId] -> Eff effs [ChainIndexTx])
                                                         :<|> ((TxoAtAddressRequest
                                                                -> Eff effs TxosResponse)
                                                               :<|> (Eff effs Tip
                                                                     :<|> (Eff effs NoContent
                                                                           :<|> Eff
                                                                                  effs
                                                                                  Diagnostics))))))))))))
forall a b. a -> b -> a :<|> b
:<|> (TxOutRef -> Eff effs (Maybe DecoratedTxOut)
forall (effs :: [* -> *]).
Member ChainIndexQueryEffect effs =>
TxOutRef -> Eff effs (Maybe DecoratedTxOut)
E.txOutFromRef (TxOutRef -> Eff effs (Maybe DecoratedTxOut))
-> (Maybe DecoratedTxOut -> Eff effs DecoratedTxOut)
-> TxOutRef
-> Eff effs DecoratedTxOut
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Maybe DecoratedTxOut -> Eff effs DecoratedTxOut
forall (effs :: [* -> *]).
Member (Error ServerError) effs =>
Maybe ~> Eff effs
handleMaybe)
    (TxOutRef -> Eff effs DecoratedTxOut)
-> ((TxOutRef -> Eff effs DecoratedTxOut)
    :<|> ((TxId -> Eff effs ChainIndexTx)
          :<|> ((TxOutRef -> Eff effs IsUtxoResponse)
                :<|> ((UtxoAtAddressRequest -> Eff effs UtxosResponse)
                      :<|> ((QueryAtAddressRequest
                             -> Eff effs (QueryResponse [(TxOutRef, DecoratedTxOut)]))
                            :<|> ((QueryAtAddressRequest -> Eff effs (QueryResponse [Datum]))
                                  :<|> ((UtxoWithCurrencyRequest -> Eff effs UtxosResponse)
                                        :<|> (([TxId] -> Eff effs [ChainIndexTx])
                                              :<|> ((TxoAtAddressRequest -> Eff effs TxosResponse)
                                                    :<|> (Eff effs Tip
                                                          :<|> (Eff effs NoContent
                                                                :<|> Eff effs Diagnostics)))))))))))
-> (TxOutRef -> Eff effs DecoratedTxOut)
   :<|> ((TxOutRef -> Eff effs DecoratedTxOut)
         :<|> ((TxId -> Eff effs ChainIndexTx)
               :<|> ((TxOutRef -> Eff effs IsUtxoResponse)
                     :<|> ((UtxoAtAddressRequest -> Eff effs UtxosResponse)
                           :<|> ((QueryAtAddressRequest
                                  -> Eff effs (QueryResponse [(TxOutRef, DecoratedTxOut)]))
                                 :<|> ((QueryAtAddressRequest -> Eff effs (QueryResponse [Datum]))
                                       :<|> ((UtxoWithCurrencyRequest -> Eff effs UtxosResponse)
                                             :<|> (([TxId] -> Eff effs [ChainIndexTx])
                                                   :<|> ((TxoAtAddressRequest
                                                          -> Eff effs TxosResponse)
                                                         :<|> (Eff effs Tip
                                                               :<|> (Eff effs NoContent
                                                                     :<|> Eff
                                                                            effs
                                                                            Diagnostics)))))))))))
forall a b. a -> b -> a :<|> b
:<|> (TxOutRef -> Eff effs (Maybe DecoratedTxOut)
forall (effs :: [* -> *]).
Member ChainIndexQueryEffect effs =>
TxOutRef -> Eff effs (Maybe DecoratedTxOut)
E.unspentTxOutFromRef (TxOutRef -> Eff effs (Maybe DecoratedTxOut))
-> (Maybe DecoratedTxOut -> Eff effs DecoratedTxOut)
-> TxOutRef
-> Eff effs DecoratedTxOut
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Maybe DecoratedTxOut -> Eff effs DecoratedTxOut
forall (effs :: [* -> *]).
Member (Error ServerError) effs =>
Maybe ~> Eff effs
handleMaybe)
    (TxOutRef -> Eff effs DecoratedTxOut)
-> ((TxId -> Eff effs ChainIndexTx)
    :<|> ((TxOutRef -> Eff effs IsUtxoResponse)
          :<|> ((UtxoAtAddressRequest -> Eff effs UtxosResponse)
                :<|> ((QueryAtAddressRequest
                       -> Eff effs (QueryResponse [(TxOutRef, DecoratedTxOut)]))
                      :<|> ((QueryAtAddressRequest -> Eff effs (QueryResponse [Datum]))
                            :<|> ((UtxoWithCurrencyRequest -> Eff effs UtxosResponse)
                                  :<|> (([TxId] -> Eff effs [ChainIndexTx])
                                        :<|> ((TxoAtAddressRequest -> Eff effs TxosResponse)
                                              :<|> (Eff effs Tip
                                                    :<|> (Eff effs NoContent
                                                          :<|> Eff effs Diagnostics))))))))))
-> (TxOutRef -> Eff effs DecoratedTxOut)
   :<|> ((TxId -> Eff effs ChainIndexTx)
         :<|> ((TxOutRef -> Eff effs IsUtxoResponse)
               :<|> ((UtxoAtAddressRequest -> Eff effs UtxosResponse)
                     :<|> ((QueryAtAddressRequest
                            -> Eff effs (QueryResponse [(TxOutRef, DecoratedTxOut)]))
                           :<|> ((QueryAtAddressRequest -> Eff effs (QueryResponse [Datum]))
                                 :<|> ((UtxoWithCurrencyRequest -> Eff effs UtxosResponse)
                                       :<|> (([TxId] -> Eff effs [ChainIndexTx])
                                             :<|> ((TxoAtAddressRequest -> Eff effs TxosResponse)
                                                   :<|> (Eff effs Tip
                                                         :<|> (Eff effs NoContent
                                                               :<|> Eff effs Diagnostics))))))))))
forall a b. a -> b -> a :<|> b
:<|> (TxId -> Eff effs (Maybe ChainIndexTx)
forall (effs :: [* -> *]).
Member ChainIndexQueryEffect effs =>
TxId -> Eff effs (Maybe ChainIndexTx)
E.txFromTxId (TxId -> Eff effs (Maybe ChainIndexTx))
-> (Maybe ChainIndexTx -> Eff effs ChainIndexTx)
-> TxId
-> Eff effs ChainIndexTx
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Maybe ChainIndexTx -> Eff effs ChainIndexTx
forall (effs :: [* -> *]).
Member (Error ServerError) effs =>
Maybe ~> Eff effs
handleMaybe)
    (TxId -> Eff effs ChainIndexTx)
-> ((TxOutRef -> Eff effs IsUtxoResponse)
    :<|> ((UtxoAtAddressRequest -> Eff effs UtxosResponse)
          :<|> ((QueryAtAddressRequest
                 -> Eff effs (QueryResponse [(TxOutRef, DecoratedTxOut)]))
                :<|> ((QueryAtAddressRequest -> Eff effs (QueryResponse [Datum]))
                      :<|> ((UtxoWithCurrencyRequest -> Eff effs UtxosResponse)
                            :<|> (([TxId] -> Eff effs [ChainIndexTx])
                                  :<|> ((TxoAtAddressRequest -> Eff effs TxosResponse)
                                        :<|> (Eff effs Tip
                                              :<|> (Eff effs NoContent
                                                    :<|> Eff effs Diagnostics)))))))))
-> (TxId -> Eff effs ChainIndexTx)
   :<|> ((TxOutRef -> Eff effs IsUtxoResponse)
         :<|> ((UtxoAtAddressRequest -> Eff effs UtxosResponse)
               :<|> ((QueryAtAddressRequest
                      -> Eff effs (QueryResponse [(TxOutRef, DecoratedTxOut)]))
                     :<|> ((QueryAtAddressRequest -> Eff effs (QueryResponse [Datum]))
                           :<|> ((UtxoWithCurrencyRequest -> Eff effs UtxosResponse)
                                 :<|> (([TxId] -> Eff effs [ChainIndexTx])
                                       :<|> ((TxoAtAddressRequest -> Eff effs TxosResponse)
                                             :<|> (Eff effs Tip
                                                   :<|> (Eff effs NoContent
                                                         :<|> Eff effs Diagnostics)))))))))
forall a b. a -> b -> a :<|> b
:<|> TxOutRef -> Eff effs IsUtxoResponse
forall (effs :: [* -> *]).
Member ChainIndexQueryEffect effs =>
TxOutRef -> Eff effs IsUtxoResponse
E.utxoSetMembership
    (TxOutRef -> Eff effs IsUtxoResponse)
-> ((UtxoAtAddressRequest -> Eff effs UtxosResponse)
    :<|> ((QueryAtAddressRequest
           -> Eff effs (QueryResponse [(TxOutRef, DecoratedTxOut)]))
          :<|> ((QueryAtAddressRequest -> Eff effs (QueryResponse [Datum]))
                :<|> ((UtxoWithCurrencyRequest -> Eff effs UtxosResponse)
                      :<|> (([TxId] -> Eff effs [ChainIndexTx])
                            :<|> ((TxoAtAddressRequest -> Eff effs TxosResponse)
                                  :<|> (Eff effs Tip
                                        :<|> (Eff effs NoContent :<|> Eff effs Diagnostics))))))))
-> (TxOutRef -> Eff effs IsUtxoResponse)
   :<|> ((UtxoAtAddressRequest -> Eff effs UtxosResponse)
         :<|> ((QueryAtAddressRequest
                -> Eff effs (QueryResponse [(TxOutRef, DecoratedTxOut)]))
               :<|> ((QueryAtAddressRequest -> Eff effs (QueryResponse [Datum]))
                     :<|> ((UtxoWithCurrencyRequest -> Eff effs UtxosResponse)
                           :<|> (([TxId] -> Eff effs [ChainIndexTx])
                                 :<|> ((TxoAtAddressRequest -> Eff effs TxosResponse)
                                       :<|> (Eff effs Tip
                                             :<|> (Eff effs NoContent
                                                   :<|> Eff effs Diagnostics))))))))
forall a b. a -> b -> a :<|> b
:<|> (\(UtxoAtAddressRequest Maybe (PageQuery TxOutRef)
pq Credential
c) -> PageQuery TxOutRef -> Credential -> Eff effs UtxosResponse
forall (effs :: [* -> *]).
Member ChainIndexQueryEffect effs =>
PageQuery TxOutRef -> Credential -> Eff effs UtxosResponse
E.utxoSetAtAddress (PageQuery TxOutRef
-> Maybe (PageQuery TxOutRef) -> PageQuery TxOutRef
forall a. a -> Maybe a -> a
fromMaybe PageQuery TxOutRef
forall a. Default a => a
def Maybe (PageQuery TxOutRef)
pq) Credential
c)
    (UtxoAtAddressRequest -> Eff effs UtxosResponse)
-> ((QueryAtAddressRequest
     -> Eff effs (QueryResponse [(TxOutRef, DecoratedTxOut)]))
    :<|> ((QueryAtAddressRequest -> Eff effs (QueryResponse [Datum]))
          :<|> ((UtxoWithCurrencyRequest -> Eff effs UtxosResponse)
                :<|> (([TxId] -> Eff effs [ChainIndexTx])
                      :<|> ((TxoAtAddressRequest -> Eff effs TxosResponse)
                            :<|> (Eff effs Tip
                                  :<|> (Eff effs NoContent :<|> Eff effs Diagnostics)))))))
-> (UtxoAtAddressRequest -> Eff effs UtxosResponse)
   :<|> ((QueryAtAddressRequest
          -> Eff effs (QueryResponse [(TxOutRef, DecoratedTxOut)]))
         :<|> ((QueryAtAddressRequest -> Eff effs (QueryResponse [Datum]))
               :<|> ((UtxoWithCurrencyRequest -> Eff effs UtxosResponse)
                     :<|> (([TxId] -> Eff effs [ChainIndexTx])
                           :<|> ((TxoAtAddressRequest -> Eff effs TxosResponse)
                                 :<|> (Eff effs Tip
                                       :<|> (Eff effs NoContent :<|> Eff effs Diagnostics)))))))
forall a b. a -> b -> a :<|> b
:<|> (\(QueryAtAddressRequest Maybe (PageQuery TxOutRef)
pq Credential
c) -> PageQuery TxOutRef
-> Credential
-> Eff effs (QueryResponse [(TxOutRef, DecoratedTxOut)])
forall (effs :: [* -> *]).
Member ChainIndexQueryEffect effs =>
PageQuery TxOutRef
-> Credential
-> Eff effs (QueryResponse [(TxOutRef, DecoratedTxOut)])
E.unspentTxOutSetAtAddress (PageQuery TxOutRef
-> Maybe (PageQuery TxOutRef) -> PageQuery TxOutRef
forall a. a -> Maybe a -> a
fromMaybe PageQuery TxOutRef
forall a. Default a => a
def Maybe (PageQuery TxOutRef)
pq) Credential
c)
    (QueryAtAddressRequest
 -> Eff effs (QueryResponse [(TxOutRef, DecoratedTxOut)]))
-> ((QueryAtAddressRequest -> Eff effs (QueryResponse [Datum]))
    :<|> ((UtxoWithCurrencyRequest -> Eff effs UtxosResponse)
          :<|> (([TxId] -> Eff effs [ChainIndexTx])
                :<|> ((TxoAtAddressRequest -> Eff effs TxosResponse)
                      :<|> (Eff effs Tip
                            :<|> (Eff effs NoContent :<|> Eff effs Diagnostics))))))
-> (QueryAtAddressRequest
    -> Eff effs (QueryResponse [(TxOutRef, DecoratedTxOut)]))
   :<|> ((QueryAtAddressRequest -> Eff effs (QueryResponse [Datum]))
         :<|> ((UtxoWithCurrencyRequest -> Eff effs UtxosResponse)
               :<|> (([TxId] -> Eff effs [ChainIndexTx])
                     :<|> ((TxoAtAddressRequest -> Eff effs TxosResponse)
                           :<|> (Eff effs Tip
                                 :<|> (Eff effs NoContent :<|> Eff effs Diagnostics))))))
forall a b. a -> b -> a :<|> b
:<|> (\(QueryAtAddressRequest Maybe (PageQuery TxOutRef)
pq Credential
c) -> PageQuery TxOutRef
-> Credential -> Eff effs (QueryResponse [Datum])
forall (effs :: [* -> *]).
Member ChainIndexQueryEffect effs =>
PageQuery TxOutRef
-> Credential -> Eff effs (QueryResponse [Datum])
E.datumsAtAddress (PageQuery TxOutRef
-> Maybe (PageQuery TxOutRef) -> PageQuery TxOutRef
forall a. a -> Maybe a -> a
fromMaybe PageQuery TxOutRef
forall a. Default a => a
def Maybe (PageQuery TxOutRef)
pq) Credential
c)
    (QueryAtAddressRequest -> Eff effs (QueryResponse [Datum]))
-> ((UtxoWithCurrencyRequest -> Eff effs UtxosResponse)
    :<|> (([TxId] -> Eff effs [ChainIndexTx])
          :<|> ((TxoAtAddressRequest -> Eff effs TxosResponse)
                :<|> (Eff effs Tip
                      :<|> (Eff effs NoContent :<|> Eff effs Diagnostics)))))
-> (QueryAtAddressRequest -> Eff effs (QueryResponse [Datum]))
   :<|> ((UtxoWithCurrencyRequest -> Eff effs UtxosResponse)
         :<|> (([TxId] -> Eff effs [ChainIndexTx])
               :<|> ((TxoAtAddressRequest -> Eff effs TxosResponse)
                     :<|> (Eff effs Tip
                           :<|> (Eff effs NoContent :<|> Eff effs Diagnostics)))))
forall a b. a -> b -> a :<|> b
:<|> (\(UtxoWithCurrencyRequest Maybe (PageQuery TxOutRef)
pq AssetClass
c) -> PageQuery TxOutRef -> AssetClass -> Eff effs UtxosResponse
forall (effs :: [* -> *]).
Member ChainIndexQueryEffect effs =>
PageQuery TxOutRef -> AssetClass -> Eff effs UtxosResponse
E.utxoSetWithCurrency (PageQuery TxOutRef
-> Maybe (PageQuery TxOutRef) -> PageQuery TxOutRef
forall a. a -> Maybe a -> a
fromMaybe PageQuery TxOutRef
forall a. Default a => a
def Maybe (PageQuery TxOutRef)
pq) AssetClass
c)
    (UtxoWithCurrencyRequest -> Eff effs UtxosResponse)
-> (([TxId] -> Eff effs [ChainIndexTx])
    :<|> ((TxoAtAddressRequest -> Eff effs TxosResponse)
          :<|> (Eff effs Tip
                :<|> (Eff effs NoContent :<|> Eff effs Diagnostics))))
-> (UtxoWithCurrencyRequest -> Eff effs UtxosResponse)
   :<|> (([TxId] -> Eff effs [ChainIndexTx])
         :<|> ((TxoAtAddressRequest -> Eff effs TxosResponse)
               :<|> (Eff effs Tip
                     :<|> (Eff effs NoContent :<|> Eff effs Diagnostics))))
forall a b. a -> b -> a :<|> b
:<|> [TxId] -> Eff effs [ChainIndexTx]
forall (effs :: [* -> *]).
Member ChainIndexQueryEffect effs =>
[TxId] -> Eff effs [ChainIndexTx]
E.txsFromTxIds
    ([TxId] -> Eff effs [ChainIndexTx])
-> ((TxoAtAddressRequest -> Eff effs TxosResponse)
    :<|> (Eff effs Tip
          :<|> (Eff effs NoContent :<|> Eff effs Diagnostics)))
-> ([TxId] -> Eff effs [ChainIndexTx])
   :<|> ((TxoAtAddressRequest -> Eff effs TxosResponse)
         :<|> (Eff effs Tip
               :<|> (Eff effs NoContent :<|> Eff effs Diagnostics)))
forall a b. a -> b -> a :<|> b
:<|> (\(TxoAtAddressRequest Maybe (PageQuery TxOutRef)
pq Credential
c) -> PageQuery TxOutRef -> Credential -> Eff effs TxosResponse
forall (effs :: [* -> *]).
Member ChainIndexQueryEffect effs =>
PageQuery TxOutRef -> Credential -> Eff effs TxosResponse
E.txoSetAtAddress (PageQuery TxOutRef
-> Maybe (PageQuery TxOutRef) -> PageQuery TxOutRef
forall a. a -> Maybe a -> a
fromMaybe PageQuery TxOutRef
forall a. Default a => a
def Maybe (PageQuery TxOutRef)
pq) Credential
c)
    (TxoAtAddressRequest -> Eff effs TxosResponse)
-> (Eff effs Tip
    :<|> (Eff effs NoContent :<|> Eff effs Diagnostics))
-> (TxoAtAddressRequest -> Eff effs TxosResponse)
   :<|> (Eff effs Tip
         :<|> (Eff effs NoContent :<|> Eff effs Diagnostics))
forall a b. a -> b -> a :<|> b
:<|> Eff effs Tip
forall (effs :: [* -> *]).
Member ChainIndexQueryEffect effs =>
Eff effs Tip
E.getTip
    Eff effs Tip
-> (Eff effs NoContent :<|> Eff effs Diagnostics)
-> Eff effs Tip :<|> (Eff effs NoContent :<|> Eff effs Diagnostics)
forall a b. a -> b -> a :<|> b
:<|> Eff effs ()
forall (effs :: [* -> *]).
Member ChainIndexControlEffect effs =>
Eff effs ()
E.collectGarbage Eff effs () -> Eff effs NoContent -> Eff effs NoContent
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> NoContent -> Eff effs NoContent
forall (f :: * -> *) a. Applicative f => a -> f a
pure NoContent
NoContent
    Eff effs NoContent
-> Eff effs Diagnostics
-> Eff effs NoContent :<|> Eff effs Diagnostics
forall a b. a -> b -> a :<|> b
:<|> Eff effs Diagnostics
forall (effs :: [* -> *]).
Member ChainIndexControlEffect effs =>
Eff effs Diagnostics
E.getDiagnostics

serveFromHashApi ::
    forall effs.
    ( Member (Error ServerError) effs
    , Member ChainIndexQueryEffect effs
    )
    => ServerT FromHashAPI (Eff effs)
serveFromHashApi :: ServerT FromHashAPI (Eff effs)
serveFromHashApi =
    (DatumHash -> Eff effs (Maybe Datum)
forall (effs :: [* -> *]).
Member ChainIndexQueryEffect effs =>
DatumHash -> Eff effs (Maybe Datum)
E.datumFromHash (DatumHash -> Eff effs (Maybe Datum))
-> (Maybe Datum -> Eff effs Datum) -> DatumHash -> Eff effs Datum
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Maybe Datum -> Eff effs Datum
forall (effs :: [* -> *]).
Member (Error ServerError) effs =>
Maybe ~> Eff effs
handleMaybe)
    (DatumHash -> Eff effs Datum)
-> ((ValidatorHash -> Eff effs (Versioned Validator))
    :<|> ((MintingPolicyHash -> Eff effs (Versioned MintingPolicy))
          :<|> ((StakeValidatorHash -> Eff effs (Versioned StakeValidator))
                :<|> (RedeemerHash -> Eff effs Redeemer))))
-> (DatumHash -> Eff effs Datum)
   :<|> ((ValidatorHash -> Eff effs (Versioned Validator))
         :<|> ((MintingPolicyHash -> Eff effs (Versioned MintingPolicy))
               :<|> ((StakeValidatorHash -> Eff effs (Versioned StakeValidator))
                     :<|> (RedeemerHash -> Eff effs Redeemer))))
forall a b. a -> b -> a :<|> b
:<|> (ValidatorHash -> Eff effs (Maybe (Versioned Validator))
forall (effs :: [* -> *]).
Member ChainIndexQueryEffect effs =>
ValidatorHash -> Eff effs (Maybe (Versioned Validator))
E.validatorFromHash (ValidatorHash -> Eff effs (Maybe (Versioned Validator)))
-> (Maybe (Versioned Validator) -> Eff effs (Versioned Validator))
-> ValidatorHash
-> Eff effs (Versioned Validator)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Maybe (Versioned Validator) -> Eff effs (Versioned Validator)
forall (effs :: [* -> *]).
Member (Error ServerError) effs =>
Maybe ~> Eff effs
handleMaybe)
    (ValidatorHash -> Eff effs (Versioned Validator))
-> ((MintingPolicyHash -> Eff effs (Versioned MintingPolicy))
    :<|> ((StakeValidatorHash -> Eff effs (Versioned StakeValidator))
          :<|> (RedeemerHash -> Eff effs Redeemer)))
-> (ValidatorHash -> Eff effs (Versioned Validator))
   :<|> ((MintingPolicyHash -> Eff effs (Versioned MintingPolicy))
         :<|> ((StakeValidatorHash -> Eff effs (Versioned StakeValidator))
               :<|> (RedeemerHash -> Eff effs Redeemer)))
forall a b. a -> b -> a :<|> b
:<|> (MintingPolicyHash -> Eff effs (Maybe (Versioned MintingPolicy))
forall (effs :: [* -> *]).
Member ChainIndexQueryEffect effs =>
MintingPolicyHash -> Eff effs (Maybe (Versioned MintingPolicy))
E.mintingPolicyFromHash (MintingPolicyHash -> Eff effs (Maybe (Versioned MintingPolicy)))
-> (Maybe (Versioned MintingPolicy)
    -> Eff effs (Versioned MintingPolicy))
-> MintingPolicyHash
-> Eff effs (Versioned MintingPolicy)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Maybe (Versioned MintingPolicy)
-> Eff effs (Versioned MintingPolicy)
forall (effs :: [* -> *]).
Member (Error ServerError) effs =>
Maybe ~> Eff effs
handleMaybe)
    (MintingPolicyHash -> Eff effs (Versioned MintingPolicy))
-> ((StakeValidatorHash -> Eff effs (Versioned StakeValidator))
    :<|> (RedeemerHash -> Eff effs Redeemer))
-> (MintingPolicyHash -> Eff effs (Versioned MintingPolicy))
   :<|> ((StakeValidatorHash -> Eff effs (Versioned StakeValidator))
         :<|> (RedeemerHash -> Eff effs Redeemer))
forall a b. a -> b -> a :<|> b
:<|> (StakeValidatorHash -> Eff effs (Maybe (Versioned StakeValidator))
forall (effs :: [* -> *]).
Member ChainIndexQueryEffect effs =>
StakeValidatorHash -> Eff effs (Maybe (Versioned StakeValidator))
E.stakeValidatorFromHash (StakeValidatorHash -> Eff effs (Maybe (Versioned StakeValidator)))
-> (Maybe (Versioned StakeValidator)
    -> Eff effs (Versioned StakeValidator))
-> StakeValidatorHash
-> Eff effs (Versioned StakeValidator)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Maybe (Versioned StakeValidator)
-> Eff effs (Versioned StakeValidator)
forall (effs :: [* -> *]).
Member (Error ServerError) effs =>
Maybe ~> Eff effs
handleMaybe)
    (StakeValidatorHash -> Eff effs (Versioned StakeValidator))
-> (RedeemerHash -> Eff effs Redeemer)
-> (StakeValidatorHash -> Eff effs (Versioned StakeValidator))
   :<|> (RedeemerHash -> Eff effs Redeemer)
forall a b. a -> b -> a :<|> b
:<|> (RedeemerHash -> Eff effs (Maybe Redeemer)
forall (effs :: [* -> *]).
Member ChainIndexQueryEffect effs =>
RedeemerHash -> Eff effs (Maybe Redeemer)
E.redeemerFromHash (RedeemerHash -> Eff effs (Maybe Redeemer))
-> (Maybe Redeemer -> Eff effs Redeemer)
-> RedeemerHash
-> Eff effs Redeemer
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Maybe Redeemer -> Eff effs Redeemer
forall (effs :: [* -> *]).
Member (Error ServerError) effs =>
Maybe ~> Eff effs
handleMaybe)

-- | Return the value of throw a 404 error
handleMaybe :: forall effs. Member (Error ServerError) effs => Maybe ~> Eff effs
handleMaybe :: Maybe ~> Eff effs
handleMaybe = Eff effs x -> (x -> Eff effs x) -> Maybe x -> Eff effs x
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (ServerError -> Eff effs x
forall e (effs :: [* -> *]) a.
Member (Error e) effs =>
e -> Eff effs a
throwError ServerError
err404) x -> Eff effs x
forall (f :: * -> *) a. Applicative f => a -> f a
pure