{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE NoImplicitPrelude #-}

module BtcLsp.Yesod.Handler.SwapUpdates
  ( getSwapUpdatesR,
    getSwapUpdate,
  )
where

import qualified BtcLsp.Storage.Model.SwapIntoLn as SwapIntoLn
import BtcLsp.Time
import BtcLsp.Yesod.Import
import qualified Data.ByteString.Base64.URL as B64
import Data.ByteString.Char8 as C8
import qualified Data.ByteString.Lazy as L
import qualified Data.Digest.Pure.SHA as SHA
  ( bytestringDigest,
    sha256,
  )
import qualified Universum as U

getSwapUpdatesR :: Uuid 'SwapIntoLnTable -> SwapHash -> Handler (Maybe SwapHash)
getSwapUpdatesR :: Uuid 'SwapIntoLnTable -> SwapHash -> Handler (Maybe SwapHash)
getSwapUpdatesR Uuid 'SwapIntoLnTable
uuid SwapHash
swapHash = do
  App
app <- HandlerFor App App
forall (m :: * -> *). MonadHandler m => m (HandlerSite m)
getYesod
  App
-> Uuid 'SwapIntoLnTable
-> SwapHash
-> Integer
-> Handler (Maybe SwapHash)
forall (m :: * -> *).
MonadHandler m =>
App
-> Uuid 'SwapIntoLnTable
-> SwapHash
-> Integer
-> m (Maybe SwapHash)
getSwapUpdateRec App
app Uuid 'SwapIntoLnTable
uuid SwapHash
swapHash Integer
60

getSwapUpdateRec :: MonadHandler m => App -> Uuid 'SwapIntoLnTable -> SwapHash -> Integer -> m (Maybe SwapHash)
getSwapUpdateRec :: forall (m :: * -> *).
MonadHandler m =>
App
-> Uuid 'SwapIntoLnTable
-> SwapHash
-> Integer
-> m (Maybe SwapHash)
getSwapUpdateRec App
app Uuid 'SwapIntoLnTable
uuid SwapHash
swapHash Integer
counter = do
  Maybe SwapHash
currentSwapUpdate <- App -> Uuid 'SwapIntoLnTable -> m (Maybe SwapHash)
forall (m :: * -> *).
MonadHandler m =>
App -> Uuid 'SwapIntoLnTable -> m (Maybe SwapHash)
getSwapUpdate App
app Uuid 'SwapIntoLnTable
uuid
  if SwapHash -> Maybe SwapHash
forall a. a -> Maybe a
Just SwapHash
swapHash Maybe SwapHash -> Maybe SwapHash -> Bool
forall a. Eq a => a -> a -> Bool
== Maybe SwapHash
currentSwapUpdate Bool -> Bool -> Bool
&& Integer
counter Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
0
    then do
      m ()
forall (m :: * -> *). MonadIO m => m ()
sleep5s
      App
-> Uuid 'SwapIntoLnTable
-> SwapHash
-> Integer
-> m (Maybe SwapHash)
forall (m :: * -> *).
MonadHandler m =>
App
-> Uuid 'SwapIntoLnTable
-> SwapHash
-> Integer
-> m (Maybe SwapHash)
getSwapUpdateRec App
app Uuid 'SwapIntoLnTable
uuid SwapHash
swapHash (Integer
counter Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
1)
    else Maybe SwapHash -> m (Maybe SwapHash)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe SwapHash
currentSwapUpdate

getSwapUpdate :: MonadHandler m => App -> Uuid 'SwapIntoLnTable -> m (Maybe SwapHash)
getSwapUpdate :: forall (m :: * -> *).
MonadHandler m =>
App -> Uuid 'SwapIntoLnTable -> m (Maybe SwapHash)
getSwapUpdate (App {appMRunner :: ()
appMRunner = UnliftIO forall a. m a -> IO a
run}) Uuid 'SwapIntoLnTable
uuid =
  (Text -> SwapHash
SwapHash (Text -> SwapHash) -> (SwapInfo -> Text) -> SwapInfo -> SwapHash
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. SwapInfo -> Text
hashFunc)
    (SwapInfo -> SwapHash) -> m (Maybe SwapInfo) -> m (Maybe SwapHash)
forall (f :: * -> *) (g :: * -> *) a b.
(Functor f, Functor g) =>
(a -> b) -> f (g a) -> f (g b)
U.<<$>> (IO (Maybe SwapInfo) -> m (Maybe SwapInfo)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Maybe SwapInfo) -> m (Maybe SwapInfo))
-> (Uuid 'SwapIntoLnTable -> IO (Maybe SwapInfo))
-> Uuid 'SwapIntoLnTable
-> m (Maybe SwapInfo)
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. m (Maybe SwapInfo) -> IO (Maybe SwapInfo)
forall a. m a -> IO a
run (m (Maybe SwapInfo) -> IO (Maybe SwapInfo))
-> (Uuid 'SwapIntoLnTable -> m (Maybe SwapInfo))
-> Uuid 'SwapIntoLnTable
-> IO (Maybe SwapInfo)
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ReaderT SqlBackend m (Maybe SwapInfo) -> m (Maybe SwapInfo)
forall (m :: * -> *) a. Storage m => ReaderT SqlBackend m a -> m a
runSql (ReaderT SqlBackend m (Maybe SwapInfo) -> m (Maybe SwapInfo))
-> (Uuid 'SwapIntoLnTable -> ReaderT SqlBackend m (Maybe SwapInfo))
-> Uuid 'SwapIntoLnTable
-> m (Maybe SwapInfo)
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Uuid 'SwapIntoLnTable -> ReaderT SqlBackend m (Maybe SwapInfo)
forall (m :: * -> *).
MonadIO m =>
Uuid 'SwapIntoLnTable -> ReaderT SqlBackend m (Maybe SwapInfo)
SwapIntoLn.getByUuidSql) Uuid 'SwapIntoLnTable
uuid

hashFunc :: SwapIntoLn.SwapInfo -> Text
hashFunc :: SwapInfo -> Text
hashFunc =
  ByteString -> Text
forall textual binary. Utf8 textual binary => binary -> textual
decodeUtf8
    (ByteString -> Text)
-> (SwapInfo -> ByteString) -> SwapInfo -> Text
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ByteString -> ByteString
B64.encode
    (ByteString -> ByteString)
-> (SwapInfo -> ByteString) -> SwapInfo -> ByteString
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ByteString -> ByteString
L.toStrict
    (ByteString -> ByteString)
-> (SwapInfo -> ByteString) -> SwapInfo -> ByteString
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Digest SHA256State -> ByteString
forall t. Digest t -> ByteString
SHA.bytestringDigest
    (Digest SHA256State -> ByteString)
-> (SwapInfo -> Digest SHA256State) -> SwapInfo -> ByteString
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ByteString -> Digest SHA256State
SHA.sha256
    (ByteString -> Digest SHA256State)
-> (SwapInfo -> ByteString) -> SwapInfo -> Digest SHA256State
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ByteString -> ByteString
L.fromStrict
    (ByteString -> ByteString)
-> (SwapInfo -> ByteString) -> SwapInfo -> ByteString
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. String -> ByteString
C8.pack
    (String -> ByteString)
-> (SwapInfo -> String) -> SwapInfo -> ByteString
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. SwapInfo -> String
forall a. Show a => a -> String
show
    (SwapInfo -> String)
-> (SwapInfo -> SwapInfo) -> SwapInfo -> String
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (\SwapInfo
x -> SwapInfo
x {swapInfoChan :: [Entity LnChan]
SwapIntoLn.swapInfoChan = []})