Search Apps Documentation Source Content File Folder Download Copy Actions Download

getter.gno

17.43 Kb · 554 lines
  1package v1
  2
  3import (
  4	"chain/runtime"
  5	"time"
  6
  7	ufmt "gno.land/p/nt/ufmt/v0"
  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// GetIncentiveAccumulatedPenaltyAmount returns the accumulated warmup penalty amount of an incentive.
134func (s *stakerV1) GetIncentiveAccumulatedPenaltyAmount(poolPath string, incentiveId string) int64 {
135	incentive := s.getIncentive(poolPath, incentiveId)
136
137	return incentive.AccumulatedPenaltyAmount()
138}
139
140// GetIncentiveDepositGnsAmount returns the deposit GNS amount of an incentive.
141func (s *stakerV1) GetIncentiveDepositGnsAmount(poolPath string, incentiveId string) int64 {
142	incentive := s.getIncentive(poolPath, incentiveId)
143
144	return incentive.DepositGnsAmount()
145}
146
147// GetIncentiveRefunded returns whether an incentive has been refunded.
148func (s *stakerV1) GetIncentiveRefunded(poolPath string, incentiveId string) bool {
149	incentive := s.getIncentive(poolPath, incentiveId)
150
151	return incentive.Refunded()
152}
153
154// IsIncentiveActive returns whether an incentive is currently active.
155func (s *stakerV1) IsIncentiveActive(poolPath string, incentiveId string) bool {
156	incentive := s.getIncentive(poolPath, incentiveId)
157	currentTime := time.Now().Unix()
158
159	resolver := NewExternalIncentiveResolver(incentive)
160	return resolver.isActive(currentTime) && !resolver.Refunded()
161}
162
163// GetIncentiveRewardToken returns the reward token of an incentive.
164func (s *stakerV1) GetIncentiveRewardToken(poolPath string, incentiveId string) string {
165	incentive := s.getIncentive(poolPath, incentiveId)
166
167	return incentive.RewardToken()
168}
169
170// GetIncentiveRewardAmount returns the reward amount of an incentive.
171func (s *stakerV1) GetIncentiveRewardAmount(poolPath string, incentiveId string) *u256.Uint {
172	incentive := s.getIncentive(poolPath, incentiveId)
173
174	return u256.NewUintFromInt64(incentive.RewardAmount())
175}
176
177// GetIncentiveRewardAmountAsString returns the reward amount of an incentive as string.
178func (s *stakerV1) GetIncentiveRewardAmountAsString(poolPath string, incentiveId string) string {
179	rewardAmount := s.GetIncentiveRewardAmount(poolPath, incentiveId)
180
181	return rewardAmount.ToString()
182}
183
184// GetIncentiveRewardPerSecondX128 returns the Q128-scaled reward per second of
185// an incentive (i.e. actual rate = value / 2^128). The Q128 form preserves
186// sub-second precision that would otherwise be lost to int64 truncation.
187func (s *stakerV1) GetIncentiveRewardPerSecondX128(poolPath string, incentiveId string) *u256.Uint {
188	incentive := s.getIncentive(poolPath, incentiveId)
189
190	return incentive.RewardPerSecondX128()
191}
192
193// GetIncentiveCreator returns the creator address of an incentive.
194func (s *stakerV1) GetIncentiveCreator(poolPath string, incentiveId string) address {
195	incentive := s.getIncentive(poolPath, incentiveId)
196
197	return incentive.Creator()
198}
199
200// getDeposit retrieves a deposit by LP token ID.
201func (s *stakerV1) getDeposit(lpTokenId uint64) *sr.Deposit {
202	deposit := s.getDeposits().get(lpTokenId)
203	if deposit == nil {
204		panic(makeErrorWithDetails(
205			errDataNotFound,
206			ufmt.Sprintf("lpTokenId(%d) deposit does not exist", lpTokenId)),
207		)
208	}
209
210	return deposit
211}
212
213// GetDeposit returns the deposit for the given LP token ID.
214func (s *stakerV1) GetDeposit(lpTokenId uint64) *sr.Deposit {
215	return s.getDeposit(lpTokenId)
216}
217
218// CollectableEmissionReward returns the claimable internal reward amount for a position.
219func (s *stakerV1) CollectableEmissionReward(positionId uint64) int64 {
220	currentTime := time.Now().Unix()
221	currentHeight := runtime.ChainHeight()
222	reward := s.calcPositionReward(currentHeight, currentTime, positionId)
223	return reward.Internal
224}
225
226// CollectableExternalIncentiveReward returns the claimable external reward amount for an incentive.
227func (s *stakerV1) CollectableExternalIncentiveReward(positionId uint64, incentiveId string) int64 {
228	currentTime := time.Now().Unix()
229	currentHeight := runtime.ChainHeight()
230	reward := s.calcPositionReward(currentHeight, currentTime, positionId)
231	amount, ok := reward.External[incentiveId]
232	if !ok {
233		return 0
234	}
235	return amount
236}
237
238// GetDepositOwner returns the owner of a deposit.
239func (s *stakerV1) GetDepositOwner(lpTokenId uint64) address {
240	deposit := s.getDeposit(lpTokenId)
241
242	return deposit.Owner()
243}
244
245// GetDepositStakeTime returns the stake time of a deposit.
246func (s *stakerV1) GetDepositStakeTime(lpTokenId uint64) int64 {
247	deposit := s.getDeposit(lpTokenId)
248
249	return deposit.StakeTime()
250}
251
252// GetDepositTargetPoolPath returns the target pool path of a deposit.
253func (s *stakerV1) GetDepositTargetPoolPath(lpTokenId uint64) string {
254	deposit := s.getDeposit(lpTokenId)
255
256	return deposit.TargetPoolPath()
257}
258
259// GetDepositTickLower returns the lower tick of a deposit.
260func (s *stakerV1) GetDepositTickLower(lpTokenId uint64) int32 {
261	deposit := s.getDeposit(lpTokenId)
262
263	return deposit.TickLower()
264}
265
266// GetDepositTickUpper returns the upper tick of a deposit.
267func (s *stakerV1) GetDepositTickUpper(lpTokenId uint64) int32 {
268	deposit := s.getDeposit(lpTokenId)
269
270	return deposit.TickUpper()
271}
272
273// GetDepositLiquidity returns the liquidity of a deposit.
274func (s *stakerV1) GetDepositLiquidity(lpTokenId uint64) *u256.Uint {
275	deposit := s.getDeposit(lpTokenId)
276
277	return deposit.Liquidity()
278}
279
280// GetDepositLiquidityAsString returns the liquidity of a deposit as string.
281func (s *stakerV1) GetDepositLiquidityAsString(lpTokenId uint64) string {
282	liquidity := s.GetDepositLiquidity(lpTokenId)
283
284	return liquidity.ToString()
285}
286
287// GetDepositInternalRewardLastCollectTimestamp returns the last collect timestamp of a deposit.
288func (s *stakerV1) GetDepositInternalRewardLastCollectTimestamp(lpTokenId uint64) int64 {
289	deposit := s.getDeposit(lpTokenId)
290
291	return deposit.InternalRewardLastCollectTime()
292}
293
294// GetDepositCollectedInternalReward returns the collected internal reward amount.
295func (s *stakerV1) GetDepositCollectedInternalReward(lpTokenId uint64) int64 {
296	deposit := s.getDeposit(lpTokenId)
297
298	return deposit.CollectedInternalReward()
299}
300
301// GetDepositCollectedExternalReward returns the collected external reward amount for an incentive.
302func (s *stakerV1) GetDepositCollectedExternalReward(lpTokenId uint64, incentiveId string) int64 {
303	return s.getDepositResolver(lpTokenId).CollectedExternalReward(incentiveId)
304}
305
306// GetDepositExternalRewardLastCollectTimestamp returns the last collect timestamp of a deposit.
307func (s *stakerV1) GetDepositExternalRewardLastCollectTimestamp(lpTokenId uint64, incentiveId string) int64 {
308	return s.getDepositResolver(lpTokenId).ExternalRewardLastCollectTime(incentiveId)
309}
310
311// GetDepositWarmUp returns the warm-up records of a deposit.
312func (s *stakerV1) GetDepositWarmUp(lpTokenId uint64) []sr.Warmup {
313	deposit := s.getDeposit(lpTokenId)
314
315	return deposit.Warmups()
316}
317
318// GetDepositExternalIncentiveIdList returns external incentive IDs for a deposit.
319func (s *stakerV1) GetDepositExternalIncentiveIdList(lpTokenId uint64) []string {
320	deposit := s.getDeposit(lpTokenId)
321
322	return deposit.GetExternalIncentiveIdList()
323}
324
325// GetPoolTier returns the tier of a pool.
326func (s *stakerV1) GetPoolTier(poolPath string) uint64 {
327	return s.getPoolTier().CurrentTier(poolPath)
328}
329
330// GetPoolTierRatio returns the current reward ratio for a pool's tier.
331func (s *stakerV1) GetPoolTierRatio(poolPath string) uint64 {
332	tier := s.GetPoolTier(poolPath)
333	ratio, err := s.getPoolTier().tierRatio.Get(tier)
334	if err != nil {
335		panic(makeErrorWithDetails(errInvalidPoolTier, err.Error()))
336	}
337
338	return ratio
339}
340
341// GetPoolTierCount returns the number of pools in a tier.
342func (s *stakerV1) GetPoolTierCount(tier uint64) uint64 {
343	if tier == 0 {
344		return 0
345	}
346	return uint64(s.getPoolTier().CurrentCount(tier))
347}
348
349// GetPoolReward returns the current reward amount for a tier.
350func (s *stakerV1) GetPoolReward(tier uint64) int64 {
351	return s.getPoolTier().CurrentReward(tier)
352}
353
354// GetPoolStakedLiquidity returns the current total staked liquidity of a pool.
355func (s *stakerV1) GetPoolStakedLiquidity(poolPath string) string {
356	pool := s.getPoolByPoolPath(poolPath)
357	liquidity := NewPoolResolver(pool).CurrentStakedLiquidity(time.Now().Unix())
358	if liquidity == nil {
359		return u256.Zero().ToString()
360	}
361
362	return liquidity.ToString()
363}
364
365// GetPoolsByTier returns the list of pools in a tier.
366func (s *stakerV1) GetPoolsByTier(tier uint64) []string {
367	if tier == 0 {
368		return []string{}
369	}
370
371	pools := make([]string, 0)
372	s.getPoolTier().membership.Iterate("", "", func(poolPath string, value any) bool {
373		currentTier, ok := value.(uint64)
374		if !ok {
375			panic("failed to cast tier to uint64")
376		}
377		if currentTier == tier {
378			pools = append(pools, poolPath)
379		}
380		return false
381	})
382
383	return pools
384}
385
386// GetTotalEmissionSent returns the total GNS emission sent.
387func (s *stakerV1) GetTotalEmissionSent() int64 {
388	return s.store.GetTotalEmissionSent()
389}
390
391// GetAllowedTokens returns the allowed external incentive token list.
392func (s *stakerV1) GetAllowedTokens() []string {
393	return s.store.GetAllowedTokens()
394}
395
396// GetWarmupTemplate returns the current warmup template.
397func (s *stakerV1) GetWarmupTemplate() []sr.Warmup {
398	return s.store.GetWarmupTemplate()
399}
400
401// IsStaked returns whether a position is staked.
402func (s *stakerV1) IsStaked(positionId uint64) bool {
403	return s.getDeposits().Has(positionId)
404}
405
406// GetExternalIncentiveByPoolPath returns all external incentives for a pool.
407func (s *stakerV1) GetExternalIncentiveByPoolPath(poolPath string) []sr.ExternalIncentive {
408	incentives := make([]sr.ExternalIncentive, 0)
409
410	s.store.GetExternalIncentives().Iterate("", "", func(_ string, value any) bool {
411		incentive, ok := value.(*sr.ExternalIncentive)
412		if !ok {
413			panic("failed to cast value to *ExternalIncentive")
414		}
415		if incentive.TargetPoolPath() == poolPath {
416			incentives = append(incentives, *incentive)
417		}
418		return false
419	})
420
421	return incentives
422}
423
424// GetPoolRewardCacheCount returns the number of reward cache entries for a pool.
425func (s *stakerV1) GetPoolRewardCacheCount(poolPath string) uint64 {
426	pool := s.getPoolByPoolPath(poolPath)
427	return uint64(pool.RewardCache().Size())
428}
429
430// GetPoolRewardCacheIDs returns a paginated list of reward cache timestamps for a pool.
431func (s *stakerV1) GetPoolRewardCacheIDs(poolPath string, offset, count int) []int64 {
432	pool := s.getPoolByPoolPath(poolPath)
433	rewardCache := pool.RewardCache()
434
435	ids := make([]int64, 0)
436	rewardCache.IterateByOffset(offset, count, func(key int64, _ any) bool {
437		ids = append(ids, key)
438		return false
439	})
440
441	return ids
442}
443
444// GetPoolRewardCache returns the reward cache value at a specific timestamp for a pool.
445func (s *stakerV1) GetPoolRewardCache(poolPath string, timestamp uint64) int64 {
446	pool := s.getPoolByPoolPath(poolPath)
447	value, ok := pool.RewardCache().Get(int64(timestamp))
448	if !ok {
449		return 0
450	}
451
452	rewardCache, ok := value.(int64)
453	if !ok {
454		panic("failed to cast reward cache value to int64")
455	}
456
457	return rewardCache
458}
459
460// GetPoolIncentiveCount returns the number of incentives for a pool.
461func (s *stakerV1) GetPoolIncentiveCount(poolPath string) uint64 {
462	pool := s.getPoolByPoolPath(poolPath)
463	return uint64(pool.Incentives().IncentiveTrees().Size())
464}
465
466// GetPoolIncentiveIDs returns a paginated list of incentive IDs for a pool.
467func (s *stakerV1) GetPoolIncentiveIDs(poolPath string, offset, count int) []string {
468	pool := s.getPoolByPoolPath(poolPath)
469	incentives := pool.Incentives().IncentiveTrees()
470
471	ids := make([]string, 0)
472	incentives.IterateByOffset(offset, count, func(key string, _ any) bool {
473		ids = append(ids, key)
474		return false
475	})
476
477	return ids
478}
479
480// GetPoolGlobalRewardRatioAccumulationCount returns the number of global reward ratio accumulation entries for a pool.
481func (s *stakerV1) GetPoolGlobalRewardRatioAccumulationCount(poolPath string) uint64 {
482	pool := s.getPoolByPoolPath(poolPath)
483	return uint64(pool.GlobalRewardRatioAccumulation().Size())
484}
485
486// GetPoolGlobalRewardRatioAccumulationIDs returns a paginated list of timestamps for global reward ratio accumulation entries.
487func (s *stakerV1) GetPoolGlobalRewardRatioAccumulationIDs(poolPath string, offset, count int) []uint64 {
488	pool := s.getPoolByPoolPath(poolPath)
489	accumulation := pool.GlobalRewardRatioAccumulation()
490
491	ids := make([]uint64, 0)
492	accumulation.IterateByOffset(offset, count, func(key int64, _ any) bool {
493		ids = append(ids, uint64(key))
494		return false
495	})
496
497	return ids
498}
499
500// GetPoolGlobalRewardRatioAccumulation returns the global reward ratio accumulation at a specific timestamp for a pool.
501func (s *stakerV1) GetPoolGlobalRewardRatioAccumulation(poolPath string, timestamp uint64) string {
502	pool := s.getPoolByPoolPath(poolPath)
503	value, ok := pool.GlobalRewardRatioAccumulation().Get(int64(timestamp))
504	if !ok {
505		return "0"
506	}
507
508	accumulationStr, ok := value.(string)
509	if !ok {
510		panic("failed to cast global reward ratio accumulation to string")
511	}
512
513	return accumulationStr
514}
515
516// GetPoolHistoricalTickCount returns the number of historical tick entries for a pool.
517func (s *stakerV1) GetPoolHistoricalTickCount(poolPath string) uint64 {
518	pool := s.getPoolByPoolPath(poolPath)
519	return uint64(pool.HistoricalTick().Size())
520}
521
522// GetPoolHistoricalTickIDs returns a paginated list of historical tick values for a pool.
523func (s *stakerV1) GetPoolHistoricalTickIDs(poolPath string, offset, count int) []int32 {
524	pool := s.getPoolByPoolPath(poolPath)
525	historicalTick := pool.HistoricalTick()
526
527	ticks := make([]int32, 0)
528	historicalTick.IterateByOffset(offset, count, func(_ int64, value any) bool {
529		tickId, ok := value.(int32)
530		if !ok {
531			return false
532		}
533		ticks = append(ticks, tickId)
534		return false
535	})
536
537	return ticks
538}
539
540// GetPoolHistoricalTick returns the historical tick at a specific timestamp for a pool.
541func (s *stakerV1) GetPoolHistoricalTick(poolPath string, tick uint64) int32 {
542	pool := s.getPoolByPoolPath(poolPath)
543	value, ok := pool.HistoricalTick().Get(int64(tick))
544	if !ok {
545		return 0
546	}
547
548	tickId, ok := value.(int32)
549	if !ok {
550		panic("failed to cast historical tick value to int32")
551	}
552
553	return tickId
554}