22-- | These functions only work with Ogmios backend (not Blockfrost!).
33-- | https://ogmios.dev/mini-protocols/local-tx-monitor/
44module Contract.Backend.Ogmios.Mempool
5- ( module Ogmios
6- , acquireMempoolSnapshot
5+ ( acquireMempoolSnapshot
76 , fetchMempoolTxs
87 , mempoolSnapshotHasTx
98 , mempoolSnapshotNextTx
@@ -19,35 +18,49 @@ import Cardano.Types.Transaction (Transaction)
1918import Cardano.Types.TransactionHash (TransactionHash )
2019import Contract.Monad (Contract )
2120import Control.Monad.Error.Class (liftMaybe , try )
21+ import Control.Monad.Reader.Trans (asks )
2222import Ctl.Internal.Contract.Monad (wrapQueryM )
23- import Ctl.Internal.QueryM
24- ( acquireMempoolSnapshot
25- , mempoolSnapshotHasTx
26- , mempoolSnapshotNextTx
27- , mempoolSnapshotSizeAndCapacity
28- , releaseMempool
29- ) as QueryM
23+ import Ctl.Internal.Logging (Logger , mkLogger )
24+ import Ctl.Internal.QueryM (QueryM )
25+ import Ctl.Internal.QueryM.JsonRpc2 as JsonRpc2
3026import Ctl.Internal.QueryM.Ogmios
31- ( MempoolSizeAndCapacity (MempoolSizeAndCapacity)
27+ ( MempoolSizeAndCapacity
3228 , MempoolSnapshotAcquired
3329 , MempoolTransaction (MempoolTransaction)
30+ , acquireMempoolSnapshotCall
3431 ) as Ogmios
32+ import Ctl.Internal.QueryM.OgmiosWebsocket.JsWebSocket (JsWebSocket )
33+ import Ctl.Internal.QueryM.OgmiosWebsocket.Mempool
34+ ( mempoolSnapshotHasTxCall
35+ , mempoolSnapshotNextTxCall
36+ , mempoolSnapshotSizeAndCapacityCall
37+ , releaseMempoolCall
38+ )
39+ import Ctl.Internal.QueryM.OgmiosWebsocket.Types
40+ ( ListenerSet
41+ , OgmiosListeners
42+ , listeners
43+ , mkRequestAff
44+ , underlyingWebSocket
45+ )
3546import Data.Array as Array
3647import Data.ByteArray (hexToByteArray )
3748import Data.List (List (Cons))
3849import Data.Maybe (Maybe (Just, Nothing))
50+ import Data.Newtype (unwrap )
51+ import Effect.Aff.Class (liftAff )
3952import Effect.Exception (error )
4053
4154-- | Establish a connection with the Local TX Monitor.
4255-- | Instantly accquires the current mempool snapshot, and will wait for the next
4356-- | mempool snapshot if used again before using `releaseMempool`.
4457acquireMempoolSnapshot :: Contract Ogmios.MempoolSnapshotAcquired
45- acquireMempoolSnapshot = wrapQueryM QueryM .acquireMempoolSnapshot
58+ acquireMempoolSnapshot = wrapQueryM acquireMempoolSnapshotFetch
4659
4760-- | Check to see if a TxHash is present in the current mempool snapshot.
4861mempoolSnapshotHasTx
4962 :: Ogmios.MempoolSnapshotAcquired -> TransactionHash -> Contract Boolean
50- mempoolSnapshotHasTx ms = wrapQueryM <<< QueryM .mempoolSnapshotHasTx ms
63+ mempoolSnapshotHasTx ms = wrapQueryM <<< mempoolSnapshotHasTxFetch ms
5164
5265-- | Get the first received TX in the current mempool snapshot. This function can
5366-- | be recursively called to traverse the finger-tree of the mempool data set.
@@ -56,7 +69,7 @@ mempoolSnapshotNextTx
5669 :: Ogmios.MempoolSnapshotAcquired
5770 -> Contract (Maybe Transaction )
5871mempoolSnapshotNextTx mempoolAcquired = do
59- mbTx <- wrapQueryM $ QueryM .mempoolSnapshotNextTx mempoolAcquired
72+ mbTx <- wrapQueryM $ mempoolSnapshotNextTxFetch mempoolAcquired
6073 for mbTx \(Ogmios.MempoolTransaction { raw }) -> do
6174 byteArray <- liftMaybe (error " Failed to decode transaction" )
6275 $ hexToByteArray raw
@@ -69,12 +82,12 @@ mempoolSnapshotNextTx mempoolAcquired = do
6982mempoolSnapshotSizeAndCapacity
7083 :: Ogmios.MempoolSnapshotAcquired -> Contract Ogmios.MempoolSizeAndCapacity
7184mempoolSnapshotSizeAndCapacity = wrapQueryM <<<
72- QueryM .mempoolSnapshotSizeAndCapacity
85+ mempoolSnapshotSizeAndCapacityFetch
7386
7487-- | Release the connection to the Local TX Monitor.
7588releaseMempool
7689 :: Ogmios.MempoolSnapshotAcquired -> Contract Unit
77- releaseMempool = wrapQueryM <<< QueryM .releaseMempool
90+ releaseMempool = wrapQueryM <<< releaseMempoolFetch
7891
7992-- | A bracket-style function for working with mempool snapshots - ensures
8093-- | release in the presence of exceptions
@@ -100,3 +113,79 @@ fetchMempoolTxs ms = Array.fromFoldable <$> go
100113 case nextTX of
101114 Just tx -> Cons tx <$> go
102115 Nothing -> pure mempty
116+
117+ acquireMempoolSnapshotFetch
118+ :: QueryM Ogmios.MempoolSnapshotAcquired
119+ acquireMempoolSnapshotFetch =
120+ mkOgmiosRequest
121+ Ogmios .acquireMempoolSnapshotCall
122+ _.acquireMempool
123+ unit
124+
125+ mempoolSnapshotHasTxFetch
126+ :: Ogmios.MempoolSnapshotAcquired
127+ -> TransactionHash
128+ -> QueryM Boolean
129+ mempoolSnapshotHasTxFetch ms txh =
130+ unwrap <$> mkOgmiosRequest
131+ (mempoolSnapshotHasTxCall ms)
132+ _.mempoolHasTx
133+ txh
134+
135+ mempoolSnapshotSizeAndCapacityFetch
136+ :: Ogmios.MempoolSnapshotAcquired
137+ -> QueryM Ogmios.MempoolSizeAndCapacity
138+ mempoolSnapshotSizeAndCapacityFetch ms =
139+ mkOgmiosRequest
140+ (mempoolSnapshotSizeAndCapacityCall ms)
141+ _.mempoolSizeAndCapacity
142+ unit
143+
144+ releaseMempoolFetch
145+ :: Ogmios.MempoolSnapshotAcquired
146+ -> QueryM Unit
147+ releaseMempoolFetch ms =
148+ unit <$ mkOgmiosRequest
149+ (releaseMempoolCall ms)
150+ _.releaseMempool
151+ unit
152+
153+ mempoolSnapshotNextTxFetch
154+ :: Ogmios.MempoolSnapshotAcquired
155+ -> QueryM (Maybe Ogmios.MempoolTransaction )
156+ mempoolSnapshotNextTxFetch ms =
157+ unwrap <$> mkOgmiosRequest
158+ (mempoolSnapshotNextTxCall ms)
159+ _.mempoolNextTx
160+ unit
161+
162+ -- | Builds an Ogmios request action using `QueryM`
163+ mkOgmiosRequest
164+ :: forall (request :: Type ) (response :: Type )
165+ . JsonRpc2.JsonRpc2Call request response
166+ -> (OgmiosListeners -> ListenerSet request response )
167+ -> request
168+ -> QueryM response
169+ mkOgmiosRequest jsonRpc2Call getLs inp = do
170+ listeners' <- asks $ listeners <<< _.ogmiosWs <<< _.runtime
171+ websocket <- asks $ underlyingWebSocket <<< _.ogmiosWs <<< _.runtime
172+ mkRequest listeners' websocket jsonRpc2Call getLs inp
173+
174+ mkRequest
175+ :: forall (request :: Type ) (response :: Type ) (listeners :: Type )
176+ . listeners
177+ -> JsWebSocket
178+ -> JsonRpc2.JsonRpc2Call request response
179+ -> (listeners -> ListenerSet request response )
180+ -> request
181+ -> QueryM response
182+ mkRequest listeners' ws jsonRpc2Call getLs inp = do
183+ logger <- getLogger
184+ liftAff $ mkRequestAff listeners' ws logger jsonRpc2Call getLs inp
185+ where
186+ getLogger :: QueryM Logger
187+ getLogger = do
188+ logLevel <- asks $ _.config >>> _.logLevel
189+ mbCustomLogger <- asks $ _.config >>> _.customLogger
190+ pure $ mkLogger logLevel mbCustomLogger
191+
0 commit comments