type.gno
5.15 Kb ยท 153 lines
1package v1
2
3import (
4 "gno.land/p/nt/avl"
5 sr "gno.land/r/gnoswap/staker"
6)
7
8type ExternalIncentiveResolver struct {
9 *sr.ExternalIncentive
10}
11
12// isActive checks if the incentive is currently active at the given timestamp
13func (self *ExternalIncentiveResolver) isActive(currentTimestamp int64) bool {
14 return currentTimestamp >= self.StartTimestamp() && currentTimestamp <= self.EndTimestamp()
15}
16
17func (self *ExternalIncentiveResolver) IsEnded(currentTimestamp int64) bool {
18 return currentTimestamp > self.EndTimestamp()
19}
20
21func (self *ExternalIncentiveResolver) IsStarted(currentTimestamp int64) bool {
22 return currentTimestamp >= self.StartTimestamp()
23}
24
25func (self *ExternalIncentiveResolver) RewardSpent(currentTimestamp int64) int64 {
26 // Check timestamps for state validation
27 if currentTimestamp < self.StartTimestamp() {
28 return 0
29 }
30
31 if currentTimestamp > self.EndTimestamp() {
32 return self.RewardAmount()
33 }
34
35 timeDuration := safeSubInt64(currentTimestamp, self.StartTimestamp())
36 rewardSpent := safeMulInt64(timeDuration, self.RewardPerSecond())
37 return rewardSpent
38}
39
40// addDistributedRewardAmount adds the given amount to the distributed reward amount.
41// This function is used to add the distributed reward amount when the incentive is un-staked and refunded.
42func (self *ExternalIncentiveResolver) addDistributedRewardAmount(amount int64) {
43 distributedRewardAmount := safeAddInt64(self.DistributedRewardAmount(), amount)
44 self.SetDistributedRewardAmount(distributedRewardAmount)
45}
46
47func (self *ExternalIncentiveResolver) Clone() *ExternalIncentiveResolver {
48 return &ExternalIncentiveResolver{
49 ExternalIncentive: self.ExternalIncentive.Clone(),
50 }
51}
52
53// NewExternalIncentive creates a new external incentive
54func NewExternalIncentiveResolver(
55 externalIncentive *sr.ExternalIncentive,
56) *ExternalIncentiveResolver {
57 return &ExternalIncentiveResolver{
58 ExternalIncentive: externalIncentive,
59 }
60}
61
62type DepositResolver struct {
63 *sr.Deposit
64}
65
66// InternalRewardLastCollectTime returns the last collect time for the internal reward.
67// If the last collect time is 0, it returns the staked time.
68func (self *DepositResolver) InternalRewardLastCollectTime() int64 {
69 if self.Deposit.InternalRewardLastCollectTime() == 0 {
70 return self.Deposit.StakeTime()
71 }
72
73 return self.Deposit.InternalRewardLastCollectTime()
74}
75
76// ExternalRewardLastCollectTime returns the last collect time for the external reward for the given incentive ID.
77// If the last collect time is 0, it returns the staked time.
78func (self *DepositResolver) ExternalRewardLastCollectTime(incentiveID string) int64 {
79 lastCollectTime, exists := self.Deposit.GetExternalRewardLastCollectTime(incentiveID)
80 if !exists || lastCollectTime == 0 {
81 return self.Deposit.StakeTime()
82 }
83
84 return lastCollectTime
85}
86
87func (self *DepositResolver) CollectedExternalReward(incentiveID string) int64 {
88 collectedExternalReward, exists := self.Deposit.GetCollectedExternalReward(incentiveID)
89 if !exists {
90 return 0
91 }
92
93 return collectedExternalReward
94}
95
96func (self *DepositResolver) addCollectedInternalReward(reward int64) {
97 self.SetCollectedInternalReward(safeAddInt64(self.CollectedInternalReward(), reward))
98}
99
100func (self *DepositResolver) addCollectedExternalReward(incentiveID string, reward int64) {
101 collectedExternalReward := safeAddInt64(self.CollectedExternalReward(incentiveID), reward)
102 self.SetCollectedExternalReward(incentiveID, collectedExternalReward)
103}
104
105// updateInternalRewardLastCollectTime updates the last collect time for the internal reward.
106// It returns an error if the current time is less than the last collect time for the internal reward.
107func (self *DepositResolver) updateInternalRewardLastCollectTime(currentTime int64) error {
108 if self.InternalRewardLastCollectTime() > currentTime {
109 return makeErrorWithDetails(errNotAvailableUpdateCollectTime, "currentTime must be greater than internal reward last collect time")
110 }
111
112 self.SetInternalRewardLastCollectTime(currentTime)
113
114 return nil
115}
116
117// updateExternalRewardLastCollectTime lazily updates the last collect time for the external reward for the given incentive ID.
118// It returns an error if the current time is less than the last collect time for the external reward for the given incentive ID.
119func (self *DepositResolver) updateExternalRewardLastCollectTime(incentiveID string, currentTime int64) error {
120 if self.ExternalRewardLastCollectTimes() == nil {
121 self.SetExternalRewardLastCollectTimes(avl.NewTree())
122 }
123
124 externalLastCollectTime, exists := self.Deposit.GetExternalRewardLastCollectTime(incentiveID)
125 if exists && externalLastCollectTime > currentTime {
126 return makeErrorWithDetails(errNotAvailableUpdateCollectTime, "currentTime must be greater than external reward last collect time")
127 }
128
129 self.Deposit.SetExternalRewardLastCollectTime(incentiveID, currentTime)
130
131 return nil
132}
133
134func (self *DepositResolver) FindWarmup(currentTime int64) int {
135 for i, warmup := range self.Warmups() {
136 if currentTime < warmup.NextWarmupTime {
137 return i
138 }
139 }
140 return len(self.Warmups()) - 1
141}
142
143func (self *DepositResolver) GetWarmup(index int) sr.Warmup {
144 return self.Warmups()[index]
145}
146
147func NewDepositResolver(
148 deposit *sr.Deposit,
149) *DepositResolver {
150 return &DepositResolver{
151 Deposit: deposit,
152 }
153}