diff --git a/staker/reward_calculation_incentives.gno b/staker/reward_calculation_incentives.gno index e96e841f..588fcc87 100644 --- a/staker/reward_calculation_incentives.gno +++ b/staker/reward_calculation_incentives.gno @@ -7,36 +7,35 @@ import ( "gno.land/p/demo/ufmt" - i256 "gno.land/p/gnoswap/int256" u256 "gno.land/p/gnoswap/uint256" ) type IncentiveBound struct { Incentive ExternalIncentive - IsEnter bool + IsEnter bool } // per-pool incentives type Incentives struct { - byTime *avl.Tree // (startTime, endTime, creator, rewardToken) => ExternalIncentive - byHeight *avl.Tree // (startHeight, endHeight, creator, rewardToken) => ExternalIncentive + byTime *avl.Tree // (startTime, endTime, creator, rewardToken) => ExternalIncentive + byHeight *avl.Tree // (startHeight, endHeight, creator, rewardToken) => ExternalIncentive byCreator *avl.Tree // (creator, startHeight, endHeight, rewardToken) => ExternalIncentive incentiveBound *UintTree // blockNumber -> []IncentiveBound - rewardCache *RewardCacheTree // blockNumber -> map[string]*u256.Uint + rewardCache *RewardCacheTree // blockNumber -> map[string]*u256.Uint lastRewardCacheHeight *uint64 } func NewIncentives(currentHeight uint64) Incentives { return Incentives{ - byTime: avl.NewTree(), - byHeight: avl.NewTree(), + byTime: avl.NewTree(), + byHeight: avl.NewTree(), byCreator: avl.NewTree(), incentiveBound: NewUintTree(), - rewardCache: NewRewardCacheTree(), + rewardCache: NewRewardCacheTree(), lastRewardCacheHeight: ¤tHeight, } } @@ -52,7 +51,7 @@ func (self *Incentives) Get(startTime, endTime int64, creator std.Address, rewar if !ok { return nil, false } - return value.(*ExternalIncentive), true + return value.(*ExternalIncentive), true } func (self *Incentives) GetByIncentiveId(incentiveId string) (*ExternalIncentive, bool) { @@ -91,7 +90,7 @@ func (self *Incentives) CurrentReward(currentHeight uint64) map[string]*u256.Uin } func (self *Incentives) create( - creator std.Address, + creator std.Address, incentive *ExternalIncentive, ) { byTimeId := incentiveIdByTime(uint64(incentive.startTimestamp), uint64(incentive.endTimestamp), creator, incentive.rewardToken) @@ -111,7 +110,7 @@ func (self *Incentives) create( startIncentiveBound := self.GetBound(uint64(incentive.startHeight)) startIncentiveBound = append(startIncentiveBound, IncentiveBound{ Incentive: *incentive, - IsEnter: true, + IsEnter: true, }) self.incentiveBound.Set(uint64(incentive.startHeight), startIncentiveBound) @@ -119,40 +118,42 @@ func (self *Incentives) create( endIncentiveBound := self.GetBound(endHeight) endIncentiveBound = append(endIncentiveBound, IncentiveBound{ Incentive: *incentive, - IsEnter: false, + IsEnter: false, }) self.incentiveBound.Set(endHeight, endIncentiveBound) - println("endIncentiveBound") - for _, bound := range endIncentiveBound { - println(bound.Incentive.rewardToken, bound.IsEnter) - } + /* + println("endIncentiveBound") + for _, bound := range endIncentiveBound { + println(bound.Incentive.rewardToken, bound.IsEnter) + } + */ } // endHeight MUST be less than or equal to the current block height func (self *Incentives) cacheRewardPerLiquidityUnit(startHeight, endHeight uint64, stakedLiquidity *u256.Uint) { currentReward := self.CurrentReward(startHeight) -/* - delta := self.GetDelta(startHeight) - reward := make(map[string]*u256.Uint) - for token, deltaAmount := range delta { - delta := i256.NewInt(deltaAmount) - deltaQ96 := delta.Mul(delta, _iQ96) - currentRewardToken, ok := currentReward[token] - if !ok { - currentRewardToken = u256.Zero() - } - deltaApplied := liquidityMathAddDelta(currentRewardToken, deltaQ96) - if deltaApplied.IsZero() { - continue + /* + delta := self.GetDelta(startHeight) + reward := make(map[string]*u256.Uint) + for token, deltaAmount := range delta { + delta := i256.NewInt(deltaAmount) + deltaQ96 := delta.Mul(delta, _iQ96) + currentRewardToken, ok := currentReward[token] + if !ok { + currentRewardToken = u256.Zero() + } + deltaApplied := liquidityMathAddDelta(currentRewardToken, deltaQ96) + if deltaApplied.IsZero() { + continue + } + reward[token] = deltaApplied + println(startHeight, token, deltaAmount, deltaQ96.ToString(), currentRewardToken.ToString(), deltaApplied.ToString()) } - reward[token] = deltaApplied - println(startHeight, token, deltaAmount, deltaQ96.ToString(), currentRewardToken.ToString(), deltaApplied.ToString()) - } - self.rewardCache.Set(startHeight, reward) -*/ + self.rewardCache.Set(startHeight, reward) + */ self.incentiveBound.Iterate(startHeight, endHeight, func(key uint64, value interface{}) bool { bound := value.([]IncentiveBound) @@ -174,4 +175,4 @@ func (self *Incentives) cacheRewardPerLiquidityUnit(startHeight, endHeight uint6 return false }) *self.lastRewardCacheHeight = endHeight -} \ No newline at end of file +} diff --git a/staker/reward_calculation_tick.gno b/staker/reward_calculation_tick.gno index d4b2a18b..c1e5b6d4 100644 --- a/staker/reward_calculation_tick.gno +++ b/staker/reward_calculation_tick.gno @@ -6,7 +6,7 @@ import ( "strconv" "strings" - avl "gno.land/p/demo/avl" + "gno.land/p/demo/avl" i256 "gno.land/p/gnoswap/int256" u256 "gno.land/p/gnoswap/uint256" diff --git a/staker/type.gno b/staker/type.gno index 91e010cf..7a4a7bbe 100644 --- a/staker/type.gno +++ b/staker/type.gno @@ -28,19 +28,19 @@ func newInternalTier(tier uint64, startTimestamp int64) InternalTier { } type ExternalIncentive struct { - incentiveId string // incentive id - startTimestamp int64 // start time for external reward - endTimestamp int64 // end time for external reward - createdHeight int64 // block height when the incentive was created - depositGnsAmount uint64 // deposited gns amount - targetPoolPath string // external reward target pool path - rewardToken string // external reward token path - rewardAmount uint64 // total reward amount - rewardLeft uint64 // remaining reward amount - startHeight int64 // start height for external reward - endHeight int64 // end height for external reward - rewardPerBlock uint64 // reward per block - refundee std.Address // refundee address + incentiveId string // incentive id + startTimestamp int64 // start time for external reward + endTimestamp int64 // end time for external reward + createdHeight int64 // block height when the incentive was created + depositGnsAmount uint64 // deposited gns amount + targetPoolPath string // external reward target pool path + rewardToken string // external reward token path + rewardAmount uint64 // total reward amount + rewardLeft uint64 // remaining reward amount + startHeight int64 // start height for external reward + endHeight int64 // end height for external reward + rewardPerBlock uint64 // reward per block + refundee std.Address // refundee address } func (e ExternalIncentive) StartTimestamp() int64 { @@ -69,21 +69,21 @@ func newExternalIncentive( refundee std.Address, ) ExternalIncentive { return ExternalIncentive{ - startTimestamp: startTimestamp, - endTimestamp: endTimestamp, - createdHeight: createdHeight, - depositGnsAmount: depositGnsAmount, - targetPoolPath: targetPoolPath, - rewardToken: rewardToken, - rewardAmount: rewardAmount, - rewardLeft: rewardLeft, - rewardPerBlock: rewardPerBlock, - refundee: refundee, + startTimestamp: startTimestamp, + endTimestamp: endTimestamp, + createdHeight: createdHeight, + depositGnsAmount: depositGnsAmount, + targetPoolPath: targetPoolPath, + rewardToken: rewardToken, + rewardAmount: rewardAmount, + rewardLeft: rewardLeft, + rewardPerBlock: rewardPerBlock, + refundee: refundee, } } - func NewExternalIncentive( + incentiveId string, targetPoolPath string, rewardToken string, rewardAmount uint64, @@ -107,16 +107,17 @@ func NewExternalIncentive( endHeight := std.GetHeight() + blocksLeftUntilEndHeight return &ExternalIncentive{ - targetPoolPath: targetPoolPath, - rewardToken: rewardToken, - rewardAmount: rewardAmount, - startTimestamp: startTimestamp, - endTimestamp: endTimestamp, - startHeight: startHeight, - endHeight: endHeight, - rewardPerBlock: rewardPerBlock, - refundee: refundee, - createdHeight: createdHeight, + incentiveId: incentiveId, + targetPoolPath: targetPoolPath, + rewardToken: rewardToken, + rewardAmount: rewardAmount, + startTimestamp: startTimestamp, + endTimestamp: endTimestamp, + startHeight: startHeight, + endHeight: endHeight, + rewardPerBlock: rewardPerBlock, + refundee: refundee, + createdHeight: createdHeight, depositGnsAmount: depositGnsAmount, } } @@ -154,12 +155,12 @@ type Deposit struct { stakeTimestamp int64 // staked time stakeHeight int64 // staked block height - targetPoolPath string // staked position's pool path - tickLower int32 // tick lower - tickUpper int32 // tick upper - liquidity *u256.Uint // liquidity + targetPoolPath string // staked position's pool path + tickLower int32 // tick lower + tickUpper int32 // tick upper + liquidity *u256.Uint // liquidity lastCollectHeight uint64 // last collect block height - warmups []Warmup // warmup information + warmups []Warmup // warmup information }