2018-09-13 14:23:44 -07:00
|
|
|
package querier
|
|
|
|
|
|
|
|
import (
|
2019-02-21 00:49:57 -08:00
|
|
|
"fmt"
|
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"
|
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-01-11 12:08:01 -08:00
|
|
|
keep "github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
2018-09-13 14:23:44 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// query endpoints supported by the staking Querier
|
|
|
|
const (
|
2018-10-20 13:25:50 -07:00
|
|
|
QueryValidators = "validators"
|
|
|
|
QueryValidator = "validator"
|
|
|
|
QueryDelegatorDelegations = "delegatorDelegations"
|
|
|
|
QueryDelegatorUnbondingDelegations = "delegatorUnbondingDelegations"
|
2018-12-18 13:43:41 -08:00
|
|
|
QueryRedelegations = "redelegations"
|
2018-10-23 00:42:06 -07:00
|
|
|
QueryValidatorDelegations = "validatorDelegations"
|
2019-01-22 09:28:39 -08:00
|
|
|
QueryValidatorRedelegations = "validatorRedelegations"
|
2018-10-20 12:30:07 -07:00
|
|
|
QueryValidatorUnbondingDelegations = "validatorUnbondingDelegations"
|
2018-10-20 13:25:50 -07:00
|
|
|
QueryDelegation = "delegation"
|
|
|
|
QueryUnbondingDelegation = "unbondingDelegation"
|
|
|
|
QueryDelegatorValidators = "delegatorValidators"
|
|
|
|
QueryDelegatorValidator = "delegatorValidator"
|
|
|
|
QueryPool = "pool"
|
|
|
|
QueryParameters = "parameters"
|
2018-09-13 14:23:44 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// creates a querier for staking REST endpoints
|
|
|
|
func NewQuerier(k keep.Keeper, cdc *codec.Codec) sdk.Querier {
|
|
|
|
return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err sdk.Error) {
|
|
|
|
switch path[0] {
|
|
|
|
case QueryValidators:
|
2019-04-24 08:48:39 -07:00
|
|
|
return queryValidators(ctx, cdc, req, k)
|
2019-05-15 07:44:47 -07:00
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
case QueryValidator:
|
|
|
|
return queryValidator(ctx, cdc, req, k)
|
2019-05-15 07:44:47 -07:00
|
|
|
|
2018-10-23 00:42:06 -07:00
|
|
|
case QueryValidatorDelegations:
|
|
|
|
return queryValidatorDelegations(ctx, cdc, req, k)
|
2019-05-15 07:44:47 -07:00
|
|
|
|
2018-10-20 12:30:07 -07:00
|
|
|
case QueryValidatorUnbondingDelegations:
|
|
|
|
return queryValidatorUnbondingDelegations(ctx, cdc, req, k)
|
2019-05-15 07:44:47 -07:00
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
case QueryDelegation:
|
|
|
|
return queryDelegation(ctx, cdc, req, k)
|
2019-05-15 07:44:47 -07:00
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
case QueryUnbondingDelegation:
|
|
|
|
return queryUnbondingDelegation(ctx, cdc, req, k)
|
2019-05-15 07:44:47 -07:00
|
|
|
|
2018-10-20 13:25:50 -07:00
|
|
|
case QueryDelegatorDelegations:
|
|
|
|
return queryDelegatorDelegations(ctx, cdc, req, k)
|
2019-05-15 07:44:47 -07:00
|
|
|
|
2018-10-20 13:25:50 -07:00
|
|
|
case QueryDelegatorUnbondingDelegations:
|
|
|
|
return queryDelegatorUnbondingDelegations(ctx, cdc, req, k)
|
2019-05-15 07:44:47 -07:00
|
|
|
|
2018-12-18 13:43:41 -08:00
|
|
|
case QueryRedelegations:
|
|
|
|
return queryRedelegations(ctx, cdc, req, k)
|
2019-05-15 07:44:47 -07:00
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
case QueryDelegatorValidators:
|
|
|
|
return queryDelegatorValidators(ctx, cdc, req, k)
|
2019-05-15 07:44:47 -07:00
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
case QueryDelegatorValidator:
|
|
|
|
return queryDelegatorValidator(ctx, cdc, req, k)
|
2019-05-15 07:44:47 -07:00
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
case QueryPool:
|
|
|
|
return queryPool(ctx, cdc, k)
|
2019-05-15 07:44:47 -07:00
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
case QueryParameters:
|
|
|
|
return queryParameters(ctx, cdc, k)
|
2019-05-15 07:44:47 -07:00
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
default:
|
2019-01-11 12:08:01 -08:00
|
|
|
return nil, sdk.ErrUnknownRequest("unknown staking query endpoint")
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// defines the params for the following queries:
|
2019-01-11 12:08:01 -08:00
|
|
|
// - 'custom/staking/delegatorDelegations'
|
|
|
|
// - 'custom/staking/delegatorUnbondingDelegations'
|
|
|
|
// - 'custom/staking/delegatorRedelegations'
|
|
|
|
// - 'custom/staking/delegatorValidators'
|
2018-09-13 14:23:44 -07:00
|
|
|
type QueryDelegatorParams struct {
|
|
|
|
DelegatorAddr sdk.AccAddress
|
|
|
|
}
|
|
|
|
|
2018-12-18 13:43:41 -08:00
|
|
|
func NewQueryDelegatorParams(delegatorAddr sdk.AccAddress) QueryDelegatorParams {
|
|
|
|
return QueryDelegatorParams{
|
|
|
|
DelegatorAddr: delegatorAddr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
// defines the params for the following queries:
|
2019-01-11 12:08:01 -08:00
|
|
|
// - 'custom/staking/validator'
|
|
|
|
// - 'custom/staking/validatorDelegations'
|
|
|
|
// - 'custom/staking/validatorUnbondingDelegations'
|
|
|
|
// - 'custom/staking/validatorRedelegations'
|
2018-09-13 14:23:44 -07:00
|
|
|
type QueryValidatorParams struct {
|
|
|
|
ValidatorAddr sdk.ValAddress
|
|
|
|
}
|
|
|
|
|
2018-12-18 13:43:41 -08:00
|
|
|
func NewQueryValidatorParams(validatorAddr sdk.ValAddress) QueryValidatorParams {
|
|
|
|
return QueryValidatorParams{
|
|
|
|
ValidatorAddr: validatorAddr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
// defines the params for the following queries:
|
2019-01-11 12:08:01 -08:00
|
|
|
// - 'custom/staking/delegation'
|
|
|
|
// - 'custom/staking/unbondingDelegation'
|
|
|
|
// - 'custom/staking/delegatorValidator'
|
2018-09-13 14:23:44 -07:00
|
|
|
type QueryBondsParams struct {
|
|
|
|
DelegatorAddr sdk.AccAddress
|
|
|
|
ValidatorAddr sdk.ValAddress
|
|
|
|
}
|
|
|
|
|
2018-12-18 13:43:41 -08:00
|
|
|
func NewQueryBondsParams(delegatorAddr sdk.AccAddress, validatorAddr sdk.ValAddress) QueryBondsParams {
|
|
|
|
return QueryBondsParams{
|
2018-11-12 14:12:25 -08:00
|
|
|
DelegatorAddr: delegatorAddr,
|
2018-12-18 13:43:41 -08:00
|
|
|
ValidatorAddr: validatorAddr,
|
2018-11-12 14:12:25 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-18 13:43:41 -08:00
|
|
|
// defines the params for the following queries:
|
2019-01-11 12:08:01 -08:00
|
|
|
// - 'custom/staking/redelegation'
|
2018-12-18 13:43:41 -08:00
|
|
|
type QueryRedelegationParams struct {
|
|
|
|
DelegatorAddr sdk.AccAddress
|
|
|
|
SrcValidatorAddr sdk.ValAddress
|
|
|
|
DstValidatorAddr sdk.ValAddress
|
2018-11-12 14:12:25 -08:00
|
|
|
}
|
|
|
|
|
2018-12-18 13:43:41 -08:00
|
|
|
func NewQueryRedelegationParams(delegatorAddr sdk.AccAddress, srcValidatorAddr sdk.ValAddress, dstValidatorAddr sdk.ValAddress) QueryRedelegationParams {
|
|
|
|
return QueryRedelegationParams{
|
|
|
|
DelegatorAddr: delegatorAddr,
|
|
|
|
SrcValidatorAddr: srcValidatorAddr,
|
|
|
|
DstValidatorAddr: dstValidatorAddr,
|
2018-11-12 14:12:25 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 08:48:39 -07:00
|
|
|
func queryValidators(ctx sdk.Context, cdc *codec.Codec, req abci.RequestQuery, k keep.Keeper) ([]byte, sdk.Error) {
|
|
|
|
var params QueryValidatorsParams
|
|
|
|
|
|
|
|
err := cdc.UnmarshalJSON(req.Data, ¶ms)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err))
|
|
|
|
}
|
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
stakingParams := k.GetParams(ctx)
|
2019-04-24 08:48:39 -07:00
|
|
|
if params.Limit == 0 {
|
|
|
|
params.Limit = int(stakingParams.MaxValidators)
|
|
|
|
}
|
|
|
|
|
|
|
|
validators := k.GetAllValidators(ctx)
|
|
|
|
filteredVals := make([]types.Validator, 0, len(validators))
|
|
|
|
|
|
|
|
for _, val := range validators {
|
|
|
|
if strings.ToLower(val.GetStatus().String()) == strings.ToLower(params.Status) {
|
|
|
|
filteredVals = append(filteredVals, val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get pagination bounds
|
|
|
|
start := (params.Page - 1) * params.Limit
|
|
|
|
end := params.Limit + start
|
|
|
|
if end >= len(filteredVals) {
|
|
|
|
end = len(filteredVals)
|
|
|
|
}
|
2018-09-13 14:23:44 -07:00
|
|
|
|
2019-04-24 08:48:39 -07:00
|
|
|
if start >= len(filteredVals) {
|
|
|
|
// page is out of bounds
|
|
|
|
filteredVals = []types.Validator{}
|
|
|
|
} else {
|
|
|
|
filteredVals = filteredVals[start:end]
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := codec.MarshalJSONIndent(cdc, filteredVals)
|
2018-09-13 14:23:44 -07:00
|
|
|
if err != nil {
|
2019-04-24 08:48:39 -07:00
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("failed to JSON marshal result: %s", 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
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryValidator(ctx sdk.Context, cdc *codec.Codec, req abci.RequestQuery, k keep.Keeper) (res []byte, err sdk.Error) {
|
|
|
|
var params QueryValidatorParams
|
|
|
|
|
|
|
|
errRes := cdc.UnmarshalJSON(req.Data, ¶ms)
|
|
|
|
if errRes != nil {
|
2019-02-21 00:49:57 -08:00
|
|
|
return []byte{}, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err))
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
validator, found := k.GetValidator(ctx, params.ValidatorAddr)
|
|
|
|
if !found {
|
|
|
|
return []byte{}, types.ErrNoValidatorFound(types.DefaultCodespace)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, errRes = codec.MarshalJSONIndent(cdc, validator)
|
|
|
|
if errRes != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2018-10-23 00:42:06 -07:00
|
|
|
func queryValidatorDelegations(ctx sdk.Context, cdc *codec.Codec, req abci.RequestQuery, k keep.Keeper) (res []byte, err sdk.Error) {
|
|
|
|
var params QueryValidatorParams
|
|
|
|
|
|
|
|
errRes := cdc.UnmarshalJSON(req.Data, ¶ms)
|
|
|
|
if errRes != nil {
|
2019-02-21 00:49:57 -08:00
|
|
|
return []byte{}, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err))
|
2018-10-23 00:42:06 -07:00
|
|
|
}
|
|
|
|
|
2018-10-23 13:30:14 -07:00
|
|
|
delegations := k.GetValidatorDelegations(ctx, params.ValidatorAddr)
|
2019-05-15 07:44:47 -07:00
|
|
|
delegationResps, err := delegationsToDelegationResponses(ctx, k, delegations)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-23 00:42:06 -07:00
|
|
|
|
2019-05-15 07:44:47 -07:00
|
|
|
res, errRes = codec.MarshalJSONIndent(cdc, delegationResps)
|
2018-10-23 00:42:06 -07:00
|
|
|
if errRes != nil {
|
2019-05-15 07:44:47 -07:00
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("failed to marshal result to JSON", errRes.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
|
|
|
|
}
|
|
|
|
|
2018-10-20 12:30:07 -07:00
|
|
|
func queryValidatorUnbondingDelegations(ctx sdk.Context, cdc *codec.Codec, req abci.RequestQuery, k keep.Keeper) (res []byte, err sdk.Error) {
|
|
|
|
var params QueryValidatorParams
|
|
|
|
|
|
|
|
errRes := cdc.UnmarshalJSON(req.Data, ¶ms)
|
|
|
|
if errRes != nil {
|
2019-02-21 00:49:57 -08:00
|
|
|
return []byte{}, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err))
|
2018-10-20 12:30:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
unbonds := k.GetUnbondingDelegationsFromValidator(ctx, params.ValidatorAddr)
|
|
|
|
|
|
|
|
res, errRes = codec.MarshalJSONIndent(cdc, unbonds)
|
|
|
|
if errRes != nil {
|
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2018-10-20 13:25:50 -07:00
|
|
|
func queryDelegatorDelegations(ctx sdk.Context, cdc *codec.Codec, req abci.RequestQuery, k keep.Keeper) (res []byte, err sdk.Error) {
|
2018-09-13 14:23:44 -07:00
|
|
|
var params QueryDelegatorParams
|
2018-10-20 13:25:50 -07:00
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
errRes := cdc.UnmarshalJSON(req.Data, ¶ms)
|
|
|
|
if errRes != nil {
|
2019-02-21 00:49:57 -08:00
|
|
|
return []byte{}, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err))
|
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)
|
2019-05-15 07:44:47 -07:00
|
|
|
delegationResps, err := delegationsToDelegationResponses(ctx, k, delegations)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-20 13:25:50 -07:00
|
|
|
|
2019-05-15 07:44:47 -07:00
|
|
|
res, errRes = codec.MarshalJSONIndent(cdc, delegationResps)
|
2018-10-20 13:25:50 -07:00
|
|
|
if errRes != nil {
|
2019-05-15 07:44:47 -07:00
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("failed to marshal result to JSON", errRes.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
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryDelegatorUnbondingDelegations(ctx sdk.Context, cdc *codec.Codec, req abci.RequestQuery, k keep.Keeper) (res []byte, err sdk.Error) {
|
|
|
|
var params QueryDelegatorParams
|
|
|
|
|
|
|
|
errRes := cdc.UnmarshalJSON(req.Data, ¶ms)
|
|
|
|
if errRes != nil {
|
2019-02-21 00:49:57 -08:00
|
|
|
return []byte{}, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err))
|
2018-10-20 13:25:50 -07:00
|
|
|
}
|
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
unbondingDelegations := k.GetAllUnbondingDelegations(ctx, params.DelegatorAddr)
|
|
|
|
|
2018-10-20 13:25:50 -07:00
|
|
|
res, errRes = codec.MarshalJSONIndent(cdc, unbondingDelegations)
|
|
|
|
if errRes != nil {
|
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
2018-10-20 13:25:50 -07:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
func queryDelegatorValidators(ctx sdk.Context, cdc *codec.Codec, req abci.RequestQuery, k keep.Keeper) (res []byte, err sdk.Error) {
|
|
|
|
var params QueryDelegatorParams
|
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
stakingParams := k.GetParams(ctx)
|
2018-09-13 14:23:44 -07:00
|
|
|
|
|
|
|
errRes := cdc.UnmarshalJSON(req.Data, ¶ms)
|
|
|
|
if errRes != nil {
|
2019-02-21 00:49:57 -08:00
|
|
|
return []byte{}, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err))
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
validators := k.GetDelegatorValidators(ctx, params.DelegatorAddr, stakingParams.MaxValidators)
|
2018-09-13 14:23:44 -07:00
|
|
|
|
|
|
|
res, errRes = codec.MarshalJSONIndent(cdc, validators)
|
|
|
|
if errRes != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryDelegatorValidator(ctx sdk.Context, cdc *codec.Codec, req abci.RequestQuery, k keep.Keeper) (res []byte, err sdk.Error) {
|
|
|
|
var params QueryBondsParams
|
|
|
|
|
|
|
|
errRes := cdc.UnmarshalJSON(req.Data, ¶ms)
|
|
|
|
if errRes != nil {
|
2019-02-21 00:49:57 -08:00
|
|
|
return []byte{}, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err))
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
validator, err := k.GetDelegatorValidator(ctx, params.DelegatorAddr, params.ValidatorAddr)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res, errRes = codec.MarshalJSONIndent(cdc, validator)
|
|
|
|
if errRes != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryDelegation(ctx sdk.Context, cdc *codec.Codec, req abci.RequestQuery, k keep.Keeper) (res []byte, err sdk.Error) {
|
|
|
|
var params QueryBondsParams
|
|
|
|
|
|
|
|
errRes := cdc.UnmarshalJSON(req.Data, ¶ms)
|
|
|
|
if errRes != nil {
|
2019-02-21 00:49:57 -08:00
|
|
|
return []byte{}, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err))
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
delegation, found := k.GetDelegation(ctx, params.DelegatorAddr, params.ValidatorAddr)
|
|
|
|
if !found {
|
|
|
|
return []byte{}, types.ErrNoDelegation(types.DefaultCodespace)
|
|
|
|
}
|
|
|
|
|
2019-05-15 07:44:47 -07:00
|
|
|
delegationResp, err := delegationToDelegationResponse(ctx, k, delegation)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, errRes = codec.MarshalJSONIndent(cdc, delegationResp)
|
2018-09-13 14:23:44 -07:00
|
|
|
if errRes != nil {
|
2019-05-15 07:44:47 -07:00
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("failed to marshal result to JSON", errRes.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
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryUnbondingDelegation(ctx sdk.Context, cdc *codec.Codec, req abci.RequestQuery, k keep.Keeper) (res []byte, err sdk.Error) {
|
|
|
|
var params QueryBondsParams
|
|
|
|
|
|
|
|
errRes := cdc.UnmarshalJSON(req.Data, ¶ms)
|
|
|
|
if errRes != nil {
|
2019-02-21 00:49:57 -08:00
|
|
|
return []byte{}, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err))
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
unbond, found := k.GetUnbondingDelegation(ctx, params.DelegatorAddr, params.ValidatorAddr)
|
|
|
|
if !found {
|
|
|
|
return []byte{}, types.ErrNoUnbondingDelegation(types.DefaultCodespace)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, errRes = codec.MarshalJSONIndent(cdc, unbond)
|
|
|
|
if errRes != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2018-12-18 13:43:41 -08:00
|
|
|
func queryRedelegations(ctx sdk.Context, cdc *codec.Codec, req abci.RequestQuery, k keep.Keeper) (res []byte, err sdk.Error) {
|
|
|
|
var params QueryRedelegationParams
|
|
|
|
|
|
|
|
errRes := cdc.UnmarshalJSON(req.Data, ¶ms)
|
|
|
|
if errRes != nil {
|
|
|
|
return []byte{}, sdk.ErrUnknownRequest(string(req.Data))
|
|
|
|
}
|
|
|
|
|
|
|
|
var redels []types.Redelegation
|
|
|
|
|
|
|
|
if !params.DelegatorAddr.Empty() && !params.SrcValidatorAddr.Empty() && !params.DstValidatorAddr.Empty() {
|
|
|
|
redel, found := k.GetRedelegation(ctx, params.DelegatorAddr, params.SrcValidatorAddr, params.DstValidatorAddr)
|
|
|
|
if !found {
|
2019-05-15 07:44:47 -07:00
|
|
|
return nil, types.ErrNoRedelegation(types.DefaultCodespace)
|
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}
|
|
|
|
} else if params.DelegatorAddr.Empty() && !params.SrcValidatorAddr.Empty() && params.DstValidatorAddr.Empty() {
|
|
|
|
redels = k.GetRedelegationsFromValidator(ctx, params.SrcValidatorAddr)
|
|
|
|
} else {
|
|
|
|
redels = k.GetAllRedelegations(ctx, params.DelegatorAddr, params.SrcValidatorAddr, params.DstValidatorAddr)
|
|
|
|
}
|
|
|
|
|
2019-05-15 07:44:47 -07:00
|
|
|
redelResponses, err := redelegationsToRedelegationResponses(ctx, k, redels)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, errRes = codec.MarshalJSONIndent(cdc, redelResponses)
|
2018-12-18 13:43:41 -08:00
|
|
|
if errRes != nil {
|
2019-05-15 07:44:47 -07:00
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("failed to marshal result to JSON", errRes.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
|
|
|
|
}
|
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
func queryPool(ctx sdk.Context, cdc *codec.Codec, k keep.Keeper) (res []byte, err sdk.Error) {
|
|
|
|
pool := k.GetPool(ctx)
|
|
|
|
|
|
|
|
res, errRes := codec.MarshalJSONIndent(cdc, pool)
|
|
|
|
if errRes != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryParameters(ctx sdk.Context, cdc *codec.Codec, k keep.Keeper) (res []byte, err sdk.Error) {
|
|
|
|
params := k.GetParams(ctx)
|
|
|
|
|
|
|
|
res, errRes := codec.MarshalJSONIndent(cdc, params)
|
|
|
|
if errRes != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
2019-04-24 08:48:39 -07:00
|
|
|
|
|
|
|
// QueryValidatorsParams defines the params for the following queries:
|
|
|
|
// - 'custom/staking/validators'
|
|
|
|
type QueryValidatorsParams struct {
|
|
|
|
Page, Limit int
|
|
|
|
Status string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewQueryValidatorsParams(page, limit int, status string) QueryValidatorsParams {
|
|
|
|
return QueryValidatorsParams{page, limit, status}
|
|
|
|
}
|