{-# LANGUAGE CPP                 #-}
{-# LANGUAGE FlexibleContexts    #-}
{-# LANGUAGE FlexibleInstances   #-}
{-# LANGUAGE LambdaCase          #-}
{-# LANGUAGE MultiWayIf          #-}
{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE RecordWildCards     #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell     #-}
{-# LANGUAGE TupleSections       #-}
module Haskoin.Store.Web
    ( -- * Web
      WebConfig (..)
    , Except (..)
    , WebLimits (..)
    , WebTimeouts (..)
    , runWeb
    ) where

import           Conduit                       (ConduitT, await, dropC,
                                                runConduit, sinkList, takeC,
                                                yield, (.|))
import           Control.Applicative           ((<|>))
import           Control.Arrow                 (second)
import           Control.Lens
import           Control.Monad                 (forever, unless, when, (<=<))
import           Control.Monad.Logger          (MonadLoggerIO, logDebugS,
                                                logErrorS, logInfoS)
import           Control.Monad.Reader          (ReaderT, ask, asks, local,
                                                runReaderT)
import           Control.Monad.Trans           (lift)
import           Control.Monad.Trans.Control   (liftWith, restoreT)
import           Control.Monad.Trans.Maybe     (MaybeT (..), runMaybeT)
import           Data.Aeson                    (Encoding, ToJSON (..), Value)
import           Data.Aeson.Encode.Pretty      (Config (..), defConfig,
                                                encodePretty')
import           Data.Aeson.Encoding           (encodingToLazyByteString, list)
import           Data.Aeson.Text               (encodeToLazyText)
import           Data.ByteString.Builder       (lazyByteString)
import qualified Data.ByteString.Lazy          as L
import qualified Data.ByteString.Lazy.Char8    as C
import           Data.Bytes.Get
import           Data.Bytes.Put
import           Data.Bytes.Serial
import           Data.Char                     (isSpace)
import qualified Data.ByteString.Lazy.Base16 as BL16
import           Data.Default                  (Default (..))
import           Data.Function                 (on, (&))
import           Data.HashMap.Strict           (HashMap)
import qualified Data.HashMap.Strict           as HashMap
import           Data.HashSet                  (HashSet)
import qualified Data.HashSet                  as HashSet
import           Data.Int                      (Int64)
import           Data.List                     (nub, sortBy)
import qualified Data.Map.Strict               as Map
import           Data.Maybe                    (catMaybes, fromJust, fromMaybe,
                                                isJust, listToMaybe, mapMaybe,
                                                maybeToList)
import           Data.Proxy                    (Proxy (..))
import qualified Data.Set                      as Set
import           Data.String                   (fromString)
import           Data.String.Conversions       (cs)
import           Data.Text                     (Text)
import qualified Data.Text                     as T
import qualified Data.Text.Encoding            as T
import           Data.Text.Lazy                (toStrict)
import qualified Data.Text.Lazy                as TL
import           Data.Time.Clock               (NominalDiffTime, diffUTCTime)
import           Data.Time.Clock.System        (getSystemTime, systemSeconds,
                                                systemToUTCTime)
import           Data.Word                     (Word32, Word64)
import           Database.RocksDB              (Property (..), getProperty)
import           Haskoin.Address
import qualified Haskoin.Block                 as H
import           Haskoin.Constants
import           Haskoin.Keys
import           Haskoin.Network
import           Haskoin.Node                  (Chain, OnlinePeer (..),
                                                PeerManager, chainGetBest,
                                                getPeers, sendMessage)
import           Haskoin.Store.BlockStore
import           Haskoin.Store.Cache
import           Haskoin.Store.Common
import           Haskoin.Store.Data
import           Haskoin.Store.Database.Reader
import           Haskoin.Store.Logic
import           Haskoin.Store.Manager
import           Haskoin.Store.Stats
import           Haskoin.Store.WebCommon
import           Haskoin.Transaction
import           Haskoin.Util
import           NQE                           (Inbox, receive,
                                                withSubscription)
import           Network.HTTP.Types            (Status (..), status400,
                                                status403, status404, status409,
                                                status500, status503,
                                                statusIsClientError,
                                                statusIsServerError,
                                                statusIsSuccessful)
import           Network.Wai                   (Middleware, Request (..),
                                                responseStatus)
import           Network.Wai.Handler.Warp      (defaultSettings, setHost,
                                                setPort)
import qualified Network.Wreq                  as Wreq
import           System.IO.Unsafe              (unsafeInterleaveIO)
import qualified System.Metrics                as Metrics
import qualified System.Metrics.Counter        as Metrics (Counter)
import qualified System.Metrics.Counter        as Metrics.Counter
import qualified System.Metrics.Gauge          as Metrics (Gauge)
import qualified System.Metrics.Gauge          as Metrics.Gauge
import           Text.Printf                   (printf)
import           UnliftIO                      (MonadIO, MonadUnliftIO, TVar,
                                                askRunInIO, atomically, bracket,
                                                bracket_, handleAny, liftIO,
                                                newTVarIO, readTVarIO, timeout,
                                                withAsync, withRunInIO,
                                                writeTVar)
import           UnliftIO.Concurrent           (threadDelay)
import           Web.Scotty.Internal.Types     (ActionT)
import           Web.Scotty.Trans              (Parsable)
import qualified Web.Scotty.Trans              as S

type WebT m = ActionT Except (ReaderT WebState m)

data WebLimits = WebLimits
    { WebLimits -> Word32
maxLimitCount      :: !Word32
    , WebLimits -> Word32
maxLimitFull       :: !Word32
    , WebLimits -> Word32
maxLimitOffset     :: !Word32
    , WebLimits -> Word32
maxLimitDefault    :: !Word32
    , WebLimits -> Word32
maxLimitGap        :: !Word32
    , WebLimits -> Word32
maxLimitInitialGap :: !Word32
    }
    deriving (WebLimits -> WebLimits -> Bool
(WebLimits -> WebLimits -> Bool)
-> (WebLimits -> WebLimits -> Bool) -> Eq WebLimits
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: WebLimits -> WebLimits -> Bool
$c/= :: WebLimits -> WebLimits -> Bool
== :: WebLimits -> WebLimits -> Bool
$c== :: WebLimits -> WebLimits -> Bool
Eq, Int -> WebLimits -> ShowS
[WebLimits] -> ShowS
WebLimits -> String
(Int -> WebLimits -> ShowS)
-> (WebLimits -> String)
-> ([WebLimits] -> ShowS)
-> Show WebLimits
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [WebLimits] -> ShowS
$cshowList :: [WebLimits] -> ShowS
show :: WebLimits -> String
$cshow :: WebLimits -> String
showsPrec :: Int -> WebLimits -> ShowS
$cshowsPrec :: Int -> WebLimits -> ShowS
Show)

instance Default WebLimits where
    def :: WebLimits
def =
        $WWebLimits :: Word32
-> Word32 -> Word32 -> Word32 -> Word32 -> Word32 -> WebLimits
WebLimits
            { maxLimitCount :: Word32
maxLimitCount = 200000
            , maxLimitFull :: Word32
maxLimitFull = 5000
            , maxLimitOffset :: Word32
maxLimitOffset = 50000
            , maxLimitDefault :: Word32
maxLimitDefault = 100
            , maxLimitGap :: Word32
maxLimitGap = 32
            , maxLimitInitialGap :: Word32
maxLimitInitialGap = 20
            }

data WebConfig = WebConfig
    { WebConfig -> String
webHost       :: !String
    , WebConfig -> Int
webPort       :: !Int
    , WebConfig -> Store
webStore      :: !Store
    , WebConfig -> Int
webMaxDiff    :: !Int
    , WebConfig -> Int
webMaxPending :: !Int
    , WebConfig -> WebLimits
webMaxLimits  :: !WebLimits
    , WebConfig -> WebTimeouts
webTimeouts   :: !WebTimeouts
    , WebConfig -> String
webVersion    :: !String
    , WebConfig -> Bool
webNoMempool  :: !Bool
    , WebConfig -> Maybe Store
webStats      :: !(Maybe Metrics.Store)
    }

data WebState = WebState
    { WebState -> WebConfig
webConfig  :: !WebConfig
    , WebState -> TVar (HashMap Text BinfoTicker)
webTicker  :: !(TVar (HashMap Text BinfoTicker))
    , WebState -> Maybe WebMetrics
webMetrics :: !(Maybe WebMetrics)
    }

data ErrorCounter = ErrorCounter
    { ErrorCounter -> Counter
clientErrors :: Metrics.Counter
    , ErrorCounter -> Counter
serverErrors :: Metrics.Counter
    }

createErrorCounter :: MonadIO m
                   => Text
                   -> Metrics.Store
                   -> m ErrorCounter
createErrorCounter :: Text -> Store -> m ErrorCounter
createErrorCounter t :: Text
t s :: Store
s = IO ErrorCounter -> m ErrorCounter
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO ErrorCounter -> m ErrorCounter)
-> IO ErrorCounter -> m ErrorCounter
forall a b. (a -> b) -> a -> b
$ do
    Counter
clientErrors <- Text -> Store -> IO Counter
Metrics.createCounter (Text
t Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> ".client_errors") Store
s
    Counter
serverErrors <- Text -> Store -> IO Counter
Metrics.createCounter (Text
t Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> ".server_errors") Store
s
    ErrorCounter -> IO ErrorCounter
forall (m :: * -> *) a. Monad m => a -> m a
return ErrorCounter :: Counter -> Counter -> ErrorCounter
ErrorCounter{..}

data WebMetrics = WebMetrics
    { WebMetrics -> StatDist
everyResponseTime       :: !StatDist
    , WebMetrics -> ErrorCounter
everyError              :: !ErrorCounter
    , WebMetrics -> StatDist
blockResponseTime       :: !StatDist
    , WebMetrics -> StatDist
rawBlockResponseTime    :: !StatDist
    , WebMetrics -> ErrorCounter
blockErrors             :: !ErrorCounter
    , WebMetrics -> StatDist
txResponseTime          :: !StatDist
    , WebMetrics -> StatDist
txsBlockResponseTime    :: !StatDist
    , WebMetrics -> ErrorCounter
txErrors                :: !ErrorCounter
    , WebMetrics -> StatDist
txAfterResponseTime     :: !StatDist
    , WebMetrics -> StatDist
postTxResponseTime      :: !StatDist
    , WebMetrics -> ErrorCounter
postTxErrors            :: !ErrorCounter
    , WebMetrics -> StatDist
mempoolResponseTime     :: !StatDist
    , WebMetrics -> StatDist
addrTxResponseTime      :: !StatDist
    , WebMetrics -> StatDist
addrTxFullResponseTime  :: !StatDist
    , WebMetrics -> StatDist
addrBalanceResponseTime :: !StatDist
    , WebMetrics -> StatDist
addrUnspentResponseTime :: !StatDist
    , WebMetrics -> StatDist
xPubResponseTime        :: !StatDist
    , WebMetrics -> StatDist
xPubTxResponseTime      :: !StatDist
    , WebMetrics -> StatDist
xPubTxFullResponseTime  :: !StatDist
    , WebMetrics -> StatDist
xPubUnspentResponseTime :: !StatDist
    , WebMetrics -> StatDist
multiaddrResponseTime   :: !StatDist
    , WebMetrics -> ErrorCounter
multiaddrErrors         :: !ErrorCounter
    , WebMetrics -> StatDist
unspentResponseTime     :: !StatDist
    , WebMetrics -> ErrorCounter
unspentErrors           :: !ErrorCounter
    , WebMetrics -> StatDist
rawtxResponseTime       :: !StatDist
    , WebMetrics -> ErrorCounter
rawtxErrors             :: !ErrorCounter
    , WebMetrics -> StatDist
peerResponseTime        :: !StatDist
    , WebMetrics -> StatDist
healthResponseTime      :: !StatDist
    , WebMetrics -> StatDist
dbStatsResponseTime     :: !StatDist
    , WebMetrics -> Gauge
eventsConnected         :: !Metrics.Gauge
    }

createMetrics :: MonadIO m => Metrics.Store -> m WebMetrics
createMetrics :: Store -> m WebMetrics
createMetrics s :: Store
s = IO WebMetrics -> m WebMetrics
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO WebMetrics -> m WebMetrics) -> IO WebMetrics -> m WebMetrics
forall a b. (a -> b) -> a -> b
$ do
    StatDist
everyResponseTime         <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "all.response_time_ms"
    ErrorCounter
everyError                <- Text -> IO ErrorCounter
forall (m :: * -> *). MonadIO m => Text -> m ErrorCounter
e "all.errors"
    StatDist
blockResponseTime         <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "block.response_time_ms"
    ErrorCounter
blockErrors               <- Text -> IO ErrorCounter
forall (m :: * -> *). MonadIO m => Text -> m ErrorCounter
e "block.errors"
    StatDist
rawBlockResponseTime      <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "block_raw.response_time_ms"
    StatDist
txResponseTime            <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "tx.response_time"
    StatDist
txsBlockResponseTime      <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "txs_block.response_time"
    ErrorCounter
txErrors                  <- Text -> IO ErrorCounter
forall (m :: * -> *). MonadIO m => Text -> m ErrorCounter
e "tx.errors"
    StatDist
txAfterResponseTime       <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "tx_after.response_time_ms"
    StatDist
postTxResponseTime        <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "tx_post.response_time_ms"
    ErrorCounter
postTxErrors              <- Text -> IO ErrorCounter
forall (m :: * -> *). MonadIO m => Text -> m ErrorCounter
e "tx_post.errors"
    StatDist
mempoolResponseTime       <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "mempool.response_time_ms"
    StatDist
addrBalanceResponseTime   <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "addr_balance.response_time_ms"
    StatDist
addrTxResponseTime        <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "addr_tx.response_time_ms"
    StatDist
addrTxFullResponseTime    <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "addr_tx_full.response_time_ms"
    StatDist
addrUnspentResponseTime   <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "addr_unspent.response_time_ms"
    StatDist
xPubResponseTime          <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "xpub_balance.response_time_ms"
    StatDist
xPubTxResponseTime        <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "xpub_tx.response_time_ms"
    StatDist
xPubTxFullResponseTime    <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "xpub_tx_full.response_time_ms"
    StatDist
xPubUnspentResponseTime   <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "xpub_unspent.response_time_ms"
    StatDist
multiaddrResponseTime     <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "multiaddr.response_time_ms"
    ErrorCounter
multiaddrErrors           <- Text -> IO ErrorCounter
forall (m :: * -> *). MonadIO m => Text -> m ErrorCounter
e "multiaddr.errors"
    StatDist
rawtxResponseTime         <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "rawtx.response_time_ms"
    ErrorCounter
rawtxErrors               <- Text -> IO ErrorCounter
forall (m :: * -> *). MonadIO m => Text -> m ErrorCounter
e "rawtx.errors"
    StatDist
peerResponseTime          <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "peer.response_time_ms"
    StatDist
healthResponseTime        <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "health.response_time_ms"
    StatDist
dbStatsResponseTime       <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "dbstats.response_time_ms"
    Gauge
eventsConnected           <- Text -> IO Gauge
g "events.connected"
    StatDist
unspentResponseTime       <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "unspent.response_time_ms"
    ErrorCounter
unspentErrors             <- Text -> IO ErrorCounter
forall (m :: * -> *). MonadIO m => Text -> m ErrorCounter
e "unspent.errors"
    WebMetrics -> IO WebMetrics
forall (m :: * -> *) a. Monad m => a -> m a
return $WWebMetrics :: StatDist
-> ErrorCounter
-> StatDist
-> StatDist
-> ErrorCounter
-> StatDist
-> StatDist
-> ErrorCounter
-> StatDist
-> StatDist
-> ErrorCounter
-> StatDist
-> StatDist
-> StatDist
-> StatDist
-> StatDist
-> StatDist
-> StatDist
-> StatDist
-> StatDist
-> StatDist
-> ErrorCounter
-> StatDist
-> ErrorCounter
-> StatDist
-> ErrorCounter
-> StatDist
-> StatDist
-> StatDist
-> Gauge
-> WebMetrics
WebMetrics{..}
  where
    d :: Text -> m StatDist
d x :: Text
x = Text -> Store -> m StatDist
forall (m :: * -> *). MonadIO m => Text -> Store -> m StatDist
createStatDist       ("web." Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
x) Store
s
    g :: Text -> IO Gauge
g x :: Text
x = Text -> Store -> IO Gauge
Metrics.createGauge  ("web." Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
x) Store
s
    e :: Text -> m ErrorCounter
e x :: Text
x = Text -> Store -> m ErrorCounter
forall (m :: * -> *). MonadIO m => Text -> Store -> m ErrorCounter
createErrorCounter   ("web." Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
x) Store
s

withGaugeIncrease :: MonadUnliftIO m
                  => (WebMetrics -> Metrics.Gauge)
                  -> WebT m a
                  -> WebT m a
withGaugeIncrease :: (WebMetrics -> Gauge) -> WebT m a -> WebT m a
withGaugeIncrease gf :: WebMetrics -> Gauge
gf go :: WebT m a
go =
    ReaderT WebState m (Maybe WebMetrics)
-> ActionT Except (ReaderT WebState m) (Maybe WebMetrics)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ((WebState -> Maybe WebMetrics)
-> ReaderT WebState m (Maybe WebMetrics)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WebState -> Maybe WebMetrics
webMetrics) ActionT Except (ReaderT WebState m) (Maybe WebMetrics)
-> (Maybe WebMetrics -> WebT m a) -> WebT m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Nothing -> WebT m a
go
        Just m :: WebMetrics
m -> do
            (Either (ActionError Except) a, ScottyResponse)
s <- (Run (ActionT Except)
 -> ReaderT
      WebState m (Either (ActionError Except) a, ScottyResponse))
-> ActionT
     Except
     (ReaderT WebState m)
     (Either (ActionError Except) a, ScottyResponse)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTransControl t, Monad m) =>
(Run t -> m a) -> t m a
liftWith ((Run (ActionT Except)
  -> ReaderT
       WebState m (Either (ActionError Except) a, ScottyResponse))
 -> ActionT
      Except
      (ReaderT WebState m)
      (Either (ActionError Except) a, ScottyResponse))
-> (Run (ActionT Except)
    -> ReaderT
         WebState m (Either (ActionError Except) a, ScottyResponse))
-> ActionT
     Except
     (ReaderT WebState m)
     (Either (ActionError Except) a, ScottyResponse)
forall a b. (a -> b) -> a -> b
$ \run :: Run (ActionT Except)
run ->
                ReaderT WebState m ()
-> ReaderT WebState m ()
-> ReaderT
     WebState m (Either (ActionError Except) a, ScottyResponse)
-> ReaderT
     WebState m (Either (ActionError Except) a, ScottyResponse)
forall (m :: * -> *) a b c.
MonadUnliftIO m =>
m a -> m b -> m c -> m c
bracket_
                (WebMetrics -> ReaderT WebState m ()
forall (m :: * -> *). MonadIO m => WebMetrics -> m ()
start WebMetrics
m)
                (WebMetrics -> ReaderT WebState m ()
forall (m :: * -> *). MonadIO m => WebMetrics -> m ()
end WebMetrics
m)
                (WebT m a -> ReaderT WebState m (StT (ActionT Except) a)
Run (ActionT Except)
run WebT m a
go)
            ReaderT WebState m (StT (ActionT Except) a) -> WebT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTransControl t, Monad m) =>
m (StT t a) -> t m a
restoreT (ReaderT WebState m (StT (ActionT Except) a) -> WebT m a)
-> ReaderT WebState m (StT (ActionT Except) a) -> WebT m a
forall a b. (a -> b) -> a -> b
$ (Either (ActionError Except) a, ScottyResponse)
-> ReaderT
     WebState m (Either (ActionError Except) a, ScottyResponse)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either (ActionError Except) a, ScottyResponse)
s
  where
    start :: WebMetrics -> m ()
start m :: WebMetrics
m = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ Gauge -> IO ()
Metrics.Gauge.inc (WebMetrics -> Gauge
gf WebMetrics
m)
    end :: WebMetrics -> m ()
end m :: WebMetrics
m = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ Gauge -> IO ()
Metrics.Gauge.dec (WebMetrics -> Gauge
gf WebMetrics
m)

withMetrics :: MonadUnliftIO m
            => (WebMetrics -> StatDist)
            -> Int
            -> WebT m a
            -> WebT m a
withMetrics :: (WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics df :: WebMetrics -> StatDist
df i :: Int
i go :: WebT m a
go =
    ReaderT WebState m (Maybe WebMetrics)
-> ActionT Except (ReaderT WebState m) (Maybe WebMetrics)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ((WebState -> Maybe WebMetrics)
-> ReaderT WebState m (Maybe WebMetrics)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WebState -> Maybe WebMetrics
webMetrics) ActionT Except (ReaderT WebState m) (Maybe WebMetrics)
-> (Maybe WebMetrics -> WebT m a) -> WebT m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Nothing -> WebT m a
go
        Just m :: WebMetrics
m -> do
            (Either (ActionError Except) a, ScottyResponse)
x <-
                (Run (ActionT Except)
 -> ReaderT
      WebState m (Either (ActionError Except) a, ScottyResponse))
-> ActionT
     Except
     (ReaderT WebState m)
     (Either (ActionError Except) a, ScottyResponse)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTransControl t, Monad m) =>
(Run t -> m a) -> t m a
liftWith ((Run (ActionT Except)
  -> ReaderT
       WebState m (Either (ActionError Except) a, ScottyResponse))
 -> ActionT
      Except
      (ReaderT WebState m)
      (Either (ActionError Except) a, ScottyResponse))
-> (Run (ActionT Except)
    -> ReaderT
         WebState m (Either (ActionError Except) a, ScottyResponse))
-> ActionT
     Except
     (ReaderT WebState m)
     (Either (ActionError Except) a, ScottyResponse)
forall a b. (a -> b) -> a -> b
$ \run :: Run (ActionT Except)
run ->
                ReaderT WebState m UTCTime
-> (UTCTime -> ReaderT WebState m ())
-> (UTCTime
    -> ReaderT
         WebState m (Either (ActionError Except) a, ScottyResponse))
-> ReaderT
     WebState m (Either (ActionError Except) a, ScottyResponse)
forall (m :: * -> *) a b c.
MonadUnliftIO m =>
m a -> (a -> m b) -> (a -> m c) -> m c
bracket
                (SystemTime -> UTCTime
systemToUTCTime (SystemTime -> UTCTime)
-> ReaderT WebState m SystemTime -> ReaderT WebState m UTCTime
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO SystemTime -> ReaderT WebState m SystemTime
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO SystemTime
getSystemTime)
                (WebMetrics -> UTCTime -> ReaderT WebState m ()
forall (m :: * -> *). MonadIO m => WebMetrics -> UTCTime -> m ()
end WebMetrics
m)
                (ReaderT WebState m (Either (ActionError Except) a, ScottyResponse)
-> UTCTime
-> ReaderT
     WebState m (Either (ActionError Except) a, ScottyResponse)
forall a b. a -> b -> a
const (WebT m a -> ReaderT WebState m (StT (ActionT Except) a)
Run (ActionT Except)
run WebT m a
go))
            ReaderT WebState m (StT (ActionT Except) a) -> WebT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTransControl t, Monad m) =>
m (StT t a) -> t m a
restoreT (ReaderT WebState m (StT (ActionT Except) a) -> WebT m a)
-> ReaderT WebState m (StT (ActionT Except) a) -> WebT m a
forall a b. (a -> b) -> a -> b
$ (Either (ActionError Except) a, ScottyResponse)
-> ReaderT
     WebState m (Either (ActionError Except) a, ScottyResponse)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either (ActionError Except) a, ScottyResponse)
x
  where
    end :: WebMetrics -> UTCTime -> m ()
end metrics :: WebMetrics
metrics t1 :: UTCTime
t1 = do
        UTCTime
t2 <- SystemTime -> UTCTime
systemToUTCTime (SystemTime -> UTCTime) -> m SystemTime -> m UTCTime
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO SystemTime -> m SystemTime
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO SystemTime
getSystemTime
        let diff :: Int64
diff = NominalDiffTime -> Int64
forall a b. (RealFrac a, Integral b) => a -> b
round (NominalDiffTime -> Int64) -> NominalDiffTime -> Int64
forall a b. (a -> b) -> a -> b
$ UTCTime -> UTCTime -> NominalDiffTime
diffUTCTime UTCTime
t2 UTCTime
t1 NominalDiffTime -> NominalDiffTime -> NominalDiffTime
forall a. Num a => a -> a -> a
* 1000
        WebMetrics -> StatDist
df WebMetrics
metrics StatDist -> StatEntry -> m ()
forall (m :: * -> *). MonadIO m => StatDist -> StatEntry -> m ()
`addStatEntry` Int64 -> Int64 -> StatEntry
StatEntry Int64
diff (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
i)
        WebMetrics -> StatDist
everyResponseTime WebMetrics
metrics StatDist -> StatEntry -> m ()
forall (m :: * -> *). MonadIO m => StatDist -> StatEntry -> m ()
`addStatEntry` Int64 -> Int64 -> StatEntry
StatEntry Int64
diff (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
i)

data WebTimeouts = WebTimeouts
    { WebTimeouts -> Word64
txTimeout    :: !Word64
    , WebTimeouts -> Word64
blockTimeout :: !Word64
    }
    deriving (WebTimeouts -> WebTimeouts -> Bool
(WebTimeouts -> WebTimeouts -> Bool)
-> (WebTimeouts -> WebTimeouts -> Bool) -> Eq WebTimeouts
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: WebTimeouts -> WebTimeouts -> Bool
$c/= :: WebTimeouts -> WebTimeouts -> Bool
== :: WebTimeouts -> WebTimeouts -> Bool
$c== :: WebTimeouts -> WebTimeouts -> Bool
Eq, Int -> WebTimeouts -> ShowS
[WebTimeouts] -> ShowS
WebTimeouts -> String
(Int -> WebTimeouts -> ShowS)
-> (WebTimeouts -> String)
-> ([WebTimeouts] -> ShowS)
-> Show WebTimeouts
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [WebTimeouts] -> ShowS
$cshowList :: [WebTimeouts] -> ShowS
show :: WebTimeouts -> String
$cshow :: WebTimeouts -> String
showsPrec :: Int -> WebTimeouts -> ShowS
$cshowsPrec :: Int -> WebTimeouts -> ShowS
Show)

data SerialAs = SerialAsBinary | SerialAsJSON | SerialAsPrettyJSON
    deriving (SerialAs -> SerialAs -> Bool
(SerialAs -> SerialAs -> Bool)
-> (SerialAs -> SerialAs -> Bool) -> Eq SerialAs
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SerialAs -> SerialAs -> Bool
$c/= :: SerialAs -> SerialAs -> Bool
== :: SerialAs -> SerialAs -> Bool
$c== :: SerialAs -> SerialAs -> Bool
Eq, Int -> SerialAs -> ShowS
[SerialAs] -> ShowS
SerialAs -> String
(Int -> SerialAs -> ShowS)
-> (SerialAs -> String) -> ([SerialAs] -> ShowS) -> Show SerialAs
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SerialAs] -> ShowS
$cshowList :: [SerialAs] -> ShowS
show :: SerialAs -> String
$cshow :: SerialAs -> String
showsPrec :: Int -> SerialAs -> ShowS
$cshowsPrec :: Int -> SerialAs -> ShowS
Show)

instance Default WebTimeouts where
    def :: WebTimeouts
def = $WWebTimeouts :: Word64 -> Word64 -> WebTimeouts
WebTimeouts {txTimeout :: Word64
txTimeout = 300, blockTimeout :: Word64
blockTimeout = 7200}

instance (MonadUnliftIO m, MonadLoggerIO m) =>
         StoreReadBase (ReaderT WebState m) where
    getNetwork :: ReaderT WebState m Network
getNetwork = CacheT (DatabaseReaderT m) Network -> ReaderT WebState m Network
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader CacheT (DatabaseReaderT m) Network
forall (m :: * -> *). StoreReadBase m => m Network
getNetwork
    getBestBlock :: ReaderT WebState m (Maybe BlockHash)
getBestBlock = CacheT (DatabaseReaderT m) (Maybe BlockHash)
-> ReaderT WebState m (Maybe BlockHash)
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader CacheT (DatabaseReaderT m) (Maybe BlockHash)
forall (m :: * -> *). StoreReadBase m => m (Maybe BlockHash)
getBestBlock
    getBlocksAtHeight :: Word32 -> ReaderT WebState m [BlockHash]
getBlocksAtHeight height :: Word32
height = CacheT (DatabaseReaderT m) [BlockHash]
-> ReaderT WebState m [BlockHash]
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader (Word32 -> CacheT (DatabaseReaderT m) [BlockHash]
forall (m :: * -> *). StoreReadBase m => Word32 -> m [BlockHash]
getBlocksAtHeight Word32
height)
    getBlock :: BlockHash -> ReaderT WebState m (Maybe BlockData)
getBlock bh :: BlockHash
bh = CacheT (DatabaseReaderT m) (Maybe BlockData)
-> ReaderT WebState m (Maybe BlockData)
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader (BlockHash -> CacheT (DatabaseReaderT m) (Maybe BlockData)
forall (m :: * -> *).
StoreReadBase m =>
BlockHash -> m (Maybe BlockData)
getBlock BlockHash
bh)
    getTxData :: TxHash -> ReaderT WebState m (Maybe TxData)
getTxData th :: TxHash
th = CacheT (DatabaseReaderT m) (Maybe TxData)
-> ReaderT WebState m (Maybe TxData)
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader (TxHash -> CacheT (DatabaseReaderT m) (Maybe TxData)
forall (m :: * -> *). StoreReadBase m => TxHash -> m (Maybe TxData)
getTxData TxHash
th)
    getSpender :: OutPoint -> ReaderT WebState m (Maybe Spender)
getSpender op :: OutPoint
op = CacheT (DatabaseReaderT m) (Maybe Spender)
-> ReaderT WebState m (Maybe Spender)
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader (OutPoint -> CacheT (DatabaseReaderT m) (Maybe Spender)
forall (m :: * -> *).
StoreReadBase m =>
OutPoint -> m (Maybe Spender)
getSpender OutPoint
op)
    getUnspent :: OutPoint -> ReaderT WebState m (Maybe Unspent)
getUnspent op :: OutPoint
op = CacheT (DatabaseReaderT m) (Maybe Unspent)
-> ReaderT WebState m (Maybe Unspent)
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader (OutPoint -> CacheT (DatabaseReaderT m) (Maybe Unspent)
forall (m :: * -> *).
StoreReadBase m =>
OutPoint -> m (Maybe Unspent)
getUnspent OutPoint
op)
    getBalance :: Address -> ReaderT WebState m (Maybe Balance)
getBalance a :: Address
a = CacheT (DatabaseReaderT m) (Maybe Balance)
-> ReaderT WebState m (Maybe Balance)
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader (Address -> CacheT (DatabaseReaderT m) (Maybe Balance)
forall (m :: * -> *).
StoreReadBase m =>
Address -> m (Maybe Balance)
getBalance Address
a)
    getMempool :: ReaderT WebState m [(Word64, TxHash)]
getMempool = CacheT (DatabaseReaderT m) [(Word64, TxHash)]
-> ReaderT WebState m [(Word64, TxHash)]
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader CacheT (DatabaseReaderT m) [(Word64, TxHash)]
forall (m :: * -> *). StoreReadBase m => m [(Word64, TxHash)]
getMempool

instance (MonadUnliftIO m, MonadLoggerIO m) =>
         StoreReadExtra (ReaderT WebState m) where
    getMaxGap :: ReaderT WebState m Word32
getMaxGap = CacheT (DatabaseReaderT m) Word32 -> ReaderT WebState m Word32
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader CacheT (DatabaseReaderT m) Word32
forall (m :: * -> *). StoreReadExtra m => m Word32
getMaxGap
    getInitialGap :: ReaderT WebState m Word32
getInitialGap = CacheT (DatabaseReaderT m) Word32 -> ReaderT WebState m Word32
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader CacheT (DatabaseReaderT m) Word32
forall (m :: * -> *). StoreReadExtra m => m Word32
getInitialGap
    getBalances :: [Address] -> ReaderT WebState m [Balance]
getBalances as :: [Address]
as = CacheT (DatabaseReaderT m) [Balance]
-> ReaderT WebState m [Balance]
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader ([Address] -> CacheT (DatabaseReaderT m) [Balance]
forall (m :: * -> *). StoreReadExtra m => [Address] -> m [Balance]
getBalances [Address]
as)
    getAddressesTxs :: [Address] -> Limits -> ReaderT WebState m [TxRef]
getAddressesTxs as :: [Address]
as = CacheT (DatabaseReaderT m) [TxRef] -> ReaderT WebState m [TxRef]
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader (CacheT (DatabaseReaderT m) [TxRef] -> ReaderT WebState m [TxRef])
-> (Limits -> CacheT (DatabaseReaderT m) [TxRef])
-> Limits
-> ReaderT WebState m [TxRef]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Address] -> Limits -> CacheT (DatabaseReaderT m) [TxRef]
forall (m :: * -> *).
StoreReadExtra m =>
[Address] -> Limits -> m [TxRef]
getAddressesTxs [Address]
as
    getAddressesUnspents :: [Address] -> Limits -> ReaderT WebState m [Unspent]
getAddressesUnspents as :: [Address]
as = CacheT (DatabaseReaderT m) [Unspent]
-> ReaderT WebState m [Unspent]
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader (CacheT (DatabaseReaderT m) [Unspent]
 -> ReaderT WebState m [Unspent])
-> (Limits -> CacheT (DatabaseReaderT m) [Unspent])
-> Limits
-> ReaderT WebState m [Unspent]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Address] -> Limits -> CacheT (DatabaseReaderT m) [Unspent]
forall (m :: * -> *).
StoreReadExtra m =>
[Address] -> Limits -> m [Unspent]
getAddressesUnspents [Address]
as
    xPubBals :: XPubSpec -> ReaderT WebState m [XPubBal]
xPubBals = CacheT (DatabaseReaderT m) [XPubBal]
-> ReaderT WebState m [XPubBal]
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader (CacheT (DatabaseReaderT m) [XPubBal]
 -> ReaderT WebState m [XPubBal])
-> (XPubSpec -> CacheT (DatabaseReaderT m) [XPubBal])
-> XPubSpec
-> ReaderT WebState m [XPubBal]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XPubSpec -> CacheT (DatabaseReaderT m) [XPubBal]
forall (m :: * -> *). StoreReadExtra m => XPubSpec -> m [XPubBal]
xPubBals
    xPubSummary :: XPubSpec -> ReaderT WebState m XPubSummary
xPubSummary = CacheT (DatabaseReaderT m) XPubSummary
-> ReaderT WebState m XPubSummary
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader (CacheT (DatabaseReaderT m) XPubSummary
 -> ReaderT WebState m XPubSummary)
-> (XPubSpec -> CacheT (DatabaseReaderT m) XPubSummary)
-> XPubSpec
-> ReaderT WebState m XPubSummary
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XPubSpec -> CacheT (DatabaseReaderT m) XPubSummary
forall (m :: * -> *). StoreReadExtra m => XPubSpec -> m XPubSummary
xPubSummary
    xPubUnspents :: XPubSpec -> Limits -> ReaderT WebState m [XPubUnspent]
xPubUnspents xpub :: XPubSpec
xpub = CacheT (DatabaseReaderT m) [XPubUnspent]
-> ReaderT WebState m [XPubUnspent]
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader (CacheT (DatabaseReaderT m) [XPubUnspent]
 -> ReaderT WebState m [XPubUnspent])
-> (Limits -> CacheT (DatabaseReaderT m) [XPubUnspent])
-> Limits
-> ReaderT WebState m [XPubUnspent]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XPubSpec -> Limits -> CacheT (DatabaseReaderT m) [XPubUnspent]
forall (m :: * -> *).
StoreReadExtra m =>
XPubSpec -> Limits -> m [XPubUnspent]
xPubUnspents XPubSpec
xpub
    xPubTxs :: XPubSpec -> Limits -> ReaderT WebState m [TxRef]
xPubTxs xpub :: XPubSpec
xpub = CacheT (DatabaseReaderT m) [TxRef] -> ReaderT WebState m [TxRef]
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader (CacheT (DatabaseReaderT m) [TxRef] -> ReaderT WebState m [TxRef])
-> (Limits -> CacheT (DatabaseReaderT m) [TxRef])
-> Limits
-> ReaderT WebState m [TxRef]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XPubSpec -> Limits -> CacheT (DatabaseReaderT m) [TxRef]
forall (m :: * -> *).
StoreReadExtra m =>
XPubSpec -> Limits -> m [TxRef]
xPubTxs XPubSpec
xpub
    getNumTxData :: Word64 -> ReaderT WebState m [TxData]
getNumTxData = CacheT (DatabaseReaderT m) [TxData] -> ReaderT WebState m [TxData]
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader (CacheT (DatabaseReaderT m) [TxData]
 -> ReaderT WebState m [TxData])
-> (Word64 -> CacheT (DatabaseReaderT m) [TxData])
-> Word64
-> ReaderT WebState m [TxData]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> CacheT (DatabaseReaderT m) [TxData]
forall (m :: * -> *). StoreReadExtra m => Word64 -> m [TxData]
getNumTxData

instance (MonadUnliftIO m, MonadLoggerIO m) => StoreReadBase (WebT m) where
    getNetwork :: WebT m Network
getNetwork = ReaderT WebState m Network -> WebT m Network
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ReaderT WebState m Network
forall (m :: * -> *). StoreReadBase m => m Network
getNetwork
    getBestBlock :: WebT m (Maybe BlockHash)
getBestBlock = ReaderT WebState m (Maybe BlockHash) -> WebT m (Maybe BlockHash)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ReaderT WebState m (Maybe BlockHash)
forall (m :: * -> *). StoreReadBase m => m (Maybe BlockHash)
getBestBlock
    getBlocksAtHeight :: Word32 -> WebT m [BlockHash]
getBlocksAtHeight = ReaderT WebState m [BlockHash] -> WebT m [BlockHash]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m [BlockHash] -> WebT m [BlockHash])
-> (Word32 -> ReaderT WebState m [BlockHash])
-> Word32
-> WebT m [BlockHash]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word32 -> ReaderT WebState m [BlockHash]
forall (m :: * -> *). StoreReadBase m => Word32 -> m [BlockHash]
getBlocksAtHeight
    getBlock :: BlockHash -> WebT m (Maybe BlockData)
getBlock = ReaderT WebState m (Maybe BlockData) -> WebT m (Maybe BlockData)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m (Maybe BlockData) -> WebT m (Maybe BlockData))
-> (BlockHash -> ReaderT WebState m (Maybe BlockData))
-> BlockHash
-> WebT m (Maybe BlockData)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BlockHash -> ReaderT WebState m (Maybe BlockData)
forall (m :: * -> *).
StoreReadBase m =>
BlockHash -> m (Maybe BlockData)
getBlock
    getTxData :: TxHash -> WebT m (Maybe TxData)
getTxData = ReaderT WebState m (Maybe TxData) -> WebT m (Maybe TxData)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m (Maybe TxData) -> WebT m (Maybe TxData))
-> (TxHash -> ReaderT WebState m (Maybe TxData))
-> TxHash
-> WebT m (Maybe TxData)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxHash -> ReaderT WebState m (Maybe TxData)
forall (m :: * -> *). StoreReadBase m => TxHash -> m (Maybe TxData)
getTxData
    getSpender :: OutPoint -> WebT m (Maybe Spender)
getSpender = ReaderT WebState m (Maybe Spender) -> WebT m (Maybe Spender)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m (Maybe Spender) -> WebT m (Maybe Spender))
-> (OutPoint -> ReaderT WebState m (Maybe Spender))
-> OutPoint
-> WebT m (Maybe Spender)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OutPoint -> ReaderT WebState m (Maybe Spender)
forall (m :: * -> *).
StoreReadBase m =>
OutPoint -> m (Maybe Spender)
getSpender
    getUnspent :: OutPoint -> WebT m (Maybe Unspent)
getUnspent = ReaderT WebState m (Maybe Unspent) -> WebT m (Maybe Unspent)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m (Maybe Unspent) -> WebT m (Maybe Unspent))
-> (OutPoint -> ReaderT WebState m (Maybe Unspent))
-> OutPoint
-> WebT m (Maybe Unspent)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OutPoint -> ReaderT WebState m (Maybe Unspent)
forall (m :: * -> *).
StoreReadBase m =>
OutPoint -> m (Maybe Unspent)
getUnspent
    getBalance :: Address -> WebT m (Maybe Balance)
getBalance = ReaderT WebState m (Maybe Balance) -> WebT m (Maybe Balance)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m (Maybe Balance) -> WebT m (Maybe Balance))
-> (Address -> ReaderT WebState m (Maybe Balance))
-> Address
-> WebT m (Maybe Balance)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Address -> ReaderT WebState m (Maybe Balance)
forall (m :: * -> *).
StoreReadBase m =>
Address -> m (Maybe Balance)
getBalance
    getMempool :: WebT m [(Word64, TxHash)]
getMempool = ReaderT WebState m [(Word64, TxHash)] -> WebT m [(Word64, TxHash)]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ReaderT WebState m [(Word64, TxHash)]
forall (m :: * -> *). StoreReadBase m => m [(Word64, TxHash)]
getMempool

instance (MonadUnliftIO m, MonadLoggerIO m) => StoreReadExtra (WebT m) where
    getBalances :: [Address] -> WebT m [Balance]
getBalances = ReaderT WebState m [Balance] -> WebT m [Balance]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m [Balance] -> WebT m [Balance])
-> ([Address] -> ReaderT WebState m [Balance])
-> [Address]
-> WebT m [Balance]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Address] -> ReaderT WebState m [Balance]
forall (m :: * -> *). StoreReadExtra m => [Address] -> m [Balance]
getBalances
    getAddressesTxs :: [Address] -> Limits -> WebT m [TxRef]
getAddressesTxs as :: [Address]
as = ReaderT WebState m [TxRef] -> WebT m [TxRef]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m [TxRef] -> WebT m [TxRef])
-> (Limits -> ReaderT WebState m [TxRef])
-> Limits
-> WebT m [TxRef]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Address] -> Limits -> ReaderT WebState m [TxRef]
forall (m :: * -> *).
StoreReadExtra m =>
[Address] -> Limits -> m [TxRef]
getAddressesTxs [Address]
as
    getAddressesUnspents :: [Address] -> Limits -> WebT m [Unspent]
getAddressesUnspents as :: [Address]
as = ReaderT WebState m [Unspent] -> WebT m [Unspent]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m [Unspent] -> WebT m [Unspent])
-> (Limits -> ReaderT WebState m [Unspent])
-> Limits
-> WebT m [Unspent]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Address] -> Limits -> ReaderT WebState m [Unspent]
forall (m :: * -> *).
StoreReadExtra m =>
[Address] -> Limits -> m [Unspent]
getAddressesUnspents [Address]
as
    xPubBals :: XPubSpec -> WebT m [XPubBal]
xPubBals = ReaderT WebState m [XPubBal] -> WebT m [XPubBal]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m [XPubBal] -> WebT m [XPubBal])
-> (XPubSpec -> ReaderT WebState m [XPubBal])
-> XPubSpec
-> WebT m [XPubBal]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XPubSpec -> ReaderT WebState m [XPubBal]
forall (m :: * -> *). StoreReadExtra m => XPubSpec -> m [XPubBal]
xPubBals
    xPubSummary :: XPubSpec -> WebT m XPubSummary
xPubSummary = ReaderT WebState m XPubSummary -> WebT m XPubSummary
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m XPubSummary -> WebT m XPubSummary)
-> (XPubSpec -> ReaderT WebState m XPubSummary)
-> XPubSpec
-> WebT m XPubSummary
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XPubSpec -> ReaderT WebState m XPubSummary
forall (m :: * -> *). StoreReadExtra m => XPubSpec -> m XPubSummary
xPubSummary
    xPubUnspents :: XPubSpec -> Limits -> WebT m [XPubUnspent]
xPubUnspents xpub :: XPubSpec
xpub = ReaderT WebState m [XPubUnspent] -> WebT m [XPubUnspent]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m [XPubUnspent] -> WebT m [XPubUnspent])
-> (Limits -> ReaderT WebState m [XPubUnspent])
-> Limits
-> WebT m [XPubUnspent]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XPubSpec -> Limits -> ReaderT WebState m [XPubUnspent]
forall (m :: * -> *).
StoreReadExtra m =>
XPubSpec -> Limits -> m [XPubUnspent]
xPubUnspents XPubSpec
xpub
    xPubTxs :: XPubSpec -> Limits -> WebT m [TxRef]
xPubTxs xpub :: XPubSpec
xpub = ReaderT WebState m [TxRef] -> WebT m [TxRef]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m [TxRef] -> WebT m [TxRef])
-> (Limits -> ReaderT WebState m [TxRef])
-> Limits
-> WebT m [TxRef]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XPubSpec -> Limits -> ReaderT WebState m [TxRef]
forall (m :: * -> *).
StoreReadExtra m =>
XPubSpec -> Limits -> m [TxRef]
xPubTxs XPubSpec
xpub
    getMaxGap :: WebT m Word32
getMaxGap = ReaderT WebState m Word32 -> WebT m Word32
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ReaderT WebState m Word32
forall (m :: * -> *). StoreReadExtra m => m Word32
getMaxGap
    getInitialGap :: WebT m Word32
getInitialGap = ReaderT WebState m Word32 -> WebT m Word32
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ReaderT WebState m Word32
forall (m :: * -> *). StoreReadExtra m => m Word32
getInitialGap
    getNumTxData :: Word64 -> WebT m [TxData]
getNumTxData = ReaderT WebState m [TxData] -> WebT m [TxData]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m [TxData] -> WebT m [TxData])
-> (Word64 -> ReaderT WebState m [TxData])
-> Word64
-> WebT m [TxData]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> ReaderT WebState m [TxData]
forall (m :: * -> *). StoreReadExtra m => Word64 -> m [TxData]
getNumTxData

-------------------
-- Path Handlers --
-------------------

runWeb :: (MonadUnliftIO m, MonadLoggerIO m) => WebConfig -> m ()
runWeb :: WebConfig -> m ()
runWeb cfg :: WebConfig
cfg@WebConfig{ webHost :: WebConfig -> String
webHost = String
host
                    , webPort :: WebConfig -> Int
webPort = Int
port
                    , webStore :: WebConfig -> Store
webStore = Store
store
                    , webStats :: WebConfig -> Maybe Store
webStats = Maybe Store
stats
                    } = do
    TVar (HashMap Text BinfoTicker)
ticker <- HashMap Text BinfoTicker -> m (TVar (HashMap Text BinfoTicker))
forall (m :: * -> *) a. MonadIO m => a -> m (TVar a)
newTVarIO HashMap Text BinfoTicker
forall k v. HashMap k v
HashMap.empty
    Maybe WebMetrics
metrics <- (Store -> m WebMetrics) -> Maybe Store -> m (Maybe WebMetrics)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Store -> m WebMetrics
forall (m :: * -> *). MonadIO m => Store -> m WebMetrics
createMetrics Maybe Store
stats
    let st :: WebState
st = $WWebState :: WebConfig
-> TVar (HashMap Text BinfoTicker) -> Maybe WebMetrics -> WebState
WebState { webConfig :: WebConfig
webConfig = WebConfig
cfg, webTicker :: TVar (HashMap Text BinfoTicker)
webTicker = TVar (HashMap Text BinfoTicker)
ticker, webMetrics :: Maybe WebMetrics
webMetrics = Maybe WebMetrics
metrics }
    m () -> (Async () -> m ()) -> m ()
forall (m :: * -> *) a b.
MonadUnliftIO m =>
m a -> (Async a -> m b) -> m b
withAsync (Network -> TVar (HashMap Text BinfoTicker) -> m ()
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
Network -> TVar (HashMap Text BinfoTicker) -> m ()
price (Store -> Network
storeNetwork Store
store) TVar (HashMap Text BinfoTicker)
ticker) ((Async () -> m ()) -> m ()) -> (Async () -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \_ -> do
        Middleware
reqLogger <- m Middleware
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
m Middleware
logIt
        m Response -> IO Response
runner <- m (m Response -> IO Response)
forall (m :: * -> *) a. MonadUnliftIO m => m (m a -> IO a)
askRunInIO
        Options
-> (ReaderT WebState m Response -> IO Response)
-> ScottyT Except (ReaderT WebState m) ()
-> m ()
forall (m :: * -> *) (n :: * -> *) e.
(Monad m, MonadIO n) =>
Options -> (m Response -> IO Response) -> ScottyT e m () -> n ()
S.scottyOptsT Options
opts (m Response -> IO Response
runner (m Response -> IO Response)
-> (ReaderT WebState m Response -> m Response)
-> ReaderT WebState m Response
-> IO Response
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ReaderT WebState m Response -> WebState -> m Response
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
`runReaderT` WebState
st)) (ScottyT Except (ReaderT WebState m) () -> m ())
-> ScottyT Except (ReaderT WebState m) () -> m ()
forall a b. (a -> b) -> a -> b
$ do
            Middleware -> ScottyT Except (ReaderT WebState m) ()
forall e (m :: * -> *). Middleware -> ScottyT e m ()
S.middleware Middleware
reqLogger
            (Except -> ActionT Except (ReaderT WebState m) ())
-> ScottyT Except (ReaderT WebState m) ()
forall e (m :: * -> *).
(ScottyError e, Monad m) =>
(e -> ActionT e m ()) -> ScottyT e m ()
S.defaultHandler Except -> ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *). Monad m => Except -> WebT m ()
defHandler
            ScottyT Except (ReaderT WebState m) ()
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
ScottyT Except (ReaderT WebState m) ()
handlePaths
            ActionT Except (ReaderT WebState m) ()
-> ScottyT Except (ReaderT WebState m) ()
forall e (m :: * -> *).
(ScottyError e, MonadIO m) =>
ActionT e m () -> ScottyT e m ()
S.notFound (ActionT Except (ReaderT WebState m) ()
 -> ScottyT Except (ReaderT WebState m) ())
-> ActionT Except (ReaderT WebState m) ()
-> ScottyT Except (ReaderT WebState m) ()
forall a b. (a -> b) -> a -> b
$ Except -> ActionT Except (ReaderT WebState m) ()
forall e (m :: * -> *) a.
(ScottyError e, Monad m) =>
e -> ActionT e m a
S.raise Except
ThingNotFound
  where
    opts :: Options
opts = Options
forall a. Default a => a
def {settings :: Settings
S.settings = Settings -> Settings
settings Settings
defaultSettings}
    settings :: Settings -> Settings
settings = Int -> Settings -> Settings
setPort Int
port (Settings -> Settings)
-> (Settings -> Settings) -> Settings -> Settings
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HostPreference -> Settings -> Settings
setHost (String -> HostPreference
forall a. IsString a => String -> a
fromString String
host)

price :: (MonadUnliftIO m, MonadLoggerIO m)
      => Network
      -> TVar (HashMap Text BinfoTicker)
      -> m ()
price :: Network -> TVar (HashMap Text BinfoTicker) -> m ()
price net :: Network
net v :: TVar (HashMap Text BinfoTicker)
v =
    case Maybe String
code of
        Nothing -> () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
        Just s :: String
s  -> String -> m ()
forall (f :: * -> *) b.
(MonadUnliftIO f, MonadLogger f) =>
String -> f b
go String
s
  where
    code :: Maybe String
code | Network
net Network -> Network -> Bool
forall a. Eq a => a -> a -> Bool
== Network
btc = String -> Maybe String
forall a. a -> Maybe a
Just "btc"
         | Network
net Network -> Network -> Bool
forall a. Eq a => a -> a -> Bool
== Network
bch = String -> Maybe String
forall a. a -> Maybe a
Just "bch"
         | Bool
otherwise = Maybe String
forall a. Maybe a
Nothing
    go :: String -> f b
go s :: String
s = f () -> f b
forall (f :: * -> *) a b. Applicative f => f a -> f b
forever (f () -> f b) -> f () -> f b
forall a b. (a -> b) -> a -> b
$ do
        let err :: a -> m ()
err e :: a
e = $(Text
LogLevel
String -> String -> String -> CharPos -> CharPos -> Loc
Loc -> Text -> LogLevel -> Text -> m ()
forall (m :: * -> *) msg.
(MonadLogger m, ToLogStr msg) =>
Loc -> Text -> LogLevel -> msg -> m ()
monadLoggerLog :: forall (m :: * -> *) msg.
(MonadLogger m, ToLogStr msg) =>
Loc -> Text -> LogLevel -> msg -> m ()
b :: Text
a :: Text
logErrorS) "Price" (Text -> m ()) -> Text -> m ()
forall a b. (a -> b) -> a -> b
$ String -> Text
forall a b. ConvertibleStrings a b => a -> b
cs (a -> String
forall a. Show a => a -> String
show a
e)
            url :: String
url = "https://api.blockchain.info/ticker" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> "?" String -> ShowS
forall a. Semigroup a => a -> a -> a
<>
                  "base" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> "=" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
s
        (SomeException -> f ()) -> f () -> f ()
forall (m :: * -> *) a.
MonadUnliftIO m =>
(SomeException -> m a) -> m a -> m a
handleAny SomeException -> f ()
forall (m :: * -> *) a. (MonadLogger m, Show a) => a -> m ()
err (f () -> f ()) -> f () -> f ()
forall a b. (a -> b) -> a -> b
$ do
            Response (HashMap Text BinfoTicker)
r <- IO (Response (HashMap Text BinfoTicker))
-> f (Response (HashMap Text BinfoTicker))
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Response (HashMap Text BinfoTicker))
 -> f (Response (HashMap Text BinfoTicker)))
-> IO (Response (HashMap Text BinfoTicker))
-> f (Response (HashMap Text BinfoTicker))
forall a b. (a -> b) -> a -> b
$ Response ByteString -> IO (Response (HashMap Text BinfoTicker))
forall (m :: * -> *) a.
(MonadThrow m, FromJSON a) =>
Response ByteString -> m (Response a)
Wreq.asJSON (Response ByteString -> IO (Response (HashMap Text BinfoTicker)))
-> IO (Response ByteString)
-> IO (Response (HashMap Text BinfoTicker))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< String -> IO (Response ByteString)
Wreq.get String
url
            STM () -> f ()
forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically (STM () -> f ())
-> (HashMap Text BinfoTicker -> STM ())
-> HashMap Text BinfoTicker
-> f ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TVar (HashMap Text BinfoTicker)
-> HashMap Text BinfoTicker -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar TVar (HashMap Text BinfoTicker)
v (HashMap Text BinfoTicker -> f ())
-> HashMap Text BinfoTicker -> f ()
forall a b. (a -> b) -> a -> b
$ Response (HashMap Text BinfoTicker)
r Response (HashMap Text BinfoTicker)
-> Getting
     (HashMap Text BinfoTicker)
     (Response (HashMap Text BinfoTicker))
     (HashMap Text BinfoTicker)
-> HashMap Text BinfoTicker
forall s a. s -> Getting a s a -> a
^. Getting
  (HashMap Text BinfoTicker)
  (Response (HashMap Text BinfoTicker))
  (HashMap Text BinfoTicker)
forall body0 body1.
Lens (Response body0) (Response body1) body0 body1
Wreq.responseBody
        Int -> f ()
forall (m :: * -> *). MonadIO m => Int -> m ()
threadDelay (Int -> f ()) -> Int -> f ()
forall a b. (a -> b) -> a -> b
$ 5 Int -> Int -> Int
forall a. Num a => a -> a -> a
* 60 Int -> Int -> Int
forall a. Num a => a -> a -> a
* 1000 Int -> Int -> Int
forall a. Num a => a -> a -> a
* 1000 -- five minutes

raise_ :: MonadIO m => Except -> WebT m a
raise_ :: Except -> WebT m a
raise_ err :: Except
err =
    ReaderT WebState m (Maybe WebMetrics)
-> ActionT Except (ReaderT WebState m) (Maybe WebMetrics)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ((WebState -> Maybe WebMetrics)
-> ReaderT WebState m (Maybe WebMetrics)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WebState -> Maybe WebMetrics
webMetrics) ActionT Except (ReaderT WebState m) (Maybe WebMetrics)
-> (Maybe WebMetrics -> WebT m a) -> WebT m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Nothing -> Except -> WebT m a
forall e (m :: * -> *) a.
(ScottyError e, Monad m) =>
e -> ActionT e m a
S.raise Except
err
    Just metrics :: WebMetrics
metrics -> do
        let status :: Status
status = Except -> Status
errStatus Except
err
        if | Status -> Bool
statusIsClientError Status
status -> IO () -> ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ActionT Except (ReaderT WebState m) ())
-> IO () -> ActionT Except (ReaderT WebState m) ()
forall a b. (a -> b) -> a -> b
$
                 Counter -> IO ()
Metrics.Counter.inc (ErrorCounter -> Counter
clientErrors (WebMetrics -> ErrorCounter
everyError WebMetrics
metrics))
           | Status -> Bool
statusIsServerError Status
status -> IO () -> ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ActionT Except (ReaderT WebState m) ())
-> IO () -> ActionT Except (ReaderT WebState m) ()
forall a b. (a -> b) -> a -> b
$
                 Counter -> IO ()
Metrics.Counter.inc (ErrorCounter -> Counter
serverErrors (WebMetrics -> ErrorCounter
everyError WebMetrics
metrics))
           | Bool
otherwise ->
                 () -> ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
        Except -> WebT m a
forall e (m :: * -> *) a.
(ScottyError e, Monad m) =>
e -> ActionT e m a
S.raise Except
err

raise :: MonadIO m => (WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise :: (WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise metric :: WebMetrics -> ErrorCounter
metric err :: Except
err =
    ReaderT WebState m (Maybe WebMetrics)
-> ActionT Except (ReaderT WebState m) (Maybe WebMetrics)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ((WebState -> Maybe WebMetrics)
-> ReaderT WebState m (Maybe WebMetrics)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WebState -> Maybe WebMetrics
webMetrics) ActionT Except (ReaderT WebState m) (Maybe WebMetrics)
-> (Maybe WebMetrics -> WebT m a) -> WebT m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Nothing -> Except -> WebT m a
forall e (m :: * -> *) a.
(ScottyError e, Monad m) =>
e -> ActionT e m a
S.raise Except
err
    Just metrics :: WebMetrics
metrics -> do
        let status :: Status
status = Except -> Status
errStatus Except
err
        if | Status -> Bool
statusIsClientError Status
status -> IO () -> ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ActionT Except (ReaderT WebState m) ())
-> IO () -> ActionT Except (ReaderT WebState m) ()
forall a b. (a -> b) -> a -> b
$ do
                 Counter -> IO ()
Metrics.Counter.inc (ErrorCounter -> Counter
clientErrors (WebMetrics -> ErrorCounter
everyError WebMetrics
metrics))
                 Counter -> IO ()
Metrics.Counter.inc (ErrorCounter -> Counter
clientErrors (WebMetrics -> ErrorCounter
metric WebMetrics
metrics))
           | Status -> Bool
statusIsServerError Status
status -> IO () -> ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ActionT Except (ReaderT WebState m) ())
-> IO () -> ActionT Except (ReaderT WebState m) ()
forall a b. (a -> b) -> a -> b
$ do
                 Counter -> IO ()
Metrics.Counter.inc (ErrorCounter -> Counter
serverErrors (WebMetrics -> ErrorCounter
everyError WebMetrics
metrics))
                 Counter -> IO ()
Metrics.Counter.inc (ErrorCounter -> Counter
serverErrors (WebMetrics -> ErrorCounter
metric WebMetrics
metrics))
           | Bool
otherwise ->
                 () -> ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
        Except -> WebT m a
forall e (m :: * -> *) a.
(ScottyError e, Monad m) =>
e -> ActionT e m a
S.raise Except
err

errStatus :: Except -> Status
errStatus :: Except -> Status
errStatus ThingNotFound     = Status
status404
errStatus BadRequest        = Status
status400
errStatus UserError{}       = Status
status400
errStatus StringError{}     = Status
status400
errStatus ServerError       = Status
status500
errStatus TxIndexConflict{} = Status
status409

defHandler :: Monad m => Except -> WebT m ()
defHandler :: Except -> WebT m ()
defHandler e :: Except
e = do
    SerialAs
proto <- Bool -> ActionT Except (ReaderT WebState m) SerialAs
forall (m :: * -> *). Monad m => Bool -> ActionT Except m SerialAs
setupContentType Bool
False
    Status -> WebT m ()
forall (m :: * -> *) e. Monad m => Status -> ActionT e m ()
S.status (Status -> WebT m ()) -> Status -> WebT m ()
forall a b. (a -> b) -> a -> b
$ Except -> Status
errStatus Except
e
    ByteString -> WebT m ()
forall (m :: * -> *) e. Monad m => ByteString -> ActionT e m ()
S.raw (ByteString -> WebT m ()) -> ByteString -> WebT m ()
forall a b. (a -> b) -> a -> b
$ SerialAs
-> (Except -> Encoding)
-> (Except -> Value)
-> Except
-> ByteString
forall a.
Serial a =>
SerialAs -> (a -> Encoding) -> (a -> Value) -> a -> ByteString
protoSerial SerialAs
proto Except -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding Except -> Value
forall a. ToJSON a => a -> Value
toJSON Except
e

handlePaths :: (MonadUnliftIO m, MonadLoggerIO m)
            => S.ScottyT Except (ReaderT WebState m) ()
handlePaths :: ScottyT Except (ReaderT WebState m) ()
handlePaths = do
    -- Block Paths
    WebT m GetBlock
-> (GetBlock -> WebT m BlockData)
-> (Network -> BlockData -> Encoding)
-> (Network -> BlockData -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty
        (BlockHash -> NoTx -> GetBlock
GetBlock (BlockHash -> NoTx -> GetBlock)
-> ActionT Except (ReaderT WebState m) BlockHash
-> ActionT Except (ReaderT WebState m) (NoTx -> GetBlock)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) BlockHash
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy ActionT Except (ReaderT WebState m) (NoTx -> GetBlock)
-> ActionT Except (ReaderT WebState m) NoTx -> WebT m GetBlock
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) NoTx
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef)
        GetBlock -> WebT m BlockData
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetBlock -> WebT m BlockData
scottyBlock
        Network -> BlockData -> Encoding
blockDataToEncoding
        Network -> BlockData -> Value
blockDataToJSON
    WebT m GetBlocks
-> (GetBlocks -> WebT m (SerialList BlockData))
-> (Network -> SerialList BlockData -> Encoding)
-> (Network -> SerialList BlockData -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        ([BlockHash] -> NoTx -> GetBlocks
GetBlocks ([BlockHash] -> NoTx -> GetBlocks)
-> ActionT Except (ReaderT WebState m) [BlockHash]
-> ActionT Except (ReaderT WebState m) (NoTx -> GetBlocks)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) [BlockHash]
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
param ActionT Except (ReaderT WebState m) (NoTx -> GetBlocks)
-> ActionT Except (ReaderT WebState m) NoTx -> WebT m GetBlocks
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) NoTx
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef)
        (([BlockData] -> SerialList BlockData)
-> ActionT Except (ReaderT WebState m) [BlockData]
-> WebT m (SerialList BlockData)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [BlockData] -> SerialList BlockData
forall a. [a] -> SerialList a
SerialList (ActionT Except (ReaderT WebState m) [BlockData]
 -> WebT m (SerialList BlockData))
-> (GetBlocks -> ActionT Except (ReaderT WebState m) [BlockData])
-> GetBlocks
-> WebT m (SerialList BlockData)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GetBlocks -> ActionT Except (ReaderT WebState m) [BlockData]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetBlocks -> WebT m [BlockData]
scottyBlocks)
        (\n :: Network
n -> (BlockData -> Encoding) -> [BlockData] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
list (Network -> BlockData -> Encoding
blockDataToEncoding Network
n) ([BlockData] -> Encoding)
-> (SerialList BlockData -> [BlockData])
-> SerialList BlockData
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialList BlockData -> [BlockData]
forall a. SerialList a -> [a]
getSerialList)
        (\n :: Network
n -> (Network -> BlockData -> Value) -> Network -> [BlockData] -> Value
forall a t a. ToJSON a => (t -> a -> a) -> t -> [a] -> Value
json_list Network -> BlockData -> Value
blockDataToJSON  Network
n ([BlockData] -> Value)
-> (SerialList BlockData -> [BlockData])
-> SerialList BlockData
-> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialList BlockData -> [BlockData]
forall a. SerialList a -> [a]
getSerialList)
    WebT m GetBlockRaw
-> (GetBlockRaw -> WebT m (RawResult Block))
-> (Network -> RawResult Block -> Encoding)
-> (Network -> RawResult Block -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        (BlockHash -> GetBlockRaw
GetBlockRaw (BlockHash -> GetBlockRaw)
-> ActionT Except (ReaderT WebState m) BlockHash
-> WebT m GetBlockRaw
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) BlockHash
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy)
        GetBlockRaw -> WebT m (RawResult Block)
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetBlockRaw -> WebT m (RawResult Block)
scottyBlockRaw
        ((RawResult Block -> Encoding)
-> Network -> RawResult Block -> Encoding
forall a b. a -> b -> a
const RawResult Block -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
        ((RawResult Block -> Value) -> Network -> RawResult Block -> Value
forall a b. a -> b -> a
const RawResult Block -> Value
forall a. ToJSON a => a -> Value
toJSON)
    WebT m GetBlockBest
-> (GetBlockBest -> WebT m BlockData)
-> (Network -> BlockData -> Encoding)
-> (Network -> BlockData -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty
        (NoTx -> GetBlockBest
GetBlockBest (NoTx -> GetBlockBest)
-> ActionT Except (ReaderT WebState m) NoTx -> WebT m GetBlockBest
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) NoTx
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef)
        GetBlockBest -> WebT m BlockData
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetBlockBest -> WebT m BlockData
scottyBlockBest
        Network -> BlockData -> Encoding
blockDataToEncoding
        Network -> BlockData -> Value
blockDataToJSON
    WebT m GetBlockBestRaw
-> (GetBlockBestRaw -> WebT m (RawResult Block))
-> (Network -> RawResult Block -> Encoding)
-> (Network -> RawResult Block -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        (GetBlockBestRaw
GetBlockBestRaw GetBlockBestRaw
-> (GetBlockBestRaw -> WebT m GetBlockBestRaw)
-> WebT m GetBlockBestRaw
forall a b. a -> (a -> b) -> b
& GetBlockBestRaw -> WebT m GetBlockBestRaw
forall (m :: * -> *) a. Monad m => a -> m a
return)
        GetBlockBestRaw -> WebT m (RawResult Block)
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetBlockBestRaw -> WebT m (RawResult Block)
scottyBlockBestRaw
        ((RawResult Block -> Encoding)
-> Network -> RawResult Block -> Encoding
forall a b. a -> b -> a
const RawResult Block -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
        ((RawResult Block -> Value) -> Network -> RawResult Block -> Value
forall a b. a -> b -> a
const RawResult Block -> Value
forall a. ToJSON a => a -> Value
toJSON)
    WebT m GetBlockLatest
-> (GetBlockLatest -> WebT m (SerialList BlockData))
-> (Network -> SerialList BlockData -> Encoding)
-> (Network -> SerialList BlockData -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        (NoTx -> GetBlockLatest
GetBlockLatest (NoTx -> GetBlockLatest)
-> ActionT Except (ReaderT WebState m) NoTx
-> WebT m GetBlockLatest
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) NoTx
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef)
        (([BlockData] -> SerialList BlockData)
-> ActionT Except (ReaderT WebState m) [BlockData]
-> WebT m (SerialList BlockData)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [BlockData] -> SerialList BlockData
forall a. [a] -> SerialList a
SerialList (ActionT Except (ReaderT WebState m) [BlockData]
 -> WebT m (SerialList BlockData))
-> (GetBlockLatest
    -> ActionT Except (ReaderT WebState m) [BlockData])
-> GetBlockLatest
-> WebT m (SerialList BlockData)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GetBlockLatest -> ActionT Except (ReaderT WebState m) [BlockData]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetBlockLatest -> WebT m [BlockData]
scottyBlockLatest)
        (\n :: Network
n -> (BlockData -> Encoding) -> [BlockData] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
list (Network -> BlockData -> Encoding
blockDataToEncoding Network
n) ([BlockData] -> Encoding)
-> (SerialList BlockData -> [BlockData])
-> SerialList BlockData
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialList BlockData -> [BlockData]
forall a. SerialList a -> [a]
getSerialList)
        (\n :: Network
n -> (Network -> BlockData -> Value) -> Network -> [BlockData] -> Value
forall a t a. ToJSON a => (t -> a -> a) -> t -> [a] -> Value
json_list Network -> BlockData -> Value
blockDataToJSON Network
n ([BlockData] -> Value)
-> (SerialList BlockData -> [BlockData])
-> SerialList BlockData
-> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialList BlockData -> [BlockData]
forall a. SerialList a -> [a]
getSerialList)
    WebT m GetBlockHeight
-> (GetBlockHeight -> WebT m (SerialList BlockData))
-> (Network -> SerialList BlockData -> Encoding)
-> (Network -> SerialList BlockData -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty
        (HeightParam -> NoTx -> GetBlockHeight
GetBlockHeight (HeightParam -> NoTx -> GetBlockHeight)
-> ActionT Except (ReaderT WebState m) HeightParam
-> ActionT Except (ReaderT WebState m) (NoTx -> GetBlockHeight)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) HeightParam
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy ActionT Except (ReaderT WebState m) (NoTx -> GetBlockHeight)
-> ActionT Except (ReaderT WebState m) NoTx
-> WebT m GetBlockHeight
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) NoTx
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef)
        (([BlockData] -> SerialList BlockData)
-> ActionT Except (ReaderT WebState m) [BlockData]
-> WebT m (SerialList BlockData)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [BlockData] -> SerialList BlockData
forall a. [a] -> SerialList a
SerialList (ActionT Except (ReaderT WebState m) [BlockData]
 -> WebT m (SerialList BlockData))
-> (GetBlockHeight
    -> ActionT Except (ReaderT WebState m) [BlockData])
-> GetBlockHeight
-> WebT m (SerialList BlockData)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GetBlockHeight -> ActionT Except (ReaderT WebState m) [BlockData]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetBlockHeight -> WebT m [BlockData]
scottyBlockHeight)
        (\n :: Network
n -> (BlockData -> Encoding) -> [BlockData] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
list (Network -> BlockData -> Encoding
blockDataToEncoding Network
n) ([BlockData] -> Encoding)
-> (SerialList BlockData -> [BlockData])
-> SerialList BlockData
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialList BlockData -> [BlockData]
forall a. SerialList a -> [a]
getSerialList)
        (\n :: Network
n -> (Network -> BlockData -> Value) -> Network -> [BlockData] -> Value
forall a t a. ToJSON a => (t -> a -> a) -> t -> [a] -> Value
json_list Network -> BlockData -> Value
blockDataToJSON Network
n ([BlockData] -> Value)
-> (SerialList BlockData -> [BlockData])
-> SerialList BlockData
-> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialList BlockData -> [BlockData]
forall a. SerialList a -> [a]
getSerialList)
    WebT m GetBlockHeights
-> (GetBlockHeights -> WebT m (SerialList BlockData))
-> (Network -> SerialList BlockData -> Encoding)
-> (Network -> SerialList BlockData -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        (HeightsParam -> NoTx -> GetBlockHeights
GetBlockHeights (HeightsParam -> NoTx -> GetBlockHeights)
-> ActionT Except (ReaderT WebState m) HeightsParam
-> ActionT Except (ReaderT WebState m) (NoTx -> GetBlockHeights)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) HeightsParam
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
param ActionT Except (ReaderT WebState m) (NoTx -> GetBlockHeights)
-> ActionT Except (ReaderT WebState m) NoTx
-> WebT m GetBlockHeights
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) NoTx
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef)
        (([BlockData] -> SerialList BlockData)
-> ActionT Except (ReaderT WebState m) [BlockData]
-> WebT m (SerialList BlockData)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [BlockData] -> SerialList BlockData
forall a. [a] -> SerialList a
SerialList (ActionT Except (ReaderT WebState m) [BlockData]
 -> WebT m (SerialList BlockData))
-> (GetBlockHeights
    -> ActionT Except (ReaderT WebState m) [BlockData])
-> GetBlockHeights
-> WebT m (SerialList BlockData)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GetBlockHeights -> ActionT Except (ReaderT WebState m) [BlockData]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetBlockHeights -> WebT m [BlockData]
scottyBlockHeights)
        (\n :: Network
n -> (BlockData -> Encoding) -> [BlockData] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
list (Network -> BlockData -> Encoding
blockDataToEncoding Network
n) ([BlockData] -> Encoding)
-> (SerialList BlockData -> [BlockData])
-> SerialList BlockData
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialList BlockData -> [BlockData]
forall a. SerialList a -> [a]
getSerialList)
        (\n :: Network
n -> (Network -> BlockData -> Value) -> Network -> [BlockData] -> Value
forall a t a. ToJSON a => (t -> a -> a) -> t -> [a] -> Value
json_list Network -> BlockData -> Value
blockDataToJSON Network
n ([BlockData] -> Value)
-> (SerialList BlockData -> [BlockData])
-> SerialList BlockData
-> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialList BlockData -> [BlockData]
forall a. SerialList a -> [a]
getSerialList)
    WebT m GetBlockHeightRaw
-> (GetBlockHeightRaw -> WebT m (RawResultList Block))
-> (Network -> RawResultList Block -> Encoding)
-> (Network -> RawResultList Block -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        (HeightParam -> GetBlockHeightRaw
GetBlockHeightRaw (HeightParam -> GetBlockHeightRaw)
-> ActionT Except (ReaderT WebState m) HeightParam
-> WebT m GetBlockHeightRaw
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) HeightParam
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy)
        GetBlockHeightRaw -> WebT m (RawResultList Block)
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetBlockHeightRaw -> WebT m (RawResultList Block)
scottyBlockHeightRaw
        ((RawResultList Block -> Encoding)
-> Network -> RawResultList Block -> Encoding
forall a b. a -> b -> a
const RawResultList Block -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
        ((RawResultList Block -> Value)
-> Network -> RawResultList Block -> Value
forall a b. a -> b -> a
const RawResultList Block -> Value
forall a. ToJSON a => a -> Value
toJSON)
    WebT m GetBlockTime
-> (GetBlockTime -> WebT m BlockData)
-> (Network -> BlockData -> Encoding)
-> (Network -> BlockData -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty
        (TimeParam -> NoTx -> GetBlockTime
GetBlockTime (TimeParam -> NoTx -> GetBlockTime)
-> ActionT Except (ReaderT WebState m) TimeParam
-> ActionT Except (ReaderT WebState m) (NoTx -> GetBlockTime)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) TimeParam
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy ActionT Except (ReaderT WebState m) (NoTx -> GetBlockTime)
-> ActionT Except (ReaderT WebState m) NoTx -> WebT m GetBlockTime
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) NoTx
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef)
        GetBlockTime -> WebT m BlockData
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetBlockTime -> WebT m BlockData
scottyBlockTime
        Network -> BlockData -> Encoding
blockDataToEncoding
        Network -> BlockData -> Value
blockDataToJSON
    WebT m GetBlockTimeRaw
-> (GetBlockTimeRaw -> WebT m (RawResult Block))
-> (Network -> RawResult Block -> Encoding)
-> (Network -> RawResult Block -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        (TimeParam -> GetBlockTimeRaw
GetBlockTimeRaw (TimeParam -> GetBlockTimeRaw)
-> ActionT Except (ReaderT WebState m) TimeParam
-> WebT m GetBlockTimeRaw
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) TimeParam
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy)
        GetBlockTimeRaw -> WebT m (RawResult Block)
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetBlockTimeRaw -> WebT m (RawResult Block)
scottyBlockTimeRaw
        ((RawResult Block -> Encoding)
-> Network -> RawResult Block -> Encoding
forall a b. a -> b -> a
const RawResult Block -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
        ((RawResult Block -> Value) -> Network -> RawResult Block -> Value
forall a b. a -> b -> a
const RawResult Block -> Value
forall a. ToJSON a => a -> Value
toJSON)
    WebT m GetBlockMTP
-> (GetBlockMTP -> WebT m BlockData)
-> (Network -> BlockData -> Encoding)
-> (Network -> BlockData -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty
        (TimeParam -> NoTx -> GetBlockMTP
GetBlockMTP (TimeParam -> NoTx -> GetBlockMTP)
-> ActionT Except (ReaderT WebState m) TimeParam
-> ActionT Except (ReaderT WebState m) (NoTx -> GetBlockMTP)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) TimeParam
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy ActionT Except (ReaderT WebState m) (NoTx -> GetBlockMTP)
-> ActionT Except (ReaderT WebState m) NoTx -> WebT m GetBlockMTP
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) NoTx
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef)
        GetBlockMTP -> WebT m BlockData
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetBlockMTP -> WebT m BlockData
scottyBlockMTP
        Network -> BlockData -> Encoding
blockDataToEncoding
        Network -> BlockData -> Value
blockDataToJSON
    WebT m GetBlockMTPRaw
-> (GetBlockMTPRaw -> WebT m (RawResult Block))
-> (Network -> RawResult Block -> Encoding)
-> (Network -> RawResult Block -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        (TimeParam -> GetBlockMTPRaw
GetBlockMTPRaw (TimeParam -> GetBlockMTPRaw)
-> ActionT Except (ReaderT WebState m) TimeParam
-> WebT m GetBlockMTPRaw
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) TimeParam
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy)
        GetBlockMTPRaw -> WebT m (RawResult Block)
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetBlockMTPRaw -> WebT m (RawResult Block)
scottyBlockMTPRaw
        ((RawResult Block -> Encoding)
-> Network -> RawResult Block -> Encoding
forall a b. a -> b -> a
const RawResult Block -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
        ((RawResult Block -> Value) -> Network -> RawResult Block -> Value
forall a b. a -> b -> a
const RawResult Block -> Value
forall a. ToJSON a => a -> Value
toJSON)
    -- Transaction Paths
    WebT m GetTx
-> (GetTx -> WebT m Transaction)
-> (Network -> Transaction -> Encoding)
-> (Network -> Transaction -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty
        (TxHash -> GetTx
GetTx (TxHash -> GetTx)
-> ActionT Except (ReaderT WebState m) TxHash -> WebT m GetTx
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) TxHash
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy)
        GetTx -> WebT m Transaction
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetTx -> WebT m Transaction
scottyTx
        Network -> Transaction -> Encoding
transactionToEncoding
        Network -> Transaction -> Value
transactionToJSON
    WebT m GetTxs
-> (GetTxs -> WebT m (SerialList Transaction))
-> (Network -> SerialList Transaction -> Encoding)
-> (Network -> SerialList Transaction -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        ([TxHash] -> GetTxs
GetTxs ([TxHash] -> GetTxs)
-> ActionT Except (ReaderT WebState m) [TxHash] -> WebT m GetTxs
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) [TxHash]
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
param)
        (([Transaction] -> SerialList Transaction)
-> ActionT Except (ReaderT WebState m) [Transaction]
-> WebT m (SerialList Transaction)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Transaction] -> SerialList Transaction
forall a. [a] -> SerialList a
SerialList (ActionT Except (ReaderT WebState m) [Transaction]
 -> WebT m (SerialList Transaction))
-> (GetTxs -> ActionT Except (ReaderT WebState m) [Transaction])
-> GetTxs
-> WebT m (SerialList Transaction)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GetTxs -> ActionT Except (ReaderT WebState m) [Transaction]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetTxs -> WebT m [Transaction]
scottyTxs)
        (\n :: Network
n -> (Transaction -> Encoding) -> [Transaction] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
list (Network -> Transaction -> Encoding
transactionToEncoding Network
n) ([Transaction] -> Encoding)
-> (SerialList Transaction -> [Transaction])
-> SerialList Transaction
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialList Transaction -> [Transaction]
forall a. SerialList a -> [a]
getSerialList)
        (\n :: Network
n -> (Network -> Transaction -> Value)
-> Network -> [Transaction] -> Value
forall a t a. ToJSON a => (t -> a -> a) -> t -> [a] -> Value
json_list Network -> Transaction -> Value
transactionToJSON Network
n ([Transaction] -> Value)
-> (SerialList Transaction -> [Transaction])
-> SerialList Transaction
-> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialList Transaction -> [Transaction]
forall a. SerialList a -> [a]
getSerialList)
    WebT m GetTxRaw
-> (GetTxRaw -> WebT m (RawResult Tx))
-> (Network -> RawResult Tx -> Encoding)
-> (Network -> RawResult Tx -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        (TxHash -> GetTxRaw
GetTxRaw (TxHash -> GetTxRaw)
-> ActionT Except (ReaderT WebState m) TxHash -> WebT m GetTxRaw
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) TxHash
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy)
        GetTxRaw -> WebT m (RawResult Tx)
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetTxRaw -> WebT m (RawResult Tx)
scottyTxRaw
        ((RawResult Tx -> Encoding) -> Network -> RawResult Tx -> Encoding
forall a b. a -> b -> a
const RawResult Tx -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
        ((RawResult Tx -> Value) -> Network -> RawResult Tx -> Value
forall a b. a -> b -> a
const RawResult Tx -> Value
forall a. ToJSON a => a -> Value
toJSON)
    WebT m GetTxsRaw
-> (GetTxsRaw -> WebT m (RawResultList Tx))
-> (Network -> RawResultList Tx -> Encoding)
-> (Network -> RawResultList Tx -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        ([TxHash] -> GetTxsRaw
GetTxsRaw ([TxHash] -> GetTxsRaw)
-> ActionT Except (ReaderT WebState m) [TxHash] -> WebT m GetTxsRaw
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) [TxHash]
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
param)
        GetTxsRaw -> WebT m (RawResultList Tx)
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetTxsRaw -> WebT m (RawResultList Tx)
scottyTxsRaw
        ((RawResultList Tx -> Encoding)
-> Network -> RawResultList Tx -> Encoding
forall a b. a -> b -> a
const RawResultList Tx -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
        ((RawResultList Tx -> Value) -> Network -> RawResultList Tx -> Value
forall a b. a -> b -> a
const RawResultList Tx -> Value
forall a. ToJSON a => a -> Value
toJSON)
    WebT m GetTxsBlock
-> (GetTxsBlock -> WebT m (SerialList Transaction))
-> (Network -> SerialList Transaction -> Encoding)
-> (Network -> SerialList Transaction -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        (BlockHash -> GetTxsBlock
GetTxsBlock (BlockHash -> GetTxsBlock)
-> ActionT Except (ReaderT WebState m) BlockHash
-> WebT m GetTxsBlock
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) BlockHash
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy)
        (([Transaction] -> SerialList Transaction)
-> ActionT Except (ReaderT WebState m) [Transaction]
-> WebT m (SerialList Transaction)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Transaction] -> SerialList Transaction
forall a. [a] -> SerialList a
SerialList (ActionT Except (ReaderT WebState m) [Transaction]
 -> WebT m (SerialList Transaction))
-> (GetTxsBlock
    -> ActionT Except (ReaderT WebState m) [Transaction])
-> GetTxsBlock
-> WebT m (SerialList Transaction)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GetTxsBlock -> ActionT Except (ReaderT WebState m) [Transaction]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetTxsBlock -> WebT m [Transaction]
scottyTxsBlock)
        (\n :: Network
n -> (Transaction -> Encoding) -> [Transaction] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
list (Network -> Transaction -> Encoding
transactionToEncoding Network
n) ([Transaction] -> Encoding)
-> (SerialList Transaction -> [Transaction])
-> SerialList Transaction
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialList Transaction -> [Transaction]
forall a. SerialList a -> [a]
getSerialList)
        (\n :: Network
n -> (Network -> Transaction -> Value)
-> Network -> [Transaction] -> Value
forall a t a. ToJSON a => (t -> a -> a) -> t -> [a] -> Value
json_list Network -> Transaction -> Value
transactionToJSON Network
n ([Transaction] -> Value)
-> (SerialList Transaction -> [Transaction])
-> SerialList Transaction
-> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialList Transaction -> [Transaction]
forall a. SerialList a -> [a]
getSerialList)
    WebT m GetTxsBlockRaw
-> (GetTxsBlockRaw -> WebT m (RawResultList Tx))
-> (Network -> RawResultList Tx -> Encoding)
-> (Network -> RawResultList Tx -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        (BlockHash -> GetTxsBlockRaw
GetTxsBlockRaw (BlockHash -> GetTxsBlockRaw)
-> ActionT Except (ReaderT WebState m) BlockHash
-> WebT m GetTxsBlockRaw
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) BlockHash
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy)
        GetTxsBlockRaw -> WebT m (RawResultList Tx)
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetTxsBlockRaw -> WebT m (RawResultList Tx)
scottyTxsBlockRaw
        ((RawResultList Tx -> Encoding)
-> Network -> RawResultList Tx -> Encoding
forall a b. a -> b -> a
const RawResultList Tx -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
        ((RawResultList Tx -> Value) -> Network -> RawResultList Tx -> Value
forall a b. a -> b -> a
const RawResultList Tx -> Value
forall a. ToJSON a => a -> Value
toJSON)
    WebT m GetTxAfter
-> (GetTxAfter -> WebT m (GenericResult (Maybe Bool)))
-> (Network -> GenericResult (Maybe Bool) -> Encoding)
-> (Network -> GenericResult (Maybe Bool) -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        (TxHash -> HeightParam -> GetTxAfter
GetTxAfter (TxHash -> HeightParam -> GetTxAfter)
-> ActionT Except (ReaderT WebState m) TxHash
-> ActionT Except (ReaderT WebState m) (HeightParam -> GetTxAfter)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) TxHash
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy ActionT Except (ReaderT WebState m) (HeightParam -> GetTxAfter)
-> ActionT Except (ReaderT WebState m) HeightParam
-> WebT m GetTxAfter
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) HeightParam
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy)
        GetTxAfter -> WebT m (GenericResult (Maybe Bool))
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetTxAfter -> WebT m (GenericResult (Maybe Bool))
scottyTxAfter
        ((GenericResult (Maybe Bool) -> Encoding)
-> Network -> GenericResult (Maybe Bool) -> Encoding
forall a b. a -> b -> a
const GenericResult (Maybe Bool) -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
        ((GenericResult (Maybe Bool) -> Value)
-> Network -> GenericResult (Maybe Bool) -> Value
forall a b. a -> b -> a
const GenericResult (Maybe Bool) -> Value
forall a. ToJSON a => a -> Value
toJSON)
    WebT m PostTx
-> (PostTx -> WebT m TxId)
-> (Network -> TxId -> Encoding)
-> (Network -> TxId -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        (Tx -> PostTx
PostTx (Tx -> PostTx)
-> ActionT Except (ReaderT WebState m) Tx -> WebT m PostTx
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) Tx
forall (m :: * -> *) a. (MonadIO m, Serial a) => WebT m a
parseBody)
        PostTx -> WebT m TxId
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
PostTx -> WebT m TxId
scottyPostTx
        ((TxId -> Encoding) -> Network -> TxId -> Encoding
forall a b. a -> b -> a
const TxId -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
        ((TxId -> Value) -> Network -> TxId -> Value
forall a b. a -> b -> a
const TxId -> Value
forall a. ToJSON a => a -> Value
toJSON)
    WebT m GetMempool
-> (GetMempool -> WebT m (SerialList TxHash))
-> (Network -> SerialList TxHash -> Encoding)
-> (Network -> SerialList TxHash -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty
        (Maybe LimitParam -> OffsetParam -> GetMempool
GetMempool (Maybe LimitParam -> OffsetParam -> GetMempool)
-> ActionT Except (ReaderT WebState m) (Maybe LimitParam)
-> ActionT Except (ReaderT WebState m) (OffsetParam -> GetMempool)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) (Maybe LimitParam)
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m (Maybe a)
paramOptional ActionT Except (ReaderT WebState m) (OffsetParam -> GetMempool)
-> ActionT Except (ReaderT WebState m) OffsetParam
-> WebT m GetMempool
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) OffsetParam
forall (m :: * -> *). MonadIO m => WebT m OffsetParam
parseOffset)
        (([TxHash] -> SerialList TxHash)
-> ActionT Except (ReaderT WebState m) [TxHash]
-> WebT m (SerialList TxHash)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [TxHash] -> SerialList TxHash
forall a. [a] -> SerialList a
SerialList (ActionT Except (ReaderT WebState m) [TxHash]
 -> WebT m (SerialList TxHash))
-> (GetMempool -> ActionT Except (ReaderT WebState m) [TxHash])
-> GetMempool
-> WebT m (SerialList TxHash)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GetMempool -> ActionT Except (ReaderT WebState m) [TxHash]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetMempool -> WebT m [TxHash]
scottyMempool)
        ((SerialList TxHash -> Encoding)
-> Network -> SerialList TxHash -> Encoding
forall a b. a -> b -> a
const SerialList TxHash -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
        ((SerialList TxHash -> Value)
-> Network -> SerialList TxHash -> Value
forall a b. a -> b -> a
const SerialList TxHash -> Value
forall a. ToJSON a => a -> Value
toJSON)
    -- Address Paths
    WebT m GetAddrTxs
-> (GetAddrTxs -> WebT m (SerialList TxRef))
-> (Network -> SerialList TxRef -> Encoding)
-> (Network -> SerialList TxRef -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty
        (Address -> LimitsParam -> GetAddrTxs
GetAddrTxs (Address -> LimitsParam -> GetAddrTxs)
-> ActionT Except (ReaderT WebState m) Address
-> ActionT Except (ReaderT WebState m) (LimitsParam -> GetAddrTxs)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) Address
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy ActionT Except (ReaderT WebState m) (LimitsParam -> GetAddrTxs)
-> ActionT Except (ReaderT WebState m) LimitsParam
-> WebT m GetAddrTxs
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) LimitsParam
forall (m :: * -> *). MonadIO m => WebT m LimitsParam
parseLimits)
        (([TxRef] -> SerialList TxRef)
-> ActionT Except (ReaderT WebState m) [TxRef]
-> WebT m (SerialList TxRef)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [TxRef] -> SerialList TxRef
forall a. [a] -> SerialList a
SerialList (ActionT Except (ReaderT WebState m) [TxRef]
 -> WebT m (SerialList TxRef))
-> (GetAddrTxs -> ActionT Except (ReaderT WebState m) [TxRef])
-> GetAddrTxs
-> WebT m (SerialList TxRef)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GetAddrTxs -> ActionT Except (ReaderT WebState m) [TxRef]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetAddrTxs -> WebT m [TxRef]
scottyAddrTxs)
        ((SerialList TxRef -> Encoding)
-> Network -> SerialList TxRef -> Encoding
forall a b. a -> b -> a
const SerialList TxRef -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
        ((SerialList TxRef -> Value) -> Network -> SerialList TxRef -> Value
forall a b. a -> b -> a
const SerialList TxRef -> Value
forall a. ToJSON a => a -> Value
toJSON)
    WebT m GetAddrsTxs
-> (GetAddrsTxs -> WebT m (SerialList TxRef))
-> (Network -> SerialList TxRef -> Encoding)
-> (Network -> SerialList TxRef -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        ([Address] -> LimitsParam -> GetAddrsTxs
GetAddrsTxs ([Address] -> LimitsParam -> GetAddrsTxs)
-> ActionT Except (ReaderT WebState m) [Address]
-> ActionT Except (ReaderT WebState m) (LimitsParam -> GetAddrsTxs)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) [Address]
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
param ActionT Except (ReaderT WebState m) (LimitsParam -> GetAddrsTxs)
-> ActionT Except (ReaderT WebState m) LimitsParam
-> WebT m GetAddrsTxs
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) LimitsParam
forall (m :: * -> *). MonadIO m => WebT m LimitsParam
parseLimits)
        (([TxRef] -> SerialList TxRef)
-> ActionT Except (ReaderT WebState m) [TxRef]
-> WebT m (SerialList TxRef)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [TxRef] -> SerialList TxRef
forall a. [a] -> SerialList a
SerialList (ActionT Except (ReaderT WebState m) [TxRef]
 -> WebT m (SerialList TxRef))
-> (GetAddrsTxs -> ActionT Except (ReaderT WebState m) [TxRef])
-> GetAddrsTxs
-> WebT m (SerialList TxRef)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GetAddrsTxs -> ActionT Except (ReaderT WebState m) [TxRef]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetAddrsTxs -> WebT m [TxRef]
scottyAddrsTxs)
        ((SerialList TxRef -> Encoding)
-> Network -> SerialList TxRef -> Encoding
forall a b. a -> b -> a
const SerialList TxRef -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
        ((SerialList TxRef -> Value) -> Network -> SerialList TxRef -> Value
forall a b. a -> b -> a
const SerialList TxRef -> Value
forall a. ToJSON a => a -> Value
toJSON)
    WebT m GetAddrTxsFull
-> (GetAddrTxsFull -> WebT m (SerialList Transaction))
-> (Network -> SerialList Transaction -> Encoding)
-> (Network -> SerialList Transaction -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        (Address -> LimitsParam -> GetAddrTxsFull
GetAddrTxsFull (Address -> LimitsParam -> GetAddrTxsFull)
-> ActionT Except (ReaderT WebState m) Address
-> ActionT
     Except (ReaderT WebState m) (LimitsParam -> GetAddrTxsFull)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) Address
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy ActionT Except (ReaderT WebState m) (LimitsParam -> GetAddrTxsFull)
-> ActionT Except (ReaderT WebState m) LimitsParam
-> WebT m GetAddrTxsFull
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) LimitsParam
forall (m :: * -> *). MonadIO m => WebT m LimitsParam
parseLimits)
        (([Transaction] -> SerialList Transaction)
-> ActionT Except (ReaderT WebState m) [Transaction]
-> WebT m (SerialList Transaction)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Transaction] -> SerialList Transaction
forall a. [a] -> SerialList a
SerialList (ActionT Except (ReaderT WebState m) [Transaction]
 -> WebT m (SerialList Transaction))
-> (GetAddrTxsFull
    -> ActionT Except (ReaderT WebState m) [Transaction])
-> GetAddrTxsFull
-> WebT m (SerialList Transaction)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GetAddrTxsFull -> ActionT Except (ReaderT WebState m) [Transaction]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetAddrTxsFull -> WebT m [Transaction]
scottyAddrTxsFull)
        (\n :: Network
n -> (Transaction -> Encoding) -> [Transaction] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
list (Network -> Transaction -> Encoding
transactionToEncoding Network
n) ([Transaction] -> Encoding)
-> (SerialList Transaction -> [Transaction])
-> SerialList Transaction
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialList Transaction -> [Transaction]
forall a. SerialList a -> [a]
getSerialList)
        (\n :: Network
n -> (Network -> Transaction -> Value)
-> Network -> [Transaction] -> Value
forall a t a. ToJSON a => (t -> a -> a) -> t -> [a] -> Value
json_list Network -> Transaction -> Value
transactionToJSON Network
n ([Transaction] -> Value)
-> (SerialList Transaction -> [Transaction])
-> SerialList Transaction
-> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialList Transaction -> [Transaction]
forall a. SerialList a -> [a]
getSerialList)
    WebT m GetAddrsTxsFull
-> (GetAddrsTxsFull -> WebT m (SerialList Transaction))
-> (Network -> SerialList Transaction -> Encoding)
-> (Network -> SerialList Transaction -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        ([Address] -> LimitsParam -> GetAddrsTxsFull
GetAddrsTxsFull ([Address] -> LimitsParam -> GetAddrsTxsFull)
-> ActionT Except (ReaderT WebState m) [Address]
-> ActionT
     Except (ReaderT WebState m) (LimitsParam -> GetAddrsTxsFull)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) [Address]
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
param ActionT
  Except (ReaderT WebState m) (LimitsParam -> GetAddrsTxsFull)
-> ActionT Except (ReaderT WebState m) LimitsParam
-> WebT m GetAddrsTxsFull
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) LimitsParam
forall (m :: * -> *). MonadIO m => WebT m LimitsParam
parseLimits)
        (([Transaction] -> SerialList Transaction)
-> ActionT Except (ReaderT WebState m) [Transaction]
-> WebT m (SerialList Transaction)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Transaction] -> SerialList Transaction
forall a. [a] -> SerialList a
SerialList (ActionT Except (ReaderT WebState m) [Transaction]
 -> WebT m (SerialList Transaction))
-> (GetAddrsTxsFull
    -> ActionT Except (ReaderT WebState m) [Transaction])
-> GetAddrsTxsFull
-> WebT m (SerialList Transaction)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GetAddrsTxsFull
-> ActionT Except (ReaderT WebState m) [Transaction]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetAddrsTxsFull -> WebT m [Transaction]
scottyAddrsTxsFull)
        (\n :: Network
n -> (Transaction -> Encoding) -> [Transaction] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
list (Network -> Transaction -> Encoding
transactionToEncoding Network
n) ([Transaction] -> Encoding)
-> (SerialList Transaction -> [Transaction])
-> SerialList Transaction
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialList Transaction -> [Transaction]
forall a. SerialList a -> [a]
getSerialList)
        (\n :: Network
n -> (Network -> Transaction -> Value)
-> Network -> [Transaction] -> Value
forall a t a. ToJSON a => (t -> a -> a) -> t -> [a] -> Value
json_list Network -> Transaction -> Value
transactionToJSON Network
n ([Transaction] -> Value)
-> (SerialList Transaction -> [Transaction])
-> SerialList Transaction
-> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialList Transaction -> [Transaction]
forall a. SerialList a -> [a]
getSerialList)
    WebT m GetAddrBalance
-> (GetAddrBalance -> WebT m Balance)
-> (Network -> Balance -> Encoding)
-> (Network -> Balance -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty
        (Address -> GetAddrBalance
GetAddrBalance (Address -> GetAddrBalance)
-> ActionT Except (ReaderT WebState m) Address
-> WebT m GetAddrBalance
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) Address
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy)
        GetAddrBalance -> WebT m Balance
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetAddrBalance -> WebT m Balance
scottyAddrBalance
        Network -> Balance -> Encoding
balanceToEncoding
        Network -> Balance -> Value
balanceToJSON
    WebT m GetAddrsBalance
-> (GetAddrsBalance -> WebT m (SerialList Balance))
-> (Network -> SerialList Balance -> Encoding)
-> (Network -> SerialList Balance -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        ([Address] -> GetAddrsBalance
GetAddrsBalance ([Address] -> GetAddrsBalance)
-> ActionT Except (ReaderT WebState m) [Address]
-> WebT m GetAddrsBalance
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) [Address]
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
param)
        (([Balance] -> SerialList Balance)
-> ActionT Except (ReaderT WebState m) [Balance]
-> WebT m (SerialList Balance)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Balance] -> SerialList Balance
forall a. [a] -> SerialList a
SerialList (ActionT Except (ReaderT WebState m) [Balance]
 -> WebT m (SerialList Balance))
-> (GetAddrsBalance
    -> ActionT Except (ReaderT WebState m) [Balance])
-> GetAddrsBalance
-> WebT m (SerialList Balance)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GetAddrsBalance -> ActionT Except (ReaderT WebState m) [Balance]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetAddrsBalance -> WebT m [Balance]
scottyAddrsBalance)
        (\n :: Network
n -> (Balance -> Encoding) -> [Balance] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
list (Network -> Balance -> Encoding
balanceToEncoding Network
n) ([Balance] -> Encoding)
-> (SerialList Balance -> [Balance])
-> SerialList Balance
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialList Balance -> [Balance]
forall a. SerialList a -> [a]
getSerialList)
        (\n :: Network
n -> (Network -> Balance -> Value) -> Network -> [Balance] -> Value
forall a t a. ToJSON a => (t -> a -> a) -> t -> [a] -> Value
json_list Network -> Balance -> Value
balanceToJSON Network
n ([Balance] -> Value)
-> (SerialList Balance -> [Balance]) -> SerialList Balance -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialList Balance -> [Balance]
forall a. SerialList a -> [a]
getSerialList)
    WebT m GetAddrUnspent
-> (GetAddrUnspent -> WebT m (SerialList Unspent))
-> (Network -> SerialList Unspent -> Encoding)
-> (Network -> SerialList Unspent -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty
        (Address -> LimitsParam -> GetAddrUnspent
GetAddrUnspent (Address -> LimitsParam -> GetAddrUnspent)
-> ActionT Except (ReaderT WebState m) Address
-> ActionT
     Except (ReaderT WebState m) (LimitsParam -> GetAddrUnspent)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) Address
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy ActionT Except (ReaderT WebState m) (LimitsParam -> GetAddrUnspent)
-> ActionT Except (ReaderT WebState m) LimitsParam
-> WebT m GetAddrUnspent
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) LimitsParam
forall (m :: * -> *). MonadIO m => WebT m LimitsParam
parseLimits)
        (([Unspent] -> SerialList Unspent)
-> ActionT Except (ReaderT WebState m) [Unspent]
-> WebT m (SerialList Unspent)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Unspent] -> SerialList Unspent
forall a. [a] -> SerialList a
SerialList (ActionT Except (ReaderT WebState m) [Unspent]
 -> WebT m (SerialList Unspent))
-> (GetAddrUnspent
    -> ActionT Except (ReaderT WebState m) [Unspent])
-> GetAddrUnspent
-> WebT m (SerialList Unspent)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GetAddrUnspent -> ActionT Except (ReaderT WebState m) [Unspent]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetAddrUnspent -> WebT m [Unspent]
scottyAddrUnspent)
        (\n :: Network
n -> (Unspent -> Encoding) -> [Unspent] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
list (Network -> Unspent -> Encoding
unspentToEncoding Network
n) ([Unspent] -> Encoding)
-> (SerialList Unspent -> [Unspent])
-> SerialList Unspent
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialList Unspent -> [Unspent]
forall a. SerialList a -> [a]
getSerialList)
        (\n :: Network
n -> (Network -> Unspent -> Value) -> Network -> [Unspent] -> Value
forall a t a. ToJSON a => (t -> a -> a) -> t -> [a] -> Value
json_list Network -> Unspent -> Value
unspentToJSON Network
n ([Unspent] -> Value)
-> (SerialList Unspent -> [Unspent]) -> SerialList Unspent -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialList Unspent -> [Unspent]
forall a. SerialList a -> [a]
getSerialList)
    WebT m GetAddrsUnspent
-> (GetAddrsUnspent -> WebT m (SerialList Unspent))
-> (Network -> SerialList Unspent -> Encoding)
-> (Network -> SerialList Unspent -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        ([Address] -> LimitsParam -> GetAddrsUnspent
GetAddrsUnspent ([Address] -> LimitsParam -> GetAddrsUnspent)
-> ActionT Except (ReaderT WebState m) [Address]
-> ActionT
     Except (ReaderT WebState m) (LimitsParam -> GetAddrsUnspent)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) [Address]
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
param ActionT
  Except (ReaderT WebState m) (LimitsParam -> GetAddrsUnspent)
-> ActionT Except (ReaderT WebState m) LimitsParam
-> WebT m GetAddrsUnspent
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) LimitsParam
forall (m :: * -> *). MonadIO m => WebT m LimitsParam
parseLimits)
        (([Unspent] -> SerialList Unspent)
-> ActionT Except (ReaderT WebState m) [Unspent]
-> WebT m (SerialList Unspent)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Unspent] -> SerialList Unspent
forall a. [a] -> SerialList a
SerialList (ActionT Except (ReaderT WebState m) [Unspent]
 -> WebT m (SerialList Unspent))
-> (GetAddrsUnspent
    -> ActionT Except (ReaderT WebState m) [Unspent])
-> GetAddrsUnspent
-> WebT m (SerialList Unspent)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GetAddrsUnspent -> ActionT Except (ReaderT WebState m) [Unspent]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetAddrsUnspent -> WebT m [Unspent]
scottyAddrsUnspent)
        (\n :: Network
n -> (Unspent -> Encoding) -> [Unspent] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
list (Network -> Unspent -> Encoding
unspentToEncoding Network
n) ([Unspent] -> Encoding)
-> (SerialList Unspent -> [Unspent])
-> SerialList Unspent
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialList Unspent -> [Unspent]
forall a. SerialList a -> [a]
getSerialList)
        (\n :: Network
n -> (Network -> Unspent -> Value) -> Network -> [Unspent] -> Value
forall a t a. ToJSON a => (t -> a -> a) -> t -> [a] -> Value
json_list Network -> Unspent -> Value
unspentToJSON Network
n ([Unspent] -> Value)
-> (SerialList Unspent -> [Unspent]) -> SerialList Unspent -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialList Unspent -> [Unspent]
forall a. SerialList a -> [a]
getSerialList)
    -- XPubs
    WebT m GetXPub
-> (GetXPub -> WebT m XPubSummary)
-> (Network -> XPubSummary -> Encoding)
-> (Network -> XPubSummary -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty
        (XPubKey -> DeriveType -> NoCache -> GetXPub
GetXPub (XPubKey -> DeriveType -> NoCache -> GetXPub)
-> ActionT Except (ReaderT WebState m) XPubKey
-> ActionT
     Except (ReaderT WebState m) (DeriveType -> NoCache -> GetXPub)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) XPubKey
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy ActionT
  Except (ReaderT WebState m) (DeriveType -> NoCache -> GetXPub)
-> ActionT Except (ReaderT WebState m) DeriveType
-> ActionT Except (ReaderT WebState m) (NoCache -> GetXPub)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) DeriveType
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef ActionT Except (ReaderT WebState m) (NoCache -> GetXPub)
-> ActionT Except (ReaderT WebState m) NoCache -> WebT m GetXPub
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) NoCache
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef)
        GetXPub -> WebT m XPubSummary
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetXPub -> WebT m XPubSummary
scottyXPub
        ((XPubSummary -> Encoding) -> Network -> XPubSummary -> Encoding
forall a b. a -> b -> a
const XPubSummary -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
        ((XPubSummary -> Value) -> Network -> XPubSummary -> Value
forall a b. a -> b -> a
const XPubSummary -> Value
forall a. ToJSON a => a -> Value
toJSON)
    WebT m GetXPubTxs
-> (GetXPubTxs -> WebT m (SerialList TxRef))
-> (Network -> SerialList TxRef -> Encoding)
-> (Network -> SerialList TxRef -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty
        (XPubKey -> DeriveType -> LimitsParam -> NoCache -> GetXPubTxs
GetXPubTxs (XPubKey -> DeriveType -> LimitsParam -> NoCache -> GetXPubTxs)
-> ActionT Except (ReaderT WebState m) XPubKey
-> ActionT
     Except
     (ReaderT WebState m)
     (DeriveType -> LimitsParam -> NoCache -> GetXPubTxs)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) XPubKey
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy ActionT
  Except
  (ReaderT WebState m)
  (DeriveType -> LimitsParam -> NoCache -> GetXPubTxs)
-> ActionT Except (ReaderT WebState m) DeriveType
-> ActionT
     Except (ReaderT WebState m) (LimitsParam -> NoCache -> GetXPubTxs)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) DeriveType
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef ActionT
  Except (ReaderT WebState m) (LimitsParam -> NoCache -> GetXPubTxs)
-> ActionT Except (ReaderT WebState m) LimitsParam
-> ActionT Except (ReaderT WebState m) (NoCache -> GetXPubTxs)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) LimitsParam
forall (m :: * -> *). MonadIO m => WebT m LimitsParam
parseLimits ActionT Except (ReaderT WebState m) (NoCache -> GetXPubTxs)
-> ActionT Except (ReaderT WebState m) NoCache -> WebT m GetXPubTxs
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) NoCache
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef)
        (([TxRef] -> SerialList TxRef)
-> ActionT Except (ReaderT WebState m) [TxRef]
-> WebT m (SerialList TxRef)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [TxRef] -> SerialList TxRef
forall a. [a] -> SerialList a
SerialList (ActionT Except (ReaderT WebState m) [TxRef]
 -> WebT m (SerialList TxRef))
-> (GetXPubTxs -> ActionT Except (ReaderT WebState m) [TxRef])
-> GetXPubTxs
-> WebT m (SerialList TxRef)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GetXPubTxs -> ActionT Except (ReaderT WebState m) [TxRef]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetXPubTxs -> WebT m [TxRef]
scottyXPubTxs)
        ((SerialList TxRef -> Encoding)
-> Network -> SerialList TxRef -> Encoding
forall a b. a -> b -> a
const SerialList TxRef -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
        ((SerialList TxRef -> Value) -> Network -> SerialList TxRef -> Value
forall a b. a -> b -> a
const SerialList TxRef -> Value
forall a. ToJSON a => a -> Value
toJSON)
    WebT m GetXPubTxsFull
-> (GetXPubTxsFull -> WebT m (SerialList Transaction))
-> (Network -> SerialList Transaction -> Encoding)
-> (Network -> SerialList Transaction -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        (XPubKey -> DeriveType -> LimitsParam -> NoCache -> GetXPubTxsFull
GetXPubTxsFull (XPubKey -> DeriveType -> LimitsParam -> NoCache -> GetXPubTxsFull)
-> ActionT Except (ReaderT WebState m) XPubKey
-> ActionT
     Except
     (ReaderT WebState m)
     (DeriveType -> LimitsParam -> NoCache -> GetXPubTxsFull)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) XPubKey
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy ActionT
  Except
  (ReaderT WebState m)
  (DeriveType -> LimitsParam -> NoCache -> GetXPubTxsFull)
-> ActionT Except (ReaderT WebState m) DeriveType
-> ActionT
     Except
     (ReaderT WebState m)
     (LimitsParam -> NoCache -> GetXPubTxsFull)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) DeriveType
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef ActionT
  Except
  (ReaderT WebState m)
  (LimitsParam -> NoCache -> GetXPubTxsFull)
-> ActionT Except (ReaderT WebState m) LimitsParam
-> ActionT Except (ReaderT WebState m) (NoCache -> GetXPubTxsFull)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) LimitsParam
forall (m :: * -> *). MonadIO m => WebT m LimitsParam
parseLimits ActionT Except (ReaderT WebState m) (NoCache -> GetXPubTxsFull)
-> ActionT Except (ReaderT WebState m) NoCache
-> WebT m GetXPubTxsFull
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) NoCache
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef)
        (([Transaction] -> SerialList Transaction)
-> ActionT Except (ReaderT WebState m) [Transaction]
-> WebT m (SerialList Transaction)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Transaction] -> SerialList Transaction
forall a. [a] -> SerialList a
SerialList (ActionT Except (ReaderT WebState m) [Transaction]
 -> WebT m (SerialList Transaction))
-> (GetXPubTxsFull
    -> ActionT Except (ReaderT WebState m) [Transaction])
-> GetXPubTxsFull
-> WebT m (SerialList Transaction)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GetXPubTxsFull -> ActionT Except (ReaderT WebState m) [Transaction]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetXPubTxsFull -> WebT m [Transaction]
scottyXPubTxsFull)
        (\n :: Network
n -> (Transaction -> Encoding) -> [Transaction] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
list (Network -> Transaction -> Encoding
transactionToEncoding Network
n) ([Transaction] -> Encoding)
-> (SerialList Transaction -> [Transaction])
-> SerialList Transaction
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialList Transaction -> [Transaction]
forall a. SerialList a -> [a]
getSerialList)
        (\n :: Network
n -> (Network -> Transaction -> Value)
-> Network -> [Transaction] -> Value
forall a t a. ToJSON a => (t -> a -> a) -> t -> [a] -> Value
json_list Network -> Transaction -> Value
transactionToJSON Network
n ([Transaction] -> Value)
-> (SerialList Transaction -> [Transaction])
-> SerialList Transaction
-> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialList Transaction -> [Transaction]
forall a. SerialList a -> [a]
getSerialList)
    WebT m GetXPubBalances
-> (GetXPubBalances -> WebT m (SerialList XPubBal))
-> (Network -> SerialList XPubBal -> Encoding)
-> (Network -> SerialList XPubBal -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty
        (XPubKey -> DeriveType -> NoCache -> GetXPubBalances
GetXPubBalances (XPubKey -> DeriveType -> NoCache -> GetXPubBalances)
-> ActionT Except (ReaderT WebState m) XPubKey
-> ActionT
     Except
     (ReaderT WebState m)
     (DeriveType -> NoCache -> GetXPubBalances)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) XPubKey
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy ActionT
  Except
  (ReaderT WebState m)
  (DeriveType -> NoCache -> GetXPubBalances)
-> ActionT Except (ReaderT WebState m) DeriveType
-> ActionT Except (ReaderT WebState m) (NoCache -> GetXPubBalances)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) DeriveType
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef ActionT Except (ReaderT WebState m) (NoCache -> GetXPubBalances)
-> ActionT Except (ReaderT WebState m) NoCache
-> WebT m GetXPubBalances
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) NoCache
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef)
        (([XPubBal] -> SerialList XPubBal)
-> ActionT Except (ReaderT WebState m) [XPubBal]
-> WebT m (SerialList XPubBal)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [XPubBal] -> SerialList XPubBal
forall a. [a] -> SerialList a
SerialList (ActionT Except (ReaderT WebState m) [XPubBal]
 -> WebT m (SerialList XPubBal))
-> (GetXPubBalances
    -> ActionT Except (ReaderT WebState m) [XPubBal])
-> GetXPubBalances
-> WebT m (SerialList XPubBal)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GetXPubBalances -> ActionT Except (ReaderT WebState m) [XPubBal]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetXPubBalances -> WebT m [XPubBal]
scottyXPubBalances)
        (\n :: Network
n -> (XPubBal -> Encoding) -> [XPubBal] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
list (Network -> XPubBal -> Encoding
xPubBalToEncoding Network
n) ([XPubBal] -> Encoding)
-> (SerialList XPubBal -> [XPubBal])
-> SerialList XPubBal
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialList XPubBal -> [XPubBal]
forall a. SerialList a -> [a]
getSerialList)
        (\n :: Network
n -> (Network -> XPubBal -> Value) -> Network -> [XPubBal] -> Value
forall a t a. ToJSON a => (t -> a -> a) -> t -> [a] -> Value
json_list Network -> XPubBal -> Value
xPubBalToJSON Network
n ([XPubBal] -> Value)
-> (SerialList XPubBal -> [XPubBal]) -> SerialList XPubBal -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialList XPubBal -> [XPubBal]
forall a. SerialList a -> [a]
getSerialList)
    WebT m GetXPubUnspent
-> (GetXPubUnspent -> WebT m (SerialList XPubUnspent))
-> (Network -> SerialList XPubUnspent -> Encoding)
-> (Network -> SerialList XPubUnspent -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty
        (XPubKey -> DeriveType -> LimitsParam -> NoCache -> GetXPubUnspent
GetXPubUnspent (XPubKey -> DeriveType -> LimitsParam -> NoCache -> GetXPubUnspent)
-> ActionT Except (ReaderT WebState m) XPubKey
-> ActionT
     Except
     (ReaderT WebState m)
     (DeriveType -> LimitsParam -> NoCache -> GetXPubUnspent)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) XPubKey
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy ActionT
  Except
  (ReaderT WebState m)
  (DeriveType -> LimitsParam -> NoCache -> GetXPubUnspent)
-> ActionT Except (ReaderT WebState m) DeriveType
-> ActionT
     Except
     (ReaderT WebState m)
     (LimitsParam -> NoCache -> GetXPubUnspent)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) DeriveType
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef ActionT
  Except
  (ReaderT WebState m)
  (LimitsParam -> NoCache -> GetXPubUnspent)
-> ActionT Except (ReaderT WebState m) LimitsParam
-> ActionT Except (ReaderT WebState m) (NoCache -> GetXPubUnspent)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) LimitsParam
forall (m :: * -> *). MonadIO m => WebT m LimitsParam
parseLimits ActionT Except (ReaderT WebState m) (NoCache -> GetXPubUnspent)
-> ActionT Except (ReaderT WebState m) NoCache
-> WebT m GetXPubUnspent
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) NoCache
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef)
        (([XPubUnspent] -> SerialList XPubUnspent)
-> ActionT Except (ReaderT WebState m) [XPubUnspent]
-> WebT m (SerialList XPubUnspent)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [XPubUnspent] -> SerialList XPubUnspent
forall a. [a] -> SerialList a
SerialList (ActionT Except (ReaderT WebState m) [XPubUnspent]
 -> WebT m (SerialList XPubUnspent))
-> (GetXPubUnspent
    -> ActionT Except (ReaderT WebState m) [XPubUnspent])
-> GetXPubUnspent
-> WebT m (SerialList XPubUnspent)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GetXPubUnspent -> ActionT Except (ReaderT WebState m) [XPubUnspent]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetXPubUnspent -> WebT m [XPubUnspent]
scottyXPubUnspent)
        (\n :: Network
n -> (XPubUnspent -> Encoding) -> [XPubUnspent] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
list (Network -> XPubUnspent -> Encoding
xPubUnspentToEncoding Network
n) ([XPubUnspent] -> Encoding)
-> (SerialList XPubUnspent -> [XPubUnspent])
-> SerialList XPubUnspent
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialList XPubUnspent -> [XPubUnspent]
forall a. SerialList a -> [a]
getSerialList)
        (\n :: Network
n -> (Network -> XPubUnspent -> Value)
-> Network -> [XPubUnspent] -> Value
forall a t a. ToJSON a => (t -> a -> a) -> t -> [a] -> Value
json_list Network -> XPubUnspent -> Value
xPubUnspentToJSON Network
n ([XPubUnspent] -> Value)
-> (SerialList XPubUnspent -> [XPubUnspent])
-> SerialList XPubUnspent
-> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialList XPubUnspent -> [XPubUnspent]
forall a. SerialList a -> [a]
getSerialList)
    -- Network
    WebT m GetPeers
-> (GetPeers -> WebT m (SerialList PeerInformation))
-> (Network -> SerialList PeerInformation -> Encoding)
-> (Network -> SerialList PeerInformation -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty
        (GetPeers
GetPeers GetPeers -> (GetPeers -> WebT m GetPeers) -> WebT m GetPeers
forall a b. a -> (a -> b) -> b
& GetPeers -> WebT m GetPeers
forall (m :: * -> *) a. Monad m => a -> m a
return)
        (([PeerInformation] -> SerialList PeerInformation)
-> ActionT Except (ReaderT WebState m) [PeerInformation]
-> WebT m (SerialList PeerInformation)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [PeerInformation] -> SerialList PeerInformation
forall a. [a] -> SerialList a
SerialList (ActionT Except (ReaderT WebState m) [PeerInformation]
 -> WebT m (SerialList PeerInformation))
-> (GetPeers
    -> ActionT Except (ReaderT WebState m) [PeerInformation])
-> GetPeers
-> WebT m (SerialList PeerInformation)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GetPeers -> ActionT Except (ReaderT WebState m) [PeerInformation]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetPeers -> WebT m [PeerInformation]
scottyPeers)
        ((SerialList PeerInformation -> Encoding)
-> Network -> SerialList PeerInformation -> Encoding
forall a b. a -> b -> a
const SerialList PeerInformation -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
        ((SerialList PeerInformation -> Value)
-> Network -> SerialList PeerInformation -> Value
forall a b. a -> b -> a
const SerialList PeerInformation -> Value
forall a. ToJSON a => a -> Value
toJSON)
    WebT m GetHealth
-> (GetHealth -> WebT m HealthCheck)
-> (Network -> HealthCheck -> Encoding)
-> (Network -> HealthCheck -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty
         (GetHealth
GetHealth GetHealth -> (GetHealth -> WebT m GetHealth) -> WebT m GetHealth
forall a b. a -> (a -> b) -> b
& GetHealth -> WebT m GetHealth
forall (m :: * -> *) a. Monad m => a -> m a
return)
         GetHealth -> WebT m HealthCheck
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetHealth -> WebT m HealthCheck
scottyHealth
         ((HealthCheck -> Encoding) -> Network -> HealthCheck -> Encoding
forall a b. a -> b -> a
const HealthCheck -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
         ((HealthCheck -> Value) -> Network -> HealthCheck -> Value
forall a b. a -> b -> a
const HealthCheck -> Value
forall a. ToJSON a => a -> Value
toJSON)
    RoutePattern
-> ActionT Except (ReaderT WebState m) ()
-> ScottyT Except (ReaderT WebState m) ()
forall e (m :: * -> *).
(ScottyError e, MonadIO m) =>
RoutePattern -> ActionT e m () -> ScottyT e m ()
S.get "/events" ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
WebT m ()
scottyEvents
    RoutePattern
-> ActionT Except (ReaderT WebState m) ()
-> ScottyT Except (ReaderT WebState m) ()
forall e (m :: * -> *).
(ScottyError e, MonadIO m) =>
RoutePattern -> ActionT e m () -> ScottyT e m ()
S.get "/dbstats" ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
WebT m ()
scottyDbStats
    -- Blockchain.info
    RoutePattern
-> ActionT Except (ReaderT WebState m) ()
-> ScottyT Except (ReaderT WebState m) ()
forall e (m :: * -> *).
(ScottyError e, MonadIO m) =>
RoutePattern -> ActionT e m () -> ScottyT e m ()
S.post "/blockchain/multiaddr" ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
WebT m ()
scottyMultiAddr
    RoutePattern
-> ActionT Except (ReaderT WebState m) ()
-> ScottyT Except (ReaderT WebState m) ()
forall e (m :: * -> *).
(ScottyError e, MonadIO m) =>
RoutePattern -> ActionT e m () -> ScottyT e m ()
S.get  "/blockchain/multiaddr" ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
WebT m ()
scottyMultiAddr
    RoutePattern
-> ActionT Except (ReaderT WebState m) ()
-> ScottyT Except (ReaderT WebState m) ()
forall e (m :: * -> *).
(ScottyError e, MonadIO m) =>
RoutePattern -> ActionT e m () -> ScottyT e m ()
S.post "/blockchain/unspent" ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
WebT m ()
scottyBinfoUnspent
    RoutePattern
-> ActionT Except (ReaderT WebState m) ()
-> ScottyT Except (ReaderT WebState m) ()
forall e (m :: * -> *).
(ScottyError e, MonadIO m) =>
RoutePattern -> ActionT e m () -> ScottyT e m ()
S.get  "/blockchain/unspent" ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
WebT m ()
scottyBinfoUnspent
    RoutePattern
-> ActionT Except (ReaderT WebState m) ()
-> ScottyT Except (ReaderT WebState m) ()
forall e (m :: * -> *).
(ScottyError e, MonadIO m) =>
RoutePattern -> ActionT e m () -> ScottyT e m ()
S.get "/blockchain/rawtx/:txid" ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
WebT m ()
scottyBinfoTx
    RoutePattern
-> ActionT Except (ReaderT WebState m) ()
-> ScottyT Except (ReaderT WebState m) ()
forall e (m :: * -> *).
(ScottyError e, MonadIO m) =>
RoutePattern -> ActionT e m () -> ScottyT e m ()
S.get "/blockchain/rawblock/:block" ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
WebT m ()
scottyBinfoBlock
  where
    json_list :: (t -> a -> a) -> t -> [a] -> Value
json_list f :: t -> a -> a
f net :: t
net = [a] -> Value
forall a. ToJSON a => [a] -> Value
toJSONList ([a] -> Value) -> ([a] -> [a]) -> [a] -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a) -> [a] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map (t -> a -> a
f t
net)

pathPretty ::
       (ApiResource a b, MonadIO m)
    => WebT m a
    -> (a -> WebT m b)
    -> (Network -> b -> Encoding)
    -> (Network -> b -> Value)
    -> S.ScottyT Except (ReaderT WebState m) ()
pathPretty :: WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty parser :: WebT m a
parser action :: a -> WebT m b
action encJson :: Network -> b -> Encoding
encJson encValue :: Network -> b -> Value
encValue =
    WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> Bool
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> Bool
-> ScottyT Except (ReaderT WebState m) ()
pathCommon WebT m a
parser a -> WebT m b
action Network -> b -> Encoding
encJson Network -> b -> Value
encValue Bool
True

pathCompact ::
       (ApiResource a b, MonadIO m)
    => WebT m a
    -> (a -> WebT m b)
    -> (Network -> b -> Encoding)
    -> (Network -> b -> Value)
    -> S.ScottyT Except (ReaderT WebState m) ()
pathCompact :: WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact parser :: WebT m a
parser action :: a -> WebT m b
action encJson :: Network -> b -> Encoding
encJson encValue :: Network -> b -> Value
encValue =
    WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> Bool
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> Bool
-> ScottyT Except (ReaderT WebState m) ()
pathCommon WebT m a
parser a -> WebT m b
action Network -> b -> Encoding
encJson Network -> b -> Value
encValue Bool
False

pathCommon ::
       (ApiResource a b, MonadIO m)
    => WebT m a
    -> (a -> WebT m b)
    -> (Network -> b -> Encoding)
    -> (Network -> b -> Value)
    -> Bool
    -> S.ScottyT Except (ReaderT WebState m) ()
pathCommon :: WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> Bool
-> ScottyT Except (ReaderT WebState m) ()
pathCommon parser :: WebT m a
parser action :: a -> WebT m b
action encJson :: Network -> b -> Encoding
encJson encValue :: Network -> b -> Value
encValue pretty :: Bool
pretty =
    StdMethod
-> RoutePattern
-> ActionT Except (ReaderT WebState m) ()
-> ScottyT Except (ReaderT WebState m) ()
forall e (m :: * -> *).
(ScottyError e, MonadIO m) =>
StdMethod -> RoutePattern -> ActionT e m () -> ScottyT e m ()
S.addroute (Proxy a -> StdMethod
forall a b. ApiResource a b => Proxy a -> StdMethod
resourceMethod Proxy a
proxy) (Proxy a -> RoutePattern
forall a b. ApiResource a b => Proxy a -> RoutePattern
capturePath Proxy a
proxy) (ActionT Except (ReaderT WebState m) ()
 -> ScottyT Except (ReaderT WebState m) ())
-> ActionT Except (ReaderT WebState m) ()
-> ScottyT Except (ReaderT WebState m) ()
forall a b. (a -> b) -> a -> b
$ do
        ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *) e. (Monad m, ScottyError e) => ActionT e m ()
setHeaders
        SerialAs
proto <- Bool -> ActionT Except (ReaderT WebState m) SerialAs
forall (m :: * -> *). Monad m => Bool -> ActionT Except m SerialAs
setupContentType Bool
pretty
        Network
net <- ReaderT WebState m Network
-> ActionT Except (ReaderT WebState m) Network
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m Network
 -> ActionT Except (ReaderT WebState m) Network)
-> ReaderT WebState m Network
-> ActionT Except (ReaderT WebState m) Network
forall a b. (a -> b) -> a -> b
$ (WebState -> Network) -> ReaderT WebState m Network
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (Store -> Network
storeNetwork (Store -> Network) -> (WebState -> Store) -> WebState -> Network
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebConfig -> Store
webStore (WebConfig -> Store)
-> (WebState -> WebConfig) -> WebState -> Store
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig)
        a
apiRes <- WebT m a
parser
        b
res <- a -> WebT m b
action a
apiRes
        ByteString -> ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *) e. Monad m => ByteString -> ActionT e m ()
S.raw (ByteString -> ActionT Except (ReaderT WebState m) ())
-> ByteString -> ActionT Except (ReaderT WebState m) ()
forall a b. (a -> b) -> a -> b
$ SerialAs -> (b -> Encoding) -> (b -> Value) -> b -> ByteString
forall a.
Serial a =>
SerialAs -> (a -> Encoding) -> (a -> Value) -> a -> ByteString
protoSerial SerialAs
proto (Network -> b -> Encoding
encJson Network
net) (Network -> b -> Value
encValue Network
net) b
res
  where
    toProxy :: WebT m a -> Proxy a
    toProxy :: WebT m a -> Proxy a
toProxy = Proxy a -> WebT m a -> Proxy a
forall a b. a -> b -> a
const Proxy a
forall k (t :: k). Proxy t
Proxy
    proxy :: Proxy a
proxy = WebT m a -> Proxy a
forall (m :: * -> *) a. WebT m a -> Proxy a
toProxy WebT m a
parser

streamEncoding :: Monad m => Encoding -> WebT m ()
streamEncoding :: Encoding -> WebT m ()
streamEncoding e :: Encoding
e = do
   Text -> Text -> WebT m ()
forall (m :: * -> *) e. Monad m => Text -> Text -> ActionT e m ()
S.setHeader "Content-Type" "application/json; charset=utf-8"
   ByteString -> WebT m ()
forall (m :: * -> *) e. Monad m => ByteString -> ActionT e m ()
S.raw (Encoding -> ByteString
forall a. Encoding' a -> ByteString
encodingToLazyByteString Encoding
e)

protoSerial
    :: Serial a
    => SerialAs
    -> (a -> Encoding)
    -> (a -> Value)
    -> a
    -> L.ByteString
protoSerial :: SerialAs -> (a -> Encoding) -> (a -> Value) -> a -> ByteString
protoSerial SerialAsBinary _ _     = Put -> ByteString
runPutL (Put -> ByteString) -> (a -> Put) -> a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Put
forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize
protoSerial SerialAsJSON f :: a -> Encoding
f _       = Encoding -> ByteString
forall a. Encoding' a -> ByteString
encodingToLazyByteString (Encoding -> ByteString) -> (a -> Encoding) -> a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Encoding
f
protoSerial SerialAsPrettyJSON _ g :: a -> Value
g =
    Config -> Value -> ByteString
forall a. ToJSON a => Config -> a -> ByteString
encodePretty' Config
defConfig {confTrailingNewline :: Bool
confTrailingNewline = Bool
True} (Value -> ByteString) -> (a -> Value) -> a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Value
g

setHeaders :: (Monad m, S.ScottyError e) => ActionT e m ()
setHeaders :: ActionT e m ()
setHeaders = Text -> Text -> ActionT e m ()
forall (m :: * -> *) e. Monad m => Text -> Text -> ActionT e m ()
S.setHeader "Access-Control-Allow-Origin" "*"

setupContentType :: Monad m => Bool -> ActionT Except m SerialAs
setupContentType :: Bool -> ActionT Except m SerialAs
setupContentType pretty :: Bool
pretty = do
    Maybe Text
accept <- Text -> ActionT Except m (Maybe Text)
forall e (m :: * -> *).
(ScottyError e, Monad m) =>
Text -> ActionT e m (Maybe Text)
S.header "accept"
    ActionT Except m SerialAs
-> (Text -> ActionT Except m SerialAs)
-> Maybe Text
-> ActionT Except m SerialAs
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ActionT Except m SerialAs
goJson Text -> ActionT Except m SerialAs
forall a. (Eq a, IsString a) => a -> ActionT Except m SerialAs
setType Maybe Text
accept
  where
    setType :: a -> ActionT Except m SerialAs
setType "application/octet-stream" = ActionT Except m SerialAs
goBinary
    setType _                          = ActionT Except m SerialAs
goJson
    goBinary :: ActionT Except m SerialAs
goBinary = do
        Text -> Text -> ActionT Except m ()
forall (m :: * -> *) e. Monad m => Text -> Text -> ActionT e m ()
S.setHeader "Content-Type" "application/octet-stream"
        SerialAs -> ActionT Except m SerialAs
forall (m :: * -> *) a. Monad m => a -> m a
return SerialAs
SerialAsBinary
    goJson :: ActionT Except m SerialAs
goJson = do
        Text -> Text -> ActionT Except m ()
forall (m :: * -> *) e. Monad m => Text -> Text -> ActionT e m ()
S.setHeader "Content-Type" "application/json"
        Bool
p <- Text -> ActionT Except m Bool
forall a e (m :: * -> *).
(Parsable a, ScottyError e, Monad m) =>
Text -> ActionT e m a
S.param "pretty" ActionT Except m Bool
-> (Except -> ActionT Except m Bool) -> ActionT Except m Bool
forall e (m :: * -> *) a.
(ScottyError e, Monad m) =>
ActionT e m a -> (e -> ActionT e m a) -> ActionT e m a
`S.rescue` ActionT Except m Bool -> Except -> ActionT Except m Bool
forall a b. a -> b -> a
const (Bool -> ActionT Except m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
pretty)
        SerialAs -> ActionT Except m SerialAs
forall (m :: * -> *) a. Monad m => a -> m a
return (SerialAs -> ActionT Except m SerialAs)
-> SerialAs -> ActionT Except m SerialAs
forall a b. (a -> b) -> a -> b
$ if Bool
p then SerialAs
SerialAsPrettyJSON else SerialAs
SerialAsJSON

-- GET Block / GET Blocks --

scottyBlock ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetBlock -> WebT m BlockData
scottyBlock :: GetBlock -> WebT m BlockData
scottyBlock (GetBlock h :: BlockHash
h (NoTx noTx :: Bool
noTx)) =
    (WebMetrics -> StatDist)
-> Int -> WebT m BlockData -> WebT m BlockData
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
blockResponseTime 1 (WebT m BlockData -> WebT m BlockData)
-> WebT m BlockData -> WebT m BlockData
forall a b. (a -> b) -> a -> b
$
    WebT m BlockData
-> (BlockData -> WebT m BlockData)
-> Maybe BlockData
-> WebT m BlockData
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ((WebMetrics -> ErrorCounter) -> Except -> WebT m BlockData
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
blockErrors Except
ThingNotFound) (BlockData -> WebT m BlockData
forall (m :: * -> *) a. Monad m => a -> m a
return (BlockData -> WebT m BlockData)
-> (BlockData -> BlockData) -> BlockData -> WebT m BlockData
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> BlockData -> BlockData
pruneTx Bool
noTx) (Maybe BlockData -> WebT m BlockData)
-> ActionT Except (ReaderT WebState m) (Maybe BlockData)
-> WebT m BlockData
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<
    BlockHash -> ActionT Except (ReaderT WebState m) (Maybe BlockData)
forall (m :: * -> *).
StoreReadBase m =>
BlockHash -> m (Maybe BlockData)
getBlock BlockHash
h

getBlocks :: (MonadUnliftIO m, MonadLoggerIO m)
          => [H.BlockHash]
          -> Bool
          -> WebT m [BlockData]
getBlocks :: [BlockHash] -> Bool -> WebT m [BlockData]
getBlocks hs :: [BlockHash]
hs notx :: Bool
notx =
    (Bool -> BlockData -> BlockData
pruneTx Bool
notx (BlockData -> BlockData) -> [BlockData] -> [BlockData]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) ([BlockData] -> [BlockData])
-> ([Maybe BlockData] -> [BlockData])
-> [Maybe BlockData]
-> [BlockData]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Maybe BlockData] -> [BlockData]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe BlockData] -> [BlockData])
-> ActionT Except (ReaderT WebState m) [Maybe BlockData]
-> WebT m [BlockData]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (BlockHash
 -> ActionT Except (ReaderT WebState m) (Maybe BlockData))
-> [BlockHash]
-> ActionT Except (ReaderT WebState m) [Maybe BlockData]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM BlockHash -> ActionT Except (ReaderT WebState m) (Maybe BlockData)
forall (m :: * -> *).
StoreReadBase m =>
BlockHash -> m (Maybe BlockData)
getBlock ([BlockHash] -> [BlockHash]
forall a. Eq a => [a] -> [a]
nub [BlockHash]
hs)

scottyBlocks ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetBlocks -> WebT m [BlockData]
scottyBlocks :: GetBlocks -> WebT m [BlockData]
scottyBlocks (GetBlocks hs :: [BlockHash]
hs (NoTx notx :: Bool
notx)) =
    (WebMetrics -> StatDist)
-> Int -> WebT m [BlockData] -> WebT m [BlockData]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
blockResponseTime ([BlockHash] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [BlockHash]
hs) (WebT m [BlockData] -> WebT m [BlockData])
-> WebT m [BlockData] -> WebT m [BlockData]
forall a b. (a -> b) -> a -> b
$ [BlockHash] -> Bool -> WebT m [BlockData]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
[BlockHash] -> Bool -> WebT m [BlockData]
getBlocks [BlockHash]
hs Bool
notx

pruneTx :: Bool -> BlockData -> BlockData
pruneTx :: Bool -> BlockData -> BlockData
pruneTx False b :: BlockData
b = BlockData
b
pruneTx True b :: BlockData
b  = BlockData
b {blockDataTxs :: [TxHash]
blockDataTxs = Int -> [TxHash] -> [TxHash]
forall a. Int -> [a] -> [a]
take 1 (BlockData -> [TxHash]
blockDataTxs BlockData
b)}

-- GET BlockRaw --

scottyBlockRaw ::
       (MonadUnliftIO m, MonadLoggerIO m)
    => GetBlockRaw
    -> WebT m (RawResult H.Block)
scottyBlockRaw :: GetBlockRaw -> WebT m (RawResult Block)
scottyBlockRaw (GetBlockRaw h :: BlockHash
h) =
    (WebMetrics -> StatDist)
-> Int -> WebT m (RawResult Block) -> WebT m (RawResult Block)
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
rawBlockResponseTime 1 (WebT m (RawResult Block) -> WebT m (RawResult Block))
-> WebT m (RawResult Block) -> WebT m (RawResult Block)
forall a b. (a -> b) -> a -> b
$
    Block -> RawResult Block
forall a. a -> RawResult a
RawResult (Block -> RawResult Block)
-> ActionT Except (ReaderT WebState m) Block
-> WebT m (RawResult Block)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> BlockHash -> ActionT Except (ReaderT WebState m) Block
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
BlockHash -> WebT m Block
getRawBlock BlockHash
h

getRawBlock ::
       (MonadUnliftIO m, MonadLoggerIO m) => H.BlockHash -> WebT m H.Block
getRawBlock :: BlockHash -> WebT m Block
getRawBlock h :: BlockHash
h = do
    BlockData
b <- WebT m BlockData
-> (BlockData -> WebT m BlockData)
-> Maybe BlockData
-> WebT m BlockData
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ((WebMetrics -> ErrorCounter) -> Except -> WebT m BlockData
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
blockErrors Except
ThingNotFound) BlockData -> WebT m BlockData
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe BlockData -> WebT m BlockData)
-> ActionT Except (ReaderT WebState m) (Maybe BlockData)
-> WebT m BlockData
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< BlockHash -> ActionT Except (ReaderT WebState m) (Maybe BlockData)
forall (m :: * -> *).
StoreReadBase m =>
BlockHash -> m (Maybe BlockData)
getBlock BlockHash
h
    ReaderT WebState m Block -> WebT m Block
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (BlockData -> ReaderT WebState m Block
forall (m :: * -> *).
(MonadUnliftIO m, StoreReadBase m) =>
BlockData -> m Block
toRawBlock BlockData
b)

toRawBlock :: (MonadUnliftIO m, StoreReadBase m) => BlockData -> m H.Block
toRawBlock :: BlockData -> m Block
toRawBlock b :: BlockData
b = do
    let ths :: [TxHash]
ths = BlockData -> [TxHash]
blockDataTxs BlockData
b
    [Tx]
txs <- (TxHash -> m Tx) -> [TxHash] -> m [Tx]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM TxHash -> m Tx
forall (m :: * -> *).
(MonadUnliftIO m, StoreReadBase m) =>
TxHash -> m Tx
f [TxHash]
ths
    Block -> m Block
forall (m :: * -> *) a. Monad m => a -> m a
return $WBlock :: BlockHeader -> [Tx] -> Block
H.Block {blockHeader :: BlockHeader
H.blockHeader = BlockData -> BlockHeader
blockDataHeader BlockData
b, blockTxns :: [Tx]
H.blockTxns = [Tx]
txs}
  where
    f :: TxHash -> m Tx
f x :: TxHash
x = ((forall a. m a -> IO a) -> IO Tx) -> m Tx
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO (((forall a. m a -> IO a) -> IO Tx) -> m Tx)
-> ((forall a. m a -> IO a) -> IO Tx) -> m Tx
forall a b. (a -> b) -> a -> b
$ \run :: forall a. m a -> IO a
run ->
          IO Tx -> IO Tx
forall a. IO a -> IO a
unsafeInterleaveIO (IO Tx -> IO Tx) -> (m Tx -> IO Tx) -> m Tx -> IO Tx
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m Tx -> IO Tx
forall a. m a -> IO a
run (m Tx -> IO Tx) -> m Tx -> IO Tx
forall a b. (a -> b) -> a -> b
$
          TxHash -> m (Maybe Transaction)
forall (m :: * -> *).
(Monad m, StoreReadBase m) =>
TxHash -> m (Maybe Transaction)
getTransaction TxHash
x m (Maybe Transaction) -> (Maybe Transaction -> m Tx) -> m Tx
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
              Nothing -> m Tx
forall a. HasCallStack => a
undefined
              Just t :: Transaction
t -> Tx -> m Tx
forall (m :: * -> *) a. Monad m => a -> m a
return (Tx -> m Tx) -> Tx -> m Tx
forall a b. (a -> b) -> a -> b
$ Transaction -> Tx
transactionData Transaction
t

-- GET BlockBest / BlockBestRaw --

scottyBlockBest ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetBlockBest -> WebT m BlockData
scottyBlockBest :: GetBlockBest -> WebT m BlockData
scottyBlockBest (GetBlockBest (NoTx notx :: Bool
notx)) =
    (WebMetrics -> StatDist)
-> Int -> WebT m BlockData -> WebT m BlockData
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
blockResponseTime 1 (WebT m BlockData -> WebT m BlockData)
-> WebT m BlockData -> WebT m BlockData
forall a b. (a -> b) -> a -> b
$
    ActionT Except (ReaderT WebState m) (Maybe BlockHash)
forall (m :: * -> *). StoreReadBase m => m (Maybe BlockHash)
getBestBlock ActionT Except (ReaderT WebState m) (Maybe BlockHash)
-> (Maybe BlockHash -> WebT m BlockData) -> WebT m BlockData
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Nothing -> (WebMetrics -> ErrorCounter) -> Except -> WebT m BlockData
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
blockErrors Except
ThingNotFound
        Just bb :: BlockHash
bb -> BlockHash -> ActionT Except (ReaderT WebState m) (Maybe BlockData)
forall (m :: * -> *).
StoreReadBase m =>
BlockHash -> m (Maybe BlockData)
getBlock BlockHash
bb ActionT Except (ReaderT WebState m) (Maybe BlockData)
-> (Maybe BlockData -> WebT m BlockData) -> WebT m BlockData
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
            Nothing -> (WebMetrics -> ErrorCounter) -> Except -> WebT m BlockData
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
blockErrors Except
ThingNotFound
            Just b :: BlockData
b  -> BlockData -> WebT m BlockData
forall (m :: * -> *) a. Monad m => a -> m a
return (BlockData -> WebT m BlockData) -> BlockData -> WebT m BlockData
forall a b. (a -> b) -> a -> b
$ Bool -> BlockData -> BlockData
pruneTx Bool
notx BlockData
b

scottyBlockBestRaw ::
       (MonadUnliftIO m, MonadLoggerIO m)
    => GetBlockBestRaw
    -> WebT m (RawResult H.Block)
scottyBlockBestRaw :: GetBlockBestRaw -> WebT m (RawResult Block)
scottyBlockBestRaw _ =
    (WebMetrics -> StatDist)
-> Int -> WebT m (RawResult Block) -> WebT m (RawResult Block)
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
rawBlockResponseTime 1 (WebT m (RawResult Block) -> WebT m (RawResult Block))
-> WebT m (RawResult Block) -> WebT m (RawResult Block)
forall a b. (a -> b) -> a -> b
$
    (Block -> RawResult Block)
-> ActionT Except (ReaderT WebState m) Block
-> WebT m (RawResult Block)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Block -> RawResult Block
forall a. a -> RawResult a
RawResult (ActionT Except (ReaderT WebState m) Block
 -> WebT m (RawResult Block))
-> ActionT Except (ReaderT WebState m) Block
-> WebT m (RawResult Block)
forall a b. (a -> b) -> a -> b
$
    ActionT Except (ReaderT WebState m) Block
-> (BlockHash -> ActionT Except (ReaderT WebState m) Block)
-> Maybe BlockHash
-> ActionT Except (ReaderT WebState m) Block
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ((WebMetrics -> ErrorCounter)
-> Except -> ActionT Except (ReaderT WebState m) Block
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
blockErrors Except
ThingNotFound) BlockHash -> ActionT Except (ReaderT WebState m) Block
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
BlockHash -> WebT m Block
getRawBlock (Maybe BlockHash -> ActionT Except (ReaderT WebState m) Block)
-> ActionT Except (ReaderT WebState m) (Maybe BlockHash)
-> ActionT Except (ReaderT WebState m) Block
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ActionT Except (ReaderT WebState m) (Maybe BlockHash)
forall (m :: * -> *). StoreReadBase m => m (Maybe BlockHash)
getBestBlock

-- GET BlockLatest --

scottyBlockLatest ::
       (MonadUnliftIO m, MonadLoggerIO m)
    => GetBlockLatest
    -> WebT m [BlockData]
scottyBlockLatest :: GetBlockLatest -> WebT m [BlockData]
scottyBlockLatest (GetBlockLatest (NoTx noTx :: Bool
noTx)) =
    (WebMetrics -> StatDist)
-> Int -> WebT m [BlockData] -> WebT m [BlockData]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
blockResponseTime 100 (WebT m [BlockData] -> WebT m [BlockData])
-> WebT m [BlockData] -> WebT m [BlockData]
forall a b. (a -> b) -> a -> b
$
    WebT m [BlockData]
-> (BlockHash -> WebT m [BlockData])
-> Maybe BlockHash
-> WebT m [BlockData]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ((WebMetrics -> ErrorCounter) -> Except -> WebT m [BlockData]
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
blockErrors Except
ThingNotFound) ([BlockData] -> Maybe BlockData -> WebT m [BlockData]
forall (m :: * -> *).
StoreReadBase m =>
[BlockData] -> Maybe BlockData -> m [BlockData]
go [] (Maybe BlockData -> WebT m [BlockData])
-> (BlockHash
    -> ActionT Except (ReaderT WebState m) (Maybe BlockData))
-> BlockHash
-> WebT m [BlockData]
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< BlockHash -> ActionT Except (ReaderT WebState m) (Maybe BlockData)
forall (m :: * -> *).
StoreReadBase m =>
BlockHash -> m (Maybe BlockData)
getBlock) (Maybe BlockHash -> WebT m [BlockData])
-> ActionT Except (ReaderT WebState m) (Maybe BlockHash)
-> WebT m [BlockData]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ActionT Except (ReaderT WebState m) (Maybe BlockHash)
forall (m :: * -> *). StoreReadBase m => m (Maybe BlockHash)
getBestBlock
  where
    go :: [BlockData] -> Maybe BlockData -> m [BlockData]
go acc :: [BlockData]
acc Nothing = [BlockData] -> m [BlockData]
forall (m :: * -> *) a. Monad m => a -> m a
return [BlockData]
acc
    go acc :: [BlockData]
acc (Just b :: BlockData
b)
        | BlockData -> Word32
blockDataHeight BlockData
b Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
<= 0 = [BlockData] -> m [BlockData]
forall (m :: * -> *) a. Monad m => a -> m a
return [BlockData]
acc
        | [BlockData] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [BlockData]
acc Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 99 = [BlockData] -> m [BlockData]
forall (m :: * -> *) a. Monad m => a -> m a
return (BlockData
bBlockData -> [BlockData] -> [BlockData]
forall a. a -> [a] -> [a]
:[BlockData]
acc)
        | Bool
otherwise = do
            let prev :: BlockHash
prev = BlockHeader -> BlockHash
H.prevBlock (BlockData -> BlockHeader
blockDataHeader BlockData
b)
            [BlockData] -> Maybe BlockData -> m [BlockData]
go (Bool -> BlockData -> BlockData
pruneTx Bool
noTx BlockData
b BlockData -> [BlockData] -> [BlockData]
forall a. a -> [a] -> [a]
: [BlockData]
acc) (Maybe BlockData -> m [BlockData])
-> m (Maybe BlockData) -> m [BlockData]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< BlockHash -> m (Maybe BlockData)
forall (m :: * -> *).
StoreReadBase m =>
BlockHash -> m (Maybe BlockData)
getBlock BlockHash
prev

-- GET BlockHeight / BlockHeights / BlockHeightRaw --

scottyBlockHeight ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetBlockHeight -> WebT m [BlockData]
scottyBlockHeight :: GetBlockHeight -> WebT m [BlockData]
scottyBlockHeight (GetBlockHeight h :: HeightParam
h (NoTx notx :: Bool
notx)) =
    (WebMetrics -> StatDist)
-> Int -> WebT m [BlockData] -> WebT m [BlockData]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
blockResponseTime 1 (WebT m [BlockData] -> WebT m [BlockData])
-> WebT m [BlockData] -> WebT m [BlockData]
forall a b. (a -> b) -> a -> b
$
    ([BlockHash] -> Bool -> WebT m [BlockData]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
[BlockHash] -> Bool -> WebT m [BlockData]
`getBlocks` Bool
notx) ([BlockHash] -> WebT m [BlockData])
-> ActionT Except (ReaderT WebState m) [BlockHash]
-> WebT m [BlockData]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Word32 -> ActionT Except (ReaderT WebState m) [BlockHash]
forall (m :: * -> *). StoreReadBase m => Word32 -> m [BlockHash]
getBlocksAtHeight (HeightParam -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral HeightParam
h)

scottyBlockHeights ::
       (MonadUnliftIO m, MonadLoggerIO m)
    => GetBlockHeights
    -> WebT m [BlockData]
scottyBlockHeights :: GetBlockHeights -> WebT m [BlockData]
scottyBlockHeights (GetBlockHeights (HeightsParam heights :: [Natural]
heights) (NoTx notx :: Bool
notx)) =
    (WebMetrics -> StatDist)
-> Int -> WebT m [BlockData] -> WebT m [BlockData]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
blockResponseTime ([Natural] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Natural]
heights) (WebT m [BlockData] -> WebT m [BlockData])
-> WebT m [BlockData] -> WebT m [BlockData]
forall a b. (a -> b) -> a -> b
$ do
    [BlockHash]
bhs <- [[BlockHash]] -> [BlockHash]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[BlockHash]] -> [BlockHash])
-> ActionT Except (ReaderT WebState m) [[BlockHash]]
-> ActionT Except (ReaderT WebState m) [BlockHash]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Word32 -> ActionT Except (ReaderT WebState m) [BlockHash])
-> [Word32] -> ActionT Except (ReaderT WebState m) [[BlockHash]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Word32 -> ActionT Except (ReaderT WebState m) [BlockHash]
forall (m :: * -> *). StoreReadBase m => Word32 -> m [BlockHash]
getBlocksAtHeight (Natural -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Natural -> Word32) -> [Natural] -> [Word32]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Natural]
heights)
    [BlockHash] -> Bool -> WebT m [BlockData]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
[BlockHash] -> Bool -> WebT m [BlockData]
getBlocks [BlockHash]
bhs Bool
notx

scottyBlockHeightRaw ::
       (MonadUnliftIO m, MonadLoggerIO m)
    => GetBlockHeightRaw
    -> WebT m (RawResultList H.Block)
scottyBlockHeightRaw :: GetBlockHeightRaw -> WebT m (RawResultList Block)
scottyBlockHeightRaw (GetBlockHeightRaw h :: HeightParam
h) =
    (WebMetrics -> StatDist)
-> Int
-> WebT m (RawResultList Block)
-> WebT m (RawResultList Block)
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
rawBlockResponseTime 1 (WebT m (RawResultList Block) -> WebT m (RawResultList Block))
-> WebT m (RawResultList Block) -> WebT m (RawResultList Block)
forall a b. (a -> b) -> a -> b
$
    [Block] -> RawResultList Block
forall a. [a] -> RawResultList a
RawResultList ([Block] -> RawResultList Block)
-> ActionT Except (ReaderT WebState m) [Block]
-> WebT m (RawResultList Block)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((BlockHash -> ActionT Except (ReaderT WebState m) Block)
-> [BlockHash] -> ActionT Except (ReaderT WebState m) [Block]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM BlockHash -> ActionT Except (ReaderT WebState m) Block
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
BlockHash -> WebT m Block
getRawBlock ([BlockHash] -> ActionT Except (ReaderT WebState m) [Block])
-> ActionT Except (ReaderT WebState m) [BlockHash]
-> ActionT Except (ReaderT WebState m) [Block]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Word32 -> ActionT Except (ReaderT WebState m) [BlockHash]
forall (m :: * -> *). StoreReadBase m => Word32 -> m [BlockHash]
getBlocksAtHeight (HeightParam -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral HeightParam
h))

-- GET BlockTime / BlockTimeRaw --

scottyBlockTime :: (MonadUnliftIO m, MonadLoggerIO m)
                => GetBlockTime -> WebT m BlockData
scottyBlockTime :: GetBlockTime -> WebT m BlockData
scottyBlockTime (GetBlockTime (TimeParam t :: Word64
t) (NoTx notx :: Bool
notx)) =
    (WebMetrics -> StatDist)
-> Int -> WebT m BlockData -> WebT m BlockData
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
blockResponseTime 1 (WebT m BlockData -> WebT m BlockData)
-> WebT m BlockData -> WebT m BlockData
forall a b. (a -> b) -> a -> b
$ do
    Chain
ch <- ReaderT WebState m Chain
-> ActionT Except (ReaderT WebState m) Chain
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m Chain
 -> ActionT Except (ReaderT WebState m) Chain)
-> ReaderT WebState m Chain
-> ActionT Except (ReaderT WebState m) Chain
forall a b. (a -> b) -> a -> b
$ (WebState -> Chain) -> ReaderT WebState m Chain
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (Store -> Chain
storeChain (Store -> Chain) -> (WebState -> Store) -> WebState -> Chain
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebConfig -> Store
webStore (WebConfig -> Store)
-> (WebState -> WebConfig) -> WebState -> Store
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig)
    Maybe BlockData
m <- Chain
-> Word64 -> ActionT Except (ReaderT WebState m) (Maybe BlockData)
forall (m :: * -> *).
(MonadIO m, StoreReadExtra m) =>
Chain -> Word64 -> m (Maybe BlockData)
blockAtOrBefore Chain
ch Word64
t
    WebT m BlockData
-> (BlockData -> WebT m BlockData)
-> Maybe BlockData
-> WebT m BlockData
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ((WebMetrics -> ErrorCounter) -> Except -> WebT m BlockData
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
blockErrors Except
ThingNotFound) (BlockData -> WebT m BlockData
forall (m :: * -> *) a. Monad m => a -> m a
return (BlockData -> WebT m BlockData)
-> (BlockData -> BlockData) -> BlockData -> WebT m BlockData
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> BlockData -> BlockData
pruneTx Bool
notx) Maybe BlockData
m

scottyBlockMTP :: (MonadUnliftIO m, MonadLoggerIO m)
               => GetBlockMTP -> WebT m BlockData
scottyBlockMTP :: GetBlockMTP -> WebT m BlockData
scottyBlockMTP (GetBlockMTP (TimeParam t :: Word64
t) (NoTx noTx :: Bool
noTx)) =
    (WebMetrics -> StatDist)
-> Int -> WebT m BlockData -> WebT m BlockData
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
blockResponseTime 1 (WebT m BlockData -> WebT m BlockData)
-> WebT m BlockData -> WebT m BlockData
forall a b. (a -> b) -> a -> b
$ do
    Chain
ch <- ReaderT WebState m Chain
-> ActionT Except (ReaderT WebState m) Chain
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m Chain
 -> ActionT Except (ReaderT WebState m) Chain)
-> ReaderT WebState m Chain
-> ActionT Except (ReaderT WebState m) Chain
forall a b. (a -> b) -> a -> b
$ (WebState -> Chain) -> ReaderT WebState m Chain
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (Store -> Chain
storeChain (Store -> Chain) -> (WebState -> Store) -> WebState -> Chain
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebConfig -> Store
webStore (WebConfig -> Store)
-> (WebState -> WebConfig) -> WebState -> Store
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig)
    Maybe BlockData
m <- Chain
-> Word64 -> ActionT Except (ReaderT WebState m) (Maybe BlockData)
forall (m :: * -> *).
(MonadIO m, StoreReadExtra m) =>
Chain -> Word64 -> m (Maybe BlockData)
blockAtOrAfterMTP Chain
ch Word64
t
    WebT m BlockData
-> (BlockData -> WebT m BlockData)
-> Maybe BlockData
-> WebT m BlockData
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ((WebMetrics -> ErrorCounter) -> Except -> WebT m BlockData
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
blockErrors Except
ThingNotFound) (BlockData -> WebT m BlockData
forall (m :: * -> *) a. Monad m => a -> m a
return (BlockData -> WebT m BlockData)
-> (BlockData -> BlockData) -> BlockData -> WebT m BlockData
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> BlockData -> BlockData
pruneTx Bool
noTx) Maybe BlockData
m

scottyBlockTimeRaw :: (MonadUnliftIO m, MonadLoggerIO m)
                   => GetBlockTimeRaw -> WebT m (RawResult H.Block)
scottyBlockTimeRaw :: GetBlockTimeRaw -> WebT m (RawResult Block)
scottyBlockTimeRaw (GetBlockTimeRaw (TimeParam t :: Word64
t)) =
    (WebMetrics -> StatDist)
-> Int -> WebT m (RawResult Block) -> WebT m (RawResult Block)
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
rawBlockResponseTime 1 (WebT m (RawResult Block) -> WebT m (RawResult Block))
-> WebT m (RawResult Block) -> WebT m (RawResult Block)
forall a b. (a -> b) -> a -> b
$ do
    Chain
ch <- ReaderT WebState m Chain
-> ActionT Except (ReaderT WebState m) Chain
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m Chain
 -> ActionT Except (ReaderT WebState m) Chain)
-> ReaderT WebState m Chain
-> ActionT Except (ReaderT WebState m) Chain
forall a b. (a -> b) -> a -> b
$ (WebState -> Chain) -> ReaderT WebState m Chain
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (Store -> Chain
storeChain (Store -> Chain) -> (WebState -> Store) -> WebState -> Chain
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebConfig -> Store
webStore (WebConfig -> Store)
-> (WebState -> WebConfig) -> WebState -> Store
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig)
    Maybe BlockData
m <- Chain
-> Word64 -> ActionT Except (ReaderT WebState m) (Maybe BlockData)
forall (m :: * -> *).
(MonadIO m, StoreReadExtra m) =>
Chain -> Word64 -> m (Maybe BlockData)
blockAtOrBefore Chain
ch Word64
t
    BlockData
b <- WebT m BlockData
-> (BlockData -> WebT m BlockData)
-> Maybe BlockData
-> WebT m BlockData
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ((WebMetrics -> ErrorCounter) -> Except -> WebT m BlockData
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
blockErrors Except
ThingNotFound) BlockData -> WebT m BlockData
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe BlockData
m
    Block -> RawResult Block
forall a. a -> RawResult a
RawResult (Block -> RawResult Block)
-> ActionT Except (ReaderT WebState m) Block
-> WebT m (RawResult Block)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReaderT WebState m Block
-> ActionT Except (ReaderT WebState m) Block
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (BlockData -> ReaderT WebState m Block
forall (m :: * -> *).
(MonadUnliftIO m, StoreReadBase m) =>
BlockData -> m Block
toRawBlock BlockData
b)

scottyBlockMTPRaw :: (MonadUnliftIO m, MonadLoggerIO m)
                  => GetBlockMTPRaw -> WebT m (RawResult H.Block)
scottyBlockMTPRaw :: GetBlockMTPRaw -> WebT m (RawResult Block)
scottyBlockMTPRaw (GetBlockMTPRaw (TimeParam t :: Word64
t)) =
    (WebMetrics -> StatDist)
-> Int -> WebT m (RawResult Block) -> WebT m (RawResult Block)
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
rawBlockResponseTime 1 (WebT m (RawResult Block) -> WebT m (RawResult Block))
-> WebT m (RawResult Block) -> WebT m (RawResult Block)
forall a b. (a -> b) -> a -> b
$ do
    Chain
ch <- ReaderT WebState m Chain
-> ActionT Except (ReaderT WebState m) Chain
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m Chain
 -> ActionT Except (ReaderT WebState m) Chain)
-> ReaderT WebState m Chain
-> ActionT Except (ReaderT WebState m) Chain
forall a b. (a -> b) -> a -> b
$ (WebState -> Chain) -> ReaderT WebState m Chain
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (Store -> Chain
storeChain (Store -> Chain) -> (WebState -> Store) -> WebState -> Chain
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebConfig -> Store
webStore (WebConfig -> Store)
-> (WebState -> WebConfig) -> WebState -> Store
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig)
    Maybe BlockData
m <- Chain
-> Word64 -> ActionT Except (ReaderT WebState m) (Maybe BlockData)
forall (m :: * -> *).
(MonadIO m, StoreReadExtra m) =>
Chain -> Word64 -> m (Maybe BlockData)
blockAtOrAfterMTP Chain
ch Word64
t
    BlockData
b <- WebT m BlockData
-> (BlockData -> WebT m BlockData)
-> Maybe BlockData
-> WebT m BlockData
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ((WebMetrics -> ErrorCounter) -> Except -> WebT m BlockData
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
blockErrors Except
ThingNotFound) BlockData -> WebT m BlockData
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe BlockData
m
    Block -> RawResult Block
forall a. a -> RawResult a
RawResult (Block -> RawResult Block)
-> ActionT Except (ReaderT WebState m) Block
-> WebT m (RawResult Block)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReaderT WebState m Block
-> ActionT Except (ReaderT WebState m) Block
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (BlockData -> ReaderT WebState m Block
forall (m :: * -> *).
(MonadUnliftIO m, StoreReadBase m) =>
BlockData -> m Block
toRawBlock BlockData
b)

-- GET Transactions --

scottyTx :: (MonadUnliftIO m, MonadLoggerIO m) => GetTx -> WebT m Transaction
scottyTx :: GetTx -> WebT m Transaction
scottyTx (GetTx txid :: TxHash
txid) =
    (WebMetrics -> StatDist)
-> Int -> WebT m Transaction -> WebT m Transaction
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
txResponseTime 1 (WebT m Transaction -> WebT m Transaction)
-> WebT m Transaction -> WebT m Transaction
forall a b. (a -> b) -> a -> b
$
    WebT m Transaction
-> (Transaction -> WebT m Transaction)
-> Maybe Transaction
-> WebT m Transaction
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ((WebMetrics -> ErrorCounter) -> Except -> WebT m Transaction
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
txErrors Except
ThingNotFound) Transaction -> WebT m Transaction
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Transaction -> WebT m Transaction)
-> ActionT Except (ReaderT WebState m) (Maybe Transaction)
-> WebT m Transaction
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< TxHash -> ActionT Except (ReaderT WebState m) (Maybe Transaction)
forall (m :: * -> *).
(Monad m, StoreReadBase m) =>
TxHash -> m (Maybe Transaction)
getTransaction TxHash
txid

scottyTxs ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetTxs -> WebT m [Transaction]
scottyTxs :: GetTxs -> WebT m [Transaction]
scottyTxs (GetTxs txids :: [TxHash]
txids) =
    (WebMetrics -> StatDist)
-> Int -> WebT m [Transaction] -> WebT m [Transaction]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
txResponseTime ([TxHash] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [TxHash]
txids) (WebT m [Transaction] -> WebT m [Transaction])
-> WebT m [Transaction] -> WebT m [Transaction]
forall a b. (a -> b) -> a -> b
$
    [Maybe Transaction] -> [Transaction]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe Transaction] -> [Transaction])
-> ActionT Except (ReaderT WebState m) [Maybe Transaction]
-> WebT m [Transaction]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (TxHash -> ActionT Except (ReaderT WebState m) (Maybe Transaction))
-> [TxHash]
-> ActionT Except (ReaderT WebState m) [Maybe Transaction]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM TxHash -> ActionT Except (ReaderT WebState m) (Maybe Transaction)
forall (m :: * -> *).
(Monad m, StoreReadBase m) =>
TxHash -> m (Maybe Transaction)
getTransaction ([TxHash] -> [TxHash]
forall a. Eq a => [a] -> [a]
nub [TxHash]
txids)

scottyTxRaw ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetTxRaw -> WebT m (RawResult Tx)
scottyTxRaw :: GetTxRaw -> WebT m (RawResult Tx)
scottyTxRaw (GetTxRaw txid :: TxHash
txid) =
    (WebMetrics -> StatDist)
-> Int -> WebT m (RawResult Tx) -> WebT m (RawResult Tx)
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
txResponseTime 1 (WebT m (RawResult Tx) -> WebT m (RawResult Tx))
-> WebT m (RawResult Tx) -> WebT m (RawResult Tx)
forall a b. (a -> b) -> a -> b
$ do
    Transaction
tx <- WebT m Transaction
-> (Transaction -> WebT m Transaction)
-> Maybe Transaction
-> WebT m Transaction
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ((WebMetrics -> ErrorCounter) -> Except -> WebT m Transaction
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
txErrors Except
ThingNotFound) Transaction -> WebT m Transaction
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Transaction -> WebT m Transaction)
-> ActionT Except (ReaderT WebState m) (Maybe Transaction)
-> WebT m Transaction
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< TxHash -> ActionT Except (ReaderT WebState m) (Maybe Transaction)
forall (m :: * -> *).
(Monad m, StoreReadBase m) =>
TxHash -> m (Maybe Transaction)
getTransaction TxHash
txid
    RawResult Tx -> WebT m (RawResult Tx)
forall (m :: * -> *) a. Monad m => a -> m a
return (RawResult Tx -> WebT m (RawResult Tx))
-> RawResult Tx -> WebT m (RawResult Tx)
forall a b. (a -> b) -> a -> b
$ Tx -> RawResult Tx
forall a. a -> RawResult a
RawResult (Tx -> RawResult Tx) -> Tx -> RawResult Tx
forall a b. (a -> b) -> a -> b
$ Transaction -> Tx
transactionData Transaction
tx

scottyTxsRaw ::
       (MonadUnliftIO m, MonadLoggerIO m)
    => GetTxsRaw
    -> WebT m (RawResultList Tx)
scottyTxsRaw :: GetTxsRaw -> WebT m (RawResultList Tx)
scottyTxsRaw (GetTxsRaw txids :: [TxHash]
txids) =
    (WebMetrics -> StatDist)
-> Int -> WebT m (RawResultList Tx) -> WebT m (RawResultList Tx)
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
txResponseTime ([TxHash] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [TxHash]
txids) (WebT m (RawResultList Tx) -> WebT m (RawResultList Tx))
-> WebT m (RawResultList Tx) -> WebT m (RawResultList Tx)
forall a b. (a -> b) -> a -> b
$ do
    [Transaction]
txs <- [Maybe Transaction] -> [Transaction]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe Transaction] -> [Transaction])
-> ActionT Except (ReaderT WebState m) [Maybe Transaction]
-> ActionT Except (ReaderT WebState m) [Transaction]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (TxHash -> ActionT Except (ReaderT WebState m) (Maybe Transaction))
-> [TxHash]
-> ActionT Except (ReaderT WebState m) [Maybe Transaction]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM TxHash -> ActionT Except (ReaderT WebState m) (Maybe Transaction)
forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(MonadTrans t, MonadUnliftIO m, StoreReadBase m) =>
TxHash -> t m (Maybe Transaction)
f ([TxHash] -> [TxHash]
forall a. Eq a => [a] -> [a]
nub [TxHash]
txids)
    RawResultList Tx -> WebT m (RawResultList Tx)
forall (m :: * -> *) a. Monad m => a -> m a
return (RawResultList Tx -> WebT m (RawResultList Tx))
-> RawResultList Tx -> WebT m (RawResultList Tx)
forall a b. (a -> b) -> a -> b
$ [Tx] -> RawResultList Tx
forall a. [a] -> RawResultList a
RawResultList ([Tx] -> RawResultList Tx) -> [Tx] -> RawResultList Tx
forall a b. (a -> b) -> a -> b
$ Transaction -> Tx
transactionData (Transaction -> Tx) -> [Transaction] -> [Tx]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Transaction]
txs
  where
    f :: TxHash -> t m (Maybe Transaction)
f x :: TxHash
x = m (Maybe Transaction) -> t m (Maybe Transaction)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Maybe Transaction) -> t m (Maybe Transaction))
-> m (Maybe Transaction) -> t m (Maybe Transaction)
forall a b. (a -> b) -> a -> b
$ ((forall a. m a -> IO a) -> IO (Maybe Transaction))
-> m (Maybe Transaction)
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO (((forall a. m a -> IO a) -> IO (Maybe Transaction))
 -> m (Maybe Transaction))
-> ((forall a. m a -> IO a) -> IO (Maybe Transaction))
-> m (Maybe Transaction)
forall a b. (a -> b) -> a -> b
$ \run :: forall a. m a -> IO a
run ->
          IO (Maybe Transaction) -> IO (Maybe Transaction)
forall a. IO a -> IO a
unsafeInterleaveIO (IO (Maybe Transaction) -> IO (Maybe Transaction))
-> (m (Maybe Transaction) -> IO (Maybe Transaction))
-> m (Maybe Transaction)
-> IO (Maybe Transaction)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m (Maybe Transaction) -> IO (Maybe Transaction)
forall a. m a -> IO a
run (m (Maybe Transaction) -> IO (Maybe Transaction))
-> m (Maybe Transaction) -> IO (Maybe Transaction)
forall a b. (a -> b) -> a -> b
$
          TxHash -> m (Maybe Transaction)
forall (m :: * -> *).
(Monad m, StoreReadBase m) =>
TxHash -> m (Maybe Transaction)
getTransaction TxHash
x

getTxsBlock :: (MonadUnliftIO m, MonadLoggerIO m)
            => H.BlockHash
            -> WebT m [Transaction]
getTxsBlock :: BlockHash -> WebT m [Transaction]
getTxsBlock h :: BlockHash
h = do
    BlockData
b <- WebT m BlockData
-> (BlockData -> WebT m BlockData)
-> Maybe BlockData
-> WebT m BlockData
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ((WebMetrics -> ErrorCounter) -> Except -> WebT m BlockData
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
txErrors Except
ThingNotFound) BlockData -> WebT m BlockData
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe BlockData -> WebT m BlockData)
-> ActionT Except (ReaderT WebState m) (Maybe BlockData)
-> WebT m BlockData
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< BlockHash -> ActionT Except (ReaderT WebState m) (Maybe BlockData)
forall (m :: * -> *).
StoreReadBase m =>
BlockHash -> m (Maybe BlockData)
getBlock BlockHash
h
    (TxHash -> ActionT Except (ReaderT WebState m) Transaction)
-> [TxHash] -> WebT m [Transaction]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM TxHash -> ActionT Except (ReaderT WebState m) Transaction
forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(MonadTrans t, MonadUnliftIO m, StoreReadBase m) =>
TxHash -> t m Transaction
f (BlockData -> [TxHash]
blockDataTxs BlockData
b)
  where
    f :: TxHash -> t m Transaction
f x :: TxHash
x = m Transaction -> t m Transaction
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m Transaction -> t m Transaction)
-> m Transaction -> t m Transaction
forall a b. (a -> b) -> a -> b
$ ((forall a. m a -> IO a) -> IO Transaction) -> m Transaction
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO (((forall a. m a -> IO a) -> IO Transaction) -> m Transaction)
-> ((forall a. m a -> IO a) -> IO Transaction) -> m Transaction
forall a b. (a -> b) -> a -> b
$ \run :: forall a. m a -> IO a
run ->
          IO Transaction -> IO Transaction
forall a. IO a -> IO a
unsafeInterleaveIO (IO Transaction -> IO Transaction)
-> (m Transaction -> IO Transaction)
-> m Transaction
-> IO Transaction
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m Transaction -> IO Transaction
forall a. m a -> IO a
run (m Transaction -> IO Transaction)
-> m Transaction -> IO Transaction
forall a b. (a -> b) -> a -> b
$
          TxHash -> m (Maybe Transaction)
forall (m :: * -> *).
(Monad m, StoreReadBase m) =>
TxHash -> m (Maybe Transaction)
getTransaction TxHash
x m (Maybe Transaction)
-> (Maybe Transaction -> m Transaction) -> m Transaction
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
              Nothing -> m Transaction
forall a. HasCallStack => a
undefined
              Just t :: Transaction
t -> Transaction -> m Transaction
forall (m :: * -> *) a. Monad m => a -> m a
return Transaction
t

scottyTxsBlock ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetTxsBlock -> WebT m [Transaction]
scottyTxsBlock :: GetTxsBlock -> WebT m [Transaction]
scottyTxsBlock (GetTxsBlock h :: BlockHash
h) =
    (WebMetrics -> StatDist)
-> Int -> WebT m [Transaction] -> WebT m [Transaction]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
txsBlockResponseTime 1 (WebT m [Transaction] -> WebT m [Transaction])
-> WebT m [Transaction] -> WebT m [Transaction]
forall a b. (a -> b) -> a -> b
$ BlockHash -> WebT m [Transaction]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
BlockHash -> WebT m [Transaction]
getTxsBlock BlockHash
h

scottyTxsBlockRaw ::
       (MonadUnliftIO m, MonadLoggerIO m)
    => GetTxsBlockRaw
    -> WebT m (RawResultList Tx)
scottyTxsBlockRaw :: GetTxsBlockRaw -> WebT m (RawResultList Tx)
scottyTxsBlockRaw (GetTxsBlockRaw h :: BlockHash
h) =
    (WebMetrics -> StatDist)
-> Int -> WebT m (RawResultList Tx) -> WebT m (RawResultList Tx)
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
txsBlockResponseTime 1 (WebT m (RawResultList Tx) -> WebT m (RawResultList Tx))
-> WebT m (RawResultList Tx) -> WebT m (RawResultList Tx)
forall a b. (a -> b) -> a -> b
$
    [Tx] -> RawResultList Tx
forall a. [a] -> RawResultList a
RawResultList ([Tx] -> RawResultList Tx)
-> ([Transaction] -> [Tx]) -> [Transaction] -> RawResultList Tx
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Transaction -> Tx) -> [Transaction] -> [Tx]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Transaction -> Tx
transactionData ([Transaction] -> RawResultList Tx)
-> ActionT Except (ReaderT WebState m) [Transaction]
-> WebT m (RawResultList Tx)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> BlockHash -> ActionT Except (ReaderT WebState m) [Transaction]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
BlockHash -> WebT m [Transaction]
getTxsBlock BlockHash
h

-- GET TransactionAfterHeight --

scottyTxAfter ::
       (MonadUnliftIO m, MonadLoggerIO m)
    => GetTxAfter
    -> WebT m (GenericResult (Maybe Bool))
scottyTxAfter :: GetTxAfter -> WebT m (GenericResult (Maybe Bool))
scottyTxAfter (GetTxAfter txid :: TxHash
txid height :: HeightParam
height) =
    (WebMetrics -> StatDist)
-> Int
-> WebT m (GenericResult (Maybe Bool))
-> WebT m (GenericResult (Maybe Bool))
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
txAfterResponseTime 1 (WebT m (GenericResult (Maybe Bool))
 -> WebT m (GenericResult (Maybe Bool)))
-> WebT m (GenericResult (Maybe Bool))
-> WebT m (GenericResult (Maybe Bool))
forall a b. (a -> b) -> a -> b
$
    Maybe Bool -> GenericResult (Maybe Bool)
forall a. a -> GenericResult a
GenericResult (Maybe Bool -> GenericResult (Maybe Bool))
-> ActionT Except (ReaderT WebState m) (Maybe Bool)
-> WebT m (GenericResult (Maybe Bool))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Word32
-> TxHash -> ActionT Except (ReaderT WebState m) (Maybe Bool)
forall (m :: * -> *).
(MonadIO m, StoreReadBase m) =>
Word32 -> TxHash -> m (Maybe Bool)
cbAfterHeight (HeightParam -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral HeightParam
height) TxHash
txid

-- | Check if any of the ancestors of this transaction is a coinbase after the
-- specified height. Returns 'Nothing' if answer cannot be computed before
-- hitting limits.
cbAfterHeight ::
       (MonadIO m, StoreReadBase m)
    => H.BlockHeight
    -> TxHash
    -> m (Maybe Bool)
cbAfterHeight :: Word32 -> TxHash -> m (Maybe Bool)
cbAfterHeight height :: Word32
height txid :: TxHash
txid =
    Integer
-> HashSet TxHash -> HashSet TxHash -> [TxHash] -> m (Maybe Bool)
forall a (m :: * -> *).
(Eq a, Num a, StoreReadBase m) =>
a -> HashSet TxHash -> HashSet TxHash -> [TxHash] -> m (Maybe Bool)
inputs 10000 HashSet TxHash
forall a. HashSet a
HashSet.empty HashSet TxHash
forall a. HashSet a
HashSet.empty [TxHash
txid]
  where
    inputs :: a -> HashSet TxHash -> HashSet TxHash -> [TxHash] -> m (Maybe Bool)
inputs 0 _ _ [] = Maybe Bool -> m (Maybe Bool)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Bool
forall a. Maybe a
Nothing
    inputs i :: a
i is :: HashSet TxHash
is ns :: HashSet TxHash
ns [] =
        let is' :: HashSet TxHash
is' = HashSet TxHash -> HashSet TxHash -> HashSet TxHash
forall a. (Eq a, Hashable a) => HashSet a -> HashSet a -> HashSet a
HashSet.union HashSet TxHash
is HashSet TxHash
ns
            ns' :: HashSet a
ns' = HashSet a
forall a. HashSet a
HashSet.empty
            ts :: [TxHash]
ts = HashSet TxHash -> [TxHash]
forall a. HashSet a -> [a]
HashSet.toList (HashSet TxHash -> HashSet TxHash -> HashSet TxHash
forall a. (Eq a, Hashable a) => HashSet a -> HashSet a -> HashSet a
HashSet.difference HashSet TxHash
ns HashSet TxHash
is)
        in case [TxHash]
ts of
               [] -> Maybe Bool -> m (Maybe Bool)
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
False)
               _  -> a -> HashSet TxHash -> HashSet TxHash -> [TxHash] -> m (Maybe Bool)
inputs a
i HashSet TxHash
is' HashSet TxHash
forall a. HashSet a
ns' [TxHash]
ts
    inputs i :: a
i is :: HashSet TxHash
is ns :: HashSet TxHash
ns (t :: TxHash
t:ts :: [TxHash]
ts) = TxHash -> m (Maybe Transaction)
forall (m :: * -> *).
(Monad m, StoreReadBase m) =>
TxHash -> m (Maybe Transaction)
getTransaction TxHash
t m (Maybe Transaction)
-> (Maybe Transaction -> m (Maybe Bool)) -> m (Maybe Bool)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Nothing -> Maybe Bool -> m (Maybe Bool)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Bool
forall a. Maybe a
Nothing
        Just tx :: Transaction
tx | Transaction -> Bool
height_check Transaction
tx ->
                      if Transaction -> Bool
cb_check Transaction
tx
                      then Maybe Bool -> m (Maybe Bool)
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True)
                      else let ns' :: HashSet TxHash
ns' = HashSet TxHash -> HashSet TxHash -> HashSet TxHash
forall a. (Eq a, Hashable a) => HashSet a -> HashSet a -> HashSet a
HashSet.union (Transaction -> HashSet TxHash
ins Transaction
tx) HashSet TxHash
ns
                          in a -> HashSet TxHash -> HashSet TxHash -> [TxHash] -> m (Maybe Bool)
inputs (a
i a -> a -> a
forall a. Num a => a -> a -> a
- 1) HashSet TxHash
is HashSet TxHash
ns' [TxHash]
ts
                | Bool
otherwise -> a -> HashSet TxHash -> HashSet TxHash -> [TxHash] -> m (Maybe Bool)
inputs (a
i a -> a -> a
forall a. Num a => a -> a -> a
- 1) HashSet TxHash
is HashSet TxHash
ns [TxHash]
ts
    cb_check :: Transaction -> Bool
cb_check = (StoreInput -> Bool) -> [StoreInput] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any StoreInput -> Bool
isCoinbase ([StoreInput] -> Bool)
-> (Transaction -> [StoreInput]) -> Transaction -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> [StoreInput]
transactionInputs
    ins :: Transaction -> HashSet TxHash
ins = [TxHash] -> HashSet TxHash
forall a. (Eq a, Hashable a) => [a] -> HashSet a
HashSet.fromList ([TxHash] -> HashSet TxHash)
-> (Transaction -> [TxHash]) -> Transaction -> HashSet TxHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (StoreInput -> TxHash) -> [StoreInput] -> [TxHash]
forall a b. (a -> b) -> [a] -> [b]
map (OutPoint -> TxHash
outPointHash (OutPoint -> TxHash)
-> (StoreInput -> OutPoint) -> StoreInput -> TxHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StoreInput -> OutPoint
inputPoint) ([StoreInput] -> [TxHash])
-> (Transaction -> [StoreInput]) -> Transaction -> [TxHash]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> [StoreInput]
transactionInputs
    height_check :: Transaction -> Bool
height_check tx :: Transaction
tx =
        case Transaction -> BlockRef
transactionBlock Transaction
tx of
            BlockRef h :: Word32
h _ -> Word32
h Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
> Word32
height
            _            -> Bool
True

-- POST Transaction --

scottyPostTx :: (MonadUnliftIO m, MonadLoggerIO m) => PostTx -> WebT m TxId
scottyPostTx :: PostTx -> WebT m TxId
scottyPostTx (PostTx tx :: Tx
tx) =
    (WebMetrics -> StatDist) -> Int -> WebT m TxId -> WebT m TxId
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
postTxResponseTime 1 (WebT m TxId -> WebT m TxId) -> WebT m TxId -> WebT m TxId
forall a b. (a -> b) -> a -> b
$
    ReaderT WebState m WebConfig
-> ActionT Except (ReaderT WebState m) WebConfig
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ((WebState -> WebConfig) -> ReaderT WebState m WebConfig
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WebState -> WebConfig
webConfig) ActionT Except (ReaderT WebState m) WebConfig
-> (WebConfig -> WebT m TxId) -> WebT m TxId
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \cfg :: WebConfig
cfg -> ReaderT WebState m (Either PubExcept ())
-> ActionT Except (ReaderT WebState m) (Either PubExcept ())
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (WebConfig -> Tx -> ReaderT WebState m (Either PubExcept ())
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m, StoreReadBase m) =>
WebConfig -> Tx -> m (Either PubExcept ())
publishTx WebConfig
cfg Tx
tx) ActionT Except (ReaderT WebState m) (Either PubExcept ())
-> (Either PubExcept () -> WebT m TxId) -> WebT m TxId
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Right ()             -> TxId -> WebT m TxId
forall (m :: * -> *) a. Monad m => a -> m a
return (TxId -> WebT m TxId) -> TxId -> WebT m TxId
forall a b. (a -> b) -> a -> b
$ TxHash -> TxId
TxId (Tx -> TxHash
txHash Tx
tx)
        Left e :: PubExcept
e@(PubReject _) -> (WebMetrics -> ErrorCounter) -> Except -> WebT m TxId
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
postTxErrors (Except -> WebT m TxId) -> Except -> WebT m TxId
forall a b. (a -> b) -> a -> b
$ String -> Except
UserError (String -> Except) -> String -> Except
forall a b. (a -> b) -> a -> b
$ PubExcept -> String
forall a. Show a => a -> String
show PubExcept
e
        _                    -> (WebMetrics -> ErrorCounter) -> Except -> WebT m TxId
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
postTxErrors Except
ServerError

-- | Publish a new transaction to the network.
publishTx ::
       (MonadUnliftIO m, MonadLoggerIO m, StoreReadBase m)
    => WebConfig
    -> Tx
    -> m (Either PubExcept ())
publishTx :: WebConfig -> Tx -> m (Either PubExcept ())
publishTx cfg :: WebConfig
cfg tx :: Tx
tx =
    Publisher StoreEvent
-> (Inbox StoreEvent -> m (Either PubExcept ()))
-> m (Either PubExcept ())
forall (m :: * -> *) msg a.
MonadUnliftIO m =>
Publisher msg -> (Inbox msg -> m a) -> m a
withSubscription Publisher StoreEvent
pub ((Inbox StoreEvent -> m (Either PubExcept ()))
 -> m (Either PubExcept ()))
-> (Inbox StoreEvent -> m (Either PubExcept ()))
-> m (Either PubExcept ())
forall a b. (a -> b) -> a -> b
$ \s :: Inbox StoreEvent
s ->
        TxHash -> m (Maybe Transaction)
forall (m :: * -> *).
(Monad m, StoreReadBase m) =>
TxHash -> m (Maybe Transaction)
getTransaction (Tx -> TxHash
txHash Tx
tx) m (Maybe Transaction)
-> (Maybe Transaction -> m (Either PubExcept ()))
-> m (Either PubExcept ())
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
            Just _  -> Either PubExcept () -> m (Either PubExcept ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Either PubExcept () -> m (Either PubExcept ()))
-> Either PubExcept () -> m (Either PubExcept ())
forall a b. (a -> b) -> a -> b
$ () -> Either PubExcept ()
forall a b. b -> Either a b
Right ()
            Nothing -> Inbox StoreEvent -> m (Either PubExcept ())
forall (m :: * -> *) (mbox :: * -> *).
(MonadIO m, InChan mbox) =>
mbox StoreEvent -> m (Either PubExcept ())
go Inbox StoreEvent
s
  where
    pub :: Publisher StoreEvent
pub = Store -> Publisher StoreEvent
storePublisher (WebConfig -> Store
webStore WebConfig
cfg)
    mgr :: PeerManager
mgr = Store -> PeerManager
storeManager (WebConfig -> Store
webStore WebConfig
cfg)
    net :: Network
net = Store -> Network
storeNetwork (WebConfig -> Store
webStore WebConfig
cfg)
    go :: mbox StoreEvent -> m (Either PubExcept ())
go s :: mbox StoreEvent
s =
        PeerManager -> m [OnlinePeer]
forall (m :: * -> *). MonadIO m => PeerManager -> m [OnlinePeer]
getPeers PeerManager
mgr m [OnlinePeer]
-> ([OnlinePeer] -> m (Either PubExcept ()))
-> m (Either PubExcept ())
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
            [] -> Either PubExcept () -> m (Either PubExcept ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Either PubExcept () -> m (Either PubExcept ()))
-> Either PubExcept () -> m (Either PubExcept ())
forall a b. (a -> b) -> a -> b
$ PubExcept -> Either PubExcept ()
forall a b. a -> Either a b
Left PubExcept
PubNoPeers
            OnlinePeer {onlinePeerMailbox :: OnlinePeer -> Peer
onlinePeerMailbox = Peer
p}:_ -> do
                Tx -> Message
MTx Tx
tx Message -> Peer -> m ()
forall (m :: * -> *). MonadIO m => Message -> Peer -> m ()
`sendMessage` Peer
p
                let v :: InvType
v =
                        if Network -> Bool
getSegWit Network
net
                            then InvType
InvWitnessTx
                            else InvType
InvTx
                Message -> Peer -> m ()
forall (m :: * -> *). MonadIO m => Message -> Peer -> m ()
sendMessage
                    (GetData -> Message
MGetData ([InvVector] -> GetData
GetData [InvType -> Hash256 -> InvVector
InvVector InvType
v (TxHash -> Hash256
getTxHash (Tx -> TxHash
txHash Tx
tx))]))
                    Peer
p
                Peer -> mbox StoreEvent -> m (Either PubExcept ())
forall (m :: * -> *) (mbox :: * -> *).
(MonadIO m, InChan mbox) =>
Peer -> mbox StoreEvent -> m (Either PubExcept ())
f Peer
p mbox StoreEvent
s
    t :: Int
t = 5 Int -> Int -> Int
forall a. Num a => a -> a -> a
* 1000 Int -> Int -> Int
forall a. Num a => a -> a -> a
* 1000
    f :: Peer -> mbox StoreEvent -> m (Either PubExcept ())
f p :: Peer
p s :: mbox StoreEvent
s
      | WebConfig -> Bool
webNoMempool WebConfig
cfg = Either PubExcept () -> m (Either PubExcept ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Either PubExcept () -> m (Either PubExcept ()))
-> Either PubExcept () -> m (Either PubExcept ())
forall a b. (a -> b) -> a -> b
$ () -> Either PubExcept ()
forall a b. b -> Either a b
Right ()
      | Bool
otherwise =
        IO (Maybe (Either PubExcept ())) -> m (Maybe (Either PubExcept ()))
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Int -> IO (Either PubExcept ()) -> IO (Maybe (Either PubExcept ()))
forall (m :: * -> *) a.
MonadUnliftIO m =>
Int -> m a -> m (Maybe a)
timeout Int
t (Peer -> mbox StoreEvent -> IO (Either PubExcept ())
forall (m :: * -> *) (mbox :: * -> *).
(InChan mbox, MonadIO m) =>
Peer -> mbox StoreEvent -> m (Either PubExcept ())
g Peer
p mbox StoreEvent
s)) m (Maybe (Either PubExcept ()))
-> (Maybe (Either PubExcept ()) -> m (Either PubExcept ()))
-> m (Either PubExcept ())
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
            Nothing         -> Either PubExcept () -> m (Either PubExcept ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Either PubExcept () -> m (Either PubExcept ()))
-> Either PubExcept () -> m (Either PubExcept ())
forall a b. (a -> b) -> a -> b
$ PubExcept -> Either PubExcept ()
forall a b. a -> Either a b
Left PubExcept
PubTimeout
            Just (Left e :: PubExcept
e)   -> Either PubExcept () -> m (Either PubExcept ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Either PubExcept () -> m (Either PubExcept ()))
-> Either PubExcept () -> m (Either PubExcept ())
forall a b. (a -> b) -> a -> b
$ PubExcept -> Either PubExcept ()
forall a b. a -> Either a b
Left PubExcept
e
            Just (Right ()) -> Either PubExcept () -> m (Either PubExcept ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Either PubExcept () -> m (Either PubExcept ()))
-> Either PubExcept () -> m (Either PubExcept ())
forall a b. (a -> b) -> a -> b
$ () -> Either PubExcept ()
forall a b. b -> Either a b
Right ()
    g :: Peer -> mbox StoreEvent -> m (Either PubExcept ())
g p :: Peer
p s :: mbox StoreEvent
s =
        mbox StoreEvent -> m StoreEvent
forall (mbox :: * -> *) (m :: * -> *) msg.
(InChan mbox, MonadIO m) =>
mbox msg -> m msg
receive mbox StoreEvent
s m StoreEvent
-> (StoreEvent -> m (Either PubExcept ()))
-> m (Either PubExcept ())
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
            StoreTxReject p' :: Peer
p' h' :: TxHash
h' c :: RejectCode
c _
                | Peer
p Peer -> Peer -> Bool
forall a. Eq a => a -> a -> Bool
== Peer
p' Bool -> Bool -> Bool
&& TxHash
h' TxHash -> TxHash -> Bool
forall a. Eq a => a -> a -> Bool
== Tx -> TxHash
txHash Tx
tx -> Either PubExcept () -> m (Either PubExcept ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Either PubExcept () -> m (Either PubExcept ()))
-> (PubExcept -> Either PubExcept ())
-> PubExcept
-> m (Either PubExcept ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PubExcept -> Either PubExcept ()
forall a b. a -> Either a b
Left (PubExcept -> m (Either PubExcept ()))
-> PubExcept -> m (Either PubExcept ())
forall a b. (a -> b) -> a -> b
$ RejectCode -> PubExcept
PubReject RejectCode
c
            StorePeerDisconnected p' :: Peer
p'
                | Peer
p Peer -> Peer -> Bool
forall a. Eq a => a -> a -> Bool
== Peer
p' -> Either PubExcept () -> m (Either PubExcept ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Either PubExcept () -> m (Either PubExcept ()))
-> Either PubExcept () -> m (Either PubExcept ())
forall a b. (a -> b) -> a -> b
$ PubExcept -> Either PubExcept ()
forall a b. a -> Either a b
Left PubExcept
PubPeerDisconnected
            StoreMempoolNew h' :: TxHash
h'
                | TxHash
h' TxHash -> TxHash -> Bool
forall a. Eq a => a -> a -> Bool
== Tx -> TxHash
txHash Tx
tx -> Either PubExcept () -> m (Either PubExcept ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Either PubExcept () -> m (Either PubExcept ()))
-> Either PubExcept () -> m (Either PubExcept ())
forall a b. (a -> b) -> a -> b
$ () -> Either PubExcept ()
forall a b. b -> Either a b
Right ()
            _ -> Peer -> mbox StoreEvent -> m (Either PubExcept ())
g Peer
p mbox StoreEvent
s

-- GET Mempool / Events --

scottyMempool ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetMempool -> WebT m [TxHash]
scottyMempool :: GetMempool -> WebT m [TxHash]
scottyMempool (GetMempool limitM :: Maybe LimitParam
limitM (OffsetParam o :: Natural
o)) =
    (WebMetrics -> StatDist)
-> Int -> WebT m [TxHash] -> WebT m [TxHash]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
mempoolResponseTime 1 (WebT m [TxHash] -> WebT m [TxHash])
-> WebT m [TxHash] -> WebT m [TxHash]
forall a b. (a -> b) -> a -> b
$ do
    WebLimits
wl <- ReaderT WebState m WebLimits
-> ActionT Except (ReaderT WebState m) WebLimits
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m WebLimits
 -> ActionT Except (ReaderT WebState m) WebLimits)
-> ReaderT WebState m WebLimits
-> ActionT Except (ReaderT WebState m) WebLimits
forall a b. (a -> b) -> a -> b
$ (WebState -> WebLimits) -> ReaderT WebState m WebLimits
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (WebConfig -> WebLimits
webMaxLimits (WebConfig -> WebLimits)
-> (WebState -> WebConfig) -> WebState -> WebLimits
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig)
    let wl' :: WebLimits
wl' = WebLimits
wl { maxLimitCount :: Word32
maxLimitCount = 0 }
        l :: Limits
l = Word32 -> Word32 -> Maybe Start -> Limits
Limits (WebLimits -> Bool -> Maybe LimitParam -> Word32
validateLimit WebLimits
wl' Bool
False Maybe LimitParam
limitM) (Natural -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Natural
o) Maybe Start
forall a. Maybe a
Nothing
    ((Word64, TxHash) -> TxHash) -> [(Word64, TxHash)] -> [TxHash]
forall a b. (a -> b) -> [a] -> [b]
map (Word64, TxHash) -> TxHash
forall a b. (a, b) -> b
snd ([(Word64, TxHash)] -> [TxHash])
-> ([(Word64, TxHash)] -> [(Word64, TxHash)])
-> [(Word64, TxHash)]
-> [TxHash]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Limits -> [(Word64, TxHash)] -> [(Word64, TxHash)]
forall a. Limits -> [a] -> [a]
applyLimits Limits
l ([(Word64, TxHash)] -> [TxHash])
-> ActionT Except (ReaderT WebState m) [(Word64, TxHash)]
-> WebT m [TxHash]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) [(Word64, TxHash)]
forall (m :: * -> *). StoreReadBase m => m [(Word64, TxHash)]
getMempool

scottyEvents :: (MonadUnliftIO m, MonadLoggerIO m) => WebT m ()
scottyEvents :: WebT m ()
scottyEvents =
    (WebMetrics -> Gauge) -> WebT m () -> WebT m ()
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> Gauge) -> WebT m a -> WebT m a
withGaugeIncrease WebMetrics -> Gauge
eventsConnected (WebT m () -> WebT m ()) -> WebT m () -> WebT m ()
forall a b. (a -> b) -> a -> b
$ do
    WebT m ()
forall (m :: * -> *) e. (Monad m, ScottyError e) => ActionT e m ()
setHeaders
    SerialAs
proto <- Bool -> ActionT Except (ReaderT WebState m) SerialAs
forall (m :: * -> *). Monad m => Bool -> ActionT Except m SerialAs
setupContentType Bool
False
    Publisher StoreEvent
pub <- ReaderT WebState m (Publisher StoreEvent)
-> ActionT Except (ReaderT WebState m) (Publisher StoreEvent)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m (Publisher StoreEvent)
 -> ActionT Except (ReaderT WebState m) (Publisher StoreEvent))
-> ReaderT WebState m (Publisher StoreEvent)
-> ActionT Except (ReaderT WebState m) (Publisher StoreEvent)
forall a b. (a -> b) -> a -> b
$ (WebState -> Publisher StoreEvent)
-> ReaderT WebState m (Publisher StoreEvent)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (Store -> Publisher StoreEvent
storePublisher (Store -> Publisher StoreEvent)
-> (WebState -> Store) -> WebState -> Publisher StoreEvent
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebConfig -> Store
webStore (WebConfig -> Store)
-> (WebState -> WebConfig) -> WebState -> Store
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig)
    StreamingBody -> WebT m ()
forall (m :: * -> *) e. Monad m => StreamingBody -> ActionT e m ()
S.stream (StreamingBody -> WebT m ()) -> StreamingBody -> WebT m ()
forall a b. (a -> b) -> a -> b
$ \io :: Builder -> IO ()
io flush' :: IO ()
flush' ->
        Publisher StoreEvent -> (Inbox StoreEvent -> IO ()) -> IO ()
forall (m :: * -> *) msg a.
MonadUnliftIO m =>
Publisher msg -> (Inbox msg -> m a) -> m a
withSubscription Publisher StoreEvent
pub ((Inbox StoreEvent -> IO ()) -> IO ())
-> (Inbox StoreEvent -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \sub :: Inbox StoreEvent
sub ->
            IO () -> IO ()
forall (f :: * -> *) a b. Applicative f => f a -> f b
forever (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
            IO ()
flush' IO () -> IO (Maybe Event) -> IO (Maybe Event)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Inbox StoreEvent -> IO (Maybe Event)
receiveEvent Inbox StoreEvent
sub IO (Maybe Event) -> (Maybe Event -> IO ()) -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= IO () -> (Event -> IO ()) -> Maybe Event -> IO ()
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (() -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()) (Builder -> IO ()
io (Builder -> IO ()) -> (Event -> Builder) -> Event -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialAs -> Event -> Builder
forall a. (Serial a, ToJSON a) => SerialAs -> a -> Builder
serial SerialAs
proto)
  where
    serial :: SerialAs -> a -> Builder
serial proto :: SerialAs
proto e :: a
e =
        ByteString -> Builder
lazyByteString (ByteString -> Builder) -> ByteString -> Builder
forall a b. (a -> b) -> a -> b
$ SerialAs -> (a -> Encoding) -> (a -> Value) -> a -> ByteString
forall a.
Serial a =>
SerialAs -> (a -> Encoding) -> (a -> Value) -> a -> ByteString
protoSerial SerialAs
proto a -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding a -> Value
forall a. ToJSON a => a -> Value
toJSON a
e ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> SerialAs -> ByteString
forall p. (Monoid p, IsString p) => SerialAs -> p
newLine SerialAs
proto
    newLine :: SerialAs -> p
newLine SerialAsBinary     = p
forall a. Monoid a => a
mempty
    newLine SerialAsJSON       = "\n"
    newLine SerialAsPrettyJSON = p
forall a. Monoid a => a
mempty

receiveEvent :: Inbox StoreEvent -> IO (Maybe Event)
receiveEvent :: Inbox StoreEvent -> IO (Maybe Event)
receiveEvent sub :: Inbox StoreEvent
sub = do
    StoreEvent
se <- Inbox StoreEvent -> IO StoreEvent
forall (mbox :: * -> *) (m :: * -> *) msg.
(InChan mbox, MonadIO m) =>
mbox msg -> m msg
receive Inbox StoreEvent
sub
    Maybe Event -> IO (Maybe Event)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Event -> IO (Maybe Event))
-> Maybe Event -> IO (Maybe Event)
forall a b. (a -> b) -> a -> b
$
        case StoreEvent
se of
            StoreBestBlock b :: BlockHash
b  -> Event -> Maybe Event
forall a. a -> Maybe a
Just (BlockHash -> Event
EventBlock BlockHash
b)
            StoreMempoolNew t :: TxHash
t -> Event -> Maybe Event
forall a. a -> Maybe a
Just (TxHash -> Event
EventTx TxHash
t)
            _                 -> Maybe Event
forall a. Maybe a
Nothing

-- GET Address Transactions --

scottyAddrTxs ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetAddrTxs -> WebT m [TxRef]
scottyAddrTxs :: GetAddrTxs -> WebT m [TxRef]
scottyAddrTxs (GetAddrTxs addr :: Address
addr pLimits :: LimitsParam
pLimits) =
    (WebMetrics -> StatDist) -> Int -> WebT m [TxRef] -> WebT m [TxRef]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
addrTxResponseTime 1 (WebT m [TxRef] -> WebT m [TxRef])
-> WebT m [TxRef] -> WebT m [TxRef]
forall a b. (a -> b) -> a -> b
$
    Address -> Limits -> WebT m [TxRef]
forall (m :: * -> *).
StoreReadExtra m =>
Address -> Limits -> m [TxRef]
getAddressTxs Address
addr (Limits -> WebT m [TxRef])
-> ActionT Except (ReaderT WebState m) Limits -> WebT m [TxRef]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Bool -> LimitsParam -> ActionT Except (ReaderT WebState m) Limits
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
Bool -> LimitsParam -> WebT m Limits
paramToLimits Bool
False LimitsParam
pLimits

scottyAddrsTxs ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetAddrsTxs -> WebT m [TxRef]
scottyAddrsTxs :: GetAddrsTxs -> WebT m [TxRef]
scottyAddrsTxs (GetAddrsTxs addrs :: [Address]
addrs pLimits :: LimitsParam
pLimits) =
    (WebMetrics -> StatDist) -> Int -> WebT m [TxRef] -> WebT m [TxRef]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
addrTxResponseTime ([Address] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Address]
addrs) (WebT m [TxRef] -> WebT m [TxRef])
-> WebT m [TxRef] -> WebT m [TxRef]
forall a b. (a -> b) -> a -> b
$
    [Address] -> Limits -> WebT m [TxRef]
forall (m :: * -> *).
StoreReadExtra m =>
[Address] -> Limits -> m [TxRef]
getAddressesTxs [Address]
addrs (Limits -> WebT m [TxRef])
-> ActionT Except (ReaderT WebState m) Limits -> WebT m [TxRef]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Bool -> LimitsParam -> ActionT Except (ReaderT WebState m) Limits
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
Bool -> LimitsParam -> WebT m Limits
paramToLimits Bool
False LimitsParam
pLimits

scottyAddrTxsFull ::
       (MonadUnliftIO m, MonadLoggerIO m)
    => GetAddrTxsFull
    -> WebT m [Transaction]
scottyAddrTxsFull :: GetAddrTxsFull -> WebT m [Transaction]
scottyAddrTxsFull (GetAddrTxsFull addr :: Address
addr pLimits :: LimitsParam
pLimits) =
    (WebMetrics -> StatDist)
-> Int -> WebT m [Transaction] -> WebT m [Transaction]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
addrTxFullResponseTime 1 (WebT m [Transaction] -> WebT m [Transaction])
-> WebT m [Transaction] -> WebT m [Transaction]
forall a b. (a -> b) -> a -> b
$ do
    [TxRef]
txs <- Address -> Limits -> ActionT Except (ReaderT WebState m) [TxRef]
forall (m :: * -> *).
StoreReadExtra m =>
Address -> Limits -> m [TxRef]
getAddressTxs Address
addr (Limits -> ActionT Except (ReaderT WebState m) [TxRef])
-> ActionT Except (ReaderT WebState m) Limits
-> ActionT Except (ReaderT WebState m) [TxRef]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Bool -> LimitsParam -> ActionT Except (ReaderT WebState m) Limits
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
Bool -> LimitsParam -> WebT m Limits
paramToLimits Bool
True LimitsParam
pLimits
    [Maybe Transaction] -> [Transaction]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe Transaction] -> [Transaction])
-> ActionT Except (ReaderT WebState m) [Maybe Transaction]
-> WebT m [Transaction]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (TxRef -> ActionT Except (ReaderT WebState m) (Maybe Transaction))
-> [TxRef]
-> ActionT Except (ReaderT WebState m) [Maybe Transaction]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (TxHash -> ActionT Except (ReaderT WebState m) (Maybe Transaction)
forall (m :: * -> *).
(Monad m, StoreReadBase m) =>
TxHash -> m (Maybe Transaction)
getTransaction (TxHash -> ActionT Except (ReaderT WebState m) (Maybe Transaction))
-> (TxRef -> TxHash)
-> TxRef
-> ActionT Except (ReaderT WebState m) (Maybe Transaction)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxRef -> TxHash
txRefHash) [TxRef]
txs

scottyAddrsTxsFull :: (MonadUnliftIO m, MonadLoggerIO m)
                   => GetAddrsTxsFull -> WebT m [Transaction]
scottyAddrsTxsFull :: GetAddrsTxsFull -> WebT m [Transaction]
scottyAddrsTxsFull (GetAddrsTxsFull addrs :: [Address]
addrs pLimits :: LimitsParam
pLimits) =
    (WebMetrics -> StatDist)
-> Int -> WebT m [Transaction] -> WebT m [Transaction]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
addrTxFullResponseTime ([Address] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Address]
addrs) (WebT m [Transaction] -> WebT m [Transaction])
-> WebT m [Transaction] -> WebT m [Transaction]
forall a b. (a -> b) -> a -> b
$ do
    [TxRef]
txs <- [Address] -> Limits -> ActionT Except (ReaderT WebState m) [TxRef]
forall (m :: * -> *).
StoreReadExtra m =>
[Address] -> Limits -> m [TxRef]
getAddressesTxs [Address]
addrs (Limits -> ActionT Except (ReaderT WebState m) [TxRef])
-> ActionT Except (ReaderT WebState m) Limits
-> ActionT Except (ReaderT WebState m) [TxRef]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Bool -> LimitsParam -> ActionT Except (ReaderT WebState m) Limits
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
Bool -> LimitsParam -> WebT m Limits
paramToLimits Bool
True LimitsParam
pLimits
    [Maybe Transaction] -> [Transaction]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe Transaction] -> [Transaction])
-> ActionT Except (ReaderT WebState m) [Maybe Transaction]
-> WebT m [Transaction]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (TxRef -> ActionT Except (ReaderT WebState m) (Maybe Transaction))
-> [TxRef]
-> ActionT Except (ReaderT WebState m) [Maybe Transaction]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (TxHash -> ActionT Except (ReaderT WebState m) (Maybe Transaction)
forall (m :: * -> *).
(Monad m, StoreReadBase m) =>
TxHash -> m (Maybe Transaction)
getTransaction (TxHash -> ActionT Except (ReaderT WebState m) (Maybe Transaction))
-> (TxRef -> TxHash)
-> TxRef
-> ActionT Except (ReaderT WebState m) (Maybe Transaction)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxRef -> TxHash
txRefHash) [TxRef]
txs

scottyAddrBalance :: (MonadUnliftIO m, MonadLoggerIO m)
                  => GetAddrBalance -> WebT m Balance
scottyAddrBalance :: GetAddrBalance -> WebT m Balance
scottyAddrBalance (GetAddrBalance addr :: Address
addr) =
    (WebMetrics -> StatDist) -> Int -> WebT m Balance -> WebT m Balance
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
addrBalanceResponseTime 1 (WebT m Balance -> WebT m Balance)
-> WebT m Balance -> WebT m Balance
forall a b. (a -> b) -> a -> b
$
    Address -> WebT m Balance
forall (m :: * -> *). StoreReadBase m => Address -> m Balance
getDefaultBalance Address
addr

scottyAddrsBalance ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetAddrsBalance -> WebT m [Balance]
scottyAddrsBalance :: GetAddrsBalance -> WebT m [Balance]
scottyAddrsBalance (GetAddrsBalance addrs :: [Address]
addrs) =
    (WebMetrics -> StatDist)
-> Int -> WebT m [Balance] -> WebT m [Balance]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
addrBalanceResponseTime ([Address] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Address]
addrs) (WebT m [Balance] -> WebT m [Balance])
-> WebT m [Balance] -> WebT m [Balance]
forall a b. (a -> b) -> a -> b
$
    [Address] -> WebT m [Balance]
forall (m :: * -> *). StoreReadExtra m => [Address] -> m [Balance]
getBalances [Address]
addrs

scottyAddrUnspent ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetAddrUnspent -> WebT m [Unspent]
scottyAddrUnspent :: GetAddrUnspent -> WebT m [Unspent]
scottyAddrUnspent (GetAddrUnspent addr :: Address
addr pLimits :: LimitsParam
pLimits) =
    (WebMetrics -> StatDist)
-> Int -> WebT m [Unspent] -> WebT m [Unspent]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
addrUnspentResponseTime 1 (WebT m [Unspent] -> WebT m [Unspent])
-> WebT m [Unspent] -> WebT m [Unspent]
forall a b. (a -> b) -> a -> b
$
    Address -> Limits -> WebT m [Unspent]
forall (m :: * -> *).
StoreReadExtra m =>
Address -> Limits -> m [Unspent]
getAddressUnspents Address
addr (Limits -> WebT m [Unspent])
-> ActionT Except (ReaderT WebState m) Limits -> WebT m [Unspent]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Bool -> LimitsParam -> ActionT Except (ReaderT WebState m) Limits
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
Bool -> LimitsParam -> WebT m Limits
paramToLimits Bool
False LimitsParam
pLimits

scottyAddrsUnspent ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetAddrsUnspent -> WebT m [Unspent]
scottyAddrsUnspent :: GetAddrsUnspent -> WebT m [Unspent]
scottyAddrsUnspent (GetAddrsUnspent addrs :: [Address]
addrs pLimits :: LimitsParam
pLimits) =
    (WebMetrics -> StatDist)
-> Int -> WebT m [Unspent] -> WebT m [Unspent]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
addrUnspentResponseTime ([Address] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Address]
addrs) (WebT m [Unspent] -> WebT m [Unspent])
-> WebT m [Unspent] -> WebT m [Unspent]
forall a b. (a -> b) -> a -> b
$
    [Address] -> Limits -> WebT m [Unspent]
forall (m :: * -> *).
StoreReadExtra m =>
[Address] -> Limits -> m [Unspent]
getAddressesUnspents [Address]
addrs (Limits -> WebT m [Unspent])
-> ActionT Except (ReaderT WebState m) Limits -> WebT m [Unspent]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Bool -> LimitsParam -> ActionT Except (ReaderT WebState m) Limits
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
Bool -> LimitsParam -> WebT m Limits
paramToLimits Bool
False LimitsParam
pLimits

-- GET XPubs --

scottyXPub ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetXPub -> WebT m XPubSummary
scottyXPub :: GetXPub -> WebT m XPubSummary
scottyXPub (GetXPub xpub :: XPubKey
xpub deriv :: DeriveType
deriv (NoCache noCache :: Bool
noCache)) =
    (WebMetrics -> StatDist)
-> Int -> WebT m XPubSummary -> WebT m XPubSummary
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
xPubResponseTime 1 (WebT m XPubSummary -> WebT m XPubSummary)
-> WebT m XPubSummary -> WebT m XPubSummary
forall a b. (a -> b) -> a -> b
$
    ReaderT WebState m XPubSummary -> WebT m XPubSummary
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m XPubSummary -> WebT m XPubSummary)
-> (ReaderT WebState m XPubSummary
    -> ReaderT WebState m XPubSummary)
-> ReaderT WebState m XPubSummary
-> WebT m XPubSummary
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool
-> ReaderT WebState m XPubSummary -> ReaderT WebState m XPubSummary
forall (m :: * -> *) a.
MonadIO m =>
Bool -> ReaderT WebState m a -> ReaderT WebState m a
runNoCache Bool
noCache (ReaderT WebState m XPubSummary -> WebT m XPubSummary)
-> ReaderT WebState m XPubSummary -> WebT m XPubSummary
forall a b. (a -> b) -> a -> b
$ XPubSpec -> ReaderT WebState m XPubSummary
forall (m :: * -> *). StoreReadExtra m => XPubSpec -> m XPubSummary
xPubSummary (XPubSpec -> ReaderT WebState m XPubSummary)
-> XPubSpec -> ReaderT WebState m XPubSummary
forall a b. (a -> b) -> a -> b
$ XPubKey -> DeriveType -> XPubSpec
XPubSpec XPubKey
xpub DeriveType
deriv

getXPubTxs :: (MonadUnliftIO m, MonadLoggerIO m)
           => XPubKey -> DeriveType -> LimitsParam -> Bool -> WebT m [TxRef]
getXPubTxs :: XPubKey -> DeriveType -> LimitsParam -> Bool -> WebT m [TxRef]
getXPubTxs xpub :: XPubKey
xpub deriv :: DeriveType
deriv plimits :: LimitsParam
plimits nocache :: Bool
nocache = do
    Limits
limits <- Bool -> LimitsParam -> WebT m Limits
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
Bool -> LimitsParam -> WebT m Limits
paramToLimits Bool
False LimitsParam
plimits
    ReaderT WebState m [TxRef] -> WebT m [TxRef]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m [TxRef] -> WebT m [TxRef])
-> (ReaderT WebState m [TxRef] -> ReaderT WebState m [TxRef])
-> ReaderT WebState m [TxRef]
-> WebT m [TxRef]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> ReaderT WebState m [TxRef] -> ReaderT WebState m [TxRef]
forall (m :: * -> *) a.
MonadIO m =>
Bool -> ReaderT WebState m a -> ReaderT WebState m a
runNoCache Bool
nocache (ReaderT WebState m [TxRef] -> WebT m [TxRef])
-> ReaderT WebState m [TxRef] -> WebT m [TxRef]
forall a b. (a -> b) -> a -> b
$ XPubSpec -> Limits -> ReaderT WebState m [TxRef]
forall (m :: * -> *).
StoreReadExtra m =>
XPubSpec -> Limits -> m [TxRef]
xPubTxs (XPubKey -> DeriveType -> XPubSpec
XPubSpec XPubKey
xpub DeriveType
deriv) Limits
limits

scottyXPubTxs ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetXPubTxs -> WebT m [TxRef]
scottyXPubTxs :: GetXPubTxs -> WebT m [TxRef]
scottyXPubTxs (GetXPubTxs xpub :: XPubKey
xpub deriv :: DeriveType
deriv plimits :: LimitsParam
plimits (NoCache nocache :: Bool
nocache)) =
    (WebMetrics -> StatDist) -> Int -> WebT m [TxRef] -> WebT m [TxRef]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
xPubTxResponseTime 1 (WebT m [TxRef] -> WebT m [TxRef])
-> WebT m [TxRef] -> WebT m [TxRef]
forall a b. (a -> b) -> a -> b
$
    XPubKey -> DeriveType -> LimitsParam -> Bool -> WebT m [TxRef]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
XPubKey -> DeriveType -> LimitsParam -> Bool -> WebT m [TxRef]
getXPubTxs XPubKey
xpub DeriveType
deriv LimitsParam
plimits Bool
nocache

scottyXPubTxsFull ::
       (MonadUnliftIO m, MonadLoggerIO m)
    => GetXPubTxsFull
    -> WebT m [Transaction]
scottyXPubTxsFull :: GetXPubTxsFull -> WebT m [Transaction]
scottyXPubTxsFull (GetXPubTxsFull xpub :: XPubKey
xpub deriv :: DeriveType
deriv plimits :: LimitsParam
plimits (NoCache nocache :: Bool
nocache)) =
    (WebMetrics -> StatDist)
-> Int -> WebT m [Transaction] -> WebT m [Transaction]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
xPubTxFullResponseTime 1 (WebT m [Transaction] -> WebT m [Transaction])
-> WebT m [Transaction] -> WebT m [Transaction]
forall a b. (a -> b) -> a -> b
$ do
    [TxRef]
refs <- XPubKey -> DeriveType -> LimitsParam -> Bool -> WebT m [TxRef]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
XPubKey -> DeriveType -> LimitsParam -> Bool -> WebT m [TxRef]
getXPubTxs XPubKey
xpub DeriveType
deriv LimitsParam
plimits Bool
nocache
    [Maybe Transaction]
txs <- ReaderT WebState m [Maybe Transaction]
-> ActionT Except (ReaderT WebState m) [Maybe Transaction]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m [Maybe Transaction]
 -> ActionT Except (ReaderT WebState m) [Maybe Transaction])
-> (ReaderT WebState m [Maybe Transaction]
    -> ReaderT WebState m [Maybe Transaction])
-> ReaderT WebState m [Maybe Transaction]
-> ActionT Except (ReaderT WebState m) [Maybe Transaction]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool
-> ReaderT WebState m [Maybe Transaction]
-> ReaderT WebState m [Maybe Transaction]
forall (m :: * -> *) a.
MonadIO m =>
Bool -> ReaderT WebState m a -> ReaderT WebState m a
runNoCache Bool
nocache (ReaderT WebState m [Maybe Transaction]
 -> ActionT Except (ReaderT WebState m) [Maybe Transaction])
-> ReaderT WebState m [Maybe Transaction]
-> ActionT Except (ReaderT WebState m) [Maybe Transaction]
forall a b. (a -> b) -> a -> b
$ (TxRef -> ReaderT WebState m (Maybe Transaction))
-> [TxRef] -> ReaderT WebState m [Maybe Transaction]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (TxHash -> ReaderT WebState m (Maybe Transaction)
forall (m :: * -> *).
(Monad m, StoreReadBase m) =>
TxHash -> m (Maybe Transaction)
getTransaction (TxHash -> ReaderT WebState m (Maybe Transaction))
-> (TxRef -> TxHash)
-> TxRef
-> ReaderT WebState m (Maybe Transaction)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxRef -> TxHash
txRefHash) [TxRef]
refs
    [Transaction] -> WebT m [Transaction]
forall (m :: * -> *) a. Monad m => a -> m a
return ([Transaction] -> WebT m [Transaction])
-> [Transaction] -> WebT m [Transaction]
forall a b. (a -> b) -> a -> b
$ [Maybe Transaction] -> [Transaction]
forall a. [Maybe a] -> [a]
catMaybes [Maybe Transaction]
txs

scottyXPubBalances ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetXPubBalances -> WebT m [XPubBal]
scottyXPubBalances :: GetXPubBalances -> WebT m [XPubBal]
scottyXPubBalances (GetXPubBalances xpub :: XPubKey
xpub deriv :: DeriveType
deriv (NoCache noCache :: Bool
noCache)) =
    (WebMetrics -> StatDist)
-> Int -> WebT m [XPubBal] -> WebT m [XPubBal]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
xPubResponseTime 1 (WebT m [XPubBal] -> WebT m [XPubBal])
-> WebT m [XPubBal] -> WebT m [XPubBal]
forall a b. (a -> b) -> a -> b
$
    (XPubBal -> Bool) -> [XPubBal] -> [XPubBal]
forall a. (a -> Bool) -> [a] -> [a]
filter XPubBal -> Bool
f ([XPubBal] -> [XPubBal]) -> WebT m [XPubBal] -> WebT m [XPubBal]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReaderT WebState m [XPubBal] -> WebT m [XPubBal]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (Bool
-> ReaderT WebState m [XPubBal] -> ReaderT WebState m [XPubBal]
forall (m :: * -> *) a.
MonadIO m =>
Bool -> ReaderT WebState m a -> ReaderT WebState m a
runNoCache Bool
noCache (XPubSpec -> ReaderT WebState m [XPubBal]
forall (m :: * -> *). StoreReadExtra m => XPubSpec -> m [XPubBal]
xPubBals XPubSpec
spec))
  where
    spec :: XPubSpec
spec = XPubKey -> DeriveType -> XPubSpec
XPubSpec XPubKey
xpub DeriveType
deriv
    f :: XPubBal -> Bool
f = Bool -> Bool
not (Bool -> Bool) -> (XPubBal -> Bool) -> XPubBal -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Balance -> Bool
nullBalance (Balance -> Bool) -> (XPubBal -> Balance) -> XPubBal -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XPubBal -> Balance
xPubBal

scottyXPubUnspent ::
       (MonadUnliftIO m, MonadLoggerIO m)
    => GetXPubUnspent
    -> WebT m [XPubUnspent]
scottyXPubUnspent :: GetXPubUnspent -> WebT m [XPubUnspent]
scottyXPubUnspent (GetXPubUnspent xpub :: XPubKey
xpub deriv :: DeriveType
deriv pLimits :: LimitsParam
pLimits (NoCache noCache :: Bool
noCache)) =
    (WebMetrics -> StatDist)
-> Int -> WebT m [XPubUnspent] -> WebT m [XPubUnspent]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
xPubUnspentResponseTime 1 (WebT m [XPubUnspent] -> WebT m [XPubUnspent])
-> WebT m [XPubUnspent] -> WebT m [XPubUnspent]
forall a b. (a -> b) -> a -> b
$ do
    Limits
limits <- Bool -> LimitsParam -> WebT m Limits
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
Bool -> LimitsParam -> WebT m Limits
paramToLimits Bool
False LimitsParam
pLimits
    ReaderT WebState m [XPubUnspent] -> WebT m [XPubUnspent]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m [XPubUnspent] -> WebT m [XPubUnspent])
-> (ReaderT WebState m [XPubUnspent]
    -> ReaderT WebState m [XPubUnspent])
-> ReaderT WebState m [XPubUnspent]
-> WebT m [XPubUnspent]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool
-> ReaderT WebState m [XPubUnspent]
-> ReaderT WebState m [XPubUnspent]
forall (m :: * -> *) a.
MonadIO m =>
Bool -> ReaderT WebState m a -> ReaderT WebState m a
runNoCache Bool
noCache (ReaderT WebState m [XPubUnspent] -> WebT m [XPubUnspent])
-> ReaderT WebState m [XPubUnspent] -> WebT m [XPubUnspent]
forall a b. (a -> b) -> a -> b
$ XPubSpec -> Limits -> ReaderT WebState m [XPubUnspent]
forall (m :: * -> *).
StoreReadExtra m =>
XPubSpec -> Limits -> m [XPubUnspent]
xPubUnspents (XPubKey -> DeriveType -> XPubSpec
XPubSpec XPubKey
xpub DeriveType
deriv) Limits
limits

---------------------------------------
-- Blockchain.info API Compatibility --
---------------------------------------

netBinfoSymbol :: Network -> BinfoSymbol
netBinfoSymbol :: Network -> BinfoSymbol
netBinfoSymbol net :: Network
net
  | Network
net Network -> Network -> Bool
forall a. Eq a => a -> a -> Bool
== Network
btc =
        $WBinfoSymbol :: Text -> Text -> Text -> Double -> Bool -> Bool -> BinfoSymbol
BinfoSymbol{ getBinfoSymbolCode :: Text
getBinfoSymbolCode = "BTC"
                   , getBinfoSymbolString :: Text
getBinfoSymbolString = "BTC"
                   , getBinfoSymbolName :: Text
getBinfoSymbolName = "Bitcoin"
                   , getBinfoSymbolConversion :: Double
getBinfoSymbolConversion = 100 Double -> Double -> Double
forall a. Num a => a -> a -> a
* 1000 Double -> Double -> Double
forall a. Num a => a -> a -> a
* 1000
                   , getBinfoSymbolAfter :: Bool
getBinfoSymbolAfter = Bool
True
                   , getBinfoSymbolLocal :: Bool
getBinfoSymbolLocal = Bool
False
                   }
  | Network
net Network -> Network -> Bool
forall a. Eq a => a -> a -> Bool
== Network
bch =
        $WBinfoSymbol :: Text -> Text -> Text -> Double -> Bool -> Bool -> BinfoSymbol
BinfoSymbol{ getBinfoSymbolCode :: Text
getBinfoSymbolCode = "BCH"
                   , getBinfoSymbolString :: Text
getBinfoSymbolString = "BCH"
                   , getBinfoSymbolName :: Text
getBinfoSymbolName = "Bitcoin Cash"
                   , getBinfoSymbolConversion :: Double
getBinfoSymbolConversion = 100 Double -> Double -> Double
forall a. Num a => a -> a -> a
* 1000 Double -> Double -> Double
forall a. Num a => a -> a -> a
* 1000
                   , getBinfoSymbolAfter :: Bool
getBinfoSymbolAfter = Bool
True
                   , getBinfoSymbolLocal :: Bool
getBinfoSymbolLocal = Bool
False
                   }
  | Bool
otherwise =
        $WBinfoSymbol :: Text -> Text -> Text -> Double -> Bool -> Bool -> BinfoSymbol
BinfoSymbol{ getBinfoSymbolCode :: Text
getBinfoSymbolCode = "XTS"
                   , getBinfoSymbolString :: Text
getBinfoSymbolString = "¤"
                   , getBinfoSymbolName :: Text
getBinfoSymbolName = "Test"
                   , getBinfoSymbolConversion :: Double
getBinfoSymbolConversion = 100 Double -> Double -> Double
forall a. Num a => a -> a -> a
* 1000 Double -> Double -> Double
forall a. Num a => a -> a -> a
* 1000
                   , getBinfoSymbolAfter :: Bool
getBinfoSymbolAfter = Bool
False
                   , getBinfoSymbolLocal :: Bool
getBinfoSymbolLocal = Bool
False
                   }

binfoTickerToSymbol :: Text -> BinfoTicker -> BinfoSymbol
binfoTickerToSymbol :: Text -> BinfoTicker -> BinfoSymbol
binfoTickerToSymbol code :: Text
code BinfoTicker{..} =
    $WBinfoSymbol :: Text -> Text -> Text -> Double -> Bool -> Bool -> BinfoSymbol
BinfoSymbol{ getBinfoSymbolCode :: Text
getBinfoSymbolCode = Text
code
               , getBinfoSymbolString :: Text
getBinfoSymbolString = Text
binfoTickerSymbol
               , getBinfoSymbolName :: Text
getBinfoSymbolName = Text
name
               , getBinfoSymbolConversion :: Double
getBinfoSymbolConversion =
                       100 Double -> Double -> Double
forall a. Num a => a -> a -> a
* 1000 Double -> Double -> Double
forall a. Num a => a -> a -> a
* 1000 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
binfoTicker15m -- sat/usd
               , getBinfoSymbolAfter :: Bool
getBinfoSymbolAfter = Bool
False
               , getBinfoSymbolLocal :: Bool
getBinfoSymbolLocal = Bool
True
               }
  where
    name :: Text
name = case Text
code of
        "EUR" -> "Euro"
        "USD" -> "U.S. dollar"
        "GBP" -> "British pound"
        x :: Text
x     -> Text
x

getBinfoAddrsParam :: MonadIO m
                   => (WebMetrics -> ErrorCounter)
                   -> Text
                   -> WebT m (HashSet BinfoAddr)
getBinfoAddrsParam :: (WebMetrics -> ErrorCounter) -> Text -> WebT m (HashSet BinfoAddr)
getBinfoAddrsParam metric :: WebMetrics -> ErrorCounter
metric name :: Text
name = do
    Network
net <- ReaderT WebState m Network
-> ActionT Except (ReaderT WebState m) Network
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ((WebState -> Network) -> ReaderT WebState m Network
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (Store -> Network
storeNetwork (Store -> Network) -> (WebState -> Store) -> WebState -> Network
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebConfig -> Store
webStore (WebConfig -> Store)
-> (WebState -> WebConfig) -> WebState -> Store
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig))
    Text
p <- Text -> ActionT Except (ReaderT WebState m) Text
forall a e (m :: * -> *).
(Parsable a, ScottyError e, Monad m) =>
Text -> ActionT e m a
S.param (Text -> Text
forall a b. ConvertibleStrings a b => a -> b
cs Text
name) ActionT Except (ReaderT WebState m) Text
-> (Except -> ActionT Except (ReaderT WebState m) Text)
-> ActionT Except (ReaderT WebState m) Text
forall e (m :: * -> *) a.
(ScottyError e, Monad m) =>
ActionT e m a -> (e -> ActionT e m a) -> ActionT e m a
`S.rescue` ActionT Except (ReaderT WebState m) Text
-> Except -> ActionT Except (ReaderT WebState m) Text
forall a b. a -> b -> a
const (Text -> ActionT Except (ReaderT WebState m) Text
forall (m :: * -> *) a. Monad m => a -> m a
return "")
    case Network -> Text -> Maybe [BinfoAddr]
parseBinfoAddr Network
net Text
p of
        Nothing -> (WebMetrics -> ErrorCounter)
-> Except -> WebT m (HashSet BinfoAddr)
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
metric (String -> Except
UserError "invalid active address")
        Just xs :: [BinfoAddr]
xs -> HashSet BinfoAddr -> WebT m (HashSet BinfoAddr)
forall (m :: * -> *) a. Monad m => a -> m a
return (HashSet BinfoAddr -> WebT m (HashSet BinfoAddr))
-> HashSet BinfoAddr -> WebT m (HashSet BinfoAddr)
forall a b. (a -> b) -> a -> b
$ [BinfoAddr] -> HashSet BinfoAddr
forall a. (Eq a, Hashable a) => [a] -> HashSet a
HashSet.fromList [BinfoAddr]
xs

getBinfoActive :: MonadIO m
               => (WebMetrics -> ErrorCounter)
               -> WebT m (HashMap XPubKey XPubSpec, HashSet Address)
getBinfoActive :: (WebMetrics -> ErrorCounter)
-> WebT m (HashMap XPubKey XPubSpec, HashSet Address)
getBinfoActive metric :: WebMetrics -> ErrorCounter
metric = do
    HashSet BinfoAddr
active <- (WebMetrics -> ErrorCounter) -> Text -> WebT m (HashSet BinfoAddr)
forall (m :: * -> *).
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Text -> WebT m (HashSet BinfoAddr)
getBinfoAddrsParam WebMetrics -> ErrorCounter
metric "active"
    HashSet BinfoAddr
p2sh <- (WebMetrics -> ErrorCounter) -> Text -> WebT m (HashSet BinfoAddr)
forall (m :: * -> *).
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Text -> WebT m (HashSet BinfoAddr)
getBinfoAddrsParam WebMetrics -> ErrorCounter
metric "activeP2SH"
    HashSet BinfoAddr
bech32 <- (WebMetrics -> ErrorCounter) -> Text -> WebT m (HashSet BinfoAddr)
forall (m :: * -> *).
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Text -> WebT m (HashSet BinfoAddr)
getBinfoAddrsParam WebMetrics -> ErrorCounter
metric "activeBech32"
    let xspec :: DeriveType -> BinfoAddr -> Maybe (XPubKey, XPubSpec)
xspec d :: DeriveType
d b :: BinfoAddr
b = (\x :: XPubKey
x -> (XPubKey
x, XPubKey -> DeriveType -> XPubSpec
XPubSpec XPubKey
x DeriveType
d)) (XPubKey -> (XPubKey, XPubSpec))
-> Maybe XPubKey -> Maybe (XPubKey, XPubSpec)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> BinfoAddr -> Maybe XPubKey
xpub BinfoAddr
b
        xspecs :: HashMap XPubKey XPubSpec
xspecs = [(XPubKey, XPubSpec)] -> HashMap XPubKey XPubSpec
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList ([(XPubKey, XPubSpec)] -> HashMap XPubKey XPubSpec)
-> [(XPubKey, XPubSpec)] -> HashMap XPubKey XPubSpec
forall a b. (a -> b) -> a -> b
$ [[(XPubKey, XPubSpec)]] -> [(XPubKey, XPubSpec)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
                 [ (BinfoAddr -> Maybe (XPubKey, XPubSpec))
-> [BinfoAddr] -> [(XPubKey, XPubSpec)]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (DeriveType -> BinfoAddr -> Maybe (XPubKey, XPubSpec)
xspec DeriveType
DeriveNormal) (HashSet BinfoAddr -> [BinfoAddr]
forall a. HashSet a -> [a]
HashSet.toList HashSet BinfoAddr
active)
                 , (BinfoAddr -> Maybe (XPubKey, XPubSpec))
-> [BinfoAddr] -> [(XPubKey, XPubSpec)]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (DeriveType -> BinfoAddr -> Maybe (XPubKey, XPubSpec)
xspec DeriveType
DeriveP2SH) (HashSet BinfoAddr -> [BinfoAddr]
forall a. HashSet a -> [a]
HashSet.toList HashSet BinfoAddr
p2sh)
                 , (BinfoAddr -> Maybe (XPubKey, XPubSpec))
-> [BinfoAddr] -> [(XPubKey, XPubSpec)]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (DeriveType -> BinfoAddr -> Maybe (XPubKey, XPubSpec)
xspec DeriveType
DeriveP2WPKH) (HashSet BinfoAddr -> [BinfoAddr]
forall a. HashSet a -> [a]
HashSet.toList HashSet BinfoAddr
bech32)
                 ]
        addrs :: HashSet Address
addrs = [Address] -> HashSet Address
forall a. (Eq a, Hashable a) => [a] -> HashSet a
HashSet.fromList ([Address] -> HashSet Address)
-> ([BinfoAddr] -> [Address]) -> [BinfoAddr] -> HashSet Address
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (BinfoAddr -> Maybe Address) -> [BinfoAddr] -> [Address]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe BinfoAddr -> Maybe Address
addr ([BinfoAddr] -> HashSet Address) -> [BinfoAddr] -> HashSet Address
forall a b. (a -> b) -> a -> b
$ HashSet BinfoAddr -> [BinfoAddr]
forall a. HashSet a -> [a]
HashSet.toList HashSet BinfoAddr
active
    (HashMap XPubKey XPubSpec, HashSet Address)
-> WebT m (HashMap XPubKey XPubSpec, HashSet Address)
forall (m :: * -> *) a. Monad m => a -> m a
return (HashMap XPubKey XPubSpec
xspecs, HashSet Address
addrs)
  where
    addr :: BinfoAddr -> Maybe Address
addr (BinfoAddr a :: Address
a) = Address -> Maybe Address
forall a. a -> Maybe a
Just Address
a
    addr (BinfoXpub x :: XPubKey
x) = Maybe Address
forall a. Maybe a
Nothing
    xpub :: BinfoAddr -> Maybe XPubKey
xpub (BinfoXpub x :: XPubKey
x) = XPubKey -> Maybe XPubKey
forall a. a -> Maybe a
Just XPubKey
x
    xpub (BinfoAddr _) = Maybe XPubKey
forall a. Maybe a
Nothing

getNumTxId :: MonadIO m => WebT m Bool
getNumTxId :: WebT m Bool
getNumTxId = (Bool -> Bool) -> WebT m Bool -> WebT m Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Bool -> Bool
not (WebT m Bool -> WebT m Bool) -> WebT m Bool -> WebT m Bool
forall a b. (a -> b) -> a -> b
$ Text -> WebT m Bool
forall a e (m :: * -> *).
(Parsable a, ScottyError e, Monad m) =>
Text -> ActionT e m a
S.param "txidindex" WebT m Bool -> (Except -> WebT m Bool) -> WebT m Bool
forall e (m :: * -> *) a.
(ScottyError e, Monad m) =>
ActionT e m a -> (e -> ActionT e m a) -> ActionT e m a
`S.rescue` WebT m Bool -> Except -> WebT m Bool
forall a b. a -> b -> a
const (Bool -> WebT m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False)

scottyBinfoUnspent :: (MonadUnliftIO m, MonadLoggerIO m) => WebT m ()
scottyBinfoUnspent :: WebT m ()
scottyBinfoUnspent =
    (WebMetrics -> ErrorCounter)
-> WebT m (HashMap XPubKey XPubSpec, HashSet Address)
forall (m :: * -> *).
MonadIO m =>
(WebMetrics -> ErrorCounter)
-> WebT m (HashMap XPubKey XPubSpec, HashSet Address)
getBinfoActive WebMetrics -> ErrorCounter
unspentErrors WebT m (HashMap XPubKey XPubSpec, HashSet Address)
-> ((HashMap XPubKey XPubSpec, HashSet Address) -> WebT m ())
-> WebT m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \(xspecs :: HashMap XPubKey XPubSpec
xspecs, addrs :: HashSet Address
addrs) ->
    WebT m Bool
forall (m :: * -> *). MonadIO m => WebT m Bool
getNumTxId WebT m Bool -> (Bool -> WebT m ()) -> WebT m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \numtxid :: Bool
numtxid ->
    ActionT Except (ReaderT WebState m) Int
get_limit ActionT Except (ReaderT WebState m) Int
-> (Int -> WebT m ()) -> WebT m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \limit :: Int
limit ->
    ActionT Except (ReaderT WebState m) Int32
get_min_conf ActionT Except (ReaderT WebState m) Int32
-> (Int32 -> WebT m ()) -> WebT m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \min_conf :: Int32
min_conf ->
    let len :: Int
len = HashSet Address -> Int
forall a. HashSet a -> Int
HashSet.size HashSet Address
addrs Int -> Int -> Int
forall a. Num a => a -> a -> a
+ HashMap XPubKey XPubSpec -> Int
forall k v. HashMap k v -> Int
HashMap.size HashMap XPubKey XPubSpec
xspecs
    in (WebMetrics -> StatDist) -> Int -> WebT m () -> WebT m ()
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
unspentResponseTime Int
len (WebT m () -> WebT m ()) -> WebT m () -> WebT m ()
forall a b. (a -> b) -> a -> b
$ do
    HashMap XPubKey [XPubUnspent]
xuns <- HashMap XPubKey XPubSpec
-> ActionT
     Except (ReaderT WebState m) (HashMap XPubKey [XPubUnspent])
get_xpub_unspents HashMap XPubKey XPubSpec
xspecs
    [Unspent]
auns <- HashSet Address -> ActionT Except (ReaderT WebState m) [Unspent]
get_addr_unspents HashSet Address
addrs
    Network
net <- ReaderT WebState m Network
-> ActionT Except (ReaderT WebState m) Network
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m Network
 -> ActionT Except (ReaderT WebState m) Network)
-> ReaderT WebState m Network
-> ActionT Except (ReaderT WebState m) Network
forall a b. (a -> b) -> a -> b
$ (WebState -> Network) -> ReaderT WebState m Network
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (Store -> Network
storeNetwork (Store -> Network) -> (WebState -> Store) -> WebState -> Network
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebConfig -> Store
webStore (WebConfig -> Store)
-> (WebState -> WebConfig) -> WebState -> Store
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig)
    Word32
height <- ActionT Except (ReaderT WebState m) Word32
get_height
    let g :: XPubKey -> [XPubUnspent] -> [BinfoUnspent]
g k :: XPubKey
k = (XPubUnspent -> BinfoUnspent) -> [XPubUnspent] -> [BinfoUnspent]
forall a b. (a -> b) -> [a] -> [b]
map (Bool -> Word32 -> XPubKey -> XPubUnspent -> BinfoUnspent
xpub_unspent Bool
numtxid Word32
height XPubKey
k)
        xbus :: [BinfoUnspent]
xbus = ((XPubKey, [XPubUnspent]) -> [BinfoUnspent])
-> [(XPubKey, [XPubUnspent])] -> [BinfoUnspent]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap ((XPubKey -> [XPubUnspent] -> [BinfoUnspent])
-> (XPubKey, [XPubUnspent]) -> [BinfoUnspent]
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry XPubKey -> [XPubUnspent] -> [BinfoUnspent]
g) (HashMap XPubKey [XPubUnspent] -> [(XPubKey, [XPubUnspent])]
forall k v. HashMap k v -> [(k, v)]
HashMap.toList HashMap XPubKey [XPubUnspent]
xuns)
        abus :: [BinfoUnspent]
abus = (Unspent -> BinfoUnspent) -> [Unspent] -> [BinfoUnspent]
forall a b. (a -> b) -> [a] -> [b]
map (Bool -> Word32 -> Unspent -> BinfoUnspent
unspent Bool
numtxid Word32
height) [Unspent]
auns
        f :: BinfoUnspent -> ((TxHash, Word32), BinfoUnspent)
f u :: BinfoUnspent
u@BinfoUnspent{..} =
            ((TxHash
getBinfoUnspentHash, Word32
getBinfoUnspentOutputIndex), BinfoUnspent
u)
        busm :: HashMap (TxHash, Word32) BinfoUnspent
busm = [((TxHash, Word32), BinfoUnspent)]
-> HashMap (TxHash, Word32) BinfoUnspent
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList ((BinfoUnspent -> ((TxHash, Word32), BinfoUnspent))
-> [BinfoUnspent] -> [((TxHash, Word32), BinfoUnspent)]
forall a b. (a -> b) -> [a] -> [b]
map BinfoUnspent -> ((TxHash, Word32), BinfoUnspent)
f ([BinfoUnspent]
xbus [BinfoUnspent] -> [BinfoUnspent] -> [BinfoUnspent]
forall a. [a] -> [a] -> [a]
++ [BinfoUnspent]
abus))
        bus :: [BinfoUnspent]
bus =
            Int -> [BinfoUnspent] -> [BinfoUnspent]
forall a. Int -> [a] -> [a]
take Int
limit ([BinfoUnspent] -> [BinfoUnspent])
-> [BinfoUnspent] -> [BinfoUnspent]
forall a b. (a -> b) -> a -> b
$
            (BinfoUnspent -> Bool) -> [BinfoUnspent] -> [BinfoUnspent]
forall a. (a -> Bool) -> [a] -> [a]
takeWhile ((Int32
min_conf Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
<=) (Int32 -> Bool) -> (BinfoUnspent -> Int32) -> BinfoUnspent -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BinfoUnspent -> Int32
getBinfoUnspentConfirmations) ([BinfoUnspent] -> [BinfoUnspent])
-> [BinfoUnspent] -> [BinfoUnspent]
forall a b. (a -> b) -> a -> b
$
            (BinfoUnspent -> BinfoUnspent -> Ordering)
-> [BinfoUnspent] -> [BinfoUnspent]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy
            ((Int32 -> Int32 -> Ordering) -> Int32 -> Int32 -> Ordering
forall a b c. (a -> b -> c) -> b -> a -> c
flip Int32 -> Int32 -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Int32 -> Int32 -> Ordering)
-> (BinfoUnspent -> Int32)
-> BinfoUnspent
-> BinfoUnspent
-> Ordering
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` BinfoUnspent -> Int32
getBinfoUnspentConfirmations)
            (HashMap (TxHash, Word32) BinfoUnspent -> [BinfoUnspent]
forall k v. HashMap k v -> [v]
HashMap.elems HashMap (TxHash, Word32) BinfoUnspent
busm)
    WebT m ()
forall (m :: * -> *) e. (Monad m, ScottyError e) => ActionT e m ()
setHeaders
    Encoding -> WebT m ()
forall (m :: * -> *). Monad m => Encoding -> WebT m ()
streamEncoding (Network -> BinfoUnspents -> Encoding
binfoUnspentsToEncoding Network
net ([BinfoUnspent] -> BinfoUnspents
BinfoUnspents [BinfoUnspent]
bus))
  where
    get_limit :: ActionT Except (ReaderT WebState m) Int
get_limit = (Int -> Int)
-> ActionT Except (ReaderT WebState m) Int
-> ActionT Except (ReaderT WebState m) Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Int -> Int -> Int
forall a. Ord a => a -> a -> a
min 1000) (ActionT Except (ReaderT WebState m) Int
 -> ActionT Except (ReaderT WebState m) Int)
-> ActionT Except (ReaderT WebState m) Int
-> ActionT Except (ReaderT WebState m) Int
forall a b. (a -> b) -> a -> b
$ Text -> ActionT Except (ReaderT WebState m) Int
forall a e (m :: * -> *).
(Parsable a, ScottyError e, Monad m) =>
Text -> ActionT e m a
S.param "limit" ActionT Except (ReaderT WebState m) Int
-> (Except -> ActionT Except (ReaderT WebState m) Int)
-> ActionT Except (ReaderT WebState m) Int
forall e (m :: * -> *) a.
(ScottyError e, Monad m) =>
ActionT e m a -> (e -> ActionT e m a) -> ActionT e m a
`S.rescue` ActionT Except (ReaderT WebState m) Int
-> Except -> ActionT Except (ReaderT WebState m) Int
forall a b. a -> b -> a
const (Int -> ActionT Except (ReaderT WebState m) Int
forall (m :: * -> *) a. Monad m => a -> m a
return 250)
    get_min_conf :: ActionT Except (ReaderT WebState m) Int32
get_min_conf = Text -> ActionT Except (ReaderT WebState m) Int32
forall a e (m :: * -> *).
(Parsable a, ScottyError e, Monad m) =>
Text -> ActionT e m a
S.param "confirmations" ActionT Except (ReaderT WebState m) Int32
-> (Except -> ActionT Except (ReaderT WebState m) Int32)
-> ActionT Except (ReaderT WebState m) Int32
forall e (m :: * -> *) a.
(ScottyError e, Monad m) =>
ActionT e m a -> (e -> ActionT e m a) -> ActionT e m a
`S.rescue` ActionT Except (ReaderT WebState m) Int32
-> Except -> ActionT Except (ReaderT WebState m) Int32
forall a b. a -> b -> a
const (Int32 -> ActionT Except (ReaderT WebState m) Int32
forall (m :: * -> *) a. Monad m => a -> m a
return 0)
    get_height :: ActionT Except (ReaderT WebState m) Word32
get_height =
        ActionT Except (ReaderT WebState m) (Maybe BlockHash)
forall (m :: * -> *). StoreReadBase m => m (Maybe BlockHash)
getBestBlock ActionT Except (ReaderT WebState m) (Maybe BlockHash)
-> (Maybe BlockHash -> ActionT Except (ReaderT WebState m) Word32)
-> ActionT Except (ReaderT WebState m) Word32
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Nothing -> (WebMetrics -> ErrorCounter)
-> Except -> ActionT Except (ReaderT WebState m) Word32
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
unspentErrors Except
ThingNotFound
        Just bh :: BlockHash
bh -> BlockHash -> ActionT Except (ReaderT WebState m) (Maybe BlockData)
forall (m :: * -> *).
StoreReadBase m =>
BlockHash -> m (Maybe BlockData)
getBlock BlockHash
bh ActionT Except (ReaderT WebState m) (Maybe BlockData)
-> (Maybe BlockData -> ActionT Except (ReaderT WebState m) Word32)
-> ActionT Except (ReaderT WebState m) Word32
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
            Nothing -> (WebMetrics -> ErrorCounter)
-> Except -> ActionT Except (ReaderT WebState m) Word32
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
unspentErrors Except
ThingNotFound
            Just b :: BlockData
b  -> Word32 -> ActionT Except (ReaderT WebState m) Word32
forall (m :: * -> *) a. Monad m => a -> m a
return (BlockData -> Word32
blockDataHeight BlockData
b)
    xpub_unspent :: Bool -> Word32 -> XPubKey -> XPubUnspent -> BinfoUnspent
xpub_unspent numtxid :: Bool
numtxid height :: Word32
height xpub :: XPubKey
xpub (XPubUnspent p :: [Word32]
p u :: Unspent
u) =
        let path :: Maybe SoftPath
path = DerivPathI AnyDeriv -> Maybe SoftPath
forall t. DerivPathI t -> Maybe SoftPath
toSoft ([Word32] -> DerivPathI AnyDeriv
listToPath [Word32]
p)
            xp :: Maybe BinfoXPubPath
xp = XPubKey -> SoftPath -> BinfoXPubPath
BinfoXPubPath XPubKey
xpub (SoftPath -> BinfoXPubPath)
-> Maybe SoftPath -> Maybe BinfoXPubPath
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe SoftPath
path
            bu :: BinfoUnspent
bu = Bool -> Word32 -> Unspent -> BinfoUnspent
unspent Bool
numtxid Word32
height Unspent
u
        in BinfoUnspent
bu {getBinfoUnspentXPub :: Maybe BinfoXPubPath
getBinfoUnspentXPub = Maybe BinfoXPubPath
xp}
    unspent :: Bool -> Word32 -> Unspent -> BinfoUnspent
unspent numtxid :: Bool
numtxid height :: Word32
height Unspent{..} =
        let conf :: Word32
conf = case BlockRef
unspentBlock of
                       MemRef{}     -> 0
                       BlockRef h :: Word32
h _ -> Word32
height Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Word32
h Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
+ 1
            hash :: TxHash
hash = OutPoint -> TxHash
outPointHash OutPoint
unspentPoint
            idx :: Word32
idx = OutPoint -> Word32
outPointIndex OutPoint
unspentPoint
            val :: Word64
val = Word64
unspentAmount
            script :: ByteString
script = ByteString
unspentScript
            txi :: BinfoTxId
txi = Bool -> TxHash -> BinfoTxId
encodeBinfoTxId Bool
numtxid TxHash
hash
        in $WBinfoUnspent :: TxHash
-> Word32
-> ByteString
-> Word64
-> Int32
-> BinfoTxId
-> Maybe BinfoXPubPath
-> BinfoUnspent
BinfoUnspent
           { getBinfoUnspentHash :: TxHash
getBinfoUnspentHash = TxHash
hash
           , getBinfoUnspentOutputIndex :: Word32
getBinfoUnspentOutputIndex = Word32
idx
           , getBinfoUnspentScript :: ByteString
getBinfoUnspentScript = ByteString
script
           , getBinfoUnspentValue :: Word64
getBinfoUnspentValue = Word64
val
           , getBinfoUnspentConfirmations :: Int32
getBinfoUnspentConfirmations = Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
conf
           , getBinfoUnspentTxIndex :: BinfoTxId
getBinfoUnspentTxIndex = BinfoTxId
txi
           , getBinfoUnspentXPub :: Maybe BinfoXPubPath
getBinfoUnspentXPub = Maybe BinfoXPubPath
forall a. Maybe a
Nothing
           }
    get_xpub_unspents :: HashMap XPubKey XPubSpec
-> ActionT
     Except (ReaderT WebState m) (HashMap XPubKey [XPubUnspent])
get_xpub_unspents = (XPubSpec -> ActionT Except (ReaderT WebState m) [XPubUnspent])
-> HashMap XPubKey XPubSpec
-> ActionT
     Except (ReaderT WebState m) (HashMap XPubKey [XPubUnspent])
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (XPubSpec
-> Limits -> ActionT Except (ReaderT WebState m) [XPubUnspent]
forall (m :: * -> *).
StoreReadExtra m =>
XPubSpec -> Limits -> m [XPubUnspent]
`xPubUnspents` Limits
forall a. Default a => a
def)
    get_addr_unspents :: HashSet Address -> ActionT Except (ReaderT WebState m) [Unspent]
get_addr_unspents = ([Address]
-> Limits -> ActionT Except (ReaderT WebState m) [Unspent]
forall (m :: * -> *).
StoreReadExtra m =>
[Address] -> Limits -> m [Unspent]
`getAddressesUnspents` Limits
forall a. Default a => a
def) ([Address] -> ActionT Except (ReaderT WebState m) [Unspent])
-> (HashSet Address -> [Address])
-> HashSet Address
-> ActionT Except (ReaderT WebState m) [Unspent]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashSet Address -> [Address]
forall a. HashSet a -> [a]
HashSet.toList

getBinfoTxs :: (StoreReadExtra m, MonadIO m)
            => HashMap Address (Maybe BinfoXPubPath) -- address book
            -> HashSet XPubSpec -- show xpubs
            -> HashSet Address -- show addrs
            -> HashSet Address -- balance addresses
            -> BinfoFilter
            -> Bool -- numtxid
            -> Bool -- prune outputs
            -> Int64 -- starting balance
            -> ConduitT () BinfoTx m ()
getBinfoTxs :: HashMap Address (Maybe BinfoXPubPath)
-> HashSet XPubSpec
-> HashSet Address
-> HashSet Address
-> BinfoFilter
-> Bool
-> Bool
-> Int64
-> ConduitT () BinfoTx m ()
getBinfoTxs abook :: HashMap Address (Maybe BinfoXPubPath)
abook sxspecs :: HashSet XPubSpec
sxspecs saddrs :: HashSet Address
saddrs baddrs :: HashSet Address
baddrs bfilter :: BinfoFilter
bfilter numtxid :: Bool
numtxid prune :: Bool
prune bal :: Int64
bal =
    (TxRef -> TxRef -> Ordering)
-> [ConduitT () TxRef m ()] -> ConduitT () TxRef m ()
forall (m :: * -> *) a.
Monad m =>
(a -> a -> Ordering) -> [ConduitT () a m ()] -> ConduitT () a m ()
joinStreams ((TxRef -> TxRef -> Ordering) -> TxRef -> TxRef -> Ordering
forall a b c. (a -> b -> c) -> b -> a -> c
flip TxRef -> TxRef -> Ordering
forall a. Ord a => a -> a -> Ordering
compare) [ConduitT () TxRef m ()]
conduits ConduitT () TxRef m ()
-> ConduitM TxRef BinfoTx m () -> ConduitT () BinfoTx m ()
forall (m :: * -> *) a b c r.
Monad m =>
ConduitM a b m () -> ConduitM b c m r -> ConduitM a c m r
.| Int64 -> ConduitM TxRef BinfoTx m ()
forall (m :: * -> *).
StoreReadBase m =>
Int64 -> ConduitT TxRef BinfoTx m ()
go Int64
bal
  where
    sxspecs_ls :: [XPubSpec]
sxspecs_ls = HashSet XPubSpec -> [XPubSpec]
forall a. HashSet a -> [a]
HashSet.toList HashSet XPubSpec
sxspecs
    saddrs_ls :: [Address]
saddrs_ls = HashSet Address -> [Address]
forall a. HashSet a -> [a]
HashSet.toList HashSet Address
saddrs
    conduits :: [ConduitT () TxRef m ()]
conduits = (XPubSpec -> ConduitT () TxRef m ())
-> [XPubSpec] -> [ConduitT () TxRef m ()]
forall a b. (a -> b) -> [a] -> [b]
map XPubSpec -> ConduitT () TxRef m ()
forall (m :: * -> *).
StoreReadExtra m =>
XPubSpec -> ConduitT () TxRef m ()
xpub_c [XPubSpec]
sxspecs_ls [ConduitT () TxRef m ()]
-> [ConduitT () TxRef m ()] -> [ConduitT () TxRef m ()]
forall a. Semigroup a => a -> a -> a
<> (Address -> ConduitT () TxRef m ())
-> [Address] -> [ConduitT () TxRef m ()]
forall a b. (a -> b) -> [a] -> [b]
map Address -> ConduitT () TxRef m ()
forall (m :: * -> *).
StoreReadExtra m =>
Address -> ConduitT () TxRef m ()
addr_c [Address]
saddrs_ls
    xpub_c :: XPubSpec -> ConduitT () TxRef m ()
xpub_c x :: XPubSpec
x = (Limits -> m [TxRef])
-> (TxRef -> TxHash) -> Limits -> ConduitT () TxRef m ()
forall (m :: * -> *) a.
Monad m =>
(Limits -> m [a]) -> (a -> TxHash) -> Limits -> ConduitT () a m ()
streamThings (XPubSpec -> Limits -> m [TxRef]
forall (m :: * -> *).
StoreReadExtra m =>
XPubSpec -> Limits -> m [TxRef]
xPubTxs XPubSpec
x) TxRef -> TxHash
txRefHash Limits
forall a. Default a => a
def
    addr_c :: Address -> ConduitT () TxRef m ()
addr_c a :: Address
a = (Limits -> m [TxRef])
-> (TxRef -> TxHash) -> Limits -> ConduitT () TxRef m ()
forall (m :: * -> *) a.
Monad m =>
(Limits -> m [a]) -> (a -> TxHash) -> Limits -> ConduitT () a m ()
streamThings (Address -> Limits -> m [TxRef]
forall (m :: * -> *).
StoreReadExtra m =>
Address -> Limits -> m [TxRef]
getAddressTxs Address
a) TxRef -> TxHash
txRefHash Limits
forall a. Default a => a
def
    binfo_tx :: Int64 -> Transaction -> BinfoTx
binfo_tx b :: Int64
b = Bool
-> HashMap Address (Maybe BinfoXPubPath)
-> Bool
-> Int64
-> Transaction
-> BinfoTx
toBinfoTx Bool
numtxid HashMap Address (Maybe BinfoXPubPath)
abook Bool
prune Int64
b
    compute_bal_change :: BinfoTx -> a
compute_bal_change BinfoTx{..} =
        let ins :: [BinfoTxOutput]
ins = (BinfoTxInput -> Maybe BinfoTxOutput)
-> [BinfoTxInput] -> [BinfoTxOutput]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe BinfoTxInput -> Maybe BinfoTxOutput
getBinfoTxInputPrevOut [BinfoTxInput]
getBinfoTxInputs
            out :: [BinfoTxOutput]
out = [BinfoTxOutput]
getBinfoTxOutputs
            f :: Bool -> BinfoTxOutput -> a
f b :: Bool
b BinfoTxOutput{..} =
                let val :: a
val = Word64 -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
getBinfoTxOutputValue
                in case Maybe Address
getBinfoTxOutputAddress of
                       Nothing -> 0
                       Just a :: Address
a | Address
a Address -> HashSet Address -> Bool
forall a. (Eq a, Hashable a) => a -> HashSet a -> Bool
`HashSet.member` HashSet Address
baddrs ->
                                    if Bool
b then a
val else a -> a
forall a. Num a => a -> a
negate a
val
                              | Bool
otherwise -> 0
        in [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ([a] -> a) -> [a] -> a
forall a b. (a -> b) -> a -> b
$ (BinfoTxOutput -> a) -> [BinfoTxOutput] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map (Bool -> BinfoTxOutput -> a
forall a. Num a => Bool -> BinfoTxOutput -> a
f Bool
False) [BinfoTxOutput]
ins [a] -> [a] -> [a]
forall a. Semigroup a => a -> a -> a
<> (BinfoTxOutput -> a) -> [BinfoTxOutput] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map (Bool -> BinfoTxOutput -> a
forall a. Num a => Bool -> BinfoTxOutput -> a
f Bool
True) [BinfoTxOutput]
out
    go :: Int64 -> ConduitT TxRef BinfoTx m ()
go b :: Int64
b = ConduitT TxRef BinfoTx m (Maybe TxRef)
forall (m :: * -> *) i. Monad m => Consumer i m (Maybe i)
await ConduitT TxRef BinfoTx m (Maybe TxRef)
-> (Maybe TxRef -> ConduitT TxRef BinfoTx m ())
-> ConduitT TxRef BinfoTx m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Nothing -> () -> ConduitT TxRef BinfoTx m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
        Just (TxRef _ t :: TxHash
t) -> m (Maybe Transaction)
-> ConduitT TxRef BinfoTx m (Maybe Transaction)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (TxHash -> m (Maybe Transaction)
forall (m :: * -> *).
(Monad m, StoreReadBase m) =>
TxHash -> m (Maybe Transaction)
getTransaction TxHash
t) ConduitT TxRef BinfoTx m (Maybe Transaction)
-> (Maybe Transaction -> ConduitT TxRef BinfoTx m ())
-> ConduitT TxRef BinfoTx m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
            Nothing -> Int64 -> ConduitT TxRef BinfoTx m ()
go Int64
b
            Just x :: Transaction
x -> do
                let a :: BinfoTx
a = Int64 -> Transaction -> BinfoTx
binfo_tx Int64
b Transaction
x
                    b' :: Int64
b' = Int64
b Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
- BinfoTx -> Int64
forall a. Num a => BinfoTx -> a
compute_bal_change BinfoTx
a
                    c :: Bool
c = Maybe Word32 -> Bool
forall a. Maybe a -> Bool
isJust (BinfoTx -> Maybe Word32
getBinfoTxBlockHeight BinfoTx
a)
                    Just (d :: Int64
d, _) = BinfoTx -> Maybe (Int64, Int64)
getBinfoTxResultBal BinfoTx
a
                    r :: Int64
r = Int64
d Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Word64 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (BinfoTx -> Word64
getBinfoTxFee BinfoTx
a)
                case BinfoFilter
bfilter of
                    BinfoFilterAll ->
                        BinfoTx -> ConduitT TxRef BinfoTx m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield BinfoTx
a ConduitT TxRef BinfoTx m ()
-> ConduitT TxRef BinfoTx m () -> ConduitT TxRef BinfoTx m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int64 -> ConduitT TxRef BinfoTx m ()
go Int64
b'
                    BinfoFilterSent
                        | 0 Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
> Int64
r -> BinfoTx -> ConduitT TxRef BinfoTx m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield BinfoTx
a ConduitT TxRef BinfoTx m ()
-> ConduitT TxRef BinfoTx m () -> ConduitT TxRef BinfoTx m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int64 -> ConduitT TxRef BinfoTx m ()
go Int64
b'
                        | Bool
otherwise -> Int64 -> ConduitT TxRef BinfoTx m ()
go Int64
b'
                    BinfoFilterReceived
                        | Int64
r Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
> 0 -> BinfoTx -> ConduitT TxRef BinfoTx m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield BinfoTx
a ConduitT TxRef BinfoTx m ()
-> ConduitT TxRef BinfoTx m () -> ConduitT TxRef BinfoTx m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int64 -> ConduitT TxRef BinfoTx m ()
go Int64
b'
                        | Bool
otherwise -> Int64 -> ConduitT TxRef BinfoTx m ()
go Int64
b'
                    BinfoFilterMoved
                        | Int64
r Int64 -> Int64 -> Bool
forall a. Eq a => a -> a -> Bool
== 0 -> BinfoTx -> ConduitT TxRef BinfoTx m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield BinfoTx
a ConduitT TxRef BinfoTx m ()
-> ConduitT TxRef BinfoTx m () -> ConduitT TxRef BinfoTx m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int64 -> ConduitT TxRef BinfoTx m ()
go Int64
b'
                        | Bool
otherwise -> Int64 -> ConduitT TxRef BinfoTx m ()
go Int64
b'
                    BinfoFilterConfirmed
                        | Bool
c -> BinfoTx -> ConduitT TxRef BinfoTx m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield BinfoTx
a ConduitT TxRef BinfoTx m ()
-> ConduitT TxRef BinfoTx m () -> ConduitT TxRef BinfoTx m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int64 -> ConduitT TxRef BinfoTx m ()
go Int64
b'
                        | Bool
otherwise -> Int64 -> ConduitT TxRef BinfoTx m ()
go Int64
b'
                    BinfoFilterMempool
                        | Bool
c -> () -> ConduitT TxRef BinfoTx m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
                        | Bool
otherwise -> BinfoTx -> ConduitT TxRef BinfoTx m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield BinfoTx
a ConduitT TxRef BinfoTx m ()
-> ConduitT TxRef BinfoTx m () -> ConduitT TxRef BinfoTx m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int64 -> ConduitT TxRef BinfoTx m ()
go Int64
b'

scottyMultiAddr :: (MonadUnliftIO m, MonadLoggerIO m) => WebT m ()
scottyMultiAddr :: WebT m ()
scottyMultiAddr =
    ActionT
  Except
  (ReaderT WebState m)
  (HashSet Address, HashSet XPubKey, HashSet Address,
   HashSet XPubKey, HashMap XPubKey XPubSpec)
get_addrs ActionT
  Except
  (ReaderT WebState m)
  (HashSet Address, HashSet XPubKey, HashSet Address,
   HashSet XPubKey, HashMap XPubKey XPubSpec)
-> ((HashSet Address, HashSet XPubKey, HashSet Address,
     HashSet XPubKey, HashMap XPubKey XPubSpec)
    -> WebT m ())
-> WebT m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \(addrs' :: HashSet Address
addrs', xpubs :: HashSet XPubKey
xpubs, saddrs :: HashSet Address
saddrs, sxpubs :: HashSet XPubKey
sxpubs, xspecs :: HashMap XPubKey XPubSpec
xspecs) ->
    WebT m Bool
forall (m :: * -> *). MonadIO m => WebT m Bool
getNumTxId WebT m Bool -> (Bool -> WebT m ()) -> WebT m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \numtxid :: Bool
numtxid ->
    ReaderT WebState m (TVar (HashMap Text BinfoTicker))
-> ActionT
     Except (ReaderT WebState m) (TVar (HashMap Text BinfoTicker))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ((WebState -> TVar (HashMap Text BinfoTicker))
-> ReaderT WebState m (TVar (HashMap Text BinfoTicker))
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WebState -> TVar (HashMap Text BinfoTicker)
webTicker) ActionT
  Except (ReaderT WebState m) (TVar (HashMap Text BinfoTicker))
-> (TVar (HashMap Text BinfoTicker) -> WebT m ()) -> WebT m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ticker :: TVar (HashMap Text BinfoTicker)
ticker ->
    TVar (HashMap Text BinfoTicker)
-> ActionT Except (ReaderT WebState m) BinfoSymbol
forall (m :: * -> *) b.
(ScottyError b, MonadIO m) =>
TVar (HashMap Text BinfoTicker) -> ActionT b m BinfoSymbol
get_price TVar (HashMap Text BinfoTicker)
ticker ActionT Except (ReaderT WebState m) BinfoSymbol
-> (BinfoSymbol -> WebT m ()) -> WebT m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \local :: BinfoSymbol
local ->
    WebT m Bool
get_cashaddr WebT m Bool -> (Bool -> WebT m ()) -> WebT m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \cashaddr :: Bool
cashaddr ->
    ActionT Except (ReaderT WebState m) Int
get_offset ActionT Except (ReaderT WebState m) Int
-> (Int -> WebT m ()) -> WebT m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \offset :: Int
offset ->
    ActionT Except (ReaderT WebState m) Int
get_count ActionT Except (ReaderT WebState m) Int
-> (Int -> WebT m ()) -> WebT m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \n :: Int
n ->
    WebT m Bool
get_prune WebT m Bool -> (Bool -> WebT m ()) -> WebT m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \prune :: Bool
prune ->
    ActionT Except (ReaderT WebState m) BinfoFilter
get_filter ActionT Except (ReaderT WebState m) BinfoFilter
-> (BinfoFilter -> WebT m ()) -> WebT m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \fltr :: BinfoFilter
fltr ->
    let len :: Int
len = HashSet Address -> Int
forall a. HashSet a -> Int
HashSet.size HashSet Address
addrs' Int -> Int -> Int
forall a. Num a => a -> a -> a
+ HashSet XPubKey -> Int
forall a. HashSet a -> Int
HashSet.size HashSet XPubKey
xpubs
    in (WebMetrics -> StatDist) -> Int -> WebT m () -> WebT m ()
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
multiaddrResponseTime Int
len (WebT m () -> WebT m ()) -> WebT m () -> WebT m ()
forall a b. (a -> b) -> a -> b
$ do
    HashMap XPubKey [XPubBal]
xbals <- HashMap XPubKey XPubSpec
-> ActionT Except (ReaderT WebState m) (HashMap XPubKey [XPubBal])
get_xbals HashMap XPubKey XPubSpec