getter.gno
18.25 Kb ยท 591 lines
1package v1
2
3import (
4 "chain/runtime"
5 "time"
6
7 "gno.land/p/nt/ufmt"
8
9 u256 "gno.land/p/gnoswap/uint256"
10
11 sr "gno.land/r/gnoswap/staker"
12)
13
14// getPoolByPoolPath retrieves the pool by its path.
15func (s *stakerV1) getPoolByPoolPath(poolPath string) *sr.Pool {
16 result, ok := s.store.GetPools().Get(poolPath)
17 if !ok {
18 panic(makeErrorWithDetails(
19 errDataNotFound,
20 ufmt.Sprintf("poolPath(%s) pool does not exist", poolPath)),
21 )
22 }
23
24 pool, ok := result.(*sr.Pool)
25 if !ok {
26 panic(makeErrorWithDetails(
27 errDataNotFound,
28 ufmt.Sprintf("poolPath(%s) pool does not exist", poolPath)),
29 )
30 }
31
32 return pool
33}
34
35// GetPool returns the pool for the given path.
36func (s *stakerV1) GetPool(poolPath string) *sr.Pool {
37 return s.getPoolByPoolPath(poolPath)
38}
39
40// GetPoolIncentiveIdList returns all incentive IDs for a pool.
41func (s *stakerV1) GetPoolIncentiveIdList(poolPath string) []string {
42 pool := s.getPoolByPoolPath(poolPath)
43
44 incentives := pool.Incentives()
45
46 ids := []string{}
47 incentives.IncentiveTrees().Iterate("", "", func(key string, value any) bool {
48 ids = append(ids, key)
49 return false
50 })
51
52 return ids
53}
54
55// getIncentive retrieves an external incentive by ID.
56func (s *stakerV1) getIncentive(poolPath string, incentiveId string) *sr.ExternalIncentive {
57 pool := s.getPoolByPoolPath(poolPath)
58
59 incentives := pool.Incentives()
60 incentive, exist := incentives.IncentiveTrees().Get(incentiveId)
61 if !exist {
62 panic(ufmt.Sprintf("incentiveId(%s) incentive does not exist", incentiveId))
63 }
64
65 ictv, ok := incentive.(*sr.ExternalIncentive)
66 if !ok {
67 panic(ufmt.Sprintf("failed to cast incentive to *ExternalIncentive: %T", incentive))
68 }
69 return ictv
70}
71
72// GetIncentive returns the incentive for the given pool and ID.
73func (s *stakerV1) GetIncentive(poolPath string, incentiveId string) *sr.ExternalIncentive {
74 return s.getIncentive(poolPath, incentiveId)
75}
76
77// GetIncentiveStartTimestamp returns the start timestamp of an incentive.
78func (s *stakerV1) GetIncentiveStartTimestamp(poolPath string, incentiveId string) int64 {
79 incentive := s.getIncentive(poolPath, incentiveId)
80
81 return incentive.StartTimestamp()
82}
83
84// GetIncentiveEndTimestamp returns the end timestamp of an incentive.
85func (s *stakerV1) GetIncentiveEndTimestamp(poolPath string, incentiveId string) int64 {
86 incentive := s.getIncentive(poolPath, incentiveId)
87
88 return incentive.EndTimestamp()
89}
90
91// GetTargetPoolPathByIncentiveId returns the target pool path of an incentive.
92func (s *stakerV1) GetTargetPoolPathByIncentiveId(poolPath string, incentiveId string) string {
93 incentive := s.getIncentive(poolPath, incentiveId)
94
95 return incentive.TargetPoolPath()
96}
97
98// GetCreatedHeightOfIncentive returns the creation height of an incentive.
99func (s *stakerV1) GetCreatedHeightOfIncentive(poolPath string, incentiveId string) int64 {
100 incentive := s.getIncentive(poolPath, incentiveId)
101
102 return incentive.CreatedHeight()
103}
104
105// GetIncentiveCreatedTimestamp returns the creation timestamp of an incentive.
106func (s *stakerV1) GetIncentiveCreatedTimestamp(poolPath string, incentiveId string) int64 {
107 incentive := s.getIncentive(poolPath, incentiveId)
108
109 return incentive.CreatedTimestamp()
110}
111
112// GetIncentiveTotalRewardAmount returns the total reward amount of an incentive.
113func (s *stakerV1) GetIncentiveTotalRewardAmount(poolPath string, incentiveId string) int64 {
114 incentive := s.getIncentive(poolPath, incentiveId)
115
116 return incentive.TotalRewardAmount()
117}
118
119// GetIncentiveDistributedRewardAmount returns the distributed reward amount of an incentive.
120func (s *stakerV1) GetIncentiveDistributedRewardAmount(poolPath string, incentiveId string) int64 {
121 incentive := s.getIncentive(poolPath, incentiveId)
122
123 return incentive.DistributedRewardAmount()
124}
125
126// GetIncentiveRemainingRewardAmount returns the remaining reward amount of an incentive.
127func (s *stakerV1) GetIncentiveRemainingRewardAmount(poolPath string, incentiveId string) int64 {
128 incentive := s.getIncentive(poolPath, incentiveId)
129
130 return incentive.RewardAmount()
131}
132
133// GetIncentiveDepositGnsAmount returns the deposit GNS amount of an incentive.
134func (s *stakerV1) GetIncentiveDepositGnsAmount(poolPath string, incentiveId string) int64 {
135 incentive := s.getIncentive(poolPath, incentiveId)
136
137 return incentive.DepositGnsAmount()
138}
139
140// GetIncentiveRefunded returns whether an incentive has been refunded.
141func (s *stakerV1) GetIncentiveRefunded(poolPath string, incentiveId string) bool {
142 incentive := s.getIncentive(poolPath, incentiveId)
143
144 return incentive.Refunded()
145}
146
147// IsIncentiveActive returns whether an incentive is currently active.
148func (s *stakerV1) IsIncentiveActive(poolPath string, incentiveId string) bool {
149 incentive := s.getIncentive(poolPath, incentiveId)
150 currentTime := time.Now().Unix()
151
152 resolver := NewExternalIncentiveResolver(incentive)
153 return resolver.isActive(currentTime) && !resolver.Refunded()
154}
155
156// GetIncentiveRewardToken returns the reward token of an incentive.
157func (s *stakerV1) GetIncentiveRewardToken(poolPath string, incentiveId string) string {
158 incentive := s.getIncentive(poolPath, incentiveId)
159
160 return incentive.RewardToken()
161}
162
163// GetIncentiveRewardAmount returns the reward amount of an incentive.
164func (s *stakerV1) GetIncentiveRewardAmount(poolPath string, incentiveId string) *u256.Uint {
165 incentive := s.getIncentive(poolPath, incentiveId)
166
167 return u256.NewUintFromInt64(incentive.RewardAmount())
168}
169
170// GetIncentiveRewardAmountAsString returns the reward amount of an incentive as string.
171func (s *stakerV1) GetIncentiveRewardAmountAsString(poolPath string, incentiveId string) string {
172 rewardAmount := s.GetIncentiveRewardAmount(poolPath, incentiveId)
173
174 return rewardAmount.ToString()
175}
176
177// GetIncentiveRewardPerSecond returns the reward per second of an incentive.
178func (s *stakerV1) GetIncentiveRewardPerSecond(poolPath string, incentiveId string) int64 {
179 incentive := s.getIncentive(poolPath, incentiveId)
180
181 return incentive.RewardPerSecond()
182}
183
184// GetIncentiveRefundee returns the refundee address of an incentive.
185func (s *stakerV1) GetIncentiveRefundee(poolPath string, incentiveId string) address {
186 incentive := s.getIncentive(poolPath, incentiveId)
187
188 return incentive.Refundee()
189}
190
191// getDeposit retrieves a deposit by LP token ID.
192func (s *stakerV1) getDeposit(lpTokenId uint64) *sr.Deposit {
193 deposit := s.getDeposits().get(lpTokenId)
194 if deposit == nil {
195 panic(makeErrorWithDetails(
196 errDataNotFound,
197 ufmt.Sprintf("lpTokenId(%d) deposit does not exist", lpTokenId)),
198 )
199 }
200
201 return deposit
202}
203
204// GetDeposit returns the deposit for the given LP token ID.
205func (s *stakerV1) GetDeposit(lpTokenId uint64) *sr.Deposit {
206 return s.getDeposit(lpTokenId)
207}
208
209// CollectableEmissionReward returns the claimable internal reward amount for a position.
210func (s *stakerV1) CollectableEmissionReward(positionId uint64) int64 {
211 currentTime := time.Now().Unix()
212 currentHeight := runtime.ChainHeight()
213 reward := s.calcPositionReward(currentHeight, currentTime, positionId)
214 return reward.Internal
215}
216
217// CollectableExternalIncentiveReward returns the claimable external reward amount for an incentive.
218func (s *stakerV1) CollectableExternalIncentiveReward(positionId uint64, incentiveId string) int64 {
219 currentTime := time.Now().Unix()
220 currentHeight := runtime.ChainHeight()
221 reward := s.calcPositionReward(currentHeight, currentTime, positionId)
222 amount, ok := reward.External[incentiveId]
223 if !ok {
224 return 0
225 }
226 return amount
227}
228
229// GetDepositOwner returns the owner of a deposit.
230func (s *stakerV1) GetDepositOwner(lpTokenId uint64) address {
231 deposit := s.getDeposit(lpTokenId)
232
233 return deposit.Owner()
234}
235
236// GetDepositStakeTime returns the stake time of a deposit.
237func (s *stakerV1) GetDepositStakeTime(lpTokenId uint64) int64 {
238 deposit := s.getDeposit(lpTokenId)
239
240 return deposit.StakeTime()
241}
242
243// GetDepositTargetPoolPath returns the target pool path of a deposit.
244func (s *stakerV1) GetDepositTargetPoolPath(lpTokenId uint64) string {
245 deposit := s.getDeposit(lpTokenId)
246
247 return deposit.TargetPoolPath()
248}
249
250// GetDepositTickLower returns the lower tick of a deposit.
251func (s *stakerV1) GetDepositTickLower(lpTokenId uint64) int32 {
252 deposit := s.getDeposit(lpTokenId)
253
254 return deposit.TickLower()
255}
256
257// GetDepositTickUpper returns the upper tick of a deposit.
258func (s *stakerV1) GetDepositTickUpper(lpTokenId uint64) int32 {
259 deposit := s.getDeposit(lpTokenId)
260
261 return deposit.TickUpper()
262}
263
264// GetDepositLiquidity returns the liquidity of a deposit.
265func (s *stakerV1) GetDepositLiquidity(lpTokenId uint64) *u256.Uint {
266 deposit := s.getDeposit(lpTokenId)
267
268 return deposit.Liquidity().Clone()
269}
270
271// GetDepositLiquidityAsString returns the liquidity of a deposit as string.
272func (s *stakerV1) GetDepositLiquidityAsString(lpTokenId uint64) string {
273 liquidity := s.GetDepositLiquidity(lpTokenId)
274
275 return liquidity.ToString()
276}
277
278// GetDepositInternalRewardLastCollectTimestamp returns the last collect timestamp of a deposit.
279func (s *stakerV1) GetDepositInternalRewardLastCollectTimestamp(lpTokenId uint64) int64 {
280 deposit := s.getDeposit(lpTokenId)
281
282 return deposit.InternalRewardLastCollectTime()
283}
284
285// GetDepositCollectedInternalReward returns the collected internal reward amount.
286func (s *stakerV1) GetDepositCollectedInternalReward(lpTokenId uint64) int64 {
287 deposit := s.getDeposit(lpTokenId)
288
289 return deposit.CollectedInternalReward()
290}
291
292// GetDepositCollectedExternalReward returns the collected external reward amount for an incentive.
293func (s *stakerV1) GetDepositCollectedExternalReward(lpTokenId uint64, incentiveId string) int64 {
294 return s.getDepositResolver(lpTokenId).CollectedExternalReward(incentiveId)
295}
296
297// GetDepositExternalRewardLastCollectTimestamp returns the last collect timestamp of a deposit.
298func (s *stakerV1) GetDepositExternalRewardLastCollectTimestamp(lpTokenId uint64, incentiveId string) int64 {
299 return s.getDepositResolver(lpTokenId).ExternalRewardLastCollectTime(incentiveId)
300}
301
302// GetDepositWarmUp returns the warm-up records of a deposit.
303func (s *stakerV1) GetDepositWarmUp(lpTokenId uint64) []sr.Warmup {
304 deposit := s.getDeposit(lpTokenId)
305
306 return deposit.Warmups()
307}
308
309// GetDepositExternalIncentiveIdList returns external incentive IDs for a deposit.
310func (s *stakerV1) GetDepositExternalIncentiveIdList(lpTokenId uint64) []string {
311 deposit := s.getDeposit(lpTokenId)
312
313 return deposit.GetExternalIncentiveIdList()
314}
315
316// GetPoolTier returns the tier of a pool.
317func (s *stakerV1) GetPoolTier(poolPath string) uint64 {
318 return s.getPoolTier().CurrentTier(poolPath)
319}
320
321// GetPoolTierRatio returns the current reward ratio for a pool's tier.
322func (s *stakerV1) GetPoolTierRatio(poolPath string) uint64 {
323 tier := s.GetPoolTier(poolPath)
324 ratio, err := s.getPoolTier().tierRatio.Get(tier)
325 if err != nil {
326 panic(makeErrorWithDetails(errInvalidPoolTier, err.Error()))
327 }
328
329 return ratio
330}
331
332// GetPoolTierCount returns the number of pools in a tier.
333func (s *stakerV1) GetPoolTierCount(tier uint64) uint64 {
334 if tier == 0 {
335 return 0
336 }
337 return uint64(s.getPoolTier().CurrentCount(tier))
338}
339
340// GetPoolReward returns the current reward amount for a tier.
341func (s *stakerV1) GetPoolReward(tier uint64) int64 {
342 return s.getPoolTier().CurrentReward(tier)
343}
344
345// GetPoolStakedLiquidity returns the current total staked liquidity of a pool.
346func (s *stakerV1) GetPoolStakedLiquidity(poolPath string) string {
347 pool := s.getPoolByPoolPath(poolPath)
348 liquidity := NewPoolResolver(pool).CurrentStakedLiquidity(time.Now().Unix())
349 if liquidity == nil {
350 return u256.Zero().ToString()
351 }
352
353 return liquidity.ToString()
354}
355
356// GetPoolsByTier returns the list of pools in a tier.
357func (s *stakerV1) GetPoolsByTier(tier uint64) []string {
358 if tier == 0 {
359 return []string{}
360 }
361
362 pools := make([]string, 0)
363 s.getPoolTier().membership.Iterate("", "", func(poolPath string, value any) bool {
364 currentTier, ok := value.(uint64)
365 if !ok {
366 panic("failed to cast tier to uint64")
367 }
368 if currentTier == tier {
369 pools = append(pools, poolPath)
370 }
371 return false
372 })
373
374 return pools
375}
376
377// GetTotalEmissionSent returns the total GNS emission sent.
378func (s *stakerV1) GetTotalEmissionSent() int64 {
379 return s.store.GetTotalEmissionSent()
380}
381
382// GetAllowedTokens returns the allowed external incentive token list.
383func (s *stakerV1) GetAllowedTokens() []string {
384 tokens := s.store.GetAllowedTokens()
385 result := make([]string, len(tokens))
386 copy(result, tokens)
387 return result
388}
389
390// GetWarmupTemplate returns the current warmup template.
391func (s *stakerV1) GetWarmupTemplate() []sr.Warmup {
392 warmups := s.store.GetWarmupTemplate()
393 result := make([]sr.Warmup, len(warmups))
394 copy(result, warmups)
395 return result
396}
397
398// GetTotalStakedUserCount returns the total number of users with staked positions.
399func (s *stakerV1) GetTotalStakedUserCount() uint64 {
400 return uint64(s.getStakers().tree.Size())
401}
402
403// GetTotalStakedUserPositionCount returns the staked position count for a user.
404func (s *stakerV1) GetTotalStakedUserPositionCount(user address) uint64 {
405 depositTreeI, ok := s.getStakers().tree.Get(user.String())
406 if !ok {
407 return 0
408 }
409
410 depositTree := retrieveDepositTree(depositTreeI)
411 return uint64(depositTree.Size())
412}
413
414// GetStakedPositionsByUser returns staked position IDs for a user with pagination.
415func (s *stakerV1) GetStakedPositionsByUser(owner address, offset, count int) []uint64 {
416 if count <= 0 {
417 return []uint64{}
418 }
419 if offset < 0 {
420 offset = 0
421 }
422
423 depositTreeI, ok := s.getStakers().tree.Get(owner.String())
424 if !ok {
425 return []uint64{}
426 }
427
428 depositTree := retrieveDepositTree(depositTreeI)
429 positions := make([]uint64, 0, count)
430 depositTree.IterateByOffset(offset, count, func(depositId string, _ any) bool {
431 positions = append(positions, sr.DecodeUint(depositId))
432 return false
433 })
434
435 return positions
436}
437
438// IsStaked returns whether a position is staked.
439func (s *stakerV1) IsStaked(positionId uint64) bool {
440 return s.getDeposits().Has(positionId)
441}
442
443// GetExternalIncentiveByPoolPath returns all external incentives for a pool.
444func (s *stakerV1) GetExternalIncentiveByPoolPath(poolPath string) []sr.ExternalIncentive {
445 incentives := make([]sr.ExternalIncentive, 0)
446
447 s.store.GetExternalIncentives().Iterate("", "", func(_ string, value any) bool {
448 incentive, ok := value.(*sr.ExternalIncentive)
449 if !ok {
450 panic("failed to cast value to *ExternalIncentive")
451 }
452 if incentive.TargetPoolPath() == poolPath {
453 incentives = append(incentives, *incentive)
454 }
455 return false
456 })
457
458 return incentives
459}
460
461// GetPoolRewardCacheCount returns the number of reward cache entries for a pool.
462func (s *stakerV1) GetPoolRewardCacheCount(poolPath string) uint64 {
463 pool := s.getPoolByPoolPath(poolPath)
464 return uint64(pool.RewardCache().Size())
465}
466
467// GetPoolRewardCacheIDs returns a paginated list of reward cache timestamps for a pool.
468func (s *stakerV1) GetPoolRewardCacheIDs(poolPath string, offset, count int) []int64 {
469 pool := s.getPoolByPoolPath(poolPath)
470 rewardCache := pool.RewardCache()
471
472 ids := make([]int64, 0)
473 rewardCache.IterateByOffset(offset, count, func(key int64, _ any) bool {
474 ids = append(ids, key)
475 return false
476 })
477
478 return ids
479}
480
481// GetPoolRewardCache returns the reward cache value at a specific timestamp for a pool.
482func (s *stakerV1) GetPoolRewardCache(poolPath string, timestamp uint64) int64 {
483 pool := s.getPoolByPoolPath(poolPath)
484 value, ok := pool.RewardCache().Get(int64(timestamp))
485 if !ok {
486 return 0
487 }
488
489 rewardCache, ok := value.(int64)
490 if !ok {
491 panic("failed to cast reward cache value to int64")
492 }
493
494 return rewardCache
495}
496
497// GetPoolIncentiveCount returns the number of incentives for a pool.
498func (s *stakerV1) GetPoolIncentiveCount(poolPath string) uint64 {
499 pool := s.getPoolByPoolPath(poolPath)
500 return uint64(pool.Incentives().IncentiveTrees().Size())
501}
502
503// GetPoolIncentiveIDs returns a paginated list of incentive IDs for a pool.
504func (s *stakerV1) GetPoolIncentiveIDs(poolPath string, offset, count int) []string {
505 pool := s.getPoolByPoolPath(poolPath)
506 incentives := pool.Incentives().IncentiveTrees()
507
508 ids := make([]string, 0)
509 incentives.IterateByOffset(offset, count, func(key string, _ any) bool {
510 ids = append(ids, key)
511 return false
512 })
513
514 return ids
515}
516
517// GetPoolGlobalRewardRatioAccumulationCount returns the number of global reward ratio accumulation entries for a pool.
518func (s *stakerV1) GetPoolGlobalRewardRatioAccumulationCount(poolPath string) uint64 {
519 pool := s.getPoolByPoolPath(poolPath)
520 return uint64(pool.GlobalRewardRatioAccumulation().Size())
521}
522
523// GetPoolGlobalRewardRatioAccumulationIDs returns a paginated list of timestamps for global reward ratio accumulation entries.
524func (s *stakerV1) GetPoolGlobalRewardRatioAccumulationIDs(poolPath string, offset, count int) []uint64 {
525 pool := s.getPoolByPoolPath(poolPath)
526 accumulation := pool.GlobalRewardRatioAccumulation()
527
528 ids := make([]uint64, 0)
529 accumulation.IterateByOffset(offset, count, func(key int64, _ any) bool {
530 ids = append(ids, uint64(key))
531 return false
532 })
533
534 return ids
535}
536
537// GetPoolGlobalRewardRatioAccumulation returns the global reward ratio accumulation at a specific timestamp for a pool.
538func (s *stakerV1) GetPoolGlobalRewardRatioAccumulation(poolPath string, timestamp uint64) *u256.Uint {
539 pool := s.getPoolByPoolPath(poolPath)
540 value, ok := pool.GlobalRewardRatioAccumulation().Get(int64(timestamp))
541 if !ok {
542 return u256.Zero()
543 }
544
545 accumulation, ok := value.(*u256.Uint)
546 if !ok {
547 panic("failed to cast global reward ratio accumulation to *u256.Uint")
548 }
549
550 return accumulation.Clone()
551}
552
553// GetPoolHistoricalTickCount returns the number of historical tick entries for a pool.
554func (s *stakerV1) GetPoolHistoricalTickCount(poolPath string) uint64 {
555 pool := s.getPoolByPoolPath(poolPath)
556 return uint64(pool.HistoricalTick().Size())
557}
558
559// GetPoolHistoricalTickIDs returns a paginated list of historical tick values for a pool.
560func (s *stakerV1) GetPoolHistoricalTickIDs(poolPath string, offset, count int) []int32 {
561 pool := s.getPoolByPoolPath(poolPath)
562 historicalTick := pool.HistoricalTick()
563
564 ticks := make([]int32, 0)
565 historicalTick.IterateByOffset(offset, count, func(_ int64, value any) bool {
566 tickId, ok := value.(int32)
567 if !ok {
568 return false
569 }
570 ticks = append(ticks, tickId)
571 return false
572 })
573
574 return ticks
575}
576
577// GetPoolHistoricalTick returns the historical tick at a specific timestamp for a pool.
578func (s *stakerV1) GetPoolHistoricalTick(poolPath string, tick uint64) int32 {
579 pool := s.getPoolByPoolPath(poolPath)
580 value, ok := pool.HistoricalTick().Get(int64(tick))
581 if !ok {
582 return 0
583 }
584
585 tickId, ok := value.(int32)
586 if !ok {
587 panic("failed to cast historical tick value to int32")
588 }
589
590 return tickId
591}