github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/distribution/client/rest/tx.go (about) 1 package rest 2 3 import ( 4 "net/http" 5 6 "github.com/gorilla/mux" 7 8 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context" 9 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/client/utils" 10 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/distribution/client/common" 11 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/distribution/types" 12 13 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 14 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/rest" 15 ) 16 17 func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router, queryRoute string) { 18 // Withdraw all delegator rewards 19 r.HandleFunc( 20 "/distribution/delegators/{delegatorAddr}/rewards", 21 withdrawDelegatorRewardsHandlerFn(cliCtx, queryRoute), 22 ).Methods("POST") 23 24 // Withdraw delegation rewards 25 r.HandleFunc( 26 "/distribution/delegators/{delegatorAddr}/rewards/{validatorAddr}", 27 withdrawDelegationRewardsHandlerFn(cliCtx), 28 ).Methods("POST") 29 30 // Replace the rewards withdrawal address 31 r.HandleFunc( 32 "/distribution/delegators/{delegatorAddr}/withdraw_address", 33 setDelegatorWithdrawalAddrHandlerFn(cliCtx), 34 ).Methods("POST") 35 36 // Withdraw validator rewards and commission 37 r.HandleFunc( 38 "/distribution/validators/{validatorAddr}/rewards", 39 withdrawValidatorRewardsHandlerFn(cliCtx), 40 ).Methods("POST") 41 42 // Fund the community pool 43 r.HandleFunc( 44 "/distribution/community_pool", 45 fundCommunityPoolHandlerFn(cliCtx), 46 ).Methods("POST") 47 48 } 49 50 type ( 51 withdrawRewardsReq struct { 52 BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"` 53 } 54 55 setWithdrawalAddrReq struct { 56 BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"` 57 WithdrawAddress sdk.AccAddress `json:"withdraw_address" yaml:"withdraw_address"` 58 } 59 60 fundCommunityPoolReq struct { 61 BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"` 62 Amount sdk.Coins `json:"amount" yaml:"amount"` 63 } 64 ) 65 66 // Withdraw delegator rewards 67 func withdrawDelegatorRewardsHandlerFn(cliCtx context.CLIContext, queryRoute string) http.HandlerFunc { 68 return func(w http.ResponseWriter, r *http.Request) { 69 var req withdrawRewardsReq 70 if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) { 71 return 72 } 73 74 req.BaseReq = req.BaseReq.Sanitize() 75 if !req.BaseReq.ValidateBasic(w) { 76 return 77 } 78 79 // read and validate URL's variables 80 delAddr, ok := checkDelegatorAddressVar(w, r) 81 if !ok { 82 return 83 } 84 85 msgs, err := common.WithdrawAllDelegatorRewards(cliCtx, queryRoute, delAddr) 86 if err != nil { 87 rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) 88 return 89 } 90 91 utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, msgs) 92 } 93 } 94 95 // Withdraw delegation rewards 96 func withdrawDelegationRewardsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { 97 return func(w http.ResponseWriter, r *http.Request) { 98 var req withdrawRewardsReq 99 100 if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) { 101 return 102 } 103 104 req.BaseReq = req.BaseReq.Sanitize() 105 if !req.BaseReq.ValidateBasic(w) { 106 return 107 } 108 109 // read and validate URL's variables 110 delAddr, ok := checkDelegatorAddressVar(w, r) 111 if !ok { 112 return 113 } 114 115 valAddr, ok := checkValidatorAddressVar(w, r) 116 if !ok { 117 return 118 } 119 120 msg := types.NewMsgWithdrawDelegatorReward(delAddr, valAddr) 121 if err := msg.ValidateBasic(); err != nil { 122 rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) 123 return 124 } 125 126 utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg}) 127 } 128 } 129 130 // Replace the rewards withdrawal address 131 func setDelegatorWithdrawalAddrHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { 132 return func(w http.ResponseWriter, r *http.Request) { 133 var req setWithdrawalAddrReq 134 135 if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) { 136 return 137 } 138 139 req.BaseReq = req.BaseReq.Sanitize() 140 if !req.BaseReq.ValidateBasic(w) { 141 return 142 } 143 144 // read and validate URL's variables 145 delAddr, ok := checkDelegatorAddressVar(w, r) 146 if !ok { 147 return 148 } 149 150 msg := types.NewMsgSetWithdrawAddress(delAddr, req.WithdrawAddress) 151 if err := msg.ValidateBasic(); err != nil { 152 rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) 153 return 154 } 155 156 utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg}) 157 } 158 } 159 160 // Withdraw validator rewards and commission 161 func withdrawValidatorRewardsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { 162 return func(w http.ResponseWriter, r *http.Request) { 163 var req withdrawRewardsReq 164 165 if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) { 166 return 167 } 168 169 req.BaseReq = req.BaseReq.Sanitize() 170 if !req.BaseReq.ValidateBasic(w) { 171 return 172 } 173 174 // read and validate URL's variable 175 valAddr, ok := checkValidatorAddressVar(w, r) 176 if !ok { 177 return 178 } 179 180 // prepare multi-message transaction 181 msgs, err := common.WithdrawValidatorRewardsAndCommission(valAddr) 182 if err != nil { 183 rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) 184 return 185 } 186 187 utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, msgs) 188 } 189 } 190 191 func fundCommunityPoolHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { 192 return func(w http.ResponseWriter, r *http.Request) { 193 var req fundCommunityPoolReq 194 if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) { 195 return 196 } 197 198 req.BaseReq = req.BaseReq.Sanitize() 199 if !req.BaseReq.ValidateBasic(w) { 200 return 201 } 202 203 fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From) 204 if err != nil { 205 rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) 206 return 207 } 208 209 msg := types.NewMsgFundCommunityPool(req.Amount, fromAddr) 210 if err := msg.ValidateBasic(); err != nil { 211 rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) 212 return 213 } 214 215 utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg}) 216 } 217 } 218 219 // Auxiliary 220 221 func checkDelegatorAddressVar(w http.ResponseWriter, r *http.Request) (sdk.AccAddress, bool) { 222 addr, err := sdk.AccAddressFromBech32(mux.Vars(r)["delegatorAddr"]) 223 if err != nil { 224 rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) 225 return nil, false 226 } 227 228 return addr, true 229 } 230 231 func checkValidatorAddressVar(w http.ResponseWriter, r *http.Request) (sdk.ValAddress, bool) { 232 addr, err := sdk.ValAddressFromBech32(mux.Vars(r)["validatorAddr"]) 233 if err != nil { 234 rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) 235 return nil, false 236 } 237 238 return addr, true 239 }