Skip to content

Commit

Permalink
usdc earn program fix (#371)
Browse files Browse the repository at this point in the history
* usdc earn program fix

* add todo
  • Loading branch information
jelysn authored Feb 13, 2024
1 parent 26ef915 commit 6750ddd
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 30 deletions.
34 changes: 18 additions & 16 deletions x/incentive/keeper/apr.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,33 +46,31 @@ func (k Keeper) CalculateApr(ctx sdk.Context, query *types.QueryAprRequest) (mat
// Calculate total Proxy TVL
totalProxyTVL := k.CalculateProxyTVL(ctx, baseCurrency)

// Calculate stable stake pool share.
poolShare := k.CalculatePoolShareForStableStakeLPs(ctx, totalProxyTVL, baseCurrency)

// Eden amount for LP in 24hrs = EpochNumBlocks is the number of block for 24 hrs
epochEdenAmount := lpIncentive.EdenAmountPerYear.Mul(lpIncentive.EpochNumBlocks).Quo(lpIncentive.TotalBlocksPerYear)
epochEdenAmount := lpIncentive.EdenAmountPerYear.
Mul(lpIncentive.EpochNumBlocks).
Quo(lpIncentive.TotalBlocksPerYear)

epochLpsMaxEdenAmount := params.MaxEdenRewardAprLps.Mul(totalProxyTVL).MulInt(lpIncentive.EpochNumBlocks).QuoInt(lpIncentive.TotalBlocksPerYear)
edenPriceDec := k.GetEdenPrice(ctx, baseCurrency)
epochLpsMaxEdenAmount := params.MaxEdenRewardAprLps.
Mul(totalProxyTVL).
MulInt(lpIncentive.EpochNumBlocks).
QuoInt(lpIncentive.TotalBlocksPerYear).
Quo(edenPriceDec)

// Use min amount (eden allocation from tokenomics and max apr based eden amount)
epochEdenAmount = sdk.MinInt(epochEdenAmount, epochLpsMaxEdenAmount.TruncateInt())

// Eden amount for stable stake LP in 24hrs
epochStableStakeEdenAmount := sdk.NewDecFromInt(epochEdenAmount).Mul(poolShare)

// Calc Eden price in usdc
// We put Elys as denom as Eden won't be avaialble in amm pool and has the same value as Elys
// TODO: replace to use spot price
edenPrice := k.EstimatePrice(ctx, sdk.NewCoin(ptypes.Elys, sdk.NewInt(100000)), baseCurrency)
stableStakePoolShare := k.CalculatePoolShareForStableStakeLPs(ctx, totalProxyTVL, baseCurrency)
epochStableStakeEdenAmount := sdk.NewDecFromInt(epochEdenAmount).Mul(stableStakePoolShare)

// Eden Apr for usdc earn program = {(Eden allocated for stable stake pool per day*365*price{eden/usdc}/(total usdc deposit)}*100
// we divide 100000 as we have use 100000elys as input in the price estimation
apr := epochStableStakeEdenAmount.
MulInt(sdk.NewInt(ptypes.DaysPerYear)).
MulInt(edenPrice).
Mul(edenPriceDec).
MulInt(sdk.NewInt(100)).
QuoInt(totalUSDCDeposit.Amount).
QuoInt(sdk.NewInt(100000))
QuoInt(totalUSDCDeposit.Amount)
return apr.TruncateInt(), nil
} else {
// Elys staking, Eden committed, EdenB committed.
Expand Down Expand Up @@ -112,7 +110,11 @@ func (k Keeper) CalculateApr(ctx sdk.Context, query *types.QueryAprRequest) (mat
} else if query.Denom == ptypes.BaseCurrency {
if query.WithdrawType == commitmenttypes.EarnType_USDC_PROGRAM {
params := k.stableKeeper.GetParams(ctx)
apr := params.InterestRate.MulInt(sdk.NewInt(100))
res, err := k.stableKeeper.BorrowRatio(ctx, &stabletypes.QueryBorrowRatioRequest{})
if err != nil {
return sdk.ZeroInt(), err
}
apr := params.InterestRate.Mul(res.BorrowRatio).MulInt(sdk.NewInt(100))
return apr.TruncateInt(), nil
} else {
// Elys staking, Eden committed, EdenB committed.
Expand Down
15 changes: 15 additions & 0 deletions x/incentive/keeper/estimate_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
ptypes "github.com/elys-network/elys/x/parameter/types"
)

// Estimate the price : eg, 1 Eden -> x usdc
Expand All @@ -26,3 +27,17 @@ func (k Keeper) EstimatePrice(ctx sdk.Context, tokenIn sdk.Coin, baseCurrency st
}
return swapResult.Amount
}

func (k Keeper) GetEdenPrice(ctx sdk.Context, baseCurrency string) math.LegacyDec {
// Calc Eden price in usdc
// We put Elys as denom as Eden won't be avaialble in amm pool and has the same value as Elys
// TODO: replace to use spot price
// TODO: Remember to use the $ value of Eden price and not eden/usdc
// TODO: to be updated as part of spot price calculation PR
edenPrice := k.EstimatePrice(ctx, sdk.NewCoin(ptypes.Elys, sdk.NewInt(100000)), baseCurrency)
edenPriceDec := sdk.NewDecFromInt(edenPrice).Quo(sdk.NewDec(100000))
if edenPriceDec.IsZero() {
edenPriceDec = sdk.OneDec()
}
return edenPriceDec
}
11 changes: 9 additions & 2 deletions x/incentive/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,14 +360,21 @@ func (k Keeper) UpdateLPRewardsUnclaimed(ctx sdk.Context, lpIncentive types.Ince
}

// Calculate eden amount per epoch
epochLpsEdenAmount := lpIncentive.EdenAmountPerYear.Mul(lpIncentive.EpochNumBlocks).Quo(lpIncentive.TotalBlocksPerYear)
epochLpsEdenAmount := lpIncentive.EdenAmountPerYear.
Mul(lpIncentive.EpochNumBlocks).
Quo(lpIncentive.TotalBlocksPerYear)

// Track the DEX rewards distribution for stakers
params := k.GetParams(ctx)

// Maximum eden based per distribution epoch on maximum APR - 30% by default
// Allocated for staking per day = (0.3/365)* (total weighted proxy TVL)
epochLpsMaxEdenAmount := params.MaxEdenRewardAprLps.Mul(totalProxyTVL).MulInt(lpIncentive.EpochNumBlocks).QuoInt(lpIncentive.TotalBlocksPerYear)
edenPriceDec := k.GetEdenPrice(ctx, baseCurrency)
epochLpsMaxEdenAmount := params.MaxEdenRewardAprLps.
Mul(totalProxyTVL).
MulInt(lpIncentive.EpochNumBlocks).
QuoInt(lpIncentive.TotalBlocksPerYear).
Quo(edenPriceDec)

// Use min amount (eden allocation from tokenomics and max apr based eden amount)
epochLpsEdenAmount = sdk.MinInt(epochLpsEdenAmount, epochLpsMaxEdenAmount.TruncateInt())
Expand Down
3 changes: 3 additions & 0 deletions x/incentive/types/expected_keepers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import (
context "context"

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/types"
Expand Down Expand Up @@ -137,6 +139,7 @@ type EpochsKeeper interface {
// StableStakeKeeper defines the expected epochs keeper used for simulations (noalias)
type StableStakeKeeper interface {
GetParams(ctx sdk.Context) (params stabletypes.Params)
BorrowRatio(goCtx context.Context, req *stabletypes.QueryBorrowRatioRequest) (*stabletypes.QueryBorrowRatioResponse, error)
}

// TokenomicsKeeper defines the expected tokenomics keeper used for simulations (noalias)
Expand Down
13 changes: 1 addition & 12 deletions x/incentive/types/query.pb.gw.go

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

0 comments on commit 6750ddd

Please sign in to comment.