2018-04-23 08:30:54 -07:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
2018-11-29 06:34:57 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/stake"
|
2018-04-23 08:30:54 -07:00
|
|
|
"net/http"
|
2018-08-08 03:38:39 -07:00
|
|
|
"strings"
|
2018-04-23 08:30:54 -07:00
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
2018-08-08 03:38:39 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/tx"
|
2018-10-19 09:55:20 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/utils"
|
2018-09-13 11:17:32 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2018-04-23 08:30:54 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-08-08 03:38:39 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/stake/tags"
|
2018-08-06 11:11:30 -07:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2018-04-23 08:30:54 -07:00
|
|
|
)
|
|
|
|
|
2018-06-29 15:22:24 -07:00
|
|
|
const storeName = "stake"
|
|
|
|
|
2018-09-13 11:17:32 -07:00
|
|
|
func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec) {
|
2018-08-08 03:38:39 -07:00
|
|
|
|
2018-10-20 13:25:50 -07:00
|
|
|
// Get all delegations from a delegator
|
2018-06-26 19:00:12 -07:00
|
|
|
r.HandleFunc(
|
2018-10-20 13:25:50 -07:00
|
|
|
"/stake/delegators/{delegatorAddr}/delegations",
|
|
|
|
delegatorDelegationsHandlerFn(cliCtx, cdc),
|
|
|
|
).Methods("GET")
|
|
|
|
|
|
|
|
// Get all unbonding delegations from a delegator
|
|
|
|
r.HandleFunc(
|
|
|
|
"/stake/delegators/{delegatorAddr}/unbonding_delegations",
|
|
|
|
delegatorUnbondingDelegationsHandlerFn(cliCtx, cdc),
|
|
|
|
).Methods("GET")
|
|
|
|
|
|
|
|
// Get all redelegations from a delegator
|
|
|
|
r.HandleFunc(
|
|
|
|
"/stake/delegators/{delegatorAddr}/redelegations",
|
|
|
|
delegatorRedelegationsHandlerFn(cliCtx, cdc),
|
2018-08-08 03:38:39 -07:00
|
|
|
).Methods("GET")
|
|
|
|
|
2018-08-13 15:06:48 -07:00
|
|
|
// Get all staking txs (i.e msgs) from a delegator
|
2018-08-08 03:38:39 -07:00
|
|
|
r.HandleFunc(
|
|
|
|
"/stake/delegators/{delegatorAddr}/txs",
|
|
|
|
delegatorTxsHandlerFn(cliCtx, cdc),
|
2018-06-26 19:00:12 -07:00
|
|
|
).Methods("GET")
|
|
|
|
|
2018-08-13 15:06:48 -07:00
|
|
|
// Query all validators that a delegator is bonded to
|
|
|
|
r.HandleFunc(
|
|
|
|
"/stake/delegators/{delegatorAddr}/validators",
|
|
|
|
delegatorValidatorsHandlerFn(cliCtx, cdc),
|
|
|
|
).Methods("GET")
|
|
|
|
|
|
|
|
// Query a validator that a delegator is bonded to
|
|
|
|
r.HandleFunc(
|
|
|
|
"/stake/delegators/{delegatorAddr}/validators/{validatorAddr}",
|
|
|
|
delegatorValidatorHandlerFn(cliCtx, cdc),
|
|
|
|
).Methods("GET")
|
|
|
|
|
|
|
|
// Query a delegation between a delegator and a validator
|
2018-06-26 19:00:12 -07:00
|
|
|
r.HandleFunc(
|
2018-08-08 03:38:39 -07:00
|
|
|
"/stake/delegators/{delegatorAddr}/delegations/{validatorAddr}",
|
|
|
|
delegationHandlerFn(cliCtx, cdc),
|
2018-06-26 19:00:12 -07:00
|
|
|
).Methods("GET")
|
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
// Query all unbonding delegations between a delegator and a validator
|
2018-05-24 08:14:10 -07:00
|
|
|
r.HandleFunc(
|
2018-08-08 03:38:39 -07:00
|
|
|
"/stake/delegators/{delegatorAddr}/unbonding_delegations/{validatorAddr}",
|
2018-09-13 14:23:44 -07:00
|
|
|
unbondingDelegationHandlerFn(cliCtx, cdc),
|
2018-05-24 08:14:10 -07:00
|
|
|
).Methods("GET")
|
2018-06-26 19:00:12 -07:00
|
|
|
|
2018-08-13 15:06:48 -07:00
|
|
|
// Get all validators
|
2018-05-28 23:07:03 -07:00
|
|
|
r.HandleFunc(
|
|
|
|
"/stake/validators",
|
2018-10-19 15:49:10 -07:00
|
|
|
validatorsHandlerFn(cliCtx, cdc),
|
2018-05-28 23:07:03 -07:00
|
|
|
).Methods("GET")
|
2018-08-08 03:38:39 -07:00
|
|
|
|
2018-08-13 15:06:48 -07:00
|
|
|
// Get a single validator info
|
2018-08-08 03:38:39 -07:00
|
|
|
r.HandleFunc(
|
2018-10-19 15:49:10 -07:00
|
|
|
"/stake/validators/{validatorAddr}",
|
2018-08-08 03:38:39 -07:00
|
|
|
validatorHandlerFn(cliCtx, cdc),
|
|
|
|
).Methods("GET")
|
2018-08-13 15:06:48 -07:00
|
|
|
|
2018-11-05 16:42:49 -08:00
|
|
|
// Get all delegations to a validator
|
2018-10-23 01:25:53 -07:00
|
|
|
r.HandleFunc(
|
|
|
|
"/stake/validators/{validatorAddr}/delegations",
|
|
|
|
validatorDelegationsHandlerFn(cliCtx, cdc),
|
|
|
|
).Methods("GET")
|
|
|
|
|
2018-10-20 12:30:07 -07:00
|
|
|
// Get all unbonding delegations from a validator
|
|
|
|
r.HandleFunc(
|
|
|
|
"/stake/validators/{validatorAddr}/unbonding_delegations",
|
|
|
|
validatorUnbondingDelegationsHandlerFn(cliCtx, cdc),
|
|
|
|
).Methods("GET")
|
|
|
|
|
2018-10-20 11:31:20 -07:00
|
|
|
// Get all outgoing redelegations from a validator
|
|
|
|
r.HandleFunc(
|
|
|
|
"/stake/validators/{validatorAddr}/redelegations",
|
|
|
|
validatorRedelegationsHandlerFn(cliCtx, cdc),
|
|
|
|
).Methods("GET")
|
|
|
|
|
2018-08-21 08:32:54 -07:00
|
|
|
// Get the current state of the staking pool
|
|
|
|
r.HandleFunc(
|
|
|
|
"/stake/pool",
|
2018-10-19 15:49:10 -07:00
|
|
|
poolHandlerFn(cliCtx, cdc),
|
2018-08-21 08:32:54 -07:00
|
|
|
).Methods("GET")
|
|
|
|
|
|
|
|
// Get the current staking parameter values
|
|
|
|
r.HandleFunc(
|
|
|
|
"/stake/parameters",
|
2018-10-19 15:49:10 -07:00
|
|
|
paramsHandlerFn(cliCtx, cdc),
|
2018-08-21 08:32:54 -07:00
|
|
|
).Methods("GET")
|
|
|
|
|
2018-04-23 08:30:54 -07:00
|
|
|
}
|
|
|
|
|
2018-08-08 03:38:39 -07:00
|
|
|
// HTTP request handler to query a delegator delegations
|
2018-10-20 13:25:50 -07:00
|
|
|
func delegatorDelegationsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
|
|
|
|
return queryDelegator(cliCtx, cdc, "custom/stake/delegatorDelegations")
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTTP request handler to query a delegator unbonding delegations
|
|
|
|
func delegatorUnbondingDelegationsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
|
|
|
|
return queryDelegator(cliCtx, cdc, "custom/stake/delegatorUnbondingDelegations")
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTTP request handler to query a delegator redelegations
|
|
|
|
func delegatorRedelegationsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
|
|
|
|
return queryDelegator(cliCtx, cdc, "custom/stake/delegatorRedelegations")
|
2018-08-08 03:38:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// HTTP request handler to query all staking txs (msgs) from a delegator
|
2018-09-13 11:17:32 -07:00
|
|
|
func delegatorTxsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
|
2018-08-08 03:38:39 -07:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var typesQuerySlice []string
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
delegatorAddr := vars["delegatorAddr"]
|
|
|
|
|
|
|
|
_, err := sdk.AccAddressFromBech32(delegatorAddr)
|
2018-04-23 08:30:54 -07:00
|
|
|
if err != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
2018-04-23 08:30:54 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-08 03:38:39 -07:00
|
|
|
node, err := cliCtx.GetNode()
|
2018-04-23 08:30:54 -07:00
|
|
|
if err != nil {
|
2018-10-19 15:49:10 -07:00
|
|
|
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-06-26 19:00:12 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-08 03:38:39 -07:00
|
|
|
// Get values from query
|
|
|
|
|
|
|
|
typesQuery := r.URL.Query().Get("type")
|
|
|
|
trimmedQuery := strings.TrimSpace(typesQuery)
|
|
|
|
if len(trimmedQuery) != 0 {
|
|
|
|
typesQuerySlice = strings.Split(trimmedQuery, " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
noQuery := len(typesQuerySlice) == 0
|
|
|
|
isBondTx := contains(typesQuerySlice, "bond")
|
|
|
|
isUnbondTx := contains(typesQuerySlice, "unbond")
|
|
|
|
isRedTx := contains(typesQuerySlice, "redelegate")
|
|
|
|
var txs = []tx.Info{}
|
|
|
|
var actions []string
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case isBondTx:
|
2018-11-29 06:34:57 -08:00
|
|
|
actions = append(actions, stake.MsgDelegate{}.Type())
|
2018-08-08 03:38:39 -07:00
|
|
|
case isUnbondTx:
|
2018-11-29 06:34:57 -08:00
|
|
|
actions = append(actions, stake.MsgBeginUnbonding{}.Type())
|
2018-08-08 03:38:39 -07:00
|
|
|
actions = append(actions, string(tags.ActionCompleteUnbonding))
|
|
|
|
case isRedTx:
|
2018-11-29 06:34:57 -08:00
|
|
|
actions = append(actions, stake.MsgBeginRedelegate{}.Type())
|
2018-08-08 03:38:39 -07:00
|
|
|
actions = append(actions, string(tags.ActionCompleteRedelegation))
|
|
|
|
case noQuery:
|
2018-11-29 06:34:57 -08:00
|
|
|
actions = append(actions, stake.MsgDelegate{}.Type())
|
|
|
|
actions = append(actions, stake.MsgBeginUnbonding{}.Type())
|
2018-08-08 03:38:39 -07:00
|
|
|
actions = append(actions, string(tags.ActionCompleteUnbonding))
|
2018-11-29 06:34:57 -08:00
|
|
|
actions = append(actions, stake.MsgBeginRedelegate{}.Type())
|
2018-08-08 03:38:39 -07:00
|
|
|
actions = append(actions, string(tags.ActionCompleteRedelegation))
|
|
|
|
default:
|
2018-06-26 19:00:12 -07:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-08 03:38:39 -07:00
|
|
|
for _, action := range actions {
|
2018-09-14 11:41:21 -07:00
|
|
|
foundTxs, errQuery := queryTxs(node, cliCtx, cdc, action, delegatorAddr)
|
2018-08-08 03:38:39 -07:00
|
|
|
if errQuery != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-08-08 03:38:39 -07:00
|
|
|
}
|
|
|
|
txs = append(txs, foundTxs...)
|
2018-07-04 14:07:06 -07:00
|
|
|
}
|
2018-06-26 19:00:12 -07:00
|
|
|
|
2018-10-19 15:49:10 -07:00
|
|
|
res, err := cdc.MarshalJSON(txs)
|
2018-06-26 19:00:12 -07:00
|
|
|
if err != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-06-26 19:00:12 -07:00
|
|
|
return
|
|
|
|
}
|
2018-10-19 15:49:10 -07:00
|
|
|
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-08 03:38:39 -07:00
|
|
|
// HTTP request handler to query an unbonding-delegation
|
2018-09-13 14:23:44 -07:00
|
|
|
func unbondingDelegationHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
|
2018-10-19 15:49:10 -07:00
|
|
|
return queryBonds(cliCtx, cdc, "custom/stake/unbondingDelegation")
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2018-10-19 15:49:10 -07:00
|
|
|
// HTTP request handler to query a delegation
|
2018-09-13 11:17:32 -07:00
|
|
|
func delegationHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
|
2018-10-19 15:49:10 -07:00
|
|
|
return queryBonds(cliCtx, cdc, "custom/stake/delegation")
|
2018-08-08 03:38:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// HTTP request handler to query all delegator bonded validators
|
2018-09-13 11:17:32 -07:00
|
|
|
func delegatorValidatorsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
|
2018-10-19 15:49:10 -07:00
|
|
|
return queryDelegator(cliCtx, cdc, "custom/stake/delegatorValidators")
|
2018-08-08 03:38:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// HTTP request handler to get information from a currently bonded validator
|
2018-09-13 11:17:32 -07:00
|
|
|
func delegatorValidatorHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
|
2018-10-19 15:49:10 -07:00
|
|
|
return queryBonds(cliCtx, cdc, "custom/stake/delegatorValidator")
|
2018-04-23 08:30:54 -07:00
|
|
|
}
|
2018-05-28 23:07:03 -07:00
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
// HTTP request handler to query list of validators
|
2018-10-19 15:49:10 -07:00
|
|
|
func validatorsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
|
2018-05-28 23:07:03 -07:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2018-09-13 14:23:44 -07:00
|
|
|
res, err := cliCtx.QueryWithData("custom/stake/validators", nil)
|
2018-05-28 23:07:03 -07:00
|
|
|
if err != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-05-28 23:07:03 -07:00
|
|
|
return
|
|
|
|
}
|
2018-10-19 15:49:10 -07:00
|
|
|
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
|
2018-05-28 23:07:03 -07:00
|
|
|
}
|
|
|
|
}
|
2018-08-08 03:38:39 -07:00
|
|
|
|
|
|
|
// HTTP request handler to query the validator information from a given validator address
|
2018-09-13 11:17:32 -07:00
|
|
|
func validatorHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
|
2018-10-20 11:31:20 -07:00
|
|
|
return queryValidator(cliCtx, cdc, "custom/stake/validator")
|
|
|
|
}
|
2018-08-08 03:38:39 -07:00
|
|
|
|
2018-10-23 01:25:53 -07:00
|
|
|
// HTTP request handler to query all unbonding delegations from a validator
|
|
|
|
func validatorDelegationsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
|
|
|
|
return queryValidator(cliCtx, cdc, "custom/stake/validatorDelegations")
|
|
|
|
}
|
|
|
|
|
2018-10-20 12:30:07 -07:00
|
|
|
// HTTP request handler to query all unbonding delegations from a validator
|
|
|
|
func validatorUnbondingDelegationsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
|
|
|
|
return queryValidator(cliCtx, cdc, "custom/stake/validatorUnbondingDelegations")
|
|
|
|
}
|
2018-08-08 03:38:39 -07:00
|
|
|
|
2018-10-20 11:31:20 -07:00
|
|
|
// HTTP request handler to query all redelegations from a source validator
|
|
|
|
func validatorRedelegationsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
|
|
|
|
return queryValidator(cliCtx, cdc, "custom/stake/validatorRedelegations")
|
2018-08-08 03:38:39 -07:00
|
|
|
}
|
2018-08-21 08:32:54 -07:00
|
|
|
|
|
|
|
// HTTP request handler to query the pool information
|
2018-10-19 15:49:10 -07:00
|
|
|
func poolHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
|
2018-08-21 08:32:54 -07:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2018-09-13 14:23:44 -07:00
|
|
|
res, err := cliCtx.QueryWithData("custom/stake/pool", nil)
|
2018-08-21 08:32:54 -07:00
|
|
|
if err != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-08-21 08:32:54 -07:00
|
|
|
return
|
|
|
|
}
|
2018-10-19 15:49:10 -07:00
|
|
|
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
|
2018-08-21 08:32:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTTP request handler to query the staking params values
|
2018-10-19 15:49:10 -07:00
|
|
|
func paramsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
|
2018-08-21 08:32:54 -07:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2018-09-13 14:23:44 -07:00
|
|
|
res, err := cliCtx.QueryWithData("custom/stake/parameters", nil)
|
2018-08-21 08:32:54 -07:00
|
|
|
if err != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-08-21 08:32:54 -07:00
|
|
|
return
|
|
|
|
}
|
2018-10-19 15:49:10 -07:00
|
|
|
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
|
2018-08-21 08:32:54 -07:00
|
|
|
}
|
|
|
|
}
|