Skip to content

Commit

Permalink
Add CreateValidator and EditValidator in REST
Browse files Browse the repository at this point in the history
  • Loading branch information
preminem committed Oct 12, 2019
1 parent 235dc49 commit ab0fb28
Showing 1 changed file with 114 additions and 0 deletions.
114 changes: 114 additions & 0 deletions x/staking/client/rest/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ import (
)

func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router) {
r.HandleFunc(
"/staking/validators/{validatorAddr}",
createValidatorHandlerFn(cliCtx),
).Methods("POST")
r.HandleFunc(
"/staking/validators/{validatorAddr}",
editValidatorHandlerFn(cliCtx),
).Methods("PUT")
r.HandleFunc(
"/staking/delegators/{delegatorAddr}/delegations",
postDelegationsHandlerFn(cliCtx),
Expand All @@ -29,6 +37,26 @@ func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router) {
}

type (
// CreateValidatorRequest defines the properties of a create validator request's body.
CreateValidatorRequest struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
ValidatorAddress sdk.ValAddress `json:"validator_address" yaml:"validator_address"` // in bech32
Pubkey string `json:"pubkey" yaml:"pubkey"`
Value sdk.Coin `json:"value" yaml:"value"`
Description types.Description `json:"description" yaml:"description"`
Commission types.CommissionRates `json:"commission" yaml:"commission"`
MinSelfDelegation sdk.Int `json:"min_self_delegation" yaml:"min_self_delegation"`
}

// EditValidatorRequest defines the properties of a edit validator request's body.
EditValidatorRequest struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
ValidatorAddress sdk.ValAddress `json:"address" yaml:"address"`
Description types.Description `json:"description" yaml:"description"`
CommissionRate *sdk.Dec `json:"commission_rate" yaml:"commission_rate"`
MinSelfDelegation *sdk.Int `json:"min_self_delegation" yaml:"min_self_delegation"`
}

// DelegateRequest defines the properties of a delegation request's body.
DelegateRequest struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
Expand All @@ -55,6 +83,92 @@ type (
}
)

// createValidatorHandlerFn implements a create validator handler that is responsible
// for constructing a properly formatted create validator transaction for signing.
//
// @Summary Generate a create validator transaction
// @Description Generate a create validator transaction that is ready for signing
// @Tags staking
// @Accept json
// @Produce json
// @Param validatorAddr path string true "The validator address"
// @Param body body rest.CreateValidatorRequest true "The create validator request payload"
// @Success 200 {object} rest.createValidator "Returns the unsigned transaction"
// @Failure 400 {object} rest.ErrorResponse "Returned if the request is invalid"
// @Failure 401 {object} rest.ErrorResponse "Returned if chain-id required but not present"
// @Failure 402 {object} rest.ErrorResponse "Returned if fees or gas are invalid"
// @Failure 500 {object} rest.ErrorResponse "Returned on server error"
// @Router /staking/validators/{validatorAddr} [post]
func createValidatorHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {

var req CreateValidatorRequest

if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) {
return
}

req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}

pk, err := sdk.GetConsPubKeyBech32(req.Pubkey)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

msg := types.NewMsgCreateValidator(req.ValidatorAddress, pk, req.Value, req.Description, req.Commission, req.MinSelfDelegation)
if err := msg.ValidateBasic(); err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg})
}
}

// editValidatorHandlerFn implements a edit validator handler that is responsible
// for constructing a properly formatted edit validator transaction for signing.
//
// @Summary Generate a edit validator transaction
// @Description Generate a edit validator transaction that is ready for signing
// @Tags staking
// @Accept json
// @Produce json
// @Param validatorAddr path string true "The validator address"
// @Param body body rest.EditValidatorRequest true "The edit validator request payload"
// @Success 200 {object} rest.editValidator "Returns the unsigned transaction"
// @Failure 400 {object} rest.ErrorResponse "Returned if the request is invalid"
// @Failure 401 {object} rest.ErrorResponse "Returned if chain-id required but not present"
// @Failure 402 {object} rest.ErrorResponse "Returned if fees or gas are invalid"
// @Failure 500 {object} rest.ErrorResponse "Returned on server error"
// @Router /staking/validators/{validatorAddr} [put]
func editValidatorHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {

var req EditValidatorRequest

if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) {
return
}

req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}

msg := types.NewMsgEditValidator(req.ValidatorAddress, req.Description, req.CommissionRate, req.MinSelfDelegation)
if err := msg.ValidateBasic(); err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg})
}
}

// postDelegationsHandlerFn implements a delegation handler that is responsible
// for constructing a properly formatted delegation transaction for signing.
//
Expand Down

0 comments on commit ab0fb28

Please sign in to comment.