2019-01-18 11:38:40 -08:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
2019-04-16 06:43:08 -07:00
|
|
|
"encoding/json"
|
2019-01-18 11:38:40 -08:00
|
|
|
|
2019-01-23 03:37:03 -08:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
|
2019-01-18 11:38:40 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-12-27 09:57:54 -08:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2019-01-18 11:38:40 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/distribution/types"
|
2019-06-05 10:42:25 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/exported"
|
2019-01-18 11:38:40 -08:00
|
|
|
)
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
func NewQuerier(k Keeper, legacyQuerierCdc *codec.LegacyAmino) sdk.Querier {
|
2019-12-27 09:57:54 -08:00
|
|
|
return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, error) {
|
2019-01-18 11:38:40 -08:00
|
|
|
switch path[0] {
|
2019-06-05 16:26:17 -07:00
|
|
|
case types.QueryParams:
|
2020-07-25 01:03:58 -07:00
|
|
|
return queryParams(ctx, path[1:], req, k, legacyQuerierCdc)
|
2019-01-29 16:06:38 -08:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
case types.QueryValidatorOutstandingRewards:
|
2020-07-25 01:03:58 -07:00
|
|
|
return queryValidatorOutstandingRewards(ctx, path[1:], req, k, legacyQuerierCdc)
|
2019-01-29 16:06:38 -08:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
case types.QueryValidatorCommission:
|
2020-07-25 01:03:58 -07:00
|
|
|
return queryValidatorCommission(ctx, path[1:], req, k, legacyQuerierCdc)
|
2019-01-29 16:06:38 -08:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
case types.QueryValidatorSlashes:
|
2020-07-25 01:03:58 -07:00
|
|
|
return queryValidatorSlashes(ctx, path[1:], req, k, legacyQuerierCdc)
|
2019-01-29 16:06:38 -08:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
case types.QueryDelegationRewards:
|
2020-07-25 01:03:58 -07:00
|
|
|
return queryDelegationRewards(ctx, path[1:], req, k, legacyQuerierCdc)
|
2019-01-29 16:06:38 -08:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
case types.QueryDelegatorTotalRewards:
|
2020-07-25 01:03:58 -07:00
|
|
|
return queryDelegatorTotalRewards(ctx, path[1:], req, k, legacyQuerierCdc)
|
2019-02-04 16:45:18 -08:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
case types.QueryDelegatorValidators:
|
2020-07-25 01:03:58 -07:00
|
|
|
return queryDelegatorValidators(ctx, path[1:], req, k, legacyQuerierCdc)
|
2019-02-04 16:45:18 -08:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
case types.QueryWithdrawAddr:
|
2020-07-25 01:03:58 -07:00
|
|
|
return queryDelegatorWithdrawAddress(ctx, path[1:], req, k, legacyQuerierCdc)
|
2019-01-29 16:06:38 -08:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
case types.QueryCommunityPool:
|
2020-07-25 01:03:58 -07:00
|
|
|
return queryCommunityPool(ctx, path[1:], req, k, legacyQuerierCdc)
|
2019-03-27 18:05:36 -07:00
|
|
|
|
2019-01-18 11:38:40 -08:00
|
|
|
default:
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown query path: %s", path[0])
|
2019-01-18 11:38:40 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
func queryParams(ctx sdk.Context, _ []string, _ abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2020-01-10 12:08:14 -08:00
|
|
|
params := k.GetParams(ctx)
|
2019-12-27 09:57:54 -08:00
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
res, err := codec.MarshalJSONIndent(legacyQuerierCdc, params)
|
2020-01-10 12:08:14 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2019-01-18 11:38:40 -08:00
|
|
|
}
|
2020-01-10 12:08:14 -08:00
|
|
|
|
|
|
|
return res, nil
|
2019-01-18 11:38:40 -08:00
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
func queryValidatorOutstandingRewards(ctx sdk.Context, _ []string, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2019-06-05 16:26:17 -07:00
|
|
|
var params types.QueryValidatorOutstandingRewardsParams
|
2020-07-25 01:03:58 -07:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2019-03-06 10:54:12 -08:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2019-03-06 10:54:12 -08:00
|
|
|
}
|
2019-12-27 09:57:54 -08:00
|
|
|
|
2019-10-28 07:09:04 -07:00
|
|
|
rewards := k.GetValidatorOutstandingRewards(ctx, params.ValidatorAddress)
|
2020-02-11 06:58:37 -08:00
|
|
|
if rewards.GetRewards() == nil {
|
|
|
|
rewards.Rewards = sdk.DecCoins{}
|
2019-10-28 07:09:04 -07:00
|
|
|
}
|
2019-12-27 09:57:54 -08:00
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, rewards)
|
2019-01-18 11:38:40 -08:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2019-01-18 11:38:40 -08:00
|
|
|
}
|
2019-12-27 09:57:54 -08:00
|
|
|
|
2019-01-18 11:38:40 -08:00
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
func queryValidatorCommission(ctx sdk.Context, _ []string, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2019-06-05 16:26:17 -07:00
|
|
|
var params types.QueryValidatorCommissionParams
|
2020-07-25 01:03:58 -07:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2019-01-18 11:38:40 -08:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2019-01-18 11:38:40 -08:00
|
|
|
}
|
2019-12-27 09:57:54 -08:00
|
|
|
|
2019-02-25 07:16:52 -08:00
|
|
|
commission := k.GetValidatorAccumulatedCommission(ctx, params.ValidatorAddress)
|
2020-02-11 06:58:37 -08:00
|
|
|
if commission.Commission == nil {
|
|
|
|
commission.Commission = sdk.DecCoins{}
|
2019-10-28 07:09:04 -07:00
|
|
|
}
|
2019-12-27 09:57:54 -08:00
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, commission)
|
2019-01-18 11:38:40 -08:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2019-01-18 11:38:40 -08:00
|
|
|
}
|
2019-12-27 09:57:54 -08:00
|
|
|
|
2019-01-18 11:38:40 -08:00
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
func queryValidatorSlashes(ctx sdk.Context, _ []string, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2019-06-05 16:26:17 -07:00
|
|
|
var params types.QueryValidatorSlashesParams
|
2020-07-25 01:03:58 -07:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2019-01-18 11:38:40 -08:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2019-01-18 11:38:40 -08:00
|
|
|
}
|
2019-12-27 09:57:54 -08:00
|
|
|
|
2019-01-18 11:38:40 -08:00
|
|
|
events := make([]types.ValidatorSlashEvent, 0)
|
2019-02-25 07:16:52 -08:00
|
|
|
k.IterateValidatorSlashEventsBetween(ctx, params.ValidatorAddress, params.StartingHeight, params.EndingHeight,
|
2019-01-18 11:38:40 -08:00
|
|
|
func(height uint64, event types.ValidatorSlashEvent) (stop bool) {
|
|
|
|
events = append(events, event)
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
)
|
2019-12-27 09:57:54 -08:00
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, events)
|
2019-01-18 11:38:40 -08:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2019-01-18 11:38:40 -08:00
|
|
|
}
|
2019-12-27 09:57:54 -08:00
|
|
|
|
2019-01-18 11:38:40 -08:00
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
func queryDelegationRewards(ctx sdk.Context, _ []string, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2019-06-05 16:26:17 -07:00
|
|
|
var params types.QueryDelegationRewardsParams
|
2020-07-25 01:03:58 -07:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2019-01-18 11:38:40 -08:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2019-01-18 11:38:40 -08:00
|
|
|
}
|
2019-01-29 16:06:38 -08:00
|
|
|
|
|
|
|
// cache-wrap context as to not persist state changes during querying
|
2019-01-18 11:38:40 -08:00
|
|
|
ctx, _ = ctx.CacheContext()
|
2019-01-29 16:06:38 -08:00
|
|
|
|
2019-02-25 07:16:52 -08:00
|
|
|
val := k.stakingKeeper.Validator(ctx, params.ValidatorAddress)
|
2019-04-04 10:53:22 -07:00
|
|
|
if val == nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(types.ErrNoValidatorExists, params.ValidatorAddress.String())
|
2019-04-04 10:53:22 -07:00
|
|
|
}
|
|
|
|
|
2019-02-25 07:16:52 -08:00
|
|
|
del := k.stakingKeeper.Delegation(ctx, params.DelegatorAddress, params.ValidatorAddress)
|
2019-04-04 10:53:22 -07:00
|
|
|
if del == nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, types.ErrNoDelegationExists
|
2019-04-04 10:53:22 -07:00
|
|
|
}
|
|
|
|
|
2020-03-05 06:00:45 -08:00
|
|
|
endingPeriod := k.IncrementValidatorPeriod(ctx, val)
|
|
|
|
rewards := k.CalculateDelegationRewards(ctx, val, del, endingPeriod)
|
2019-10-28 07:09:04 -07:00
|
|
|
if rewards == nil {
|
|
|
|
rewards = sdk.DecCoins{}
|
|
|
|
}
|
2019-01-29 16:06:38 -08:00
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, rewards)
|
2019-01-18 11:38:40 -08:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2019-01-18 11:38:40 -08:00
|
|
|
}
|
2019-01-29 16:06:38 -08:00
|
|
|
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
func queryDelegatorTotalRewards(ctx sdk.Context, _ []string, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2019-06-05 16:26:17 -07:00
|
|
|
var params types.QueryDelegatorParams
|
2020-07-25 01:03:58 -07:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2019-01-29 16:06:38 -08:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2019-01-29 16:06:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// cache-wrap context as to not persist state changes during querying
|
|
|
|
ctx, _ = ctx.CacheContext()
|
|
|
|
|
2019-04-16 06:43:08 -07:00
|
|
|
total := sdk.DecCoins{}
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2019-04-16 06:43:08 -07:00
|
|
|
var delRewards []types.DelegationDelegatorReward
|
2019-01-29 16:06:38 -08:00
|
|
|
|
|
|
|
k.stakingKeeper.IterateDelegations(
|
2019-02-25 07:16:52 -08:00
|
|
|
ctx, params.DelegatorAddress,
|
2019-06-05 10:42:25 -07:00
|
|
|
func(_ int64, del exported.DelegationI) (stop bool) {
|
2019-04-16 06:43:08 -07:00
|
|
|
valAddr := del.GetValidatorAddr()
|
|
|
|
val := k.stakingKeeper.Validator(ctx, valAddr)
|
2020-03-05 06:00:45 -08:00
|
|
|
endingPeriod := k.IncrementValidatorPeriod(ctx, val)
|
|
|
|
delReward := k.CalculateDelegationRewards(ctx, val, del, endingPeriod)
|
2019-01-29 16:06:38 -08:00
|
|
|
|
2019-04-16 06:43:08 -07:00
|
|
|
delRewards = append(delRewards, types.NewDelegationDelegatorReward(valAddr, delReward))
|
2020-01-03 12:44:53 -08:00
|
|
|
total = total.Add(delReward...)
|
2019-01-29 16:06:38 -08:00
|
|
|
return false
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2019-04-16 06:43:08 -07:00
|
|
|
totalRewards := types.NewQueryDelegatorTotalRewardsResponse(delRewards, total)
|
2019-12-27 09:57:54 -08:00
|
|
|
|
2019-04-16 06:43:08 -07:00
|
|
|
bz, err := json.Marshal(totalRewards)
|
2019-01-29 16:06:38 -08:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2019-01-29 16:06:38 -08:00
|
|
|
}
|
|
|
|
|
2019-01-18 11:38:40 -08:00
|
|
|
return bz, nil
|
|
|
|
}
|
2019-02-04 16:45:18 -08:00
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
func queryDelegatorValidators(ctx sdk.Context, _ []string, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2019-06-05 16:26:17 -07:00
|
|
|
var params types.QueryDelegatorParams
|
2020-07-25 01:03:58 -07:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2019-02-04 16:45:18 -08:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2019-02-04 16:45:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// cache-wrap context as to not persist state changes during querying
|
|
|
|
ctx, _ = ctx.CacheContext()
|
|
|
|
|
|
|
|
var validators []sdk.ValAddress
|
|
|
|
|
|
|
|
k.stakingKeeper.IterateDelegations(
|
2019-02-25 07:16:52 -08:00
|
|
|
ctx, params.DelegatorAddress,
|
2019-06-05 10:42:25 -07:00
|
|
|
func(_ int64, del exported.DelegationI) (stop bool) {
|
2019-08-19 09:06:27 -07:00
|
|
|
validators = append(validators, del.GetValidatorAddr())
|
2019-02-04 16:45:18 -08:00
|
|
|
return false
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, validators)
|
2019-02-04 16:45:18 -08:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2019-02-04 16:45:18 -08:00
|
|
|
}
|
2019-12-27 09:57:54 -08:00
|
|
|
|
2019-02-04 16:45:18 -08:00
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
func queryDelegatorWithdrawAddress(ctx sdk.Context, _ []string, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2019-06-05 16:26:17 -07:00
|
|
|
var params types.QueryDelegatorWithdrawAddrParams
|
2020-07-25 01:03:58 -07:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2019-02-04 16:45:18 -08:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2019-02-04 16:45:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// cache-wrap context as to not persist state changes during querying
|
|
|
|
ctx, _ = ctx.CacheContext()
|
2019-02-25 07:16:52 -08:00
|
|
|
withdrawAddr := k.GetDelegatorWithdrawAddr(ctx, params.DelegatorAddress)
|
2019-02-04 16:45:18 -08:00
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, withdrawAddr)
|
2019-02-04 16:45:18 -08:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2019-02-04 16:45:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
|
|
|
}
|
2019-03-27 18:05:36 -07:00
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
func queryCommunityPool(ctx sdk.Context, _ []string, _ abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2019-10-28 07:09:04 -07:00
|
|
|
pool := k.GetFeePoolCommunityCoins(ctx)
|
|
|
|
if pool == nil {
|
|
|
|
pool = sdk.DecCoins{}
|
|
|
|
}
|
2019-12-27 09:57:54 -08:00
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
bz, err := legacyQuerierCdc.MarshalJSON(pool)
|
2019-03-27 18:05:36 -07:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2019-03-27 18:05:36 -07:00
|
|
|
}
|
2019-12-27 09:57:54 -08:00
|
|
|
|
2019-03-27 18:05:36 -07:00
|
|
|
return bz, nil
|
|
|
|
}
|