import Data.Text (Text, pack)
import Data.Time (defaultTimeLocale, formatTime, getCurrentTime)
import Data.Word (Word64)
+
import Marconi.Sidechain.Api.Query.Indexers.EpochSPD qualified as EpochSPD
import Marconi.Sidechain.Api.Query.Indexers.Utxo qualified as Q.Utxo
import Marconi.Sidechain.Api.Routes (API, AddressUtxoResult, CurrentSyncedPointResult, EpochNonceResult,
EpochStakePoolDelegationResult, JsonRpcAPI, MintingPolicyHashTxResult, RestAPI)
-
import Marconi.Sidechain.Api.Types (HasSidechainEnv (httpSettings, queryEnv), IndexerEnv, QueryExceptions, SidechainEnv)
+
import Marconi.Sidechain.Api.Types (QueryExceptions, SidechainEnv, sidechainAddressUtxoIndexer,
+
sidechainEnvHttpSettings, sidechainEnvIndexers)
import Network.JsonRpc.Server.Types ()
import Network.JsonRpc.Types (JsonRpcErr (JsonRpcErr, errorCode, errorData, errorMessage), parseErrorCode)
import Network.Wai.Handler.Warp (runSettings)
-- | Bootstraps the HTTP server
bootstrap :: SidechainEnv -> IO ()
bootstrap env = runSettings
-
(marconiApp (env ^. queryEnv))
+
(env ^. sidechainEnvHttpSettings)
-
marconiApp :: IndexerEnv -> Application
+
marconiApp :: SidechainEnv -> Application
marconiApp env = serve (Proxy @API) (httpRpcServer env)
-
:: IndexerEnv -- ^ Utxo Environment to access Utxo Storage running on the marconi thread
+
:: SidechainEnv -- ^ Utxo Environment to access Utxo Storage running on the marconi thread
:<|> getTargetAddressesQueryHandler env
:<|> getEpochNonceHandler env
restApiServer env = getTimeHandler :<|> getTargetAddressesHandler env
-
:: IndexerEnv -- ^ Utxo Environment to access Utxo Storage running on the marconi thread
+
:: SidechainEnv -- ^ Utxo Environment to access Utxo Storage running on the marconi thread
httpRpcServer env = jsonRpcServer env :<|> restApiServer env
timeString = formatTime defaultTimeLocale "%T"
-
-- | prints TargetAddresses Bech32 representation to the console
+
-- | Prints TargetAddresses Bech32 representation to the console
getTargetAddressesHandler
-
:: IndexerEnv -- ^ Utxo Environment to access Utxo Storage running on the marconi thread
+
:: SidechainEnv -- ^ Utxo Environment to access Utxo Storage running on the marconi thread
-
getTargetAddressesHandler = pure . Q.Utxo.reportBech32Addresses
+
getTargetAddressesHandler env =
+
pure $ Q.Utxo.reportBech32Addresses
+
$ env ^. sidechainEnvIndexers . sidechainAddressUtxoIndexer
-- | prints TargetAddresses Bech32 representation as thru JsonRpc
getTargetAddressesQueryHandler
-
:: IndexerEnv -- ^ database configuration
+
:: SidechainEnv -- ^ database configuration
-- ^ Will always be an empty string as we are ignoring this param, and returning everything
-> Handler (Either (JsonRpcErr String) [Text])
-
getTargetAddressesQueryHandler env _ = pure . Right . Q.Utxo.reportBech32Addresses $ env
+
getTargetAddressesQueryHandler env _ =
+
$ Q.Utxo.reportBech32Addresses (env ^. sidechainEnvIndexers . sidechainAddressUtxoIndexer)
-- | Handler for retrieving current synced chain point.
getCurrentSyncedPointHandler
-
:: IndexerEnv -- ^ Utxo Environment to access Utxo Storage running on the marconi thread
+
:: SidechainEnv -- ^ Utxo Environment to access Utxo Storage running on the marconi thread
-> String -- ^ Dummy parameter
-> Handler (Either (JsonRpcErr String) CurrentSyncedPointResult)
getCurrentSyncedPointHandler _ _ =
pure $ Left $ JsonRpcErr 1 "Endpoint not implemented yet" Nothing
-- | Handler for retrieving UTXOs by Address
-
:: IndexerEnv -- ^ Utxo Environment to access Utxo Storage running on the marconi thread
-
-> String -- ^ Bech32 addressCredential
+
:: SidechainEnv -- ^ Utxo Environment to access Utxo Storage running on the marconi thread
+
-> String -- ^ Bech32 addressCredential
-> Handler (Either (JsonRpcErr String) AddressUtxoResult)
getAddressUtxoHandler env address = liftIO $
-
first toRpcErr <$> (Q.Utxo.findByBech32Address env . pack $ address)
+
<$> Q.Utxo.findByBech32Address
+
(env ^. sidechainEnvIndexers . sidechainAddressUtxoIndexer)
-
-- | Handler for retrieving TXs by Minting Policy Hash.
+
-- | Handler for retrieving Txs by Minting Policy Hash.
getMintingPolicyHashTxHandler
-
:: IndexerEnv -- ^ Utxo Environment to access Utxo Storage running on the marconi thread
+
:: SidechainEnv -- ^ Utxo Environment to access Utxo Storage running on the marconi thread
-> String -- ^ Minting policy hash
-> Handler (Either (JsonRpcErr String) MintingPolicyHashTxResult)
getMintingPolicyHashTxHandler _ _ = pure $ Left $ JsonRpcErr 1 "Endpoint not implemented yet" Nothing
-- | Handler for retrieving stake pool delegation per epoch
getEpochStakePoolDelegationHandler
-
:: IndexerEnv -- ^ Utxo Environment to access Utxo Storage running on the marconi thread
+
:: SidechainEnv -- ^ Utxo Environment to access Utxo Storage running on the marconi thread
-> Handler (Either (JsonRpcErr String) EpochStakePoolDelegationResult)
-
getEpochStakePoolDelegationHandler _ _ = pure $ Left $ JsonRpcErr 1 "Endpoint not implemented yet" Nothing
+
getEpochStakePoolDelegationHandler env epochNo = liftIO $
+
<$> EpochSPD.querySPDByEpochNo env epochNo
-- | Handler for retrieving stake pool delegation per epoch
-
:: IndexerEnv -- ^ Utxo Environment to access Utxo Storage running on the marconi thread
+
:: SidechainEnv -- ^ Utxo Environment to access Utxo Storage running on the marconi thread
-> Handler (Either (JsonRpcErr String) EpochNonceResult)
getEpochNonceHandler _ _ = pure $ Left $ JsonRpcErr 1 "Endpoint not implemented yet" Nothing
-
-- | convert form to Jsonrpc protocal error
+
-- | Convert to JSON-RPC protocol error.