github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/staking/client/rest/tx.go (about) 1 package rest 2 3 import ( 4 "bytes" 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 func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router) { 17 r.HandleFunc( 18 "/staking/delegators/{delegatorAddr}/delegations", 19 postDelegationsHandlerFn(cliCtx), 20 ).Methods("POST") 21 r.HandleFunc( 22 "/staking/delegators/{delegatorAddr}/unbonding_delegations", 23 postUnbondingDelegationsHandlerFn(cliCtx), 24 ).Methods("POST") 25 r.HandleFunc( 26 "/staking/delegators/{delegatorAddr}/redelegations", 27 postRedelegationsHandlerFn(cliCtx), 28 ).Methods("POST") 29 } 30 31 type ( 32 // DelegateRequest defines the properties of a delegation request's body. 33 DelegateRequest struct { 34 BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"` 35 DelegatorAddress sdk.AccAddress `json:"delegator_address" yaml:"delegator_address"` // in bech32 36 ValidatorAddress sdk.ValAddress `json:"validator_address" yaml:"validator_address"` // in bech32 37 Amount sdk.Coin `json:"amount" yaml:"amount"` 38 } 39 40 // RedelegateRequest defines the properties of a redelegate request's body. 41 RedelegateRequest struct { 42 BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"` 43 DelegatorAddress sdk.AccAddress `json:"delegator_address" yaml:"delegator_address"` // in bech32 44 ValidatorSrcAddress sdk.ValAddress `json:"validator_src_address" yaml:"validator_src_address"` // in bech32 45 ValidatorDstAddress sdk.ValAddress `json:"validator_dst_address" yaml:"validator_dst_address"` // in bech32 46 Amount sdk.Coin `json:"amount" yaml:"amount"` 47 } 48 49 // UndelegateRequest defines the properties of a undelegate request's body. 50 UndelegateRequest struct { 51 BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"` 52 DelegatorAddress sdk.AccAddress `json:"delegator_address" yaml:"delegator_address"` // in bech32 53 ValidatorAddress sdk.ValAddress `json:"validator_address" yaml:"validator_address"` // in bech32 54 Amount sdk.Coin `json:"amount" yaml:"amount"` 55 } 56 ) 57 58 func postDelegationsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { 59 return func(w http.ResponseWriter, r *http.Request) { 60 var req DelegateRequest 61 62 if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) { 63 return 64 } 65 66 req.BaseReq = req.BaseReq.Sanitize() 67 if !req.BaseReq.ValidateBasic(w) { 68 return 69 } 70 71 msg := types.NewMsgDelegate(req.DelegatorAddress, req.ValidatorAddress, req.Amount) 72 if err := msg.ValidateBasic(); err != nil { 73 rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) 74 return 75 } 76 77 fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From) 78 if err != nil { 79 rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) 80 return 81 } 82 83 if !bytes.Equal(fromAddr, req.DelegatorAddress) { 84 rest.WriteErrorResponse(w, http.StatusUnauthorized, "must use own delegator address") 85 return 86 } 87 88 utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg}) 89 } 90 } 91 92 func postRedelegationsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { 93 return func(w http.ResponseWriter, r *http.Request) { 94 var req RedelegateRequest 95 96 if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) { 97 return 98 } 99 100 req.BaseReq = req.BaseReq.Sanitize() 101 if !req.BaseReq.ValidateBasic(w) { 102 return 103 } 104 105 msg := types.NewMsgBeginRedelegate(req.DelegatorAddress, req.ValidatorSrcAddress, req.ValidatorDstAddress, req.Amount) 106 if err := msg.ValidateBasic(); err != nil { 107 rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) 108 return 109 } 110 111 fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From) 112 if err != nil { 113 rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) 114 return 115 } 116 117 if !bytes.Equal(fromAddr, req.DelegatorAddress) { 118 rest.WriteErrorResponse(w, http.StatusUnauthorized, "must use own delegator address") 119 return 120 } 121 122 utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg}) 123 } 124 } 125 126 func postUnbondingDelegationsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { 127 return func(w http.ResponseWriter, r *http.Request) { 128 var req UndelegateRequest 129 130 if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) { 131 return 132 } 133 134 req.BaseReq = req.BaseReq.Sanitize() 135 if !req.BaseReq.ValidateBasic(w) { 136 return 137 } 138 139 msg := types.NewMsgUndelegate(req.DelegatorAddress, req.ValidatorAddress, req.Amount) 140 if err := msg.ValidateBasic(); err != nil { 141 rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) 142 return 143 } 144 145 fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From) 146 if err != nil { 147 rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) 148 return 149 } 150 151 if !bytes.Equal(fromAddr, req.DelegatorAddress) { 152 rest.WriteErrorResponse(w, http.StatusUnauthorized, "must use own delegator address") 153 return 154 } 155 156 utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg}) 157 } 158 }