Skip to content

Commit b706d77

Browse files
committed
wip: slot <-> time convcersion
- cleanup TimeSlot module - refactoring - removing `SlotConfig` from `PABConfig`
1 parent aa382a4 commit b706d77

File tree

11 files changed

+79
-172
lines changed

11 files changed

+79
-172
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ main = do
8484
, pcPort = 9080
8585
, pcProtocolParams = protocolParams
8686
, pcTipPollingInterval = 10_000_000
87-
, -- | Slot configuration of the network, the default value can be used for the mainnet
88-
pcSlotConfig = def
8987
, pcOwnPubKeyHash = "0f45aaf1b2959db6e5ff94dbb1f823bf257680c3c723ac2d49f97546"
9088
, pcOwnStakePubKeyHash = Nothing
9189
, -- Directory name of the script and data files

examples/ex-units/src/PlutipRun.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ mkPabConf pparams pparamsFile clusterDir ownPkh =
7474
, pcPort = 9080
7575
, pcProtocolParams = pparams
7676
, pcTipPollingInterval = 1_000_000
77-
, pcSlotConfig = def {scSlotZeroTime = POSIXTime $ 1652956123 * 1000}
77+
-- , pcSlotConfig = def {scSlotZeroTime = POSIXTime $ 1652956123 * 1000}
7878
, pcOwnPubKeyHash = ownPkh
7979
, pcOwnStakePubKeyHash = Nothing
8080
, pcScriptFileDir = Text.pack $ clusterDir </> "bot-plutus-interface/scripts"

examples/ex-units/src/TestnetRun.hs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@ import Cardano.Api.Shelley (ProtocolParameters)
77
import Control.Concurrent.STM (newTVarIO, readTVarIO)
88
import Data.Aeson (decodeFileStrict, (.=))
99
import Data.Aeson qualified as JSON
10-
import Data.Default (def)
1110
import Data.Text (Text)
1211
import Data.Text qualified as Text
1312
import Data.UUID.V4 qualified as UUID
1413
import Ledger (PubKeyHash)
1514
import TimeDebugContract qualified
1615

17-
-- import LockSpendSingle (lockThenSpendSingle)
18-
1916
import GHC.IO.Encoding
2017
import Plutus.PAB.Core.ContractInstance.STM (Activity (Active))
2118
import Servant.Client (BaseUrl (BaseUrl), Scheme (Http))
@@ -75,7 +72,6 @@ mkPabConf pparams pparamsFile bpiDir ownPkh =
7572
, pcPort = 9080
7673
, pcProtocolParams = pparams
7774
, pcTipPollingInterval = 1_000_000
78-
, pcSlotConfig = def
7975
, pcOwnPubKeyHash = ownPkh
8076
, pcOwnStakePubKeyHash = Nothing
8177
, pcScriptFileDir = Text.pack $ bpiDir </> "scripts"

examples/plutus-game/app/Main.hs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import Cardano.PlutusExample.Game (
2222
import Data.Aeson qualified as JSON
2323
import Data.Aeson.TH (defaultOptions, deriveJSON)
2424
import Data.ByteString.Lazy qualified as LazyByteString
25-
import Data.Default (def)
2625
import Data.Maybe (fromMaybe)
2726
import Playground.Types (FunctionSchema)
2827
import Schema (FormSchema)
@@ -59,7 +58,6 @@ main = do
5958
, pcPort = 9080
6059
, pcProtocolParams = protocolParams
6160
, pcTipPollingInterval = 10_000_000
62-
, pcSlotConfig = def
6361
, pcOwnPubKeyHash = "0f45aaf1b2959db6e5ff94dbb1f823bf257680c3c723ac2d49f97546"
6462
, pcOwnStakePubKeyHash = Nothing
6563
, pcScriptFileDir = "./scripts"

examples/plutus-nft/app/Main.hs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import Cardano.PlutusExample.NFT
1717
import Data.Aeson qualified as JSON
1818
import Data.Aeson.TH (defaultOptions, deriveJSON)
1919
import Data.ByteString.Lazy qualified as LazyByteString
20-
import Data.Default (def)
2120
import Data.Maybe (fromMaybe)
2221
import Playground.Types (FunctionSchema)
2322
import Schema (FormSchema)
@@ -55,7 +54,6 @@ main = do
5554
, pcPort = 9080
5655
, pcProtocolParams = protocolParams
5756
, pcTipPollingInterval = 10_000_000
58-
, pcSlotConfig = def
5957
, pcOwnPubKeyHash = "0f45aaf1b2959db6e5ff94dbb1f823bf257680c3c723ac2d49f97546"
6058
, pcOwnStakePubKeyHash = Nothing
6159
, pcScriptFileDir = "./scripts"

examples/plutus-transfer/app/Main.hs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import Cardano.PlutusExample.Transfer (
2121
import Data.Aeson qualified as JSON
2222
import Data.Aeson.TH (defaultOptions, deriveJSON)
2323
import Data.ByteString.Lazy qualified as LazyByteString
24-
import Data.Default (def)
2524
import Data.Maybe (fromMaybe)
2625
import Playground.Types (FunctionSchema)
2726
import Schema (FormSchema)
@@ -58,7 +57,6 @@ main = do
5857
, pcPort = 9080
5958
, pcProtocolParams = protocolParams
6059
, pcTipPollingInterval = 10_000_000
61-
, pcSlotConfig = def
6260
, pcOwnPubKeyHash = "0f45aaf1b2959db6e5ff94dbb1f823bf257680c3c723ac2d49f97546"
6361
, pcOwnStakePubKeyHash = Nothing
6462
, pcScriptFileDir = "./scripts"

src/BotPlutusInterface/Balance.hs

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ import Ledger.Interval (
4242
)
4343
import Ledger.Scripts (Datum, DatumHash)
4444
import Ledger.Time (POSIXTimeRange)
45-
import Ledger.TimeSlot (posixTimeRangeToContainedSlotRange)
4645
import Ledger.Tx (
4746
Tx (..),
4847
TxIn (..),
@@ -60,8 +59,8 @@ import Plutus.V1.Ledger.Api (
6059
)
6160

6261
import BotPlutusInterface.BodyBuilder qualified as BodyBuilder
63-
import Debug.Trace (traceM, traceShowId)
6462
import Prelude
63+
import Data.Bifunctor (bimap)
6564

6665
{- | Collect necessary tx inputs and collaterals, add minimum lovelace values and balance non ada
6766
assets
@@ -89,15 +88,14 @@ balanceTxIO pabConf ownPkh unbalancedTx =
8988

9089
tx <-
9190
newEitherT $
92-
addValidRange2 @w
93-
pabConf
91+
addValidRange @w
9492
(unBalancedTxValidityTimeRange unbalancedTx)
9593
(unBalancedTxTx unbalancedTx)
9694

9795
lift $ printLog @w Debug $ show utxoIndex
9896

9997
-- We need this folder on the CLI machine, which may not be the local machine
100-
lift $ createDirectoryIfMissingCLI @w False (Text.unpack pabConf.pcTxFileDir)
98+
lift $ createDirectoryIfMissingCLI @w False (Text.unpack "pcTxFileDir" )
10199

102100
-- Adds required collaterals, only needs to happen once
103101
-- Also adds signatures for fee calculation
@@ -359,34 +357,22 @@ addSignatories ownPkh privKeys pkhs tx =
359357
tx
360358
(ownPkh : pkhs)
361359

362-
addValidRange :: PABConfig -> POSIXTimeRange -> Tx -> Either Text Tx
363-
addValidRange pabConf timeRange tx =
364-
if validateRange timeRange
365-
then
366-
let r = traceShowId (posixTimeRangeToContainedSlotRange pabConf.pcSlotConfig timeRange)
367-
in Right $ tx {txValidRange = r}
368-
else Left "Invalid validity interval."
369-
370-
addValidRange2 ::
360+
addValidRange ::
371361
forall (w :: Type) (effs :: [Type -> Type]).
372362
Member (PABEffect w) effs =>
373-
PABConfig ->
374363
POSIXTimeRange ->
375364
Tx ->
376365
Eff effs (Either Text Tx)
377-
addValidRange2 pabConf timeRange tx =
366+
addValidRange timeRange tx =
378367
if validateRange timeRange
379-
then do
380-
let oldWaySlotRange = posixTimeRangeToContainedSlotRange pabConf.pcSlotConfig timeRange
381-
traceM $ "Ledger SlotRange: " ++ show oldWaySlotRange
382-
newWaySlotRange <- convertTimeRangeToSlotRange @w timeRange
383-
case newWaySlotRange of
384-
Right range -> do
385-
traceM $ "Query SlotRange: " ++ show range
386-
pure $ Right $ tx {txValidRange = range}
387-
Left err -> pure $ Left (Text.pack $ show err)
368+
then bimap (Text.pack . show) (setRange tx) <$>
369+
convertTimeRangeToSlotRange @w timeRange
388370
else pure $ Left "Invalid validity interval."
389371

372+
where
373+
setRange tx' range = tx' {txValidRange = range}
374+
375+
390376
validateRange :: forall (a :: Type). Ord a => Interval a -> Bool
391377
validateRange (Interval (LowerBound PosInf _) _) = False
392378
validateRange (Interval _ (UpperBound NegInf _)) = False

src/BotPlutusInterface/Contract.hs

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import BotPlutusInterface.Effects (
2626
)
2727
import BotPlutusInterface.Files (DummyPrivKey (FromSKey, FromVKey))
2828
import BotPlutusInterface.Files qualified as Files
29-
import BotPlutusInterface.TimeSlot (ToWhichSlotTime (ToEndTime), stackExchangeConvert)
3029
import BotPlutusInterface.Types (
3130
ContractEnvironment (..),
3231
LogLevel (Debug, Warn),
@@ -51,13 +50,11 @@ import Data.Map qualified as Map
5150
import Data.Row (Row)
5251
import Data.Text (Text)
5352
import Data.Text qualified as Text
54-
import Debug.Trace (traceM)
5553
import Ledger (POSIXTime)
5654
import Ledger qualified
5755
import Ledger.Address (PaymentPubKeyHash (PaymentPubKeyHash))
5856
import Ledger.Constraints.OffChain (UnbalancedTx (..))
5957
import Ledger.Slot (Slot (Slot))
60-
import Ledger.TimeSlot (SlotConversionError, posixTimeToEnclosingSlot, slotToEndPOSIXTime)
6158
import Ledger.Tx (CardanoTx)
6259
import Ledger.Tx qualified as Tx
6360
import Plutus.ChainIndex.TxIdState (fromTx, transactionStatus)
@@ -165,13 +162,8 @@ handlePABReq contractEnv req = do
165162
CurrentSlotReq -> CurrentSlotResp <$> currentSlot @w contractEnv
166163
CurrentTimeReq -> CurrentTimeResp <$> currentTime @w contractEnv
167164
PosixTimeRangeToContainedSlotRangeReq posixTimeRange ->
168-
PosixTimeRangeToContainedSlotRangeResp
169-
<$> posixTimeRangeToContainedSlotRange_ @w posixTimeRange
170-
-- PosixTimeRangeToContainedSlotRangeReq posixTimeRange ->
171-
-- pure $
172-
-- PosixTimeRangeToContainedSlotRangeResp $
173-
-- Right $
174-
-- posixTimeRangeToContainedSlotRange contractEnv.cePABConfig.pcSlotConfig posixTimeRange
165+
either (error . show) (PosixTimeRangeToContainedSlotRangeResp . Right)
166+
<$> convertTimeRangeToSlotRange @w posixTimeRange
175167
AwaitTxStatusChangeReq txId -> AwaitTxStatusChangeResp txId <$> awaitTxStatusChange @w contractEnv txId
176168
------------------------
177169
-- Unhandled requests --
@@ -362,27 +354,11 @@ awaitTime ::
362354
POSIXTime ->
363355
Eff effs POSIXTime
364356
awaitTime ce pTime = do
365-
startingTip <- CardanoCLI.queryTip @w ce.cePABConfig
366-
traceM $ "Starting tip: " ++ show startingTip
367-
368-
slotFromTime <- posixTimeToSlot @w pTime >>= either (error . show) return
369-
traceM $ "Converting " ++ show pTime ++ " to slot:"
370-
traceM $ "- with node queries: " ++ show slotFromTime
371-
traceM $ "- with stack exchng: " ++ show (stackExchangeConvert pTime)
372-
traceM $ "- with ledger stuff: " ++ show oldSlotFromTime
357+
slotFromTime <- rightOrErr <$> posixTimeToSlot @w pTime
373358
slot' <- awaitSlot ce slotFromTime
374-
traceM $ "Converting " ++ show slot' ++ " to time:"
375-
ethTime <- slotToPOSIXTime @w ToEndTime slot'
376-
time' <- either (error . show) return ethTime
377-
traceM $ "- with node queries: " ++ show time'
378-
traceM $ "- with ledger stuff: " ++ show (oldTimeFromSlot slot')
379-
380-
endTip <- CardanoCLI.queryTip @w ce.cePABConfig
381-
traceM $ "Current tip: " ++ show endTip
382-
return time'
359+
rightOrErr <$> slotToPOSIXTime @w slot'
383360
where
384-
oldSlotFromTime = posixTimeToEnclosingSlot ce.cePABConfig.pcSlotConfig pTime
385-
oldTimeFromSlot = slotToEndPOSIXTime ce.cePABConfig.pcSlotConfig
361+
rightOrErr = either (error . show) id
386362

387363
currentSlot ::
388364
forall (w :: Type) (effs :: [Type -> Type]).
@@ -407,14 +383,5 @@ currentTime ::
407383
Eff effs POSIXTime
408384
currentTime contractEnv =
409385
currentSlot @w contractEnv
410-
>>= slotToPOSIXTime @w ToEndTime
386+
>>= slotToPOSIXTime @w
411387
>>= either (error . show) return
412-
413-
posixTimeRangeToContainedSlotRange_ ::
414-
forall (w :: Type) (effs :: [Type -> Type]).
415-
Member (PABEffect w) effs =>
416-
Ledger.POSIXTimeRange ->
417-
Eff effs (Either SlotConversionError Ledger.SlotRange)
418-
posixTimeRangeToContainedSlotRange_ posixTimeRange =
419-
convertTimeRangeToSlotRange @w posixTimeRange
420-
>>= either (error . show) (return . Right)

src/BotPlutusInterface/Effects.hs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ import Data.Text qualified as Text
5959
import Ledger qualified
6060
import Plutus.Contract.Effects (ChainIndexQuery, ChainIndexResponse)
6161
import Plutus.PAB.Core.ContractInstance.STM (Activity)
62-
import PlutusTx.Builtins.Internal (BuiltinByteString (BuiltinByteString), error)
62+
import PlutusTx.Builtins.Internal (BuiltinByteString (BuiltinByteString))
6363
import System.Directory qualified as Directory
6464
import System.Exit (ExitCode (ExitFailure, ExitSuccess))
6565
import System.Process (readProcess, readProcessWithExitCode)
@@ -102,7 +102,6 @@ data PABEffect (w :: Type) (r :: Type) where
102102
EstimateBudget :: TxFile -> PABEffect w (Either BudgetEstimationError TxBudget)
103103
SaveBudget :: Ledger.TxId -> TxBudget -> PABEffect w ()
104104
SlotToPOSIXTime ::
105-
TimeSlot.ToWhichSlotTime ->
106105
Ledger.Slot ->
107106
PABEffect w (Either TimeSlot.TimeSlotConversionError Ledger.POSIXTime)
108107
POSIXTimeToSlot :: Ledger.POSIXTime -> PABEffect w (Either TimeSlot.TimeSlotConversionError Ledger.Slot)
@@ -158,8 +157,8 @@ handlePABEffect contractEnv =
158157
EstimateBudget txPath ->
159158
ExBudget.estimateBudget contractEnv.cePABConfig txPath
160159
SaveBudget txId exBudget -> saveBudgetImpl contractEnv txId exBudget
161-
SlotToPOSIXTime toWhichTime slot ->
162-
TimeSlot.slotToPOSIXTimeImpl contractEnv.cePABConfig toWhichTime slot
160+
SlotToPOSIXTime slot ->
161+
TimeSlot.slotToPOSIXTimeImpl contractEnv.cePABConfig slot
163162
POSIXTimeToSlot pTime ->
164163
TimeSlot.posixTimeToSlotImpl contractEnv.cePABConfig pTime
165164
POSIXTimeRangeToSlotRange pTimeRange ->
@@ -324,10 +323,9 @@ saveBudget txId budget = send @(PABEffect w) $ SaveBudget txId budget
324323
slotToPOSIXTime ::
325324
forall (w :: Type) (effs :: [Type -> Type]).
326325
Member (PABEffect w) effs =>
327-
TimeSlot.ToWhichSlotTime ->
328326
Ledger.Slot ->
329327
Eff effs (Either TimeSlot.TimeSlotConversionError Ledger.POSIXTime)
330-
slotToPOSIXTime tw s = send @(PABEffect w) (SlotToPOSIXTime tw s)
328+
slotToPOSIXTime = send @(PABEffect w) . SlotToPOSIXTime
331329

332330
posixTimeToSlot ::
333331
forall (w :: Type) (effs :: [Type -> Type]).

0 commit comments

Comments
 (0)