type.gno
5.00 Kb · 139 lines
1package staker
2
3import (
4 "gno.land/p/gnoswap/gnsmath"
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
25// addDistributedRewardAmount adds the given amount to the distributed reward amount.
26// This function is used to add the distributed reward amount when the incentive is un-staked and refunded.
27func (self *ExternalIncentiveResolver) addDistributedRewardAmount(amount int64) {
28 distributedRewardAmount := gnsmath.SafeAddInt64(self.DistributedRewardAmount(), amount)
29 self.SetDistributedRewardAmount(distributedRewardAmount)
30}
31
32// addAccumulatedPenaltyAmount adds the given amount to the accumulated penalty amount.
33// Called during CollectReward to track warmup penalties for later collection.
34func (self *ExternalIncentiveResolver) addAccumulatedPenaltyAmount(amount int64) {
35 accumulatedPenaltyAmount := gnsmath.SafeAddInt64(self.AccumulatedPenaltyAmount(), amount)
36 self.SetAccumulatedPenaltyAmount(accumulatedPenaltyAmount)
37}
38
39// NewExternalIncentive creates a new external incentive
40func NewExternalIncentiveResolver(
41 externalIncentive *sr.ExternalIncentive,
42) *ExternalIncentiveResolver {
43 return &ExternalIncentiveResolver{
44 ExternalIncentive: externalIncentive,
45 }
46}
47
48type DepositResolver struct {
49 *sr.Deposit
50}
51
52// InternalRewardLastCollectTime returns the last collect time for the internal reward.
53// If the last collect time is 0, it returns the staked time.
54func (self *DepositResolver) InternalRewardLastCollectTime() int64 {
55 if self.Deposit.InternalRewardLastCollectTime() == 0 {
56 return self.Deposit.StakeTime()
57 }
58
59 return self.Deposit.InternalRewardLastCollectTime()
60}
61
62// ExternalRewardLastCollectTime returns the last collect time for the external reward for the given incentive ID.
63// If the last collect time is 0, it returns the staked time.
64func (self *DepositResolver) ExternalRewardLastCollectTime(incentiveID string) int64 {
65 lastCollectTime, exists := self.Deposit.GetExternalRewardLastCollectTime(incentiveID)
66 if !exists || lastCollectTime == 0 {
67 return self.Deposit.StakeTime()
68 }
69
70 return lastCollectTime
71}
72
73func (self *DepositResolver) CollectedExternalReward(incentiveID string) int64 {
74 collectedExternalReward, exists := self.Deposit.GetCollectedExternalReward(incentiveID)
75 if !exists {
76 return 0
77 }
78
79 return collectedExternalReward
80}
81
82func (self *DepositResolver) addCollectedInternalReward(reward int64) {
83 self.SetCollectedInternalReward(gnsmath.SafeAddInt64(self.CollectedInternalReward(), reward))
84}
85
86func (self *DepositResolver) addCollectedExternalReward(incentiveID string, reward int64) {
87 collectedExternalReward := gnsmath.SafeAddInt64(self.CollectedExternalReward(incentiveID), reward)
88 self.SetCollectedExternalReward(incentiveID, collectedExternalReward)
89}
90
91// updateInternalRewardLastCollectTime updates the last collect time for the internal reward.
92// It returns an error if the current time is less than the last collect time for the internal reward.
93func (self *DepositResolver) updateInternalRewardLastCollectTime(currentTime int64) error {
94 if self.InternalRewardLastCollectTime() > currentTime {
95 return makeErrorWithDetails(errNotAvailableUpdateCollectTime, "currentTime must be greater than internal reward last collect time")
96 }
97
98 self.SetInternalRewardLastCollectTime(currentTime)
99
100 return nil
101}
102
103// updateExternalRewardLastCollectTime lazily updates the last collect time for the external reward for the given incentive ID.
104// It returns an error if the current time is less than the last collect time for the external reward for the given incentive ID.
105func (self *DepositResolver) updateExternalRewardLastCollectTime(incentiveID string, currentTime int64) error {
106 if self.ExternalRewardLastCollectTimes() == nil {
107 self.SetExternalRewardLastCollectTimes(make(map[string]int64))
108 }
109
110 externalLastCollectTime, exists := self.Deposit.GetExternalRewardLastCollectTime(incentiveID)
111 if exists && externalLastCollectTime > currentTime {
112 return makeErrorWithDetails(errNotAvailableUpdateCollectTime, "currentTime must be greater than external reward last collect time")
113 }
114
115 self.Deposit.SetExternalRewardLastCollectTime(incentiveID, currentTime)
116
117 return nil
118}
119
120func (self *DepositResolver) FindWarmup(currentTime int64) int {
121 for i, warmup := range self.Warmups() {
122 if currentTime < warmup.NextWarmupTime {
123 return i
124 }
125 }
126 return len(self.Warmups()) - 1
127}
128
129func (self *DepositResolver) GetWarmup(index int) sr.Warmup {
130 return self.Warmups()[index]
131}
132
133func NewDepositResolver(
134 deposit *sr.Deposit,
135) *DepositResolver {
136 return &DepositResolver{
137 Deposit: deposit,
138 }
139}