Skip to content

Commit

Permalink
feat/QB-2231: Add metrics api for x/pot
Browse files Browse the repository at this point in the history
  • Loading branch information
BoThe1K committed Oct 18, 2023
1 parent c213bf1 commit 9d9ae58
Show file tree
Hide file tree
Showing 11 changed files with 1,876 additions and 142 deletions.
69 changes: 69 additions & 0 deletions proto/stratos/pot/v1/pot.proto
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,73 @@ message TotalReward {
(gogoproto.moretags) = "yaml:\"traffic_reward\"",
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
];
}

message Metrics {
string total_mining_supply = 1 [
(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 = 2 [
(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 = 3 [
(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 = 4 [
(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 = 5 [
(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 = 6 [
(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 = 7 [
(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 = 8 [
(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 = 9 [
(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 = 10 [
(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 = 11 [
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.jsontag) = "meta_mining_reward",
(gogoproto.moretags) = "yaml:\"meta_mining_reward\""
];
}
74 changes: 74 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,73 @@ message QueryTotalRewardByEpochResponse {
];
}

message QueryMetricsRequest {}

message QueryMetricsResponse {
string total_mining_supply = 1 [
(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 = 2 [
(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 = 3 [
(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 = 4 [
(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 = 5 [
(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 = 6 [
(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 = 7 [
(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 = 8 [
(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 = 9 [
(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 = 10 [
(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 = 11 [
(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)
}
}
24 changes: 24 additions & 0 deletions x/pot/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,27 @@ 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{
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
}
53 changes: 53 additions & 0 deletions x/pot/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,56 @@ func (k Keeper) GetTotalReward(ctx sdk.Context, epoch sdk.Int) (totalReward type
}
return
}

func (k Keeper) GetMetrics(ctx sdk.Context) *types.Metrics {
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{
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

0 comments on commit 9d9ae58

Please sign in to comment.