package rest import ( "fmt" "net/http" "github.com/gorilla/mux" rpcclient "github.com/tendermint/tendermint/rpc/client" "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/tx" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/rest" "github.com/cosmos/cosmos-sdk/x/staking" "github.com/cosmos/cosmos-sdk/x/staking/tags" ) // contains checks if the a given query contains one of the tx types func contains(stringSlice []string, txType string) bool { for _, word := range stringSlice { if word == txType { return true } } return false } // queries staking txs func queryTxs(node rpcclient.Client, cliCtx context.CLIContext, cdc *codec.Codec, tag string, delegatorAddr string) ([]sdk.TxResponse, error) { page := 0 perPage := 100 prove := !cliCtx.TrustNode query := fmt.Sprintf("%s='%s' AND %s='%s'", tags.Action, tag, tags.Delegator, delegatorAddr) res, err := node.TxSearch(query, prove, page, perPage) if err != nil { return nil, err } if prove { for _, txData := range res.Txs { err := tx.ValidateTxResult(cliCtx, txData) if err != nil { return nil, err } } } return tx.FormatTxResults(cdc, res.Txs) } func queryRedelegations(cliCtx context.CLIContext, cdc *codec.Codec, endpoint string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) bech32delegator := vars["delegatorAddr"] bech32srcValidator := vars["srcValidatorAddr"] bech32dstValidator := vars["dstValidatorAddr"] delegatorAddr, err := sdk.AccAddressFromBech32(bech32delegator) if err != nil { rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) return } srcValidatorAddr, err := sdk.ValAddressFromBech32(bech32srcValidator) if err != nil { rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) return } dstValidatorAddr, err := sdk.ValAddressFromBech32(bech32dstValidator) if err != nil { rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) return } params := staking.QueryRedelegationParams{ DelegatorAddr: delegatorAddr, SrcValidatorAddr: srcValidatorAddr, DstValidatorAddr: dstValidatorAddr, } bz, err := cdc.MarshalJSON(params) if err != nil { rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) return } res, err := cliCtx.QueryWithData(endpoint, bz) if err != nil { rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } rest.PostProcessResponse(w, cdc, res, cliCtx.Indent) } } func queryBonds(cliCtx context.CLIContext, cdc *codec.Codec, endpoint string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) bech32delegator := vars["delegatorAddr"] bech32validator := vars["validatorAddr"] delegatorAddr, err := sdk.AccAddressFromBech32(bech32delegator) validatorAddr, err := sdk.ValAddressFromBech32(bech32validator) if err != nil { rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) return } params := staking.NewQueryBondsParams(delegatorAddr, validatorAddr) bz, err := cdc.MarshalJSON(params) if err != nil { rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) return } res, err := cliCtx.QueryWithData(endpoint, bz) if err != nil { rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } rest.PostProcessResponse(w, cdc, res, cliCtx.Indent) } } func queryDelegator(cliCtx context.CLIContext, cdc *codec.Codec, endpoint string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) bech32delegator := vars["delegatorAddr"] delegatorAddr, err := sdk.AccAddressFromBech32(bech32delegator) if err != nil { rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) return } params := staking.NewQueryDelegatorParams(delegatorAddr) bz, err := cdc.MarshalJSON(params) if err != nil { rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) return } res, err := cliCtx.QueryWithData(endpoint, bz) if err != nil { rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } rest.PostProcessResponse(w, cdc, res, cliCtx.Indent) } } func queryValidator(cliCtx context.CLIContext, cdc *codec.Codec, endpoint string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) bech32validatorAddr := vars["validatorAddr"] validatorAddr, err := sdk.ValAddressFromBech32(bech32validatorAddr) if err != nil { rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) return } params := staking.NewQueryValidatorParams(validatorAddr) bz, err := cdc.MarshalJSON(params) if err != nil { rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) return } res, err := cliCtx.QueryWithData(endpoint, bz) if err != nil { rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } rest.PostProcessResponse(w, cdc, res, cliCtx.Indent) } }