2018-09-13 14:23:44 -07:00
|
|
|
package querier
|
|
|
|
|
|
|
|
import (
|
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"
|
2018-10-20 12:30:07 -07:00
|
|
|
QueryValidatorUnbondingDelegations = "validatorUnbondingDelegations"
|
|
|
|
QueryDelegator = "delegator"
|
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:
|
|
|
|
return queryValidators(ctx, cdc, k)
|
|
|
|
case QueryValidator:
|
|
|
|
return queryValidator(ctx, cdc, req, k)
|
2018-10-23 00:42:06 -07:00
|
|
|
case QueryValidatorDelegations:
|
|
|
|
return queryValidatorDelegations(ctx, cdc, req, k)
|
2018-10-20 12:30:07 -07:00
|
|
|
case QueryValidatorUnbondingDelegations:
|
|
|
|
return queryValidatorUnbondingDelegations(ctx, cdc, req, k)
|
2018-09-13 14:23:44 -07:00
|
|
|
case QueryDelegation:
|
|
|
|
return queryDelegation(ctx, cdc, req, k)
|
|
|
|
case QueryUnbondingDelegation:
|
|
|
|
return queryUnbondingDelegation(ctx, cdc, req, k)
|
2018-10-20 13:25:50 -07:00
|
|
|
case QueryDelegatorDelegations:
|
|
|
|
return queryDelegatorDelegations(ctx, cdc, req, k)
|
|
|
|
case QueryDelegatorUnbondingDelegations:
|
|
|
|
return queryDelegatorUnbondingDelegations(ctx, cdc, req, k)
|
2018-12-18 13:43:41 -08:00
|
|
|
case QueryRedelegations:
|
|
|
|
return queryRedelegations(ctx, cdc, req, k)
|
2018-09-13 14:23:44 -07:00
|
|
|
case QueryDelegatorValidators:
|
|
|
|
return queryDelegatorValidators(ctx, cdc, req, k)
|
|
|
|
case QueryDelegatorValidator:
|
|
|
|
return queryDelegatorValidator(ctx, cdc, req, k)
|
|
|
|
case QueryPool:
|
|
|
|
return queryPool(ctx, cdc, k)
|
|
|
|
case QueryParameters:
|
|
|
|
return queryParameters(ctx, cdc, k)
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
func queryValidators(ctx sdk.Context, cdc *codec.Codec, k keep.Keeper) (res []byte, err sdk.Error) {
|
2019-01-11 12:08:01 -08:00
|
|
|
stakingParams := k.GetParams(ctx)
|
|
|
|
validators := k.GetValidators(ctx, stakingParams.MaxValidators)
|
2018-09-13 14:23:44 -07:00
|
|
|
|
|
|
|
res, errRes := codec.MarshalJSONIndent(cdc, validators)
|
|
|
|
if err != 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 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 {
|
2018-10-19 09:55:20 -07:00
|
|
|
return []byte{}, sdk.ErrUnknownAddress("")
|
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 {
|
|
|
|
return []byte{}, sdk.ErrUnknownAddress("")
|
|
|
|
}
|
|
|
|
|
2018-10-23 13:30:14 -07:00
|
|
|
delegations := k.GetValidatorDelegations(ctx, params.ValidatorAddr)
|
2018-10-23 00:42:06 -07:00
|
|
|
|
|
|
|
res, errRes = codec.MarshalJSONIndent(cdc, delegations)
|
|
|
|
if errRes != nil {
|
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
return []byte{}, sdk.ErrUnknownAddress("")
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2018-10-19 09:55:20 -07:00
|
|
|
return []byte{}, sdk.ErrUnknownAddress("")
|
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)
|
2018-10-20 13:25:50 -07:00
|
|
|
|
|
|
|
res, errRes = codec.MarshalJSONIndent(cdc, delegations)
|
|
|
|
if errRes != nil {
|
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
return []byte{}, sdk.ErrUnknownAddress("")
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-10-19 09:55:20 -07:00
|
|
|
return []byte{}, sdk.ErrUnknownAddress("")
|
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 {
|
2018-10-19 09:55:20 -07:00
|
|
|
return []byte{}, sdk.ErrUnknownAddress("")
|
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 {
|
2018-10-19 09:55:20 -07:00
|
|
|
return []byte{}, sdk.ErrUnknownAddress("")
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, errRes = codec.MarshalJSONIndent(cdc, delegation)
|
|
|
|
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 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 {
|
2018-10-19 09:55:20 -07:00
|
|
|
return []byte{}, sdk.ErrUnknownAddress("")
|
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 {
|
|
|
|
return []byte{}, types.ErrNoRedelegation(types.DefaultCodespace)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, errRes = codec.MarshalJSONIndent(cdc, redels)
|
|
|
|
if errRes != nil {
|
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", errRes.Error()))
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|