2019-06-05 16:26:17 -07:00
|
|
|
package keeper
|
2018-09-13 14:23:44 -07:00
|
|
|
|
|
|
|
import (
|
2019-12-27 09:57:54 -08:00
|
|
|
"errors"
|
2019-04-24 08:48:39 -07:00
|
|
|
"strings"
|
2019-02-21 00:49:57 -08:00
|
|
|
|
2018-12-10 06:27:25 -08:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
|
2019-07-02 06:00:46 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2018-09-13 14:23:44 -07: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-11 12:08:01 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
2018-09-13 14:23:44 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// creates a querier for staking REST endpoints
|
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) {
|
2018-09-13 14:23:44 -07:00
|
|
|
switch path[0] {
|
2019-06-05 16:26:17 -07:00
|
|
|
case types.QueryValidators:
|
2020-07-25 01:03:58 -07:00
|
|
|
return queryValidators(ctx, req, k, legacyQuerierCdc)
|
2019-12-27 09:57:54 -08:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
case types.QueryValidator:
|
2020-07-25 01:03:58 -07:00
|
|
|
return queryValidator(ctx, req, k, legacyQuerierCdc)
|
2019-12-27 09:57:54 -08:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
case types.QueryValidatorDelegations:
|
2020-07-25 01:03:58 -07:00
|
|
|
return queryValidatorDelegations(ctx, req, k, legacyQuerierCdc)
|
2019-12-27 09:57:54 -08:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
case types.QueryValidatorUnbondingDelegations:
|
2020-07-25 01:03:58 -07:00
|
|
|
return queryValidatorUnbondingDelegations(ctx, req, k, legacyQuerierCdc)
|
2019-12-27 09:57:54 -08:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
case types.QueryDelegation:
|
2020-07-25 01:03:58 -07:00
|
|
|
return queryDelegation(ctx, req, k, legacyQuerierCdc)
|
2019-12-27 09:57:54 -08:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
case types.QueryUnbondingDelegation:
|
2020-07-25 01:03:58 -07:00
|
|
|
return queryUnbondingDelegation(ctx, req, k, legacyQuerierCdc)
|
2019-12-27 09:57:54 -08:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
case types.QueryDelegatorDelegations:
|
2020-07-25 01:03:58 -07:00
|
|
|
return queryDelegatorDelegations(ctx, req, k, legacyQuerierCdc)
|
2019-12-27 09:57:54 -08:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
case types.QueryDelegatorUnbondingDelegations:
|
2020-07-25 01:03:58 -07:00
|
|
|
return queryDelegatorUnbondingDelegations(ctx, req, k, legacyQuerierCdc)
|
2019-12-27 09:57:54 -08:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
case types.QueryRedelegations:
|
2020-07-25 01:03:58 -07:00
|
|
|
return queryRedelegations(ctx, req, k, legacyQuerierCdc)
|
2019-12-27 09:57:54 -08:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
case types.QueryDelegatorValidators:
|
2020-07-25 01:03:58 -07:00
|
|
|
return queryDelegatorValidators(ctx, req, k, legacyQuerierCdc)
|
2019-12-27 09:57:54 -08:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
case types.QueryDelegatorValidator:
|
2020-07-25 01:03:58 -07:00
|
|
|
return queryDelegatorValidator(ctx, req, k, legacyQuerierCdc)
|
2019-12-27 09:57:54 -08:00
|
|
|
|
2019-12-18 05:20:02 -08:00
|
|
|
case types.QueryHistoricalInfo:
|
2020-07-25 01:03:58 -07:00
|
|
|
return queryHistoricalInfo(ctx, req, k, legacyQuerierCdc)
|
2019-12-27 09:57:54 -08:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
case types.QueryPool:
|
2020-07-25 01:03:58 -07:00
|
|
|
return queryPool(ctx, k, legacyQuerierCdc)
|
2019-12-27 09:57:54 -08:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
case types.QueryParameters:
|
2020-07-25 01:03:58 -07:00
|
|
|
return queryParameters(ctx, k, legacyQuerierCdc)
|
2019-12-27 09:57:54 -08:00
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
default:
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown %s query endpoint: %s", types.ModuleName, path[0])
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
func queryValidators(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2019-06-05 16:26:17 -07:00
|
|
|
var params types.QueryValidatorsParams
|
2019-04-24 08:48:39 -07:00
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2019-04-24 08:48:39 -07:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2019-04-24 08:48:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
validators := k.GetAllValidators(ctx)
|
|
|
|
filteredVals := make([]types.Validator, 0, len(validators))
|
|
|
|
|
|
|
|
for _, val := range validators {
|
2019-08-19 09:06:27 -07:00
|
|
|
if strings.EqualFold(val.GetStatus().String(), params.Status) {
|
2019-04-24 08:48:39 -07:00
|
|
|
filteredVals = append(filteredVals, val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-02 06:00:46 -07:00
|
|
|
start, end := client.Paginate(len(filteredVals), params.Page, params.Limit, int(k.GetParams(ctx).MaxValidators))
|
|
|
|
if start < 0 || end < 0 {
|
2019-04-24 08:48:39 -07:00
|
|
|
filteredVals = []types.Validator{}
|
|
|
|
} else {
|
|
|
|
filteredVals = filteredVals[start:end]
|
|
|
|
}
|
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
res, err := codec.MarshalJSONIndent(legacyQuerierCdc, filteredVals)
|
2018-09-13 14:23:44 -07:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
2019-04-24 08:48:39 -07:00
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
func queryValidator(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2019-06-05 16:26:17 -07:00
|
|
|
var params types.QueryValidatorParams
|
2018-09-13 14:23:44 -07:00
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2019-06-03 06:31:51 -07:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
validator, found := k.GetValidator(ctx, params.ValidatorAddr)
|
|
|
|
if !found {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, types.ErrNoValidatorFound
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
res, err := codec.MarshalJSONIndent(legacyQuerierCdc, validator)
|
2019-06-03 06:31:51 -07:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
2019-06-03 06:31:51 -07:00
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
func queryValidatorDelegations(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2019-06-05 16:26:17 -07:00
|
|
|
var params types.QueryValidatorParams
|
2018-10-23 00:42:06 -07:00
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2019-06-03 06:31:51 -07:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2018-10-23 00:42:06 -07:00
|
|
|
}
|
|
|
|
|
2018-10-23 13:30:14 -07:00
|
|
|
delegations := k.GetValidatorDelegations(ctx, params.ValidatorAddr)
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2020-06-06 10:32:15 -07:00
|
|
|
start, end := client.Paginate(len(delegations), params.Page, params.Limit, int(k.GetParams(ctx).MaxValidators))
|
|
|
|
if start < 0 || end < 0 {
|
|
|
|
delegations = []types.Delegation{}
|
|
|
|
} else {
|
|
|
|
delegations = delegations[start:end]
|
|
|
|
}
|
|
|
|
|
2020-07-14 10:41:30 -07:00
|
|
|
delegationResps, err := DelegationsToDelegationResponses(ctx, k, delegations)
|
2019-05-15 07:44:47 -07:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, err
|
2019-05-15 07:44:47 -07:00
|
|
|
}
|
2018-10-23 00:42:06 -07:00
|
|
|
|
2019-10-28 07:09:04 -07:00
|
|
|
if delegationResps == nil {
|
|
|
|
delegationResps = types.DelegationResponses{}
|
|
|
|
}
|
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
res, err := codec.MarshalJSONIndent(legacyQuerierCdc, delegationResps)
|
2019-06-03 06:31:51 -07:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2018-10-23 00:42:06 -07:00
|
|
|
}
|
2019-05-15 07:44:47 -07:00
|
|
|
|
2018-10-23 00:42:06 -07:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
func queryValidatorUnbondingDelegations(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2019-06-05 16:26:17 -07:00
|
|
|
var params types.QueryValidatorParams
|
2018-10-20 12:30:07 -07:00
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2019-06-03 06:31:51 -07:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2018-10-20 12:30:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
unbonds := k.GetUnbondingDelegationsFromValidator(ctx, params.ValidatorAddr)
|
2019-10-28 07:09:04 -07:00
|
|
|
if unbonds == nil {
|
|
|
|
unbonds = types.UnbondingDelegations{}
|
|
|
|
}
|
2018-10-20 12:30:07 -07:00
|
|
|
|
2020-06-06 10:32:15 -07:00
|
|
|
start, end := client.Paginate(len(unbonds), params.Page, params.Limit, int(k.GetParams(ctx).MaxValidators))
|
|
|
|
if start < 0 || end < 0 {
|
|
|
|
unbonds = types.UnbondingDelegations{}
|
|
|
|
} else {
|
|
|
|
unbonds = unbonds[start:end]
|
|
|
|
}
|
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
res, err := codec.MarshalJSONIndent(legacyQuerierCdc, unbonds)
|
2019-06-03 06:31:51 -07:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2018-10-20 12:30:07 -07:00
|
|
|
}
|
2019-06-03 06:31:51 -07:00
|
|
|
|
2018-10-20 12:30:07 -07:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
func queryDelegatorDelegations(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2019-06-05 16:26:17 -07:00
|
|
|
var params types.QueryDelegatorParams
|
2018-10-20 13:25:50 -07:00
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2019-06-03 06:31:51 -07:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
2018-10-20 13:25:50 -07:00
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
delegations := k.GetAllDelegatorDelegations(ctx, params.DelegatorAddr)
|
2020-07-14 10:41:30 -07:00
|
|
|
delegationResps, err := DelegationsToDelegationResponses(ctx, k, delegations)
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2019-05-15 07:44:47 -07:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, err
|
2019-05-15 07:44:47 -07:00
|
|
|
}
|
2018-10-20 13:25:50 -07:00
|
|
|
|
2019-10-28 07:09:04 -07:00
|
|
|
if delegationResps == nil {
|
|
|
|
delegationResps = types.DelegationResponses{}
|
|
|
|
}
|
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
res, err := codec.MarshalJSONIndent(legacyQuerierCdc, delegationResps)
|
2019-06-03 06:31:51 -07:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2018-10-20 13:25:50 -07:00
|
|
|
}
|
2019-05-15 07:44:47 -07:00
|
|
|
|
2018-10-20 13:25:50 -07:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
func queryDelegatorUnbondingDelegations(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2019-06-05 16:26:17 -07:00
|
|
|
var params types.QueryDelegatorParams
|
2018-10-20 13:25:50 -07:00
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2019-06-03 06:31:51 -07:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2018-10-20 13:25:50 -07:00
|
|
|
}
|
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
unbondingDelegations := k.GetAllUnbondingDelegations(ctx, params.DelegatorAddr)
|
2019-10-28 07:09:04 -07:00
|
|
|
if unbondingDelegations == nil {
|
|
|
|
unbondingDelegations = types.UnbondingDelegations{}
|
|
|
|
}
|
2018-09-13 14:23:44 -07:00
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
res, err := codec.MarshalJSONIndent(legacyQuerierCdc, unbondingDelegations)
|
2019-06-03 06:31:51 -07:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
2019-06-03 06:31:51 -07:00
|
|
|
|
2018-10-20 13:25:50 -07:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
func queryDelegatorValidators(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2019-06-05 16:26:17 -07:00
|
|
|
var params types.QueryDelegatorParams
|
2018-09-13 14:23:44 -07:00
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
stakingParams := k.GetParams(ctx)
|
2018-09-13 14:23:44 -07:00
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2019-06-03 06:31:51 -07:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
validators := k.GetDelegatorValidators(ctx, params.DelegatorAddr, stakingParams.MaxValidators)
|
2019-10-28 07:09:04 -07:00
|
|
|
if validators == nil {
|
|
|
|
validators = types.Validators{}
|
|
|
|
}
|
2018-09-13 14:23:44 -07:00
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
res, err := codec.MarshalJSONIndent(legacyQuerierCdc, validators)
|
2019-06-03 06:31:51 -07:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
2019-06-03 06:31:51 -07:00
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
func queryDelegatorValidator(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2020-07-14 10:41:30 -07:00
|
|
|
var params types.QueryDelegatorValidatorRequest
|
2018-09-13 14:23:44 -07:00
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2019-06-03 06:31:51 -07:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
validator, err := k.GetDelegatorValidator(ctx, params.DelegatorAddr, params.ValidatorAddr)
|
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, err
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
res, err := codec.MarshalJSONIndent(legacyQuerierCdc, validator)
|
2019-06-03 06:31:51 -07:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
2019-06-03 06:31:51 -07:00
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
func queryDelegation(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2020-07-14 10:41:30 -07:00
|
|
|
var params types.QueryDelegatorValidatorRequest
|
2018-09-13 14:23:44 -07:00
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2019-06-03 06:31:51 -07:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
delegation, found := k.GetDelegation(ctx, params.DelegatorAddr, params.ValidatorAddr)
|
|
|
|
if !found {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, types.ErrNoDelegation
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
|
|
|
|
2020-07-14 10:41:30 -07:00
|
|
|
delegationResp, err := DelegationToDelegationResponse(ctx, k, delegation)
|
2019-05-15 07:44:47 -07:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, err
|
2019-05-15 07:44:47 -07:00
|
|
|
}
|
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
res, err := codec.MarshalJSONIndent(legacyQuerierCdc, delegationResp)
|
2019-06-03 06:31:51 -07:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
2019-05-15 07:44:47 -07:00
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
func queryUnbondingDelegation(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2020-07-14 10:41:30 -07:00
|
|
|
var params types.QueryDelegatorValidatorRequest
|
2018-09-13 14:23:44 -07:00
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2019-06-03 06:31:51 -07:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
unbond, found := k.GetUnbondingDelegation(ctx, params.DelegatorAddr, params.ValidatorAddr)
|
|
|
|
if !found {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, types.ErrNoUnbondingDelegation
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
res, err := codec.MarshalJSONIndent(legacyQuerierCdc, unbond)
|
2019-06-03 06:31:51 -07:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
2019-06-03 06:31:51 -07:00
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
func queryRedelegations(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2019-06-05 16:26:17 -07:00
|
|
|
var params types.QueryRedelegationParams
|
2018-12-18 13:43:41 -08:00
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2019-06-03 06:31:51 -07:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2018-12-18 13:43:41 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
var redels []types.Redelegation
|
|
|
|
|
2019-08-19 09:06:27 -07:00
|
|
|
switch {
|
|
|
|
case !params.DelegatorAddr.Empty() && !params.SrcValidatorAddr.Empty() && !params.DstValidatorAddr.Empty():
|
2018-12-18 13:43:41 -08:00
|
|
|
redel, found := k.GetRedelegation(ctx, params.DelegatorAddr, params.SrcValidatorAddr, params.DstValidatorAddr)
|
|
|
|
if !found {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, types.ErrNoRedelegation
|
2018-12-18 13:43:41 -08:00
|
|
|
}
|
2019-05-15 07:44:47 -07:00
|
|
|
|
2018-12-18 13:43:41 -08:00
|
|
|
redels = []types.Redelegation{redel}
|
2019-08-19 09:06:27 -07:00
|
|
|
case params.DelegatorAddr.Empty() && !params.SrcValidatorAddr.Empty() && params.DstValidatorAddr.Empty():
|
2019-06-28 13:11:27 -07:00
|
|
|
redels = k.GetRedelegationsFromSrcValidator(ctx, params.SrcValidatorAddr)
|
2019-08-19 09:06:27 -07:00
|
|
|
default:
|
2018-12-18 13:43:41 -08:00
|
|
|
redels = k.GetAllRedelegations(ctx, params.DelegatorAddr, params.SrcValidatorAddr, params.DstValidatorAddr)
|
|
|
|
}
|
|
|
|
|
2020-07-14 10:41:30 -07:00
|
|
|
redelResponses, err := RedelegationsToRedelegationResponses(ctx, k, redels)
|
2019-05-15 07:44:47 -07:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, err
|
2019-05-15 07:44:47 -07:00
|
|
|
}
|
|
|
|
|
2019-10-28 07:09:04 -07:00
|
|
|
if redelResponses == nil {
|
|
|
|
redelResponses = types.RedelegationResponses{}
|
|
|
|
}
|
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
res, err := codec.MarshalJSONIndent(legacyQuerierCdc, redelResponses)
|
2019-06-03 06:31:51 -07:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2018-12-18 13:43:41 -08:00
|
|
|
}
|
2019-05-15 07:44:47 -07:00
|
|
|
|
2018-12-18 13:43:41 -08:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
func queryHistoricalInfo(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2020-07-14 10:41:30 -07:00
|
|
|
var params types.QueryHistoricalInfoRequest
|
2019-12-18 05:20:02 -08:00
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2019-12-18 05:20:02 -08:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2019-12-18 05:20:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
hi, found := k.GetHistoricalInfo(ctx, params.Height)
|
|
|
|
if !found {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, types.ErrNoHistoricalInfo
|
2019-12-18 05:20:02 -08:00
|
|
|
}
|
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
res, err := codec.MarshalJSONIndent(legacyQuerierCdc, hi)
|
2019-12-18 05:20:02 -08:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2019-12-18 05:20:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
func queryPool(ctx sdk.Context, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2019-06-28 13:11:27 -07:00
|
|
|
bondDenom := k.BondDenom(ctx)
|
|
|
|
bondedPool := k.GetBondedPool(ctx)
|
|
|
|
notBondedPool := k.GetNotBondedPool(ctx)
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2019-06-28 13:11:27 -07:00
|
|
|
if bondedPool == nil || notBondedPool == nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, errors.New("pool accounts haven't been set")
|
2019-06-28 13:11:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pool := types.NewPool(
|
2020-01-30 13:31:16 -08:00
|
|
|
k.bankKeeper.GetBalance(ctx, notBondedPool.GetAddress(), bondDenom).Amount,
|
|
|
|
k.bankKeeper.GetBalance(ctx, bondedPool.GetAddress(), bondDenom).Amount,
|
2019-06-28 13:11:27 -07:00
|
|
|
)
|
2018-09-13 14:23:44 -07:00
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
res, err := codec.MarshalJSONIndent(legacyQuerierCdc, pool)
|
2019-06-03 06:31:51 -07:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
2019-06-03 06:31:51 -07:00
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
func queryParameters(ctx sdk.Context, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2018-09-13 14:23:44 -07:00
|
|
|
params := k.GetParams(ctx)
|
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
res, err := codec.MarshalJSONIndent(legacyQuerierCdc, params)
|
2019-06-03 06:31:51 -07:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
2019-06-03 06:31:51 -07:00
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
return res, nil
|
|
|
|
}
|
2019-04-24 08:48:39 -07:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
//______________________________________________________
|
|
|
|
// util
|
|
|
|
|
2020-07-14 10:41:30 -07:00
|
|
|
func DelegationToDelegationResponse(ctx sdk.Context, k Keeper, del types.Delegation) (types.DelegationResponse, error) {
|
2019-06-05 16:26:17 -07:00
|
|
|
val, found := k.GetValidator(ctx, del.ValidatorAddress)
|
|
|
|
if !found {
|
2019-12-27 09:57:54 -08:00
|
|
|
return types.DelegationResponse{}, types.ErrNoValidatorFound
|
2019-06-05 16:26:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return types.NewDelegationResp(
|
|
|
|
del.DelegatorAddress,
|
|
|
|
del.ValidatorAddress,
|
|
|
|
del.Shares,
|
2019-08-02 09:37:35 -07:00
|
|
|
sdk.NewCoin(k.BondDenom(ctx), val.TokensFromShares(del.Shares).TruncateInt()),
|
2019-06-05 16:26:17 -07:00
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2020-07-14 10:41:30 -07:00
|
|
|
func DelegationsToDelegationResponses(
|
2019-06-05 16:26:17 -07:00
|
|
|
ctx sdk.Context, k Keeper, delegations types.Delegations,
|
2019-12-27 09:57:54 -08:00
|
|
|
) (types.DelegationResponses, error) {
|
2019-09-16 10:11:17 -07:00
|
|
|
resp := make(types.DelegationResponses, len(delegations))
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
for i, del := range delegations {
|
2020-07-14 10:41:30 -07:00
|
|
|
delResp, err := DelegationToDelegationResponse(ctx, k, del)
|
2019-06-05 16:26:17 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp[i] = delResp
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
2019-04-24 08:48:39 -07:00
|
|
|
}
|
|
|
|
|
2020-07-14 10:41:30 -07:00
|
|
|
func RedelegationsToRedelegationResponses(
|
2019-06-05 16:26:17 -07:00
|
|
|
ctx sdk.Context, k Keeper, redels types.Redelegations,
|
2019-12-27 09:57:54 -08:00
|
|
|
) (types.RedelegationResponses, error) {
|
2019-09-16 10:11:17 -07:00
|
|
|
resp := make(types.RedelegationResponses, len(redels))
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
for i, redel := range redels {
|
|
|
|
val, found := k.GetValidator(ctx, redel.ValidatorDstAddress)
|
|
|
|
if !found {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, types.ErrNoValidatorFound
|
2019-06-05 16:26:17 -07:00
|
|
|
}
|
|
|
|
|
2019-09-16 10:11:17 -07:00
|
|
|
entryResponses := make([]types.RedelegationEntryResponse, len(redel.Entries))
|
2019-06-05 16:26:17 -07:00
|
|
|
for j, entry := range redel.Entries {
|
|
|
|
entryResponses[j] = types.NewRedelegationEntryResponse(
|
|
|
|
entry.CreationHeight,
|
|
|
|
entry.CompletionTime,
|
|
|
|
entry.SharesDst,
|
|
|
|
entry.InitialBalance,
|
|
|
|
val.TokensFromShares(entry.SharesDst).TruncateInt(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp[i] = types.NewRedelegationResponse(
|
|
|
|
redel.DelegatorAddress,
|
|
|
|
redel.ValidatorSrcAddress,
|
|
|
|
redel.ValidatorDstAddress,
|
|
|
|
entryResponses,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
2019-04-24 08:48:39 -07:00
|
|
|
}
|