-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add server implementation, better handling of Environment
- Loading branch information
Showing
18 changed files
with
447 additions
and
249 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
{-# LANGUAGE NamedFieldPuns #-} | ||
{-| Application monad used by CLI and server | ||
-} | ||
module Wst.App ( | ||
WstApp(..), | ||
runWstApp, | ||
runWstAppServant | ||
) where | ||
|
||
import Blammo.Logging.Simple (MonadLogger, MonadLoggerIO, WithLogger (..)) | ||
import Cardano.Api qualified as C | ||
import Control.Monad.Except (ExceptT, MonadError, runExceptT, throwError) | ||
import Control.Monad.IO.Class (MonadIO (..)) | ||
import Control.Monad.Reader (MonadReader, ReaderT, runReaderT) | ||
import Convex.Blockfrost (BlockfrostT (..), evalBlockfrostT) | ||
import Convex.Class (MonadBlockchain, MonadUtxoQuery) | ||
import Data.String (IsString (..)) | ||
import Servant.Server (Handler (..)) | ||
import Servant.Server qualified as S | ||
import Wst.AppError (AppError (BlockfrostErr)) | ||
import Wst.Offchain.Env (RuntimeEnv (..)) | ||
import Wst.Offchain.Env qualified as Env | ||
|
||
newtype WstApp env era a = WstApp { unWstApp :: ReaderT env (ExceptT (AppError era) (BlockfrostT IO)) a } | ||
deriving newtype (Monad, Applicative, Functor, MonadIO, MonadReader env, MonadError (AppError era), MonadUtxoQuery, MonadBlockchain C.ConwayEra) | ||
deriving | ||
(MonadLogger, MonadLoggerIO) | ||
via (WithLogger env (ExceptT (AppError era) (BlockfrostT IO))) | ||
|
||
runWstApp :: forall env era a. (Env.HasRuntimeEnv env) => env -> WstApp env era a -> IO (Either (AppError era) a) | ||
runWstApp env WstApp{unWstApp} = do | ||
let RuntimeEnv{envBlockfrost} = Env.runtimeEnv env | ||
evalBlockfrostT envBlockfrost (runExceptT (runReaderT unWstApp env)) >>= \case | ||
Left e -> pure (Left $ BlockfrostErr e) | ||
Right a -> pure a | ||
|
||
{-| Interpret the 'WstApp' in a servant handler | ||
-} | ||
runWstAppServant :: forall env era a. (Env.HasRuntimeEnv env) => env -> WstApp env era a -> Handler a | ||
runWstAppServant env action = liftIO (runWstApp env action) >>= \case | ||
Left err -> do | ||
let err_ = S.err500 { S.errBody = fromString (show err) } | ||
throwError err_ | ||
Right a -> pure a |
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,15 @@ | ||
{-| Error type for endpoints and queries | ||
-} | ||
module Wst.AppError( | ||
AppError(..) | ||
) where | ||
|
||
import Blockfrost.Client.Core (BlockfrostError) | ||
import Convex.CoinSelection qualified as CoinSelection | ||
|
||
data AppError era = | ||
OperatorNoUTxOs -- ^ The operator does not have any UTxOs | ||
| GlobalParamsNodeNotFound -- ^ The node with the global parameters was not found | ||
| BalancingError (CoinSelection.BalanceTxError era) | ||
| BlockfrostErr BlockfrostError | ||
deriving stock (Show) |
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 was deleted.
Oops, something went wrong.
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.