mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-27 08:45:19 +00:00
614d4e40fe
* Update cometbft, cosmos, ethermint, and ibc-go * Replace github.com/tendermint/tendermint by github.com/cometbft/cometbft * Replace github.com/tendermint/tm-db by github.com/cometbft/cometbft-db * Replace gogo/protobuf with cosmos/gogoproto & simapp replacement * Replace cosmos-sdk/simapp/helpers with cosmos-sdk/testutil/sims * Remove no longer used simulations * Replace ibchost with ibcexported See https://github.com/cosmos/ibc-go/blob/v7.2.2/docs/migrations/v6-to-v7.md#ibc-module-constants * Add new consensus params keeper * Add consensus keeper to blockers * Fix keeper and module issues in app.go * Add IsSendEnabledCoins and update SetParams interface changes * Fix protobuf build for cosmos 47 (#1800) * fix cp errors by using -f; fix lint by only linting our proto dir; and use proofs.proto directly from ics23 for ibc-go v7 * run proto-all; commit updated third party deps and swagger changes * regenerate proto files * use correct gocosmos build plugin for buf * re-gen all protobuf files to update paths for new gocosmos plugin * update protoc and buf to latest versions * fix staking keeper issues in app.go * update tally handler for gov changes * chain id fix and flag fixes * update deps for cometbft 47.7 upgrade * remove all module legacy queriers * update stakingKeeper to pointer * Replace ModuleCdc from govv1beta1 to govcodec * remove simulations * abci.LastCommitInfo → abci.CommitInfo * Remove unused code in keys.go * simapp.MakeTestEncodingConfig -> moduletestutil.MakeTestEncodingConfi * Fix chain id issues in tests * Fix remaining unit test issues * Update changelog for upgrade * Fix e2e tests using updated kvtool * Update protonet to v47 compatible genesis * Bump cometbft-db to v0.9.1-kava.1 * Update kvtool * Remove extra changelog * Fix merged rocksdb issues * go mod cleanup * Bump cometbft-db to v9 and go to 1.21 * Bump rocksdb version to v8.10.0 * Update kvtool to latest version * Update gin to v1.9.0 * Use ibctm.ModuleName in app_test * Fallback to genesis chain id instead of client toml * Remove all simulations * Fix cdp migrations issue with v47 * Update dependencies to correct tags --------- Co-authored-by: Nick DeLuca <nickdeluca08@gmail.com>
145 lines
4.5 KiB
Go
145 lines
4.5 KiB
Go
package keeper
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
sdkmath "cosmossdk.io/math"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
earntypes "github.com/kava-labs/kava/x/earn/types"
|
|
"github.com/kava-labs/kava/x/incentive/types"
|
|
liquidtypes "github.com/kava-labs/kava/x/liquid/types"
|
|
)
|
|
|
|
const (
|
|
SecondsPerYear = 31536000
|
|
)
|
|
|
|
// GetStakingAPR returns the total APR for staking and incentive rewards
|
|
func GetStakingAPR(ctx sdk.Context, k Keeper, params types.Params) (sdk.Dec, error) {
|
|
// Get staking APR + incentive APR
|
|
inflationRate := k.mintKeeper.GetMinter(ctx).Inflation
|
|
communityTax := k.distrKeeper.GetCommunityTax(ctx)
|
|
|
|
bondedTokens := k.stakingKeeper.TotalBondedTokens(ctx)
|
|
circulatingSupply := k.bankKeeper.GetSupply(ctx, types.BondDenom)
|
|
|
|
// Staking APR = (Inflation Rate * (1 - Community Tax)) / (Bonded Tokens / Circulating Supply)
|
|
stakingAPR := inflationRate.
|
|
Mul(sdk.OneDec().Sub(communityTax)).
|
|
Quo(sdk.NewDecFromInt(bondedTokens).
|
|
Quo(sdk.NewDecFromInt(circulatingSupply.Amount)))
|
|
|
|
// Get incentive APR
|
|
bkavaRewardPeriod, found := params.EarnRewardPeriods.GetMultiRewardPeriod(liquidtypes.DefaultDerivativeDenom)
|
|
if !found {
|
|
// No incentive rewards for bkava, only staking rewards
|
|
return stakingAPR, nil
|
|
}
|
|
|
|
// Total amount of bkava in earn vaults, this may be lower than total bank
|
|
// supply of bkava as some bkava may not be deposited in earn vaults
|
|
totalEarnBkavaDeposited := sdk.ZeroInt()
|
|
|
|
var iterErr error
|
|
k.earnKeeper.IterateVaultRecords(ctx, func(record earntypes.VaultRecord) (stop bool) {
|
|
if !k.liquidKeeper.IsDerivativeDenom(ctx, record.TotalShares.Denom) {
|
|
return false
|
|
}
|
|
|
|
vaultValue, err := k.earnKeeper.GetVaultTotalValue(ctx, record.TotalShares.Denom)
|
|
if err != nil {
|
|
iterErr = err
|
|
return false
|
|
}
|
|
|
|
totalEarnBkavaDeposited = totalEarnBkavaDeposited.Add(vaultValue.Amount)
|
|
|
|
return false
|
|
})
|
|
|
|
if iterErr != nil {
|
|
return sdk.ZeroDec(), iterErr
|
|
}
|
|
|
|
// Incentive APR = rewards per second * seconds per year / total supplied to earn vaults
|
|
// Override collateral type to use "kava" instead of "bkava" when fetching
|
|
incentiveAPY, err := GetAPYFromMultiRewardPeriod(ctx, k, types.BondDenom, bkavaRewardPeriod, totalEarnBkavaDeposited)
|
|
if err != nil {
|
|
return sdk.ZeroDec(), err
|
|
}
|
|
|
|
totalAPY := stakingAPR.Add(incentiveAPY)
|
|
return totalAPY, nil
|
|
}
|
|
|
|
// GetAPYFromMultiRewardPeriod calculates the APY for a given MultiRewardPeriod
|
|
func GetAPYFromMultiRewardPeriod(
|
|
ctx sdk.Context,
|
|
k Keeper,
|
|
collateralType string,
|
|
rewardPeriod types.MultiRewardPeriod,
|
|
totalSupply sdkmath.Int,
|
|
) (sdk.Dec, error) {
|
|
if totalSupply.IsZero() {
|
|
return sdk.ZeroDec(), nil
|
|
}
|
|
|
|
// Get USD value of collateral type
|
|
collateralUSDValue, err := k.pricefeedKeeper.GetCurrentPrice(ctx, getMarketID(collateralType))
|
|
if err != nil {
|
|
return sdk.ZeroDec(), fmt.Errorf(
|
|
"failed to get price for incentive collateralType %s with market ID %s: %w",
|
|
collateralType, getMarketID(collateralType), err,
|
|
)
|
|
}
|
|
|
|
// Total USD value of the collateral type total supply
|
|
totalSupplyUSDValue := sdk.NewDecFromInt(totalSupply).Mul(collateralUSDValue.Price)
|
|
|
|
totalUSDRewardsPerSecond := sdk.ZeroDec()
|
|
|
|
// In many cases, RewardsPerSecond are assets that are different from the
|
|
// CollateralType, so we need to use the USD value of CollateralType and
|
|
// RewardsPerSecond to determine the APY.
|
|
for _, reward := range rewardPeriod.RewardsPerSecond {
|
|
// Get USD value of 1 unit of reward asset type, using TWAP
|
|
rewardDenomUSDValue, err := k.pricefeedKeeper.GetCurrentPrice(ctx, getMarketID(reward.Denom))
|
|
if err != nil {
|
|
return sdk.ZeroDec(), fmt.Errorf("failed to get price for RewardsPerSecond asset %s: %w", reward.Denom, err)
|
|
}
|
|
|
|
rewardPerSecond := sdk.NewDecFromInt(reward.Amount).Mul(rewardDenomUSDValue.Price)
|
|
totalUSDRewardsPerSecond = totalUSDRewardsPerSecond.Add(rewardPerSecond)
|
|
}
|
|
|
|
// APY = USD rewards per second * seconds per year / USD total supplied
|
|
apy := totalUSDRewardsPerSecond.
|
|
MulInt64(SecondsPerYear).
|
|
Quo(totalSupplyUSDValue)
|
|
|
|
return apy, nil
|
|
}
|
|
|
|
func getMarketID(denom string) string {
|
|
// Rewrite denoms as pricefeed has different names for some assets,
|
|
// e.g. "ukava" -> "kava", "erc20/multichain/usdc" -> "usdc"
|
|
// bkava is not included as it is handled separately
|
|
|
|
// TODO: Replace hardcoded conversion with possible params set somewhere
|
|
// to be more flexible. E.g. a map of denoms to pricefeed market denoms in
|
|
// pricefeed params.
|
|
switch denom {
|
|
case types.BondDenom:
|
|
denom = "kava"
|
|
case "erc20/multichain/usdc":
|
|
denom = "usdc"
|
|
case "erc20/multichain/usdt":
|
|
denom = "usdt"
|
|
case "erc20/multichain/dai":
|
|
denom = "dai"
|
|
}
|
|
|
|
return fmt.Sprintf("%s:usd:30", denom)
|
|
}
|