Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/QB-2231: Add metrics api for x/pot #322

Merged
merged 1 commit into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 76 additions & 1 deletion proto/stratos/pot/v1/pot.proto
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,79 @@ message TotalReward {
(gogoproto.moretags) = "yaml:\"traffic_reward\"",
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
];
}
}

message Metrics {
string total_supply = 1 [
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.jsontag) = "total_supply",
(gogoproto.moretags) = "yaml:\"total_supply\""
];
string total_mining_supply = 2 [
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.jsontag) = "total_mining_supply",
(gogoproto.moretags) = "yaml:\"total_mining_supply\""
];
string total_mined_tokens = 3 [
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.jsontag) = "total_mined_tokens",
(gogoproto.moretags) = "yaml:\"total_mined_tokens\""
];
string total_resource_nodes_deposit = 4 [
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.jsontag) = "total_resource_nodes_deposit",
(gogoproto.moretags) = "yaml:\"total_resource_nodes_deposit\""
];
string total_bonded_delegation = 5 [
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.jsontag) = "total_bonded_delegation",
(gogoproto.moretags) = "yaml:\"total_bonded_delegation\""
];
string total_unbonded_delegation = 6 [
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.jsontag) = "total_unbonded_delegation",
(gogoproto.moretags) = "yaml:\"total_unbonded_delegation\""
];
string total_unbonding_delegation = 7 [
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.jsontag) = "total_unbonding_delegation",
(gogoproto.moretags) = "yaml:\"total_unbonding_delegation\""
];
string circulation_supply = 8 [
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.jsontag) = "circulation_supply",
(gogoproto.moretags) = "yaml:\"circulation_supply\""
];
string total_mining_reward = 9 [
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.jsontag) = "total_mining_reward",
(gogoproto.moretags) = "yaml:\"total_mining_reward\""
];
string chain_mining_reward = 10 [
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.jsontag) = "chain_mining_reward",
(gogoproto.moretags) = "yaml:\"chain_mining_reward\""
];
string resource_mining_reward = 11 [
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.jsontag) = "resource_mining_reward",
(gogoproto.moretags) = "yaml:\"resource_mining_reward\""
];
string meta_mining_reward = 12 [
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.jsontag) = "meta_mining_reward",
(gogoproto.moretags) = "yaml:\"meta_mining_reward\""
];
}
80 changes: 80 additions & 0 deletions proto/stratos/pot/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ service Query {
rpc TotalRewardByEpoch(QueryTotalRewardByEpochRequest) returns (QueryTotalRewardByEpochResponse) {
option (google.api.http).get = "/stratos/pot/v1/total-reward/{epoch}";
}

rpc Metrics(QueryMetricsRequest) returns (QueryMetricsResponse) {
option (google.api.http).get = "/stratos/pot/v1/metrics";
}
}

// QueryVolumeReportRequest is request type for the Query/VolumeReport RPC method
Expand Down Expand Up @@ -158,3 +162,79 @@ message QueryTotalRewardByEpochResponse {
];
}

message QueryMetricsRequest {}

message QueryMetricsResponse {
string total_supply = 1 [
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.jsontag) = "total_supply",
(gogoproto.moretags) = "yaml:\"total_supply\""
];
string total_mining_supply = 2 [
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.jsontag) = "total_mining_supply",
(gogoproto.moretags) = "yaml:\"total_mining_supply\""
];
string total_mined_tokens = 3 [
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.jsontag) = "total_mined_tokens",
(gogoproto.moretags) = "yaml:\"total_mined_tokens\""
];
string total_resource_nodes_deposit = 4 [
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.jsontag) = "total_resource_nodes_deposit",
(gogoproto.moretags) = "yaml:\"total_resource_nodes_deposit\""
];
string total_bonded_delegation = 5 [
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.jsontag) = "total_bonded_delegation",
(gogoproto.moretags) = "yaml:\"total_bonded_delegation\""
];
string total_unbonded_delegation = 6 [
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.jsontag) = "total_unbonded_delegation",
(gogoproto.moretags) = "yaml:\"total_unbonded_delegation\""
];
string total_unbonding_delegation = 7 [
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.jsontag) = "total_unbonding_delegation",
(gogoproto.moretags) = "yaml:\"total_unbonding_delegation\""
];
string circulation_supply = 8 [
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.jsontag) = "circulation_supply",
(gogoproto.moretags) = "yaml:\"circulation_supply\""
];
string total_mining_reward = 9 [
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.jsontag) = "total_mining_reward",
(gogoproto.moretags) = "yaml:\"total_mining_reward\""
];
string chain_mining_reward = 10 [
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.jsontag) = "chain_mining_reward",
(gogoproto.moretags) = "yaml:\"chain_mining_reward\""
];
string resource_mining_reward = 11 [
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.jsontag) = "resource_mining_reward",
(gogoproto.moretags) = "yaml:\"resource_mining_reward\""
];
string meta_mining_reward = 12 [
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.jsontag) = "meta_mining_reward",
(gogoproto.moretags) = "yaml:\"meta_mining_reward\""
];
}
19 changes: 19 additions & 0 deletions x/pot/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func registerQueryRoutes(clientCtx client.Context, r *mux.Router) {
r.HandleFunc("/pot/total-mined-token", getTotalMinedTokenHandlerFn(clientCtx, types.QueryTotalMinedToken)).Methods("GET")
r.HandleFunc("/pot/circulation-supply", getCirculationSupplyHandlerFn(clientCtx, types.QueryCirculationSupply)).Methods("GET")
r.HandleFunc("/pot/total-reward/{epoch}", getTotalRewardByEpochHandlerFn(clientCtx, types.QueryTotalRewardByEpoch)).Methods("GET")
r.HandleFunc("/pot/metrics", getMetricsHandlerFn(clientCtx, types.QueryMetrics)).Methods("GET")
}

// GET request handler to query params of POT module
Expand Down Expand Up @@ -259,3 +260,21 @@ func getTotalRewardByEpochHandlerFn(clientCtx client.Context, queryPath string)
rest.PostProcessResponse(w, cliCtx, res)
}
}

func getMetricsHandlerFn(clientCtx client.Context, queryPath string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, clientCtx, r)
if !ok {
return
}

route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, queryPath)
res, height, err := cliCtx.Query(route)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, res)
}
}
25 changes: 25 additions & 0 deletions x/pot/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,28 @@ func (q Querier) TotalRewardByEpoch(c context.Context, req *types.QueryTotalRewa
totalReward := q.GetTotalReward(ctx, epoch)
return &types.QueryTotalRewardByEpochResponse{TotalReward: totalReward}, nil
}

func (q Querier) Metrics(c context.Context, req *types.QueryMetricsRequest) (
*types.QueryMetricsResponse, error) {
if req == nil {
return &types.QueryMetricsResponse{}, status.Error(codes.InvalidArgument, "empty request")
}

ctx := sdk.UnwrapSDKContext(c)

metrics := q.GetMetrics(ctx)
return &types.QueryMetricsResponse{
TotalSupply: metrics.TotalSupply,
TotalMiningSupply: metrics.TotalMiningSupply,
TotalMinedTokens: metrics.TotalMinedTokens,
TotalResourceNodesDeposit: metrics.TotalResourceNodesDeposit,
TotalBondedDelegation: metrics.TotalBondedDelegation,
TotalUnbondedDelegation: metrics.TotalUnbondedDelegation,
TotalUnbondingDelegation: metrics.TotalUnbondingDelegation,
CirculationSupply: metrics.CirculationSupply,
TotalMiningReward: metrics.TotalMiningReward,
ChainMiningReward: metrics.ChainMiningReward,
ResourceMiningReward: metrics.ResourceMiningReward,
MetaMiningReward: metrics.MetaMiningReward,
}, nil
}
61 changes: 58 additions & 3 deletions x/pot/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ func (k Keeper) GetCirculationSupply(ctx sdk.Context) (circulationSupply sdk.Coi
metaNodeBondedPoolAcc := k.accountKeeper.GetModuleAddress(registertypes.MetaNodeNotBondedPool)
metaNodeDeposit := k.bankKeeper.GetBalance(ctx, metaNodeBondedPoolAcc, k.BondDenom(ctx))

miningPoolAcc := k.accountKeeper.GetModuleAddress(types.FoundationAccount)
miningPool := k.bankKeeper.GetBalance(ctx, miningPoolAcc, k.BondDenom(ctx))
totalMining := k.GetTotalMining(ctx)
totalMinedTokens := k.GetTotalMinedTokens(ctx)

unissuedPrepayAcc := k.accountKeeper.GetModuleAddress(registertypes.TotalUnissuedPrepay)
unissuedPrepay := k.bankKeeper.GetBalance(ctx, unissuedPrepayAcc, k.BondDenom(ctx))
Expand All @@ -178,7 +178,7 @@ func (k Keeper) GetCirculationSupply(ctx sdk.Context) (circulationSupply sdk.Coi
Sub(validatorStaking).
Sub(resourceNodeDeposit).
Sub(metaNodeDeposit).
Sub(miningPool).
Sub(totalMining.Sub(totalMinedTokens)).
Sub(unissuedPrepay)

circulationSupply = sdk.NewCoins(circulationSupplyStos)
Expand Down Expand Up @@ -243,3 +243,58 @@ func (k Keeper) GetTotalReward(ctx sdk.Context, epoch sdk.Int) (totalReward type
}
return
}

func (k Keeper) GetMetrics(ctx sdk.Context) *types.Metrics {
totalSupply := k.bankKeeper.GetSupply(ctx, k.BondDenom(ctx))
totalMining := k.GetTotalMining(ctx)
totalMinedTokens := k.GetTotalMinedTokens(ctx)

totalBondedDepositOfResourceNodes := k.registerKeeper.GetResourceNodeBondedToken(ctx).Amount
totalUnbondedDepositOfResourceNodes := k.registerKeeper.GetResourceNodeNotBondedToken(ctx).Amount
totalResourceNodesDeposit := totalBondedDepositOfResourceNodes.Add(totalUnbondedDepositOfResourceNodes)

denom := k.BondDenom(ctx)

validatorBondedPoolAcc := k.accountKeeper.GetModuleAddress(stakingtypes.BondedPoolName)
boundedDelegation := k.bankKeeper.GetBalance(ctx, validatorBondedPoolAcc, denom)

validatorUnbondedPoolAcc := k.accountKeeper.GetModuleAddress(stakingtypes.NotBondedPoolName)
unbondedDelegation := k.bankKeeper.GetBalance(ctx, validatorUnbondedPoolAcc, denom)

unbondingDelegation := sdk.NewCoin(denom, sdk.NewInt(0))
// NOTE: Uncomment to get all unboundings, not tested on performance
// k.stakingKeeper.IterateUnbondingDelegations(ctx, func(_ int64, ubd stakingtypes.UnbondingDelegation) (stop bool) {
// for _, entry := range ubd.Entries {
// unbondingDelegation = unbondingDelegation.Add(entry.Balance)
// }
// return false
// })

circulationSupply := k.GetCirculationSupply(ctx)

totalMiningReward := sdk.NewInt(0)
chainMiningReward := sdk.NewInt(0)
resourceMiningReward := sdk.NewInt(0)
metaMiningReward := sdk.NewInt(0)
if miningParam, err := k.GetMiningRewardParamByMinedToken(ctx, totalMinedTokens); err == nil {
totalMiningReward = miningParam.MiningReward.Amount
chainMiningReward = totalMiningReward.Mul(miningParam.BlockChainPercentageInBp).Quo(sdk.NewInt(10000))
resourceMiningReward = totalMiningReward.Mul(miningParam.ResourceNodePercentageInBp).Quo(sdk.NewInt(10000))
metaMiningReward = totalMiningReward.Mul(miningParam.MetaNodePercentageInBp).Quo(sdk.NewInt(10000))
}

return &types.Metrics{
TotalSupply: totalSupply.Amount,
TotalMiningSupply: totalMining.Amount,
TotalMinedTokens: totalMinedTokens.Amount,
TotalResourceNodesDeposit: totalResourceNodesDeposit,
TotalBondedDelegation: boundedDelegation.Amount,
TotalUnbondedDelegation: unbondedDelegation.Amount,
TotalUnbondingDelegation: unbondingDelegation.Amount,
CirculationSupply: circulationSupply.AmountOf(denom),
TotalMiningReward: totalMiningReward,
ChainMiningReward: chainMiningReward,
ResourceMiningReward: resourceMiningReward,
MetaMiningReward: metaMiningReward,
}
}
5 changes: 5 additions & 0 deletions x/pot/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ func (k Keeper) GetMiningRewardParamByMinedToken(ctx sdk.Context, minedToken sdk
return miningRewardParams[len(miningRewardParams)-1], types.ErrOutOfIssuance
}

func (k Keeper) GetTotalMining(ctx sdk.Context) sdk.Coin {
miningRewardParams := k.MiningRewardParams(ctx)
return miningRewardParams[len(miningRewardParams)-1].TotalMinedValveEnd
}

func (k Keeper) GetCommunityTax(ctx sdk.Context) (res sdk.Dec) {
k.paramSpace.Get(ctx, types.KeyCommunityTax, &res)
return
Expand Down
12 changes: 12 additions & 0 deletions x/pot/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func NewQuerier(k Keeper, legacyQuerierCdc *codec.LegacyAmino) sdk.Querier {
return getCirculationSupply(ctx, req, k, legacyQuerierCdc)
case types.QueryTotalRewardByEpoch:
return getTotalRewardByEpoch(ctx, req, k, legacyQuerierCdc)
case types.QueryMetrics:
return getMetrics(ctx, req, k, legacyQuerierCdc)
default:
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "unknown pot query endpoint")
}
Expand Down Expand Up @@ -194,3 +196,13 @@ func getTotalRewardByEpoch(ctx sdk.Context, req abci.RequestQuery, k Keeper, leg
}
return bz, nil
}

func getMetrics(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
metrics := k.GetMetrics(ctx)

bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, metrics)
if err != nil {
return []byte{}, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
3 changes: 3 additions & 0 deletions x/pot/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

stratos "github.com/stratosnet/stratos-chain/types"
"github.com/stratosnet/stratos-chain/x/register/types"
Expand Down Expand Up @@ -41,6 +42,7 @@ type RegisterKeeper interface {
GetDepositNozRate(ctx sdk.Context) (depositNozRate sdk.Dec)

GetResourceNodeBondedToken(ctx sdk.Context) (token sdk.Coin)
GetResourceNodeNotBondedToken(ctx sdk.Context) (token sdk.Coin)
GetMetaNodeBondedToken(ctx sdk.Context) (token sdk.Coin)

GetEffectiveTotalDeposit(ctx sdk.Context) (deposit sdk.Int)
Expand All @@ -58,6 +60,7 @@ type RegisterKeeper interface {

type StakingKeeper interface {
TotalBondedTokens(ctx sdk.Context) sdk.Int
IterateUnbondingDelegations(ctx sdk.Context, fn func(index int64, ubd stakingtypes.UnbondingDelegation) (stop bool))
}

type DistrKeeper interface {
Expand Down
Loading