github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/distribution/client/rest/tx.go (about) 1 package rest 2 3 import ( 4 "fmt" 5 "net/http" 6 7 comm "github.com/fibonacci-chain/fbc/x/common" 8 9 "github.com/gorilla/mux" 10 11 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context" 12 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 13 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/rest" 14 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/client/utils" 15 16 "github.com/fibonacci-chain/fbc/x/distribution/client/common" 17 "github.com/fibonacci-chain/fbc/x/distribution/types" 18 ) 19 20 func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router, _ string) { 21 // Replace the rewards withdrawal address 22 r.HandleFunc( 23 "/distribution/delegators/{delegatorAddr}/withdraw_address", 24 setDelegatorWithdrawalAddrHandlerFn(cliCtx), 25 ).Methods("POST") 26 27 // Withdraw validator rewards and commission 28 r.HandleFunc( 29 "/distribution/validators/{validatorAddr}/rewards", 30 withdrawValidatorRewardsHandlerFn(cliCtx), 31 ).Methods("POST") 32 33 } 34 35 type ( 36 withdrawRewardsReq struct { 37 BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"` 38 } 39 40 setWithdrawalAddrReq struct { 41 BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"` 42 WithdrawAddress sdk.AccAddress `json:"withdraw_address" yaml:"withdraw_address"` 43 } 44 ) 45 46 // Replace the rewards withdrawal address 47 func setDelegatorWithdrawalAddrHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { 48 return func(w http.ResponseWriter, r *http.Request) { 49 var req setWithdrawalAddrReq 50 51 if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) { 52 return 53 } 54 55 req.BaseReq = req.BaseReq.Sanitize() 56 if !req.BaseReq.ValidateBasic(w) { 57 return 58 } 59 60 // read and validate URL's variables 61 delAddr, ok := checkDelegatorAddressVar(w, r) 62 if !ok { 63 return 64 } 65 66 msg := types.NewMsgSetWithdrawAddress(delAddr, req.WithdrawAddress) 67 if err := msg.ValidateBasic(); err != nil { 68 comm.HandleErrorMsg(w, cliCtx, comm.CodeInvalidParam, err.Error()) 69 return 70 } 71 72 utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg}) 73 } 74 } 75 76 // Withdraw validator rewards and commission 77 func withdrawValidatorRewardsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { 78 return func(w http.ResponseWriter, r *http.Request) { 79 var req withdrawRewardsReq 80 81 if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) { 82 return 83 } 84 85 req.BaseReq = req.BaseReq.Sanitize() 86 if !req.BaseReq.ValidateBasic(w) { 87 return 88 } 89 90 // read and validate URL's variable 91 valAddr, ok := checkValidatorAddressVar(w, r) 92 if !ok { 93 return 94 } 95 96 // prepare multi-message transaction 97 msgs, err := common.WithdrawValidatorRewardsAndCommission(valAddr) 98 if err != nil { 99 comm.HandleErrorMsg(w, cliCtx, comm.CodeInvalidParam, err.Error()) 100 return 101 } 102 103 utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, msgs) 104 } 105 } 106 107 // Auxiliary 108 109 func checkDelegatorAddressVar(w http.ResponseWriter, r *http.Request) (sdk.AccAddress, bool) { 110 addr, err := sdk.AccAddressFromBech32(mux.Vars(r)["delegatorAddr"]) 111 if err != nil { 112 rest.WriteErrorResponse(w, int(types.CodeAccAddressFromBech32Failed), fmt.Sprintf("invalid address:%s", mux.Vars(r)["delegatorAddr"])) 113 return nil, false 114 } 115 116 return addr, true 117 } 118 119 func checkValidatorAddressVar(w http.ResponseWriter, r *http.Request) (sdk.ValAddress, bool) { 120 addr, err := sdk.ValAddressFromBech32(mux.Vars(r)["validatorAddr"]) 121 if err != nil { 122 rest.WriteErrorResponse(w, int(types.CodeValAddressFromBech32), fmt.Sprintf("invalid address:%s", mux.Vars(r)["validatorAddr"])) 123 return nil, false 124 } 125 126 return addr, true 127 }