cosmos-sdk/x/staking/client/rest/tx.go
SaReN 39f53ac22f
client: rename CliContext to Context (#6290)
* Refactor CliContext as Context

* Fix lint issues

* Fix goimports

* Fix gov tests

* Resolved ci-lint issues

* Add changelog

* Rename cliCtx to clientCtx

* Fix mocks and routes

* Add changelog

* Update changelog

* Apply suggestions from code review

Co-authored-by: Alessio Treglia <alessio@tendermint.com>

* merge client/rpc/ro{ot,utes}.go

* Update docs

* client/rpc: remove redundant client/rpc.RegisterRPCRoutes

* regenerate mocks

* Update ADRs

Co-authored-by: Alessio Treglia <alessio@tendermint.com>
Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2020-06-01 12:46:03 +00:00

267 lines
7.9 KiB
Go

package rest
import (
"bytes"
"net/http"
"github.com/gorilla/mux"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)
func registerTxHandlers(clientCtx client.Context, r *mux.Router) {
r.HandleFunc(
"/staking/delegators/{delegatorAddr}/delegations",
newPostDelegationsHandlerFn(clientCtx),
).Methods("POST")
r.HandleFunc(
"/staking/delegators/{delegatorAddr}/unbonding_delegations",
newPostUnbondingDelegationsHandlerFn(clientCtx),
).Methods("POST")
r.HandleFunc(
"/staking/delegators/{delegatorAddr}/redelegations",
newPostRedelegationsHandlerFn(clientCtx),
).Methods("POST")
}
type (
// DelegateRequest defines the properties of a delegation request's body.
DelegateRequest struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
DelegatorAddress sdk.AccAddress `json:"delegator_address" yaml:"delegator_address"` // in bech32
ValidatorAddress sdk.ValAddress `json:"validator_address" yaml:"validator_address"` // in bech32
Amount sdk.Coin `json:"amount" yaml:"amount"`
}
// RedelegateRequest defines the properties of a redelegate request's body.
RedelegateRequest struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
DelegatorAddress sdk.AccAddress `json:"delegator_address" yaml:"delegator_address"` // in bech32
ValidatorSrcAddress sdk.ValAddress `json:"validator_src_address" yaml:"validator_src_address"` // in bech32
ValidatorDstAddress sdk.ValAddress `json:"validator_dst_address" yaml:"validator_dst_address"` // in bech32
Amount sdk.Coin `json:"amount" yaml:"amount"`
}
// UndelegateRequest defines the properties of a undelegate request's body.
UndelegateRequest struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
DelegatorAddress sdk.AccAddress `json:"delegator_address" yaml:"delegator_address"` // in bech32
ValidatorAddress sdk.ValAddress `json:"validator_address" yaml:"validator_address"` // in bech32
Amount sdk.Coin `json:"amount" yaml:"amount"`
}
)
func newPostDelegationsHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req DelegateRequest
if !rest.ReadRESTReq(w, r, clientCtx.Codec, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
msg := types.NewMsgDelegate(req.DelegatorAddress, req.ValidatorAddress, req.Amount)
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
if rest.CheckBadRequestError(w, err) {
return
}
if !bytes.Equal(fromAddr, req.DelegatorAddress) {
rest.WriteErrorResponse(w, http.StatusUnauthorized, "must use own delegator address")
return
}
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}
func newPostRedelegationsHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req RedelegateRequest
if !rest.ReadRESTReq(w, r, clientCtx.Codec, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
msg := types.NewMsgBeginRedelegate(req.DelegatorAddress, req.ValidatorSrcAddress, req.ValidatorDstAddress, req.Amount)
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
if rest.CheckBadRequestError(w, err) {
return
}
if !bytes.Equal(fromAddr, req.DelegatorAddress) {
rest.WriteErrorResponse(w, http.StatusUnauthorized, "must use own delegator address")
return
}
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}
func newPostUnbondingDelegationsHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req UndelegateRequest
if !rest.ReadRESTReq(w, r, clientCtx.Codec, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
msg := types.NewMsgUndelegate(req.DelegatorAddress, req.ValidatorAddress, req.Amount)
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
if rest.CheckBadRequestError(w, err) {
return
}
if !bytes.Equal(fromAddr, req.DelegatorAddress) {
rest.WriteErrorResponse(w, http.StatusUnauthorized, "must use own delegator address")
return
}
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}
// ---------------------------------------------------------------------------
// Deprecated
//
// TODO: Remove once client-side Protobuf migration has been completed.
// ---------------------------------------------------------------------------
func registerTxRoutes(clientCtx client.Context, r *mux.Router) {
r.HandleFunc(
"/staking/delegators/{delegatorAddr}/delegations",
postDelegationsHandlerFn(clientCtx),
).Methods("POST")
r.HandleFunc(
"/staking/delegators/{delegatorAddr}/unbonding_delegations",
postUnbondingDelegationsHandlerFn(clientCtx),
).Methods("POST")
r.HandleFunc(
"/staking/delegators/{delegatorAddr}/redelegations",
postRedelegationsHandlerFn(clientCtx),
).Methods("POST")
}
func postDelegationsHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req DelegateRequest
if !rest.ReadRESTReq(w, r, clientCtx.Codec, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
msg := types.NewMsgDelegate(req.DelegatorAddress, req.ValidatorAddress, req.Amount)
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
if rest.CheckBadRequestError(w, err) {
return
}
if !bytes.Equal(fromAddr, req.DelegatorAddress) {
rest.WriteErrorResponse(w, http.StatusUnauthorized, "must use own delegator address")
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
}
}
func postRedelegationsHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req RedelegateRequest
if !rest.ReadRESTReq(w, r, clientCtx.Codec, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
msg := types.NewMsgBeginRedelegate(req.DelegatorAddress, req.ValidatorSrcAddress, req.ValidatorDstAddress, req.Amount)
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
if rest.CheckBadRequestError(w, err) {
return
}
if !bytes.Equal(fromAddr, req.DelegatorAddress) {
rest.WriteErrorResponse(w, http.StatusUnauthorized, "must use own delegator address")
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
}
}
func postUnbondingDelegationsHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req UndelegateRequest
if !rest.ReadRESTReq(w, r, clientCtx.Codec, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
msg := types.NewMsgUndelegate(req.DelegatorAddress, req.ValidatorAddress, req.Amount)
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
if rest.CheckBadRequestError(w, err) {
return
}
if !bytes.Equal(fromAddr, req.DelegatorAddress) {
rest.WriteErrorResponse(w, http.StatusUnauthorized, "must use own delegator address")
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
}
}