@@ -23,7 +23,7 @@ import BotPlutusInterface.Types (
2323 )
2424import Cardano.Api (CardanoMode , EraHistory )
2525import Cardano.Api qualified as CAPI
26- import Cardano.Ledger.Alonzo.PParams (_protocolVersion )
26+ import Cardano.Ledger.Alonzo.PParams (_protocolVersion , PParams )
2727import Cardano.Ledger.Alonzo.TxInfo (slotToPOSIXTime )
2828import Cardano.Ledger.Slot (EpochInfo )
2929import Cardano.Slotting.EpochInfo (hoistEpochInfo )
@@ -53,6 +53,8 @@ import Ouroboros.Consensus.HardFork.History qualified as Consensus
5353import Ouroboros.Consensus.HardFork.History.Qry qualified as HF
5454import System.Environment (getEnv )
5555import Prelude
56+ import Cardano.Ledger.Crypto (StandardCrypto )
57+ import Cardano.Ledger.Alonzo (AlonzoEra )
5658
5759-- | Error returned by the functions of this module
5860data TimeSlotConversionError
@@ -65,9 +67,9 @@ data TimeSlotConversionError
6567slotToPOSIXTimeIO :: PABConfig -> Ledger. Slot -> IO (Either TimeSlotConversionError Ledger. POSIXTime )
6668slotToPOSIXTimeIO pabConf lSlot = runEitherT $ do
6769 nodeInfo <- liftIO $ mkNodeInfo pabConf
68- eraHsitory <- newET (queryEraHistory nodeInfo)
70+ eraHistory <- newET (queryEraHistory nodeInfo)
6971 sysStart <- newET $ querySystemStart nodeInfo
70- let epochInfo = toLedgerEpochInfo eraHsitory
72+ let epochInfo = toLedgerEpochInfo eraHistory
7173 pparams =
7274 CAPI. toLedgerPParams
7375 CAPI. ShelleyBasedEraAlonzo
@@ -105,21 +107,22 @@ posixTimeRangeToContainedSlotRangeIO
105107 -- getting required info from node
106108 nodeInfo <- liftIO $ mkNodeInfo pabConf
107109 sysStart <- newET $ querySystemStart nodeInfo
108- eraHsitory <- newET $ queryEraHistory nodeInfo
109- let epochInfo = toLedgerEpochInfo eraHsitory
110+ eraHistory <- newET $ queryEraHistory nodeInfo
111+ let epochInfo = toLedgerEpochInfo eraHistory
110112 pparams =
111113 CAPI. toLedgerPParams
112114 CAPI. ShelleyBasedEraAlonzo
113115 (pcProtocolParams pabConf)
114116
115- let extTimeToExtSlot = convertExtended sysStart eraHsitory
117+ let extTimeToExtSlot = convertExtended sysStart eraHistory
116118 getClosure = getExtClosure pparams epochInfo sysStart
117119
118120 -- conversions
119121 startSlot <- extTimeToExtSlot start
120122 startSlotClosure <- getClosure startSlot startIncl
121123 endSlot <- extTimeToExtSlot end
122124 endSlotClosure <- getClosure endSlot endIncl
125+
123126 let lowerB :: LowerBound Ledger. Slot
124127 lowerB = LowerBound startSlot startSlotClosure
125128
@@ -131,14 +134,30 @@ posixTimeRangeToContainedSlotRangeIO
131134
132135 pure range
133136 where
137+ -- helper to convert `Extended POSIXTime` to `Extended Slot`
138+ -- using `posixTimeToSlot`
139+ convertExtended ::
140+ Monad m =>
141+ SystemStart ->
142+ EraHistory CardanoMode ->
143+ Extended Ledger. POSIXTime ->
144+ EitherT TimeSlotConversionError m (Extended Ledger. Slot )
134145 convertExtended sysStart eraHist =
135146 firstEitherT toError . hoistEither . \ case
136- Finite pTime -> do
137- s <- posixTimeToSlot sysStart eraHist pTime
138- pure . Finite . Ledger. Slot . toInteger $ s
147+ Finite pTime -> Finite <$> posixTimeToSlot sysStart eraHist pTime
139148 NegInf -> pure NegInf
140149 PosInf -> pure PosInf
141150
151+ -- helper to calulate bound's closure
152+ -- if bound is not `NegInf` or `PosInf`, then `Closure` need to be calculated
153+ -- https://github.com/input-output-hk/plutus-apps/blob/e51f57fa99f4cc0942ba6476b0689e43f0948eb3/plutus-ledger/src/Ledger/TimeSlot.hs#L125-L130
154+ getExtClosure ::
155+ PParams (AlonzoEra StandardCrypto ) ->
156+ EpochInfo (Either CAPI. TransactionValidityError ) ->
157+ SystemStart ->
158+ Extended Ledger. Slot ->
159+ Bool -> -- current `Closure` of lower or upper bound of `Ledger.POSIXTimeRange`
160+ EitherT TimeSlotConversionError IO Bool
142161 getExtClosure pparams epochInfo sysStart exSlot currentClosure =
143162 firstEitherT toError . hoistEither $
144163 case exSlot of
0 commit comments