Skip to content

Commit

Permalink
pool tier reward cache, pool reward cache, halving, warmup
Browse files Browse the repository at this point in the history
  • Loading branch information
mconcat committed Dec 17, 2024
1 parent 8254469 commit 96df3ab
Show file tree
Hide file tree
Showing 10 changed files with 892 additions and 117 deletions.
7 changes: 7 additions & 0 deletions _deploy/r/gnoswap/gns/gns.gno
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ func Mint(address pusers.AddressOrName) uint64 {
halvingYearMintAmount[year] += amount
amountToMint += amount
}
for i := fromYear; i < toYear; i++ {
amount := GetAmountByYear(i+1)
halvingHeight := halvingYearBlock[i]
addPerBlockMintUpdate(halvingHeight, amount)
}
}

err := privateLedger.Mint(users.Resolve(address), amountToMint)
Expand All @@ -165,6 +170,8 @@ func checkAndHandleIfLastBlockOfHalvingYear(height int64, amount uint64) uint64
if height == lastBlock {
leftForThisYear := halvingYearAmount[year] - halvingYearMintAmount[year]
amount = leftForThisYear
addPerBlockMintUpdate(height, amount)
addPerBlockMintUpdate(height+1, amountPerBlockPerHalvingYear(year+1))
return amount
}

Expand Down
37 changes: 37 additions & 0 deletions _deploy/r/gnoswap/gns/halving.gno
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"std"
"time"

"gno.land/p/demo/avl"

"gno.land/p/demo/ufmt"

"gno.land/r/gnoswap/v1/common"
Expand Down Expand Up @@ -35,6 +37,38 @@ var (
startTimestamp int64
)

var perBlockMint *avl.Tree // height => uint64

// endHeight MUST be equal or before the current height(must not be future)
// setAvgBlockTimeInMsByAdmin does not change past emissions, safe
func LastPerBlockMintUpdate(endHeight uint64) (uint64, uint64) {
height := uint64(0)
amount := uint64(0)
perBlockMint.ReverseIterate(0, endHeight, func(key string, value interface{}) bool {
height = std.MustParseUint(key, 10)
amount = value.(uint64)
return false
})

return height, amount
}

func PerBlockMintUpdates(startHeight, endHeight uint64) ([]uint64, []uint64) {
heights := make([]uint64, 0)
updates := make([]uint64, 0)
perBlockMint.Iterate(startHeight, endHeight, func(key string, value interface{}) bool {
heights = append(heights, std.MustParseUint(key, 10))
updates = append(updates, value.(uint64))
return false
})

return heights, updates
}

func addPerBlockMintUpdate(height uint64, amount uint64) {
perBlockMint.Insert(ufmt.Sprintf("%d", height), amount)
}

var halvingYearBlock = make(map[int64]int64) // year => block
var halvingYearTimestamp = make(map[int64]int64) // year => timestamp

Expand Down Expand Up @@ -91,6 +125,8 @@ func init() {

amountPerBlockPerHalvingYear[i] = uint64(amountPerBlock)
}

addPerBlockMintUpdate(height, amountPerBlockPerHalvingYear[1])
}

func GetAvgBlockTimeInMs() int64 {
Expand Down Expand Up @@ -161,6 +197,7 @@ func setAvgBlockTimeInMs(ms int64) {

// update it
amountPerBlockPerHalvingYear[year] = adjustedAmountPerBlock
addPerBlockMintUpdate(height, adjustedAmountPerBlock)

// adjust halving block
for keyYear, _ := range halvingYearBlock {
Expand Down
4 changes: 2 additions & 2 deletions pool/pool.gno
Original file line number Diff line number Diff line change
Expand Up @@ -611,14 +611,14 @@ func tickTransition(step StepComputations, zeroForOne bool, state SwapState, poo
newState.liquidity = liquidityMathAddDelta(state.liquidity, liquidityNet)
}

pool.tickCrossHook(GetPoolPath(pool.token0Path, pool.token1Path, pool.fee), step.tickNext, zeroForOne)

if zeroForOne {
newState.tick = step.tickNext - 1
} else {
newState.tick = step.tickNext
}

pool.tickCrossHook(GetPoolPath(pool.token0Path, pool.token1Path, pool.fee), newState.tick, zeroForOne)

return newState
}

Expand Down
32 changes: 28 additions & 4 deletions staker/calculate_pool_position_reward.gno
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ var poolLastTmpGns map[string]uint64 = make(map[string]uint64)
var poolAccuGns map[string]uint64 = make(map[string]uint64)

// tokenId -> gnsAmount
var positionGns map[uint64]uint64 = make(map[uint64]uint64)
//var positionGns map[uint64]uint64 = make(map[uint64]uint64)

// tokenId -> lastGnsAmount
var positionLastGns map[uint64]uint64 = make(map[uint64]uint64)
//var positionLastGns map[uint64]uint64 = make(map[uint64]uint64)

// tokenId -> incentiveId -> lastRewardAmount
var positionLastExternal map[uint64]map[string]*u256.Uint = make(map[uint64]map[string]*u256.Uint)
Expand Down Expand Up @@ -213,6 +213,28 @@ func CalcPoolPositionRefactor() bool {
return true
}




func CalcPoolPositionPerDeposit(tokenId uint64, startHeight, endHeight uint64) {
deposit := deposits[tokenId]
tickUpper := pn.PositionGetPositionTickUpper(tokenId)
tickLower := pn.PositionGetPositionTickLower(tokenId)

// Filtering intervals where the staked position is in range

tickUpperCrosses := stakerTickInfo.Get(tickUpper).crossInfo()
tickLowerCrosses := stakerTickInfo.Get(tickLower).crossInfo()

pool := stakerPoolInfo.Get(deposit.targetPoolPath)
initialStakedLiquidity := pool.lastStakedLiquidity()
eligibleIntervals := pool.tickCrossesToEligibleIntervals(tickUpperCrosses, tickLowerCrosses, startHeight, endHeight, initialStakedLiquidity)

// Calculate internal reward


}

// XXX: need to improve nested iteration
// CalcPoolPosition calculates and updates the position of pools and rewards for stakers.
//
Expand Down Expand Up @@ -364,8 +386,10 @@ func CalcPoolPosition() {
positionAmountX := u256.Zero().Div(positionAmountX96, _q96)
positionAmount := positionAmountX.Uint64()

positionLastGns[tokenId] = positionGns[tokenId]
positionGns[tokenId] += positionAmount
reward := depositReward[tokenId]
reward.prepareDistribution(positionAmount)
depositReward[tokenId] = reward

poolLastTmpGns[poolPath] += positionAmount

// calculate internal amount from previous to now
Expand Down
4 changes: 2 additions & 2 deletions staker/calculate_pool_position_reward_math.gno
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func computeRewardByRatio(amount uint64, ratioX96 *u256.Uint) uint64 {
return rewardAmount
}

func rewardMathComputeInternalRewardAmount(tokenId uint64) (uint64, uint64) {
func rewardMathComputeInternalRewardAmount(tokenId uint64, reward *DepositReward) (uint64, uint64) {
deposit := deposits[tokenId]

// using block
Expand All @@ -117,7 +117,7 @@ func rewardMathComputeInternalRewardAmount(tokenId uint64) (uint64, uint64) {
durationRatio := getRewardRatio(stakedDuration)

// Distribute the accumulated rewards from the last calculation point to the current block according to each weight
toDistribute := positionGns[tokenId] - positionLastGns[tokenId]
toDistribute := reward.toDistribute()

until30 := uint64(stakeHeight + warmUp[50] - 1) // 150
until50 := uint64(stakeHeight + warmUp[70] - 1) // 300
Expand Down
7 changes: 3 additions & 4 deletions staker/manage_pool_tiers.gno
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,7 @@ func setPoolTier(pool string, tier uint64) {
}

// panic if pool exists in poolTiers
_, exist := poolTiers[pool]
if exist {
if poolTier.hasPool(pool) {
panic(addDetailToError(
errAlreadyHasTier,
ufmt.Sprintf("manage_pool_tiers.gno__SetPoolTier() || pool(%s) already exists in poolTiers", pool),
Expand All @@ -190,10 +189,10 @@ func setPoolTier(pool string, tier uint64) {
// check if tier is valid
mustValidTier(tier)

poolTiers[pool] = InternalTier{
poolTier.of(tier).Set(pool, InternalTier{
tier: tier,
startTimestamp: time.Now().Unix(),
}
})
}

func ChangePoolTierByAdmin(poolPath string, tier uint64) {
Expand Down
Loading

0 comments on commit 96df3ab

Please sign in to comment.