forked from OpShin/opshin-pioneer-program
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vesting.py
32 lines (22 loc) · 1.32 KB
/
vesting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from opshin.ledger.interval import *
@dataclass()
class VestingParams(PlutusData):
beneficiary: PubKeyHash
deadline: POSIXTime
def signed_by_beneficiary(params: VestingParams, context: ScriptContext) -> bool:
return params.beneficiary in context.tx_info.signatories
def is_after(deadline: POSIXTime, valid_range: POSIXTimeRange) -> bool:
# To ensure that the `valid_range` occurs after the `deadline`,
# we construct an interval from `deadline` to infinity
# then check whether that interval contains the `valid_range` interval.
from_interval: POSIXTimeRange = make_from(deadline)
return contains(from_interval, valid_range)
def deadline_reached(params: VestingParams, context: ScriptContext) -> bool:
# The current transaction can only execute in `valid_range`,
# so the current execution time is always within `valid_range`.
# Therefore, to make all possible execution times occur after the deadline,
# we need to make sure the whole `valid_range` interval occurs after the `deadline`.
return is_after(params.deadline, context.tx_info.valid_range)
def validator(datum: VestingParams, redeemer: None, context: ScriptContext) -> None:
assert signed_by_beneficiary(datum, context), "beneficiary's signature missing"
assert deadline_reached(datum, context), "deadline not reached"