delay.gno
1.98 Kb · 46 lines
1package manager
2
3// Delay is a value that changes only after a setback, mirroring
4// OpenZeppelin's packed `Delay` type and Union's Delay struct.
5// OpenZeppelin: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/5fd1781b1454fd1ef8e722282f86f9293cacf256/contracts/utils/types/Time.sol#L61
6// Union: https://github.com/unionlabs/union/blob/8cff0ff34f6baa4cdb1e4650a08985dd05de0c5a/lib/access-manager-types/src/time.rs#L16-L20
7type Delay struct {
8 effectDate TimePoint
9 valueBefore uint32
10 valueAfter uint32
11}
12
13func NewDelay(delay uint32) Delay {
14 return Delay{effectDate: 0, valueBefore: 0, valueAfter: delay}
15}
16
17// Get returns the value active at timestamp.
18// OpenZeppelin: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/5fd1781b1454fd1ef8e722282f86f9293cacf256/contracts/utils/types/Time.sol#L86-L96
19// Union: https://github.com/unionlabs/union/blob/8cff0ff34f6baa4cdb1e4650a08985dd05de0c5a/lib/access-manager-types/src/time.rs#L89-L103
20func (d Delay) Get(timestamp TimePoint) uint32 {
21 if d.effectDate.After(timestamp) {
22 return d.valueBefore
23 }
24
25 return d.valueAfter
26}
27
28// WithUpdate schedules newDelay to take effect at timestamp plus a setback:
29// at least minSetback, or longer when newDelay decreases the active value
30// by more than minSetback. Returns the updated Delay and the effect timePoint.
31// OpenZeppelin: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/5fd1781b1454fd1ef8e722282f86f9293cacf256/contracts/utils/types/Time.sol#L103-L112
32// Union: https://github.com/unionlabs/union/blob/8cff0ff34f6baa4cdb1e4650a08985dd05de0c5a/lib/access-manager-types/src/time.rs#L119-L131
33func (d Delay) WithUpdate(timestamp TimePoint, newDelay uint32, minSetback uint32) (Delay, TimePoint) {
34 value := d.Get(timestamp)
35
36 setback := minSetback
37 if value > newDelay {
38 if diff := value - newDelay; diff > setback {
39 setback = diff
40 }
41 }
42
43 effect := NewTimePoint(timestamp.Int64() + int64(setback))
44
45 return Delay{effectDate: effect, valueBefore: value, valueAfter: newDelay}, effect
46}