Im using the time module in the ledger class to do some custom validation logic, but everything is done as intervals and I cant find the function that gets the current time. What is it?

1 Answer 1


each transaction on Cardano has a ttl (Time To Live) determined in the moment of transaction creation, it can't be submitted outside that interval, and even if the transaction is submited, other node would not reconize it

on cardano-cli the ttl defaults to ~2/~3 hours after the tx-body creation

and this information is reported in the ScriptContext from the Plutus.V1.Ledger.Contexts module, in particular yo need the TxInfos and from then the POSIXTimeRange you get form the txInfoValidRange function.

so starting from the ScriptContext your function would be somehting like:

getTTL :: ScritpContext -> POSIXTimeRange
getTTL = txInfoValidRange . scriptContextTxInfo

If you need to express logic valid before a specific time you should check for the upper limit of the interval to be before that timestamp since POSIXTimeRange is just an alias for Interval POSIXTime

type POSIXTimeRange = Interval POSIXTime

you can check the Interval documentation for more

but you can get the upper limit of the interval like so

extractFiniteUpper :: Interval a -> Maybe a
extractFiniteUpper interval = case ivTo interval of
    UpperBound (Finite value) _isInclusive -> Just value
    _ -> Nothing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.