* Refactor CliContext as Context
* Fix lint issues
* Fix goimports
* Fix gov tests
* Resolved ci-lint issues
* Add changelog
* Rename cliCtx to clientCtx
* Fix mocks and routes
* Add changelog
* Update changelog
* Apply suggestions from code review
Co-authored-by: Alessio Treglia <alessio@tendermint.com>
* merge client/rpc/ro{ot,utes}.go
* Update docs
* client/rpc: remove redundant client/rpc.RegisterRPCRoutes
* regenerate mocks
* Update ADRs
Co-authored-by: Alessio Treglia <alessio@tendermint.com>
Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
140 lines
3.5 KiB
Go
140 lines
3.5 KiB
Go
package rest
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
|
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
|
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
|
)
|
|
|
|
// 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(clientCtx client.Context, action string, delegatorAddr string) (*sdk.SearchTxsResult, error) {
|
|
page := 1
|
|
limit := 100
|
|
events := []string{
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, action),
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeySender, delegatorAddr),
|
|
}
|
|
|
|
return authclient.QueryTxsByEvents(clientCtx, events, page, limit, "")
|
|
}
|
|
|
|
func queryBonds(clientCtx client.Context, 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)
|
|
if rest.CheckBadRequestError(w, err) {
|
|
return
|
|
}
|
|
|
|
validatorAddr, err := sdk.ValAddressFromBech32(bech32validator)
|
|
if rest.CheckBadRequestError(w, err) {
|
|
return
|
|
}
|
|
|
|
clientCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, clientCtx, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
params := types.NewQueryBondsParams(delegatorAddr, validatorAddr)
|
|
|
|
bz, err := clientCtx.Codec.MarshalJSON(params)
|
|
if rest.CheckBadRequestError(w, err) {
|
|
return
|
|
}
|
|
|
|
res, height, err := clientCtx.QueryWithData(endpoint, bz)
|
|
if rest.CheckInternalServerError(w, err) {
|
|
return
|
|
}
|
|
|
|
clientCtx = clientCtx.WithHeight(height)
|
|
rest.PostProcessResponse(w, clientCtx, res)
|
|
}
|
|
}
|
|
|
|
func queryDelegator(clientCtx client.Context, 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 rest.CheckBadRequestError(w, err) {
|
|
return
|
|
}
|
|
|
|
clientCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, clientCtx, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
params := types.NewQueryDelegatorParams(delegatorAddr)
|
|
|
|
bz, err := clientCtx.Codec.MarshalJSON(params)
|
|
if rest.CheckBadRequestError(w, err) {
|
|
return
|
|
}
|
|
|
|
res, height, err := clientCtx.QueryWithData(endpoint, bz)
|
|
if rest.CheckInternalServerError(w, err) {
|
|
return
|
|
}
|
|
|
|
clientCtx = clientCtx.WithHeight(height)
|
|
rest.PostProcessResponse(w, clientCtx, res)
|
|
}
|
|
}
|
|
|
|
func queryValidator(clientCtx client.Context, 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 rest.CheckBadRequestError(w, err) {
|
|
return
|
|
}
|
|
|
|
clientCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, clientCtx, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
params := types.NewQueryValidatorParams(validatorAddr)
|
|
|
|
bz, err := clientCtx.Codec.MarshalJSON(params)
|
|
if rest.CheckBadRequestError(w, err) {
|
|
return
|
|
}
|
|
|
|
res, height, err := clientCtx.QueryWithData(endpoint, bz)
|
|
if rest.CheckInternalServerError(w, err) {
|
|
return
|
|
}
|
|
|
|
clientCtx = clientCtx.WithHeight(height)
|
|
rest.PostProcessResponse(w, clientCtx, res)
|
|
}
|
|
}
|