Skip to content

Commit

Permalink
Remove EdenBoostApr field on IncentiveInfo to avoid complexity & simp…
Browse files Browse the repository at this point in the history
…lify CalculateEdenBoostRewards calculation
  • Loading branch information
jelysn committed Jan 31, 2024
1 parent 22d8581 commit 6ddc3f5
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 168 deletions.
49 changes: 14 additions & 35 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39665,14 +39665,11 @@ paths:
distribution_epoch_in_blocks:
type: string
title: >-
number of block intervals that distribute rewards,
this is set from params.distribution_interval
number of block intervals that distribute rewards, set
from params.distribution_interval
current_epoch_in_blocks:
type: string
title: current epoch in block number
eden_boost_apr:
type: string
title: eden boost apr (0-1) range
title: Incentive Info
stake_incentives:
type: object
Expand All @@ -39697,14 +39694,11 @@ paths:
distribution_epoch_in_blocks:
type: string
title: >-
number of block intervals that distribute rewards,
this is set from params.distribution_interval
number of block intervals that distribute rewards, set
from params.distribution_interval
current_epoch_in_blocks:
type: string
title: current epoch in block number
eden_boost_apr:
type: string
title: eden boost apr (0-1) range
title: Incentive Info
reward_portion_for_lps:
type: string
Expand Down Expand Up @@ -84119,7 +84113,7 @@ definitions:
Accumulated rewards tracked by other (when it's for staking, from lp,
if it's for lp, from staking)
title: >-
DexRewardsTracker is used for tracking rewards for stakers & lps - all
DexRewardsTracker is used for tracking rewards for stakers and LPs, all
amount here is in USDC
elys.incentive.IncentiveInfo:
type: object
Expand All @@ -84142,14 +84136,11 @@ definitions:
distribution_epoch_in_blocks:
type: string
title: >-
number of block intervals that distribute rewards, this is set from
number of block intervals that distribute rewards, set from
params.distribution_interval
current_epoch_in_blocks:
type: string
title: current epoch in block number
eden_boost_apr:
type: string
title: eden boost apr (0-1) range
title: Incentive Info
elys.incentive.MsgUpdateIncentiveParamsResponse:
type: object
Expand Down Expand Up @@ -84189,14 +84180,11 @@ definitions:
distribution_epoch_in_blocks:
type: string
title: >-
number of block intervals that distribute rewards, this is set
from params.distribution_interval
number of block intervals that distribute rewards, set from
params.distribution_interval
current_epoch_in_blocks:
type: string
title: current epoch in block number
eden_boost_apr:
type: string
title: eden boost apr (0-1) range
title: Incentive Info
stake_incentives:
type: object
Expand All @@ -84219,14 +84207,11 @@ definitions:
distribution_epoch_in_blocks:
type: string
title: >-
number of block intervals that distribute rewards, this is set
from params.distribution_interval
number of block intervals that distribute rewards, set from
params.distribution_interval
current_epoch_in_blocks:
type: string
title: current epoch in block number
eden_boost_apr:
type: string
title: eden boost apr (0-1) range
title: Incentive Info
reward_portion_for_lps:
type: string
Expand Down Expand Up @@ -84411,14 +84396,11 @@ definitions:
distribution_epoch_in_blocks:
type: string
title: >-
number of block intervals that distribute rewards, this is set
from params.distribution_interval
number of block intervals that distribute rewards, set from
params.distribution_interval
current_epoch_in_blocks:
type: string
title: current epoch in block number
eden_boost_apr:
type: string
title: eden boost apr (0-1) range
title: Incentive Info
stake_incentives:
type: object
Expand All @@ -84441,14 +84423,11 @@ definitions:
distribution_epoch_in_blocks:
type: string
title: >-
number of block intervals that distribute rewards, this is set
from params.distribution_interval
number of block intervals that distribute rewards, set from
params.distribution_interval
current_epoch_in_blocks:
type: string
title: current epoch in block number
eden_boost_apr:
type: string
title: eden boost apr (0-1) range
title: Incentive Info
reward_portion_for_lps:
type: string
Expand Down
3 changes: 0 additions & 3 deletions proto/elys/incentive/incentive.proto
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,4 @@ message IncentiveInfo {
// current epoch in block number
string current_epoch_in_blocks = 7
[ (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false];
// eden boost apr (0-1) range
string eden_boost_apr = 8
[ (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
}
10 changes: 3 additions & 7 deletions x/incentive/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ func (k Keeper) ProcessUpdateIncentiveParams(ctx sdk.Context) bool {
if totalBlocksPerYear == sdk.ZeroInt() {
continue
}
currentEpochInBlocks := sdk.NewInt(ctx.BlockHeight() - int64(inflation.StartBlockHeight)).Mul(totalDistributionEpochPerYear).Quo(totalBlocksPerYear)
currentEpochInBlocks := sdk.NewInt(ctx.BlockHeight() - int64(inflation.StartBlockHeight)).
Mul(totalDistributionEpochPerYear).
Quo(totalBlocksPerYear)

// PerAllocation means per day - since allocation's once per day
maxEdenPerAllocation := sdk.NewInt(int64(inflation.Inflation.LmRewards)).Mul(EpochNumBlocks).Quo(totalBlocksPerYear)
Expand All @@ -132,8 +134,6 @@ func (k Keeper) ProcessUpdateIncentiveParams(ctx sdk.Context) bool {
DistributionEpochInBlocks: sdk.NewInt(params.DistributionInterval),
// current epoch in block number
CurrentEpochInBlocks: currentEpochInBlocks,
// eden boost apr (0-1) range
EdenBoostApr: sdk.NewDec(1),
}

if params.LpIncentives == nil {
Expand All @@ -150,7 +150,6 @@ func (k Keeper) ProcessUpdateIncentiveParams(ctx sdk.Context) bool {
params.LpIncentives.TotalBlocksPerYear = incentiveInfo.TotalBlocksPerYear
params.LpIncentives.EpochNumBlocks = incentiveInfo.EpochNumBlocks
params.LpIncentives.DistributionEpochInBlocks = incentiveInfo.DistributionEpochInBlocks
params.LpIncentives.EdenBoostApr = incentiveInfo.EdenBoostApr
}

// ------------- Stakers parameter -------------
Expand All @@ -172,8 +171,6 @@ func (k Keeper) ProcessUpdateIncentiveParams(ctx sdk.Context) bool {
DistributionEpochInBlocks: sdk.NewInt(params.DistributionInterval),
// current epoch in block number
CurrentEpochInBlocks: currentEpochInBlocks,
// eden boost apr (0-1) range
EdenBoostApr: sdk.NewDec(1),
}

if params.StakeIncentives == nil {
Expand All @@ -190,7 +187,6 @@ func (k Keeper) ProcessUpdateIncentiveParams(ctx sdk.Context) bool {
params.StakeIncentives.TotalBlocksPerYear = incentiveInfo.TotalBlocksPerYear
params.StakeIncentives.EpochNumBlocks = incentiveInfo.EpochNumBlocks
params.StakeIncentives.DistributionEpochInBlocks = incentiveInfo.DistributionEpochInBlocks
params.StakeIncentives.EdenBoostApr = incentiveInfo.EdenBoostApr
}
break
}
Expand Down
2 changes: 1 addition & 1 deletion x/incentive/keeper/apr.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (k Keeper) CalculateApr(ctx sdk.Context, query *types.QueryAprRequest) (sdk
return apr.TruncateInt(), nil
}
} else if query.Denom == ptypes.EdenB {
apr := lpIncentive.EdenBoostApr.MulInt(sdk.NewInt(100)).TruncateInt()
apr := types.EdenBoostApr.MulInt(sdk.NewInt(100)).TruncateInt()
return apr, nil
}

Expand Down
23 changes: 16 additions & 7 deletions x/incentive/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

errorsmod "cosmossdk.io/errors"
"cosmossdk.io/math"
ammtypes "github.com/elys-network/elys/x/amm/types"
assetprofiletypes "github.com/elys-network/elys/x/assetprofile/types"
ctypes "github.com/elys-network/elys/x/commitment/types"
Expand Down Expand Up @@ -114,7 +115,6 @@ func (k Keeper) UpdateStakersRewardsUnclaimed(ctx sdk.Context, stakeIncentive ty
dexRevenueLPsAmtPerDistribution := dexRevenueForLpsPerDistribution.AmountOf(baseCurrency)
dexRevenueStakersAmtPerDistribution := dexRevenueForStakersPerDistribution.AmountOf(baseCurrency)
gasFeesLPsAmtPerDistribution := gasFeesForLps.AmountOf(baseCurrency)
edenBoostAPR := stakeIncentive.EdenBoostApr

// Calculate eden amount per epoch
params := k.GetParams(ctx)
Expand Down Expand Up @@ -264,11 +264,12 @@ func (k Keeper) UpdateStakersRewardsUnclaimed(ctx sdk.Context, stakeIncentive ty
// Calculate new unclaimed Eden-Boost tokens for staker and Eden token holders
// ----------------------------------------------------------
// ----------------------------------------------------------
newUnclaimedEdenBoostTokens, newUnclaimedEdenBoostFromElysStaking, newUnclaimedEdenBoostFromEdenCommited := k.CalculateEdenBoostRewards(ctx, delegatedAmt, commitments, stakeIncentive, edenBoostAPR)
rewardsByElysStaking = rewardsByElysStaking.Add(sdk.NewCoin(ptypes.EdenB, newUnclaimedEdenBoostFromElysStaking))
rewardsByEdenCommitted = rewardsByEdenCommitted.Add(sdk.NewCoin(ptypes.EdenB, newUnclaimedEdenBoostFromEdenCommited))
newEdenBTokens, newEdenBFromElysStaking, newEdenBFromEdenCommited := k.CalculateEdenBoostRewards(
ctx, delegatedAmt, commitments, stakeIncentive, types.EdenBoostApr)
rewardsByElysStaking = rewardsByElysStaking.Add(sdk.NewCoin(ptypes.EdenB, newEdenBFromElysStaking))
rewardsByEdenCommitted = rewardsByEdenCommitted.Add(sdk.NewCoin(ptypes.EdenB, newEdenBFromEdenCommited))

newSumEdenBRewardsUnClaimed = newSumEdenBRewardsUnClaimed.Add(newUnclaimedEdenBoostTokens)
newSumEdenBRewardsUnClaimed = newSumEdenBRewardsUnClaimed.Add(newEdenBTokens)
// ----------------------------------------------------------
// ----------------------------------------------------------

Expand Down Expand Up @@ -459,11 +460,19 @@ func (k Keeper) UpdateLPRewardsUnclaimed(ctx sdk.Context, lpIncentive types.Ince
}

// Update commitment record
func (k Keeper) UpdateCommitments(ctx sdk.Context, creator string, commitments *ctypes.Commitments, newUnclaimedEdenTokens sdk.Int, newUnclaimedEdenBoostTokens sdk.Int, dexRewards sdk.Int, baseCurrency string) {
func (k Keeper) UpdateCommitments(
ctx sdk.Context,
creator string,
commitments *ctypes.Commitments,
newUnclaimedEdenTokens math.Int,
newUnclaimedEdenBTokens math.Int,
dexRewards math.Int,
baseCurrency string,
) {
// Update unclaimed Eden balances in the Commitments structure
commitments.AddRewardsUnclaimed(sdk.NewCoin(ptypes.Eden, newUnclaimedEdenTokens))
// Update unclaimed Eden-Boost token balances in the Commitments structure
commitments.AddRewardsUnclaimed(sdk.NewCoin(ptypes.EdenB, newUnclaimedEdenBoostTokens))
commitments.AddRewardsUnclaimed(sdk.NewCoin(ptypes.EdenB, newUnclaimedEdenBTokens))

// All dex revenue are collected to incentive module in USDC
// Gas fees (Elys) are also converted into USDC and collected into total dex revenue wallet of incentive module.
Expand Down
2 changes: 0 additions & 2 deletions x/incentive/keeper/keeper_apr_per_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ func TestAPRCalculationPerPool(t *testing.T) {
DistributionEpochInBlocks: sdk.NewInt(10),
// current epoch in block number
CurrentEpochInBlocks: sdk.NewInt(1),
// eden boost apr (0-1) range
EdenBoostApr: sdk.NewDec(1),
}

ctx = ctx.WithBlockHeight(lpIncentive.DistributionEpochInBlocks.Int64())
Expand Down
47 changes: 22 additions & 25 deletions x/incentive/keeper/keeper_stakers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
ctypes "github.com/elys-network/elys/x/commitment/types"
"github.com/elys-network/elys/x/incentive/types"
Expand Down Expand Up @@ -46,33 +47,29 @@ func (k Keeper) CalculateRewardsForStakersByCommitted(ctx sdk.Context, amt sdk.I
}

// Calculate new Eden-Boost token amounts based on the given conditions and user's current unclaimed token balance
func (k Keeper) CalculateEdenBoostRewards(ctx sdk.Context, delegatedAmt sdk.Int, commitments ctypes.Commitments, incentiveInfo types.IncentiveInfo, edenBoostAPR sdk.Dec) (sdk.Int, sdk.Int, sdk.Int) {
func (k Keeper) CalculateEdenBoostRewards(
ctx sdk.Context,
delegatedAmt math.Int,
commitments ctypes.Commitments,
incentiveInfo types.IncentiveInfo,
edenBoostAPR sdk.Dec,
) (math.Int, math.Int, math.Int) {
// Get eden commitments
edenCommitted := commitments.GetCommittedAmountForDenom(ptypes.Eden)

// Compute eden reward based on above and param factors for each
totalEden := delegatedAmt.Add(edenCommitted)

// Ensure incentiveInfo.DistributionEpochInBlocks is not zero to avoid division by zero
if incentiveInfo.DistributionEpochInBlocks.IsZero() {
return sdk.ZeroInt(), sdk.ZeroInt(), sdk.ZeroInt()
}

// Calculate edenBoostAPR % APR for eden boost
epochNumsPerYear := incentiveInfo.TotalBlocksPerYear.Quo(incentiveInfo.DistributionEpochInBlocks)
if epochNumsPerYear.IsZero() {
return sdk.ZeroInt(), sdk.ZeroInt(), sdk.ZeroInt()
}

apr := edenBoostAPR.TruncateInt()
newEdenBoost := totalEden.Quo(epochNumsPerYear).Mul(apr)

// Calculate the portion of each program contribution
newEdenBoostByElysStaked := sdk.ZeroInt()
if !totalEden.IsZero() {
newEdenBoostByElysStaked = sdk.NewDecFromInt(delegatedAmt).QuoInt(totalEden).MulInt(newEdenBoost).TruncateInt()
}
newEdenBoostByEdenCommitted := newEdenBoost.Sub(newEdenBoostByElysStaked)

return newEdenBoost, newEdenBoostByElysStaked, newEdenBoostByEdenCommitted
newEdenBByElysStaked := sdk.NewDecFromInt(delegatedAmt).
Mul(edenBoostAPR).
MulInt(incentiveInfo.DistributionEpochInBlocks).
QuoInt(incentiveInfo.TotalBlocksPerYear).
RoundInt()

newEdenBByEdenCommitted := sdk.NewDecFromInt(edenCommitted).
Mul(edenBoostAPR).
MulInt(incentiveInfo.DistributionEpochInBlocks).
QuoInt(incentiveInfo.TotalBlocksPerYear).
RoundInt()

newEdenBoost := newEdenBByElysStaked.Add(newEdenBByEdenCommitted)
return newEdenBoost, newEdenBByElysStaked, newEdenBByEdenCommitted
}
2 changes: 1 addition & 1 deletion x/incentive/types/dex_rewards_traker.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6ddc3f5

Please sign in to comment.