-
Notifications
You must be signed in to change notification settings - Fork 41
/
apr_stable_stake.go
78 lines (64 loc) · 2.49 KB
/
apr_stable_stake.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package keeper
import (
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
assetprofiletypes "github.com/elys-network/elys/x/assetprofile/types"
"github.com/elys-network/elys/x/masterchef/types"
ptypes "github.com/elys-network/elys/x/parameter/types"
stabletypes "github.com/elys-network/elys/x/stablestake/types"
)
func (k Keeper) CalculateStableStakeApr(ctx sdk.Context, query *types.QueryStableStakeAprRequest) (math.Int, error) {
// Fetch incentive params
params := k.GetParams(ctx)
// Update params
defer k.SetParams(ctx, params)
// If we don't have enough params
if query.Denom == ptypes.Eden {
lpIncentive := params.LpIncentives
if lpIncentive == nil || lpIncentive.EdenAmountPerYear.IsNil() {
return sdk.ZeroInt(), nil
}
totalBlocksPerYear := k.parameterKeeper.GetParams(ctx).TotalBlocksPerYear
if totalBlocksPerYear == 0 {
return sdk.ZeroInt(), nil
}
baseCurrency, found := k.assetProfileKeeper.GetUsdcDenom(ctx)
if !found {
return sdk.ZeroInt(), errorsmod.Wrapf(assetprofiletypes.ErrAssetProfileNotFound, "asset %s not found", ptypes.BaseCurrency)
}
stableTvl := k.stableKeeper.TVL(ctx, k.oracleKeeper, baseCurrency)
if stableTvl.IsZero() {
return sdk.ZeroInt(), nil
}
// Calculate total Proxy TVL
totalProxyTVL := k.CalculateProxyTVL(ctx, baseCurrency)
edenAmount := lpIncentive.EdenAmountPerYear.Quo(sdk.NewInt(totalBlocksPerYear))
edenDenomPrice := k.amm.GetEdenDenomPrice(ctx, baseCurrency)
// Eden amount for stable stake LP in 24hrs
stableStakePoolShare := k.CalculatePoolShareForStableStakeLPs(ctx, totalProxyTVL, baseCurrency)
stableStakeEdenAmount := sdk.NewDecFromInt(edenAmount).Mul(stableStakePoolShare)
params := k.GetParams(ctx)
poolMaxEdenAmount := params.MaxEdenRewardAprLps.
Mul(stableTvl).
QuoInt64(totalBlocksPerYear).
Quo(edenDenomPrice)
stableStakeEdenAmount = sdk.MinDec(stableStakeEdenAmount, poolMaxEdenAmount)
// Eden Apr for usdc earn program
apr := stableStakeEdenAmount.
MulInt64(totalBlocksPerYear).
Mul(edenDenomPrice).
MulInt(sdk.NewInt(100)).
Quo(stableTvl)
return apr.TruncateInt(), nil
} else if query.Denom == ptypes.BaseCurrency {
params := k.stableKeeper.GetParams(ctx)
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
}
return sdk.ZeroInt(), nil
}