deposit.gno
9.83 Kb · 342 lines
1package staker
2
3import (
4 "math"
5
6 u256 "gno.land/p/gnoswap/uint256"
7)
8
9type Deposit struct {
10 warmups []Warmup // warmup information
11 liquidity *u256.Uint // liquidity
12 targetPoolPath string // staked position's pool path
13 owner address // owner address
14 stakeTime int64 // staked time
15 internalRewardLastCollectTime int64 // last collect time for internal reward
16 collectedInternalReward int64 // collected internal reward
17 collectedExternalRewards map[string]int64 // collected external reward by incentive id (incentiveID -> int64)
18 externalRewardLastCollectTimes map[string]int64 // last collect time for external rewards by incentive id (incentiveID -> int64)
19 externalIncentiveIds map[string]bool // external incentive ids for this deposit (incentiveID -> bool)
20 lastExternalIncentiveUpdatedAt int64 // last time when external incentive ids were synced
21 tickLower int32 // tick lower
22 tickUpper int32 // tick upper
23}
24
25func (d *Deposit) Owner() address {
26 return d.owner
27}
28
29func (d *Deposit) SetOwner(owner address) {
30 d.owner = owner
31}
32
33func (d *Deposit) TargetPoolPath() string {
34 return d.targetPoolPath
35}
36
37func (d *Deposit) SetTargetPoolPath(targetPoolPath string) {
38 d.targetPoolPath = targetPoolPath
39}
40
41func (d *Deposit) Liquidity() *u256.Uint {
42 return d.liquidity
43}
44
45func (d *Deposit) SetLiquidity(liquidity *u256.Uint) {
46 d.liquidity = u256.Zero().Set(liquidity)
47}
48
49func (d *Deposit) StakeTime() int64 {
50 return d.stakeTime
51}
52
53func (d *Deposit) SetStakeTime(stakeTime int64) {
54 d.stakeTime = stakeTime
55}
56
57func (d *Deposit) InternalRewardLastCollectTime() int64 {
58 return d.internalRewardLastCollectTime
59}
60
61func (d *Deposit) SetInternalRewardLastCollectTime(internalRewardLastCollectTime int64) {
62 d.internalRewardLastCollectTime = internalRewardLastCollectTime
63}
64
65func (d *Deposit) CollectedInternalReward() int64 {
66 return d.collectedInternalReward
67}
68
69func (d *Deposit) SetCollectedInternalReward(collectedInternalReward int64) {
70 d.collectedInternalReward = collectedInternalReward
71}
72
73func (d *Deposit) CollectedExternalRewards() map[string]int64 {
74 return d.collectedExternalRewards
75}
76
77func (d *Deposit) SetCollectedExternalRewards(collectedExternalRewards map[string]int64) {
78 d.collectedExternalRewards = collectedExternalRewards
79}
80
81// GetCollectedExternalReward returns the collected external reward for the given incentive ID.
82// Returns 0 if the incentive ID does not exist.
83func (d *Deposit) GetCollectedExternalReward(incentiveID string) (int64, bool) {
84 reward, exists := d.collectedExternalRewards[incentiveID]
85 if !exists {
86 return 0, false
87 }
88
89 return reward, true
90}
91
92func (d *Deposit) SetCollectedExternalReward(incentiveID string, reward int64) {
93 if d.collectedExternalRewards == nil {
94 d.collectedExternalRewards = make(map[string]int64)
95 }
96
97 d.collectedExternalRewards[incentiveID] = reward
98}
99
100func (d *Deposit) ExternalRewardLastCollectTimes() map[string]int64 {
101 return d.externalRewardLastCollectTimes
102}
103
104func (d *Deposit) SetExternalRewardLastCollectTimes(externalRewardLastCollectTimes map[string]int64) {
105 d.externalRewardLastCollectTimes = externalRewardLastCollectTimes
106}
107
108// GetExternalRewardLastCollectTime returns the last collect time for the given incentive ID.
109// Returns 0 if the incentive ID does not exist.
110func (d *Deposit) GetExternalRewardLastCollectTime(incentiveID string) (int64, bool) {
111 time, exists := d.externalRewardLastCollectTimes[incentiveID]
112 if !exists {
113 return 0, false
114 }
115
116 return time, true
117}
118
119func (d *Deposit) SetExternalRewardLastCollectTime(incentiveID string, currentTime int64) {
120 if d.externalRewardLastCollectTimes == nil {
121 d.externalRewardLastCollectTimes = make(map[string]int64)
122 }
123
124 d.externalRewardLastCollectTimes[incentiveID] = currentTime
125}
126
127func (d *Deposit) TickLower() int32 {
128 return d.tickLower
129}
130
131func (d *Deposit) SetTickLower(tickLower int32) {
132 d.tickLower = tickLower
133}
134
135func (d *Deposit) TickUpper() int32 {
136 return d.tickUpper
137}
138
139func (d *Deposit) SetTickUpper(tickUpper int32) {
140 d.tickUpper = tickUpper
141}
142
143func (d *Deposit) ExternalIncentiveIds() map[string]bool {
144 return d.externalIncentiveIds
145}
146
147func (d *Deposit) SetExternalIncentiveIds(externalIncentiveIds map[string]bool) {
148 d.externalIncentiveIds = externalIncentiveIds
149}
150
151// AddExternalIncentiveId adds an external incentive id to the deposit.
152func (d *Deposit) AddExternalIncentiveId(incentiveId string) {
153 if d.externalIncentiveIds == nil {
154 d.externalIncentiveIds = make(map[string]bool)
155 }
156
157 d.externalIncentiveIds[incentiveId] = true
158}
159
160// HasExternalIncentiveId checks if the deposit has the given external incentive id.
161func (d *Deposit) HasExternalIncentiveId(incentiveId string) bool {
162 if d.externalIncentiveIds == nil {
163 return false
164 }
165
166 return d.externalIncentiveIds[incentiveId]
167}
168
169// RemoveExternalIncentiveId removes an external incentive id from the deposit.
170func (d *Deposit) RemoveExternalIncentiveId(incentiveId string) {
171 if d.externalIncentiveIds == nil {
172 return
173 }
174
175 delete(d.externalIncentiveIds, incentiveId)
176}
177
178// GetExternalIncentiveIdList returns a list of external incentive ids for the deposit.
179func (d *Deposit) GetExternalIncentiveIdList() []string {
180 if d.externalIncentiveIds == nil {
181 return []string{}
182 }
183
184 ids := make([]string, 0, len(d.externalIncentiveIds))
185
186 for incentiveId := range d.externalIncentiveIds {
187 ids = append(ids, incentiveId)
188 }
189
190 return ids
191}
192
193// IterateExternalIncentiveIds iterates over external incentive IDs without allocating a slice.
194// The callback function receives each incentive ID and should return false to continue iteration,
195// or true to stop early. This method is more memory-efficient than GetExternalIncentiveIdList
196// for cases where you only need to process IDs sequentially.
197func (d *Deposit) IterateExternalIncentiveIds(fn func(incentiveId string) bool) {
198 if d.externalIncentiveIds == nil {
199 return
200 }
201
202 for incentiveId := range d.externalIncentiveIds {
203 if fn(incentiveId) {
204 return
205 }
206 }
207}
208
209func (d *Deposit) Warmups() []Warmup {
210 return d.warmups
211}
212
213func (d *Deposit) SetWarmups(warmups []Warmup) {
214 d.warmups = warmups
215}
216
217func (d *Deposit) LastExternalIncentiveUpdatedAt() int64 {
218 return d.lastExternalIncentiveUpdatedAt
219}
220
221func (d *Deposit) SetLastExternalIncentiveUpdatedAt(timestamp int64) {
222 d.lastExternalIncentiveUpdatedAt = timestamp
223}
224
225// Clone returns a deep copy of the deposit.
226func (d *Deposit) Clone() *Deposit {
227 if d == nil {
228 return nil
229 }
230
231 return &Deposit{
232 warmups: cloneWarmups(d.warmups),
233 liquidity: d.liquidity.Clone(),
234 targetPoolPath: d.targetPoolPath,
235 owner: d.owner,
236 stakeTime: d.stakeTime,
237 internalRewardLastCollectTime: d.internalRewardLastCollectTime,
238 collectedInternalReward: d.collectedInternalReward,
239 collectedExternalRewards: cloneStringInt64Map(d.collectedExternalRewards),
240 externalRewardLastCollectTimes: cloneStringInt64Map(d.externalRewardLastCollectTimes),
241 externalIncentiveIds: cloneStringBoolMap(d.externalIncentiveIds),
242 lastExternalIncentiveUpdatedAt: d.lastExternalIncentiveUpdatedAt,
243 tickLower: d.tickLower,
244 tickUpper: d.tickUpper,
245 }
246}
247
248func NewDeposit(
249 owner address,
250 targetPoolPath string,
251 liquidity *u256.Uint,
252 currentTime int64,
253 tickLower, tickUpper int32,
254 warmups []Warmup,
255) *Deposit {
256 return &Deposit{
257 owner: owner,
258 targetPoolPath: targetPoolPath,
259 liquidity: liquidity,
260 warmups: warmups,
261 stakeTime: currentTime,
262 tickLower: tickLower,
263 tickUpper: tickUpper,
264 internalRewardLastCollectTime: currentTime,
265 externalRewardLastCollectTimes: make(map[string]int64),
266 collectedInternalReward: 0,
267 collectedExternalRewards: make(map[string]int64),
268 externalIncentiveIds: make(map[string]bool),
269 lastExternalIncentiveUpdatedAt: 0,
270 }
271}
272
273type Warmup struct {
274 TimeDuration int64
275 NextWarmupTime int64 // time when this warmup period ends
276 WarmupRatio uint64
277}
278
279func NewWarmup(timeDuration, nextWarmupTime int64, warmupRatio uint64) Warmup {
280 return Warmup{
281 TimeDuration: timeDuration,
282 NextWarmupTime: nextWarmupTime,
283 WarmupRatio: warmupRatio,
284 }
285}
286
287func (w *Warmup) SetNextWarmupTime(nextWarmupTime int64) {
288 w.NextWarmupTime = nextWarmupTime
289}
290
291func (w *Warmup) SetWarmupRatio(warmupRatio uint64) {
292 w.WarmupRatio = warmupRatio
293}
294
295func (w *Warmup) SetTimeDuration(timeDuration int64) {
296 w.TimeDuration = timeDuration
297}
298
299func DefaultWarmupTemplate() []Warmup {
300 secondsInDay := int64(86400)
301 secondsIn5Days := int64(5 * secondsInDay)
302 secondsIn10Days := int64(10 * secondsInDay)
303 secondsIn30Days := int64(30 * secondsInDay)
304
305 // NextWarmupTime is set to 0 for template.
306 // They will be set by InstantiateWarmup()
307 return []Warmup{
308 {
309 TimeDuration: secondsIn5Days,
310 // NextWarmupTime will be set based on currentTime
311 // NextWarmupTime: currentTime + secondsIn5Days,
312 WarmupRatio: 30,
313 },
314 {
315 TimeDuration: secondsIn10Days,
316 // NextWarmupTime will be set based on currentTime
317 // NextWarmupTime: currentTime + secondsIn10Days,
318 WarmupRatio: 50,
319 },
320 {
321 TimeDuration: secondsIn30Days,
322 // NextWarmupTime will be set based on currentTime
323 // NextWarmupTime: currentTime + secondsIn30Days,
324 WarmupRatio: 70,
325 },
326 {
327 TimeDuration: math.MaxInt64,
328 // NextWarmupTime will be set to math.MaxInt64
329 // NextWarmupTime: math.MaxInt64,
330 WarmupRatio: 100,
331 },
332 }
333}
334
335const (
336 GNS_PATH string = "gno.land/r/gnoswap/gns"
337 WUGNOT_PATH string = "gno.land/r/gnoland/wugnot"
338)
339
340func DefaultAllowedTokens() []string {
341 return []string{GNS_PATH, WUGNOT_PATH}
342}