How to compare POSIXTime to POSIXTimeRange? #69
-
I am implementing the vesting contract in plutus-pioneers-program, and I need to compare the posix time provided in the datum is within the valid range specified in the script context. I have some skeleton code below: from eopsin.prelude import *
@dataclass()
class VestingParams(PlutusData):
beneficiary: PubKeyHash
deadline: POSIXTime
def signed_by_beneficiary(datum: VestingParams, context: ScriptContext) -> bool:
return datum.beneficiary in context.tx_info.signatories
def deadline_reached(datum: VestingParams, context: ScriptContext) -> bool:
deadline: POSIXTime = datum.deadline
valid_range: POSIXTimeRange = context.tx_info.valid_range
# comparison logic here
return False
def validator(datum: VestingParams, redeemer: None, context: ScriptContext) -> bool:
assert signed_by_beneficiary(datum, context), "beneficiary's signature missing"
assert deadline_reached(datum, context), "deadline not reached"
return True Looking at the prelude, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Okay I found that So I made an unoptimized implementation of the contains comparison: def contains(time: POSIXTime, time_range: POSIXTimeRange) -> bool:
# Lower bound
lower_comparison = False
lower_bound_limit = time_range.lower_bound.limit
lower_bound_closed = time_range.lower_bound.closed
if isinstance(lower_bound_limit, FinitePOSIXTime):
lower_time = lower_bound_limit.time
if isinstance(lower_bound_closed, TrueData):
lower_comparison = lower_time <= time
else:
lower_comparison = lower_time < time
if isinstance(lower_bound_limit, NegInfPOSIXTime):
lower_comparison = True
# Upper bound
upper_comparison = False
upper_bound_limit = time_range.upper_bound.limit
upper_bound_closed = time_range.upper_bound.closed
if isinstance(upper_bound_limit, FinitePOSIXTime):
upper_time = upper_bound_limit.time
if isinstance(upper_bound_closed, TrueData):
upper_comparison = time <= upper_time
else:
upper_comparison = time < upper_time
if isinstance(upper_bound_limit, PosInfPOSIXTime):
upper_comparison = True
return lower_comparison and upper_comparison |
Beta Was this translation helpful? Give feedback.
Okay I found that
isinstance
is implemented in the condition ofif isinstance(data, Union)
where data is a Union typehttps://github.com/ImperatorLang/eopsin/blob/cee2aa9bb2910732f283ff9ac69e870a94024770/eopsin/type_inference.py#L222-L262
So I made an unoptimized implementation of the contains comparison: