Plutus : Slot and Time Ranges
Here is the updated tutorial with:
โ Numbered sections
โ A clean Table of Contents
โ A clear Glossary at the end
โ All links updated to the Intersect Plutus Apps Haddock
๐ Plutus : Slot and Time Ranges
๐ Table of Contents
Introduction
Slot vs POSIXTime
POSIXTimeRange and Interval
ScriptContext Time Checks
Off-Chain Slot Conversions
Full Example: Deadline Validator
Glossary
References
1. ๐งญ Introduction
Time-sensitive logic is common in Plutus smart contracts. You often need to:
Enforce deadlines
Allow scheduled unlocks
Handle auction close times
Plutus uses POSIXTime
and Interval POSIXTime
(a.k.a. POSIXTimeRange
) to model time. You do not check Slot
directly on-chainโit's for off-chain conversion.
2. ๐งฎ Slot vs POSIXTime
Slot
Blockchain slot number (SlotNo
)
Off-chain
POSIXTime
Unix time in milliseconds
On-chain
To use real time on-chain, you write Plutus validators in terms of POSIXTime
. Slot-related logic is done off-chain.
๐ Off-chain conversion: Ledger.TimeSlot
slotToBeginPOSIXTime :: Slot -> POSIXTime
3. ๐ POSIXTimeRange and Interval
๐ Definition
On-chain time constraints are represented as:
type POSIXTimeRange = Interval POSIXTime
This lets you express โwhenโ a transaction is valid.
๐ Docs:
๐ Common Constructors
always
Infinite range (no bounds)
from t
Time โฅ t
to t
Time โค t
interval a b
Between a
and b
inclusive
๐งช Example
deadline :: POSIXTime
deadline = 1660000000
validRange = to deadline -- Valid before or at the deadline
4. ๐งพ ScriptContext Time Checks
The transactionโs valid range is pulled from the context:
txInfoValidRange :: TxInfo -> POSIXTimeRange
scriptContextTxInfo :: ScriptContext -> TxInfo
โ
Check if TX is before deadline:
{-# INLINABLE validateBeforeDeadline #-}
validateBeforeDeadline :: POSIXTime -> () -> ScriptContext -> Bool
validateBeforeDeadline deadline _ ctx =
contains (to deadline) (txInfoValidRange $ scriptContextTxInfo ctx)
5. ๐ Off-Chain Slot Conversions
To convert between slot numbers and real time (POSIX), use:
slotToPOSIXTime :: Slot -> POSIXTime
slotToBeginPOSIXTime :: Slot -> POSIXTime
slotToEndPOSIXTime :: Slot -> POSIXTime
This is useful when:
You know the deadline in slots
You're building the transaction off-chain
๐ Module: Ledger.TimeSlot
6. โ
Full Example: Validator with Deadline
{-# INLINABLE mkValidator #-}
mkValidator :: POSIXTime -> () -> ScriptContext -> Bool
mkValidator deadline _ ctx =
traceIfFalse "deadline passed" withinRange
where
info = scriptContextTxInfo ctx
withinRange = contains (to deadline) (txInfoValidRange info)
๐ This validator:
Accepts a
POSIXTime
as deadlineFails if the transaction is after that time
7. ๐ Glossary
Slot
A unit of blockchain time (e.g., one block every 1 sec)
POSIXTime
Unix time in milliseconds (standard time format)
Interval a
A range with lower and upper bounds of type a
POSIXTimeRange
A type alias: Interval POSIXTime
ScriptContext
All on-chain data available to the validator
TxInfo
Contains TX metadata, inputs, outputs, range, signers, etc.
txInfoValidRange
Time range the transaction declares itself valid for
contains
Checks whether one interval is completely within another
8. ๐ References
Last updated