Search Apps Documentation Source Content File Folder Download Copy Actions Download

reward_manager.gno

7.65 Kb ยท 199 lines
  1package launchpad
  2
  3import (
  4	"gno.land/p/nt/avl"
  5
  6	u256 "gno.land/p/gnoswap/uint256"
  7)
  8
  9// RewardManager manages the distribution of rewards for a project tier.
 10//
 11// This struct contains the necessary data and methods to calculate and track
 12// rewards for deposits associated with a project tier.
 13//
 14// Fields:
 15// - rewards (avl.Tree): A map of deposit IDs to their associated reward states.
 16// - distributeAmountPerSecondX128 (u256.Uint): The amount of tokens to be distributed per second, represented as a Q128 fixed-point number.
 17// - accumulatedRewardPerDepositX128 (u256.Uint): The accumulated reward per GNS stake, represented as a Q128 fixed-point number.
 18// - totalDistributeAmount (int64): The total amount of tokens to be distributed.
 19// - totalClaimedAmount (int64): The total amount of tokens claimed.
 20// - distributeStartTime (int64): The start time of the reward calculation.
 21// - distributeEndTime (int64): The end time of the reward calculation.
 22// - accumulatedDistributeAmount (int64): The accumulated amount of tokens distributed.
 23// - accumulatedHeight (int64): The last height when reward was calculated.
 24// - rewardClaimableDuration (int64): The duration of reward claimable.
 25type RewardManager struct {
 26	rewards *avl.Tree // depositId -> RewardState
 27
 28	distributeAmountPerSecondX128   *u256.Uint // distribute amount per second, Q128
 29	accumulatedRewardPerDepositX128 *u256.Uint // accumulated reward per GNS stake, Q128
 30
 31	totalDistributeAmount       int64 // total distributed amount
 32	totalClaimedAmount          int64 // total claimed amount
 33	distributeStartTime         int64 // start time of reward calculation
 34	distributeEndTime           int64 // end time of reward calculation
 35	accumulatedDistributeAmount int64 // accumulated distribute amount
 36	accumulatedHeight           int64 // last height when reward was calculated
 37	accumulatedTime             int64 // last time when reward was calculated
 38	rewardClaimableDuration     int64 // duration of reward claimable
 39}
 40
 41// Rewards returns the rewards tree of the reward manager.
 42func (rm *RewardManager) Rewards() *avl.Tree {
 43	return rm.rewards
 44}
 45
 46// SetRewards sets the rewards tree of the reward manager.
 47func (rm *RewardManager) SetRewards(rewards *avl.Tree) {
 48	rm.rewards = rewards
 49}
 50
 51// DistributeAmountPerSecondX128 returns the distribute amount per second (Q128) of the reward manager.
 52func (rm *RewardManager) DistributeAmountPerSecondX128() *u256.Uint {
 53	return rm.distributeAmountPerSecondX128
 54}
 55
 56// SetDistributeAmountPerSecondX128 sets the distribute amount per second (Q128) of the reward manager.
 57func (rm *RewardManager) SetDistributeAmountPerSecondX128(amount *u256.Uint) {
 58	rm.distributeAmountPerSecondX128 = amount
 59}
 60
 61// AccumulatedRewardPerDepositX128 returns the accumulated reward per deposit (Q128) of the reward manager.
 62func (rm *RewardManager) AccumulatedRewardPerDepositX128() *u256.Uint {
 63	return rm.accumulatedRewardPerDepositX128
 64}
 65
 66// SetAccumulatedRewardPerDepositX128 sets the accumulated reward per deposit (Q128) of the reward manager.
 67func (rm *RewardManager) SetAccumulatedRewardPerDepositX128(amount *u256.Uint) {
 68	rm.accumulatedRewardPerDepositX128 = amount
 69}
 70
 71// TotalDistributeAmount returns the total distribute amount of the reward manager.
 72func (rm *RewardManager) TotalDistributeAmount() int64 {
 73	return rm.totalDistributeAmount
 74}
 75
 76// SetTotalDistributeAmount sets the total distribute amount of the reward manager.
 77func (rm *RewardManager) SetTotalDistributeAmount(amount int64) {
 78	rm.totalDistributeAmount = amount
 79}
 80
 81// TotalClaimedAmount returns the total claimed amount of the reward manager.
 82func (rm *RewardManager) TotalClaimedAmount() int64 {
 83	return rm.totalClaimedAmount
 84}
 85
 86// SetTotalClaimedAmount sets the total claimed amount of the reward manager.
 87func (rm *RewardManager) SetTotalClaimedAmount(amount int64) {
 88	rm.totalClaimedAmount = amount
 89}
 90
 91// DistributeStartTime returns the distribute start time of the reward manager.
 92func (rm *RewardManager) DistributeStartTime() int64 {
 93	return rm.distributeStartTime
 94}
 95
 96// SetDistributeStartTime sets the distribute start time of the reward manager.
 97func (rm *RewardManager) SetDistributeStartTime(time int64) {
 98	rm.distributeStartTime = time
 99}
100
101// DistributeEndTime returns the distribute end time of the reward manager.
102func (rm *RewardManager) DistributeEndTime() int64 {
103	return rm.distributeEndTime
104}
105
106// SetDistributeEndTime sets the distribute end time of the reward manager.
107func (rm *RewardManager) SetDistributeEndTime(time int64) {
108	rm.distributeEndTime = time
109}
110
111// AccumulatedDistributeAmount returns the accumulated distribute amount of the reward manager.
112func (rm *RewardManager) AccumulatedDistributeAmount() int64 {
113	return rm.accumulatedDistributeAmount
114}
115
116// SetAccumulatedDistributeAmount sets the accumulated distribute amount of the reward manager.
117func (rm *RewardManager) SetAccumulatedDistributeAmount(amount int64) {
118	rm.accumulatedDistributeAmount = amount
119}
120
121// AccumulatedHeight returns the accumulated height of the reward manager.
122func (rm *RewardManager) AccumulatedHeight() int64 {
123	return rm.accumulatedHeight
124}
125
126// SetAccumulatedHeight sets the accumulated height of the reward manager.
127func (rm *RewardManager) SetAccumulatedHeight(height int64) {
128	rm.accumulatedHeight = height
129}
130
131// AccumulatedTime returns the accumulated time of the reward manager.
132func (rm *RewardManager) AccumulatedTime() int64 {
133	return rm.accumulatedTime
134}
135
136// SetAccumulatedTime sets the accumulated time of the reward manager.
137func (rm *RewardManager) SetAccumulatedTime(time int64) {
138	rm.accumulatedTime = time
139}
140
141// RewardClaimableDuration returns the reward claimable duration of the reward manager.
142func (rm *RewardManager) RewardClaimableDuration() int64 {
143	return rm.rewardClaimableDuration
144}
145
146// SetRewardClaimableDuration sets the reward claimable duration of the reward manager.
147func (rm *RewardManager) SetRewardClaimableDuration(duration int64) {
148	rm.rewardClaimableDuration = duration
149}
150
151func (rm RewardManager) Clone() *RewardManager {
152	rewardsTree := avl.NewTree()
153	rm.rewards.Iterate("", "", func(key string, value interface{}) bool {
154		rewardState, ok := value.(*RewardState)
155		if !ok {
156			return true
157		}
158		rewardsTree.Set(key, rewardState.Clone())
159		return false
160	})
161
162	return &RewardManager{
163		rewards:                         rewardsTree,
164		distributeAmountPerSecondX128:   rm.distributeAmountPerSecondX128.Clone(),
165		accumulatedRewardPerDepositX128: rm.accumulatedRewardPerDepositX128.Clone(),
166		totalDistributeAmount:           rm.totalDistributeAmount,
167		totalClaimedAmount:              rm.totalClaimedAmount,
168		distributeStartTime:             rm.distributeStartTime,
169		distributeEndTime:               rm.distributeEndTime,
170		accumulatedDistributeAmount:     rm.accumulatedDistributeAmount,
171		accumulatedHeight:               rm.accumulatedHeight,
172		accumulatedTime:                 rm.accumulatedTime,
173		rewardClaimableDuration:         rm.rewardClaimableDuration,
174	}
175}
176
177// NewRewardManager returns a pointer to a new RewardManager with the given values.
178func NewRewardManager(
179	totalDistributeAmount int64,
180	distributeStartTime int64,
181	distributeEndTime int64,
182	rewardCollectableDuration int64,
183	currentHeight int64,
184	currentTime int64,
185) *RewardManager {
186	return &RewardManager{
187		totalDistributeAmount:           totalDistributeAmount,
188		distributeStartTime:             distributeStartTime,
189		distributeEndTime:               distributeEndTime,
190		totalClaimedAmount:              0,
191		accumulatedDistributeAmount:     0,
192		accumulatedHeight:               0,
193		accumulatedTime:                 0,
194		accumulatedRewardPerDepositX128: u256.Zero(),
195		distributeAmountPerSecondX128:   u256.Zero(),
196		rewardClaimableDuration:         rewardCollectableDuration,
197		rewards:                         avl.NewTree(),
198	}
199}