Skip to content

Commit

Permalink
chore: apr calculation per pool
Browse files Browse the repository at this point in the history
  • Loading branch information
kenta-elys committed Dec 4, 2023
1 parent 44196c1 commit 25f1a33
Show file tree
Hide file tree
Showing 12 changed files with 544 additions and 126 deletions.
66 changes: 63 additions & 3 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39646,10 +39646,25 @@ paths:
multiplier:
type: string
title: multiplier for lp rewards
num_blocks:
type: string
title: Block number since creation
dex_reward_amount_given:
type: string
title: Total dex rewards given
eden_reward_amount_given:
type: string
title: Total eden rewards given
eden_apr:
type: string
title: Eden APR
dex_apr:
type: string
title: Dex APR
title: Pool Info
title: |-
Pool information
poolId, reward wallet, mulitplier
poolId, reward wallet, mulitplier, dex rewards given
elys_stake_tracking_rate:
type: string
format: int64
Expand Down Expand Up @@ -84080,10 +84095,25 @@ definitions:
multiplier:
type: string
title: multiplier for lp rewards
num_blocks:
type: string
title: Block number since creation
dex_reward_amount_given:
type: string
title: Total dex rewards given
eden_reward_amount_given:
type: string
title: Total eden rewards given
eden_apr:
type: string
title: Eden APR
dex_apr:
type: string
title: Dex APR
title: Pool Info
title: |-
Pool information
poolId, reward wallet, mulitplier
poolId, reward wallet, mulitplier, dex rewards given
elys_stake_tracking_rate:
type: string
format: int64
Expand Down Expand Up @@ -84135,6 +84165,21 @@ definitions:
multiplier:
type: string
title: multiplier for lp rewards
num_blocks:
type: string
title: Block number since creation
dex_reward_amount_given:
type: string
title: Total dex rewards given
eden_reward_amount_given:
type: string
title: Total eden rewards given
eden_apr:
type: string
title: Eden APR
dex_apr:
type: string
title: Dex APR
title: Pool Info
elys.incentive.QueryCommunityPoolResponse:
type: object
Expand Down Expand Up @@ -84250,10 +84295,25 @@ definitions:
multiplier:
type: string
title: multiplier for lp rewards
num_blocks:
type: string
title: Block number since creation
dex_reward_amount_given:
type: string
title: Total dex rewards given
eden_reward_amount_given:
type: string
title: Total eden rewards given
eden_apr:
type: string
title: Eden APR
dex_apr:
type: string
title: Dex APR
title: Pool Info
title: |-
Pool information
poolId, reward wallet, mulitplier
poolId, reward wallet, mulitplier, dex rewards given
elys_stake_tracking_rate:
type: string
format: int64
Expand Down
4 changes: 2 additions & 2 deletions proto/elys/incentive/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package elys.incentive;
import "gogoproto/gogo.proto";
import "elys/incentive/incentive.proto";
import "elys/incentive/pool.proto";
import "elys/incentive/dex_rewards_stakers.proto";
import "elys/incentive/dex_rewards_traker.proto";

option go_package = "github.com/elys-network/elys/x/incentive/types";

Expand Down Expand Up @@ -33,7 +33,7 @@ message Params {
];

// Pool information
// poolId, reward wallet, mulitplier
// poolId, reward wallet, mulitplier, dex rewards given
repeated PoolInfo pool_infos = 7 [(gogoproto.nullable) = false];

int64 elys_stake_tracking_rate = 8;
Expand Down
26 changes: 26 additions & 0 deletions proto/elys/incentive/pool.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,30 @@ message PoolInfo {
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];

// Block number since creation
string num_blocks = 4 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
// Total dex rewards given
string dex_reward_amount_given = 5 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
// Total eden rewards given
string eden_reward_amount_given = 6 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
// Eden APR
string eden_apr = 7 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
// Dex APR
string dex_apr = 8 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
}
2 changes: 1 addition & 1 deletion x/incentive/keeper/hooks_amm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

// AfterPoolCreated is called after CreatePool
func (k Keeper) AfterPoolCreated(ctx sdk.Context, sender sdk.AccAddress, poolId uint64) {
k.InitPoolMultiplier(ctx, poolId)
k.InitPoolParams(ctx, poolId)
}

// AfterJoinPool is called after JoinPool, JoinSwapExternAmountIn, and JoinSwapShareAmountOut
Expand Down
51 changes: 50 additions & 1 deletion x/incentive/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,9 @@ func (k Keeper) UpdateLPRewardsUnclaimed(ctx sdk.Context, lpIncentive types.Ince
k.SetFeePool(ctx, feePool)
// ----------------------------------

// Update APR for amm pools
k.UpdateAmmPoolAPR(ctx, lpIncentive)

return nil
}

Expand Down Expand Up @@ -531,7 +534,7 @@ func (k Keeper) CalculateProxyTVL(ctx sdk.Context, baseCurrency string) sdk.Dec
// Get pool info from incentive param
poolInfo, found := k.GetPoolInfo(ctx, stableStakePoolId)
if !found {
k.InitStableStakePoolMultiplier(ctx, stableStakePoolId)
k.InitStableStakePoolParams(ctx, stableStakePoolId)
poolInfo, _ = k.GetPoolInfo(ctx, stableStakePoolId)
}
tvl := stabletypes.TVL(ctx, k.authKeeper, k.bankKeeper, baseCurrency)
Expand All @@ -557,3 +560,49 @@ func (k Keeper) CalculateTVL(ctx sdk.Context) sdk.Dec {

return TVL
}

// Update APR for AMM pool
func (k Keeper) UpdateAmmPoolAPR(ctx sdk.Context, lpIncentive types.IncentiveInfo, totalProxyTVL sdk.Dec) {
// Iterate to calculate total Eden from LpElys, MElys committed
k.amm.IterateLiquidityPools(ctx, func(p ammtypes.Pool) bool {
tvl, err := p.TVL(ctx, k.oracleKeeper)
if err != nil {
return false
}

// Get pool Id
poolId := p.GetPoolId()

// Get pool info from incentive param
poolInfo, found := k.GetPoolInfo(ctx, poolId)
if !found {
return false
}

poolInfo.NumBlocks = poolInfo.NumBlocks.Add(lpIncentive.DistributionEpochInBlocks)
// Invalid block number
if poolInfo.NumBlocks.IsZero() {
return false
}

// Calculate Proxy TVL share considering multiplier
proxyTVL := tvl.Mul(poolInfo.Multiplier)
poolShare := sdk.ZeroDec()
if totalProxyTVL.IsPositive() {
poolShare = proxyTVL.Quo(totalProxyTVL)
}

// Dex reward Apr per pool = (total LM Dex reward allocated per day*((tvl of pool * multiplier)/total proxy TVL) ) * 365 / TVL of pool
totalLMDexRewardsAllocatedPerDay := poolInfo.DexRewardAmountGiven.MulInt(lpIncentive.AllocationEpochInBlocks).QuoInt(poolInfo.NumBlocks)
poolInfo.DexApr = totalLMDexRewardsAllocatedPerDay.Mul(poolShare).MulInt(sdk.NewInt(ptypes.DaysPerYear)).Quo(tvl)

// Eden reward Apr per pool = (total LM Eden reward allocated per day*((tvl of pool * multiplier)/total proxy TVL) ) * 365 / TVL of pool
totalLMEdenRewardsAllocatedPerDay := poolInfo.EdenRewardAmountGiven.Mul(lpIncentive.AllocationEpochInBlocks).Quo(poolInfo.NumBlocks)
poolInfo.EdenApr = sdk.NewDecFromInt(totalLMEdenRewardsAllocatedPerDay).Mul(poolShare).MulInt(sdk.NewInt(ptypes.DaysPerYear)).Quo(tvl)

// Update Pool Info
k.SetPoolInfo(ctx, poolId, poolInfo)

return false
})
}
6 changes: 6 additions & 0 deletions x/incentive/keeper/keeper_lps.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ func (k Keeper) CalculateRewardsForLPs(ctx sdk.Context, totalProxyTVL sdk.Dec, c
totalDexRewardsAllocatedPerDistribution = totalDexRewardsAllocatedPerDistribution.Add(gasRewardsForLP)

//----------------------------------------------------------------

poolInfo.EdenRewardAmountGiven = poolInfo.EdenRewardAmountGiven.Add(totalNewEdenAllocatedPerDistribution)
poolInfo.DexRewardAmountGiven = poolInfo.DexRewardAmountGiven.Add(totalDexRewardsAllocatedPerDistribution)
// Update Pool Info
k.SetPoolInfo(ctx, poolId, poolInfo)

return false
})

Expand Down
34 changes: 32 additions & 2 deletions x/incentive/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,26 @@ func (k Keeper) GetPoolInfo(ctx sdk.Context, poolId uint64) (types.PoolInfo, boo
return types.PoolInfo{}, false
}

// SetPoolInfo
func (k Keeper) SetPoolInfo(ctx sdk.Context, poolId uint64, poolInfo types.PoolInfo) bool {
// Fetch incentive params
params := k.GetParams(ctx)

poolInfos := params.PoolInfos
for i, ps := range poolInfos {
if ps.PoolId == poolId {
params.PoolInfos[i] = poolInfo
k.SetParams(ctx, params)

return true
}
}

return false
}

// InitPoolMultiplier: create a pool information responding to the pool creation.
func (k Keeper) InitPoolMultiplier(ctx sdk.Context, poolId uint64) bool {
func (k Keeper) InitPoolParams(ctx sdk.Context, poolId uint64) bool {
// Fetch incentive params
params := k.GetParams(ctx)
poolInfos := params.PoolInfos
Expand All @@ -81,6 +99,12 @@ func (k Keeper) InitPoolMultiplier(ctx sdk.Context, poolId uint64) bool {
RewardWallet: ammtypes.NewPoolRevenueAddress(poolId).String(),
// multiplier for lp rewards
Multiplier: sdk.NewDec(1),
// Number of blocks since creation
NumBlocks: sdk.NewInt(1),
// Total dex rewards given since creation
DexRewardAmountGiven: sdk.ZeroDec(),
// Total eden rewards given since creation
EdenRewardAmountGiven: sdk.ZeroInt(),
}

// Update pool information
Expand All @@ -91,7 +115,7 @@ func (k Keeper) InitPoolMultiplier(ctx sdk.Context, poolId uint64) bool {
}

// InitStableStakePoolMultiplier: create a stable stake pool information responding to the pool creation.
func (k Keeper) InitStableStakePoolMultiplier(ctx sdk.Context, poolId uint64) bool {
func (k Keeper) InitStableStakePoolParams(ctx sdk.Context, poolId uint64) bool {
// Fetch incentive params
params := k.GetParams(ctx)
poolInfos := params.PoolInfos
Expand All @@ -110,6 +134,12 @@ func (k Keeper) InitStableStakePoolMultiplier(ctx sdk.Context, poolId uint64) bo
RewardWallet: stabletypes.PoolAddress().String(),
// multiplier for lp rewards
Multiplier: sdk.NewDec(1),
// Number of blocks since creation
NumBlocks: sdk.NewInt(1),
// Total dex rewards given since creation
DexRewardAmountGiven: sdk.ZeroDec(),
// Total eden rewards given since creation
EdenRewardAmountGiven: sdk.ZeroInt(),
}

// Update pool information
Expand Down
Loading

0 comments on commit 25f1a33

Please sign in to comment.