github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/staking/client/rest/utils.go (about) 1 package rest 2 3 import ( 4 "fmt" 5 "net/http" 6 7 "github.com/gorilla/mux" 8 9 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context" 10 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 11 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/rest" 12 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/client/utils" 13 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/staking/types" 14 ) 15 16 // contains checks if the a given query contains one of the tx types 17 func contains(stringSlice []string, txType string) bool { 18 for _, word := range stringSlice { 19 if word == txType { 20 return true 21 } 22 } 23 return false 24 } 25 26 // queries staking txs 27 func queryTxs(cliCtx context.CLIContext, action string, delegatorAddr string) (*sdk.SearchTxsResult, error) { 28 page := 1 29 limit := 100 30 events := []string{ 31 fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, action), 32 fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeySender, delegatorAddr), 33 } 34 35 return utils.QueryTxsByEvents(cliCtx, events, page, limit) 36 } 37 38 func queryBonds(cliCtx context.CLIContext, endpoint string) http.HandlerFunc { 39 return func(w http.ResponseWriter, r *http.Request) { 40 vars := mux.Vars(r) 41 bech32delegator := vars["delegatorAddr"] 42 bech32validator := vars["validatorAddr"] 43 44 delegatorAddr, err := sdk.AccAddressFromBech32(bech32delegator) 45 if err != nil { 46 rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) 47 return 48 } 49 50 validatorAddr, err := sdk.ValAddressFromBech32(bech32validator) 51 if err != nil { 52 rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) 53 return 54 } 55 56 cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) 57 if !ok { 58 return 59 } 60 61 params := types.NewQueryBondsParams(delegatorAddr, validatorAddr) 62 63 bz, err := cliCtx.Codec.MarshalJSON(params) 64 if err != nil { 65 rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) 66 return 67 } 68 69 res, height, err := cliCtx.QueryWithData(endpoint, bz) 70 if err != nil { 71 rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) 72 return 73 } 74 75 cliCtx = cliCtx.WithHeight(height) 76 rest.PostProcessResponse(w, cliCtx, res) 77 } 78 } 79 80 func queryDelegator(cliCtx context.CLIContext, endpoint string) http.HandlerFunc { 81 return func(w http.ResponseWriter, r *http.Request) { 82 vars := mux.Vars(r) 83 bech32delegator := vars["delegatorAddr"] 84 85 delegatorAddr, err := sdk.AccAddressFromBech32(bech32delegator) 86 if err != nil { 87 rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) 88 return 89 } 90 91 cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) 92 if !ok { 93 return 94 } 95 96 params := types.NewQueryDelegatorParams(delegatorAddr) 97 98 bz, err := cliCtx.Codec.MarshalJSON(params) 99 if err != nil { 100 rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) 101 return 102 } 103 104 res, height, err := cliCtx.QueryWithData(endpoint, bz) 105 if err != nil { 106 rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) 107 return 108 } 109 110 cliCtx = cliCtx.WithHeight(height) 111 rest.PostProcessResponse(w, cliCtx, res) 112 } 113 } 114 115 func queryValidator(cliCtx context.CLIContext, endpoint string) http.HandlerFunc { 116 return func(w http.ResponseWriter, r *http.Request) { 117 vars := mux.Vars(r) 118 bech32validatorAddr := vars["validatorAddr"] 119 120 validatorAddr, err := sdk.ValAddressFromBech32(bech32validatorAddr) 121 if err != nil { 122 rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) 123 return 124 } 125 126 cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) 127 if !ok { 128 return 129 } 130 131 params := types.NewQueryValidatorParams(validatorAddr) 132 133 bz, err := cliCtx.Codec.MarshalJSON(params) 134 if err != nil { 135 rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) 136 return 137 } 138 139 res, height, err := cliCtx.QueryWithData(endpoint, bz) 140 if err != nil { 141 rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) 142 return 143 } 144 145 cliCtx = cliCtx.WithHeight(height) 146 rest.PostProcessResponse(w, cliCtx, res) 147 } 148 }