Search Apps Documentation Source Content File Folder Download Copy Actions Download

reward_manager.gno

7.34 Kb · 191 lines
  1package launchpad
  2
  3import (
  4	bptree "gno.land/p/nt/bptree/v0"
  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 (bptree.BPTree): 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// - rewardClaimableDuration (int64): The duration of reward claimable.
 24type RewardManager struct {
 25	rewards *bptree.BPTree // depositId -> RewardState
 26
 27	distributeAmountPerSecondX128   *u256.Uint // distribute amount per second, Q128
 28	accumulatedRewardPerDepositX128 *u256.Uint // accumulated reward per GNS stake, Q128
 29
 30	totalDistributeAmount       int64 // total distributed amount
 31	totalClaimedAmount          int64 // total claimed amount
 32	distributeStartTime         int64 // start time of reward calculation
 33	distributeEndTime           int64 // end time of reward calculation
 34	accumulatedDistributeAmount int64 // accumulated distribute amount
 35	accumulatedTime             int64 // last time when reward was calculated
 36	rewardClaimableDuration     int64 // duration of reward claimable
 37}
 38
 39// Rewards returns the rewards tree of the reward manager.
 40func (rm *RewardManager) Rewards() *bptree.BPTree {
 41	return rm.rewards
 42}
 43
 44// SetRewards sets the rewards tree of the reward manager.
 45func (rm *RewardManager) SetRewards(rewards *bptree.BPTree) {
 46	rm.rewards = rewards
 47}
 48
 49func (rm *RewardManager) SetReward(depositID string, rewardState *RewardState) {
 50	rm.rewards.Set(depositID, rewardState)
 51}
 52
 53func (rm *RewardManager) RemoveReward(depositID string) {
 54	rm.rewards.Remove(depositID)
 55}
 56
 57// DistributeAmountPerSecondX128 returns the distribute amount per second (Q128) of the reward manager.
 58func (rm *RewardManager) DistributeAmountPerSecondX128() *u256.Uint {
 59	return rm.distributeAmountPerSecondX128
 60}
 61
 62// SetDistributeAmountPerSecondX128 sets the distribute amount per second (Q128) of the reward manager.
 63func (rm *RewardManager) SetDistributeAmountPerSecondX128(amount *u256.Uint) {
 64	rm.distributeAmountPerSecondX128 = u256.Zero().Set(amount)
 65}
 66
 67// AccumulatedRewardPerDepositX128 returns the accumulated reward per deposit (Q128) of the reward manager.
 68func (rm *RewardManager) AccumulatedRewardPerDepositX128() *u256.Uint {
 69	return rm.accumulatedRewardPerDepositX128
 70}
 71
 72// SetAccumulatedRewardPerDepositX128 sets the accumulated reward per deposit (Q128) of the reward manager.
 73func (rm *RewardManager) SetAccumulatedRewardPerDepositX128(amount *u256.Uint) {
 74	rm.accumulatedRewardPerDepositX128 = u256.Zero().Set(amount)
 75}
 76
 77// TotalDistributeAmount returns the total distribute amount of the reward manager.
 78func (rm *RewardManager) TotalDistributeAmount() int64 {
 79	return rm.totalDistributeAmount
 80}
 81
 82// SetTotalDistributeAmount sets the total distribute amount of the reward manager.
 83func (rm *RewardManager) SetTotalDistributeAmount(amount int64) {
 84	rm.totalDistributeAmount = amount
 85}
 86
 87// TotalClaimedAmount returns the total claimed amount of the reward manager.
 88func (rm *RewardManager) TotalClaimedAmount() int64 {
 89	return rm.totalClaimedAmount
 90}
 91
 92// SetTotalClaimedAmount sets the total claimed amount of the reward manager.
 93func (rm *RewardManager) SetTotalClaimedAmount(amount int64) {
 94	rm.totalClaimedAmount = amount
 95}
 96
 97// DistributeStartTime returns the distribute start time of the reward manager.
 98func (rm *RewardManager) DistributeStartTime() int64 {
 99	return rm.distributeStartTime
100}
101
102// SetDistributeStartTime sets the distribute start time of the reward manager.
103func (rm *RewardManager) SetDistributeStartTime(time int64) {
104	rm.distributeStartTime = time
105}
106
107// DistributeEndTime returns the distribute end time of the reward manager.
108func (rm *RewardManager) DistributeEndTime() int64 {
109	return rm.distributeEndTime
110}
111
112// SetDistributeEndTime sets the distribute end time of the reward manager.
113func (rm *RewardManager) SetDistributeEndTime(time int64) {
114	rm.distributeEndTime = time
115}
116
117// AccumulatedDistributeAmount returns the accumulated distribute amount of the reward manager.
118func (rm *RewardManager) AccumulatedDistributeAmount() int64 {
119	return rm.accumulatedDistributeAmount
120}
121
122// SetAccumulatedDistributeAmount sets the accumulated distribute amount of the reward manager.
123func (rm *RewardManager) SetAccumulatedDistributeAmount(amount int64) {
124	rm.accumulatedDistributeAmount = amount
125}
126
127// AccumulatedTime returns the accumulated time of the reward manager.
128func (rm *RewardManager) AccumulatedTime() int64 {
129	return rm.accumulatedTime
130}
131
132// SetAccumulatedTime sets the accumulated time of the reward manager.
133func (rm *RewardManager) SetAccumulatedTime(time int64) {
134	rm.accumulatedTime = time
135}
136
137// RewardClaimableDuration returns the reward claimable duration of the reward manager.
138func (rm *RewardManager) RewardClaimableDuration() int64 {
139	return rm.rewardClaimableDuration
140}
141
142// SetRewardClaimableDuration sets the reward claimable duration of the reward manager.
143func (rm *RewardManager) SetRewardClaimableDuration(duration int64) {
144	rm.rewardClaimableDuration = duration
145}
146
147func (rm RewardManager) Clone() *RewardManager {
148	rewardsTree := bptree.NewBPTreeN(16)
149	rm.rewards.Iterate("", "", func(key string, value interface{}) bool {
150		rewardState, ok := value.(*RewardState)
151		if !ok {
152			return true
153		}
154		rewardsTree.Set(key, rewardState.Clone())
155		return false
156	})
157
158	return &RewardManager{
159		rewards:                         rewardsTree,
160		distributeAmountPerSecondX128:   rm.distributeAmountPerSecondX128.Clone(),
161		accumulatedRewardPerDepositX128: rm.accumulatedRewardPerDepositX128.Clone(),
162		totalDistributeAmount:           rm.totalDistributeAmount,
163		totalClaimedAmount:              rm.totalClaimedAmount,
164		distributeStartTime:             rm.distributeStartTime,
165		distributeEndTime:               rm.distributeEndTime,
166		accumulatedDistributeAmount:     rm.accumulatedDistributeAmount,
167		accumulatedTime:                 rm.accumulatedTime,
168		rewardClaimableDuration:         rm.rewardClaimableDuration,
169	}
170}
171
172// NewRewardManager returns a pointer to a new RewardManager with the given values.
173func NewRewardManager(
174	totalDistributeAmount int64,
175	distributeStartTime int64,
176	distributeEndTime int64,
177	rewardCollectableDuration int64,
178) *RewardManager {
179	return &RewardManager{
180		totalDistributeAmount:           totalDistributeAmount,
181		distributeStartTime:             distributeStartTime,
182		distributeEndTime:               distributeEndTime,
183		totalClaimedAmount:              0,
184		accumulatedDistributeAmount:     0,
185		accumulatedTime:                 0,
186		accumulatedRewardPerDepositX128: u256.Zero(),
187		distributeAmountPerSecondX128:   u256.Zero(),
188		rewardClaimableDuration:         rewardCollectableDuration,
189		rewards:                         bptree.NewBPTreeN(16),
190	}
191}