2018-05-14 16:43:51 -07:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
2018-05-24 08:41:17 -07:00
|
|
|
"bytes"
|
2018-05-14 16:43:51 -07:00
|
|
|
"net/http"
|
|
|
|
|
2019-02-14 08:53:36 -08:00
|
|
|
"github.com/gorilla/mux"
|
2019-02-04 07:48:26 -08:00
|
|
|
|
2018-05-14 16:43:51 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-02-14 08:53:36 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
2019-06-15 05:34:11 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
|
2019-06-05 16:26:17 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
2018-05-14 16:43:51 -07:00
|
|
|
)
|
|
|
|
|
2019-06-06 08:43:15 -07:00
|
|
|
func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router) {
|
2018-05-24 08:41:17 -07:00
|
|
|
r.HandleFunc(
|
2019-01-11 12:08:01 -08:00
|
|
|
"/staking/delegators/{delegatorAddr}/delegations",
|
2019-06-06 08:43:15 -07:00
|
|
|
postDelegationsHandlerFn(cliCtx),
|
2018-12-11 06:02:26 -08:00
|
|
|
).Methods("POST")
|
|
|
|
r.HandleFunc(
|
2019-01-11 12:08:01 -08:00
|
|
|
"/staking/delegators/{delegatorAddr}/unbonding_delegations",
|
2019-06-06 08:43:15 -07:00
|
|
|
postUnbondingDelegationsHandlerFn(cliCtx),
|
2018-12-11 06:02:26 -08:00
|
|
|
).Methods("POST")
|
|
|
|
r.HandleFunc(
|
2019-01-11 12:08:01 -08:00
|
|
|
"/staking/delegators/{delegatorAddr}/redelegations",
|
2019-06-06 08:43:15 -07:00
|
|
|
postRedelegationsHandlerFn(cliCtx),
|
2018-05-24 08:41:17 -07:00
|
|
|
).Methods("POST")
|
2018-05-14 16:43:51 -07:00
|
|
|
}
|
|
|
|
|
2018-09-26 06:29:39 -07:00
|
|
|
type (
|
2019-03-25 14:13:02 -07:00
|
|
|
// DelegateRequest defines the properties of a delegation request's body.
|
|
|
|
DelegateRequest struct {
|
2019-07-05 16:25:56 -07:00
|
|
|
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"`
|
2018-09-26 06:29:39 -07:00
|
|
|
}
|
2018-06-05 21:53:04 -07:00
|
|
|
|
2019-03-25 14:13:02 -07:00
|
|
|
// RedelegateRequest defines the properties of a redelegate request's body.
|
|
|
|
RedelegateRequest struct {
|
2019-07-05 16:25:56 -07:00
|
|
|
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"`
|
2018-09-26 06:29:39 -07:00
|
|
|
}
|
|
|
|
|
2019-03-25 14:13:02 -07:00
|
|
|
// UndelegateRequest defines the properties of a undelegate request's body.
|
|
|
|
UndelegateRequest struct {
|
2019-07-05 16:25:56 -07:00
|
|
|
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"`
|
2018-09-26 06:29:39 -07:00
|
|
|
}
|
|
|
|
)
|
2018-05-14 16:43:51 -07:00
|
|
|
|
2019-06-06 08:43:15 -07:00
|
|
|
func postDelegationsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
2018-05-14 16:43:51 -07:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2019-03-25 14:13:02 -07:00
|
|
|
var req DelegateRequest
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2019-06-06 08:43:15 -07:00
|
|
|
if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) {
|
2018-05-14 16:43:51 -07:00
|
|
|
return
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2018-12-19 16:26:33 -08:00
|
|
|
req.BaseReq = req.BaseReq.Sanitize()
|
|
|
|
if !req.BaseReq.ValidateBasic(w) {
|
2018-09-26 06:29:39 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
msg := types.NewMsgDelegate(req.DelegatorAddress, req.ValidatorAddress, req.Amount)
|
2019-02-04 16:45:18 -08:00
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
2019-02-04 07:48:26 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
2018-05-14 16:43:51 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-19 10:15:39 -08:00
|
|
|
fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
|
2018-12-11 06:02:26 -08:00
|
|
|
if err != nil {
|
2019-02-04 07:48:26 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
2018-12-11 06:02:26 -08:00
|
|
|
return
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2019-02-25 07:16:52 -08:00
|
|
|
if !bytes.Equal(fromAddr, req.DelegatorAddress) {
|
2019-02-04 07:48:26 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusUnauthorized, "must use own delegator address")
|
2019-01-29 11:22:47 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-15 05:34:11 -07:00
|
|
|
utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg})
|
2018-12-11 06:02:26 -08:00
|
|
|
}
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2019-06-06 08:43:15 -07:00
|
|
|
func postRedelegationsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
2018-12-11 06:02:26 -08:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2019-03-25 14:13:02 -07:00
|
|
|
var req RedelegateRequest
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2019-06-06 08:43:15 -07:00
|
|
|
if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) {
|
2018-12-11 06:02:26 -08:00
|
|
|
return
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2018-12-19 16:26:33 -08:00
|
|
|
req.BaseReq = req.BaseReq.Sanitize()
|
|
|
|
if !req.BaseReq.ValidateBasic(w) {
|
2018-12-11 06:02:26 -08:00
|
|
|
return
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
msg := types.NewMsgBeginRedelegate(req.DelegatorAddress, req.ValidatorSrcAddress, req.ValidatorDstAddress, req.Amount)
|
2019-02-04 16:45:18 -08:00
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
2019-02-04 07:48:26 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
2018-09-11 17:31:30 -07:00
|
|
|
return
|
|
|
|
}
|
2018-09-26 06:29:39 -07:00
|
|
|
|
2019-02-19 10:15:39 -08:00
|
|
|
fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
|
2018-12-11 06:02:26 -08:00
|
|
|
if err != nil {
|
2019-02-04 07:48:26 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
2018-12-11 06:02:26 -08:00
|
|
|
return
|
2018-08-06 11:11:30 -07:00
|
|
|
}
|
2018-06-11 18:12:37 -07:00
|
|
|
|
2019-02-25 07:16:52 -08:00
|
|
|
if !bytes.Equal(fromAddr, req.DelegatorAddress) {
|
2019-02-04 07:48:26 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusUnauthorized, "must use own delegator address")
|
2019-01-29 11:22:47 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-15 05:34:11 -07:00
|
|
|
utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg})
|
2018-12-11 06:02:26 -08:00
|
|
|
}
|
|
|
|
}
|
2018-09-26 06:29:39 -07:00
|
|
|
|
2019-06-06 08:43:15 -07:00
|
|
|
func postUnbondingDelegationsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
2018-12-11 06:02:26 -08:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2019-03-25 14:13:02 -07:00
|
|
|
var req UndelegateRequest
|
2018-09-26 06:29:39 -07:00
|
|
|
|
2019-06-06 08:43:15 -07:00
|
|
|
if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) {
|
2018-12-11 06:02:26 -08:00
|
|
|
return
|
|
|
|
}
|
2018-08-22 04:38:55 -07:00
|
|
|
|
2018-12-19 16:26:33 -08:00
|
|
|
req.BaseReq = req.BaseReq.Sanitize()
|
|
|
|
if !req.BaseReq.ValidateBasic(w) {
|
2018-12-11 06:02:26 -08:00
|
|
|
return
|
|
|
|
}
|
2018-05-14 16:43:51 -07:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
msg := types.NewMsgUndelegate(req.DelegatorAddress, req.ValidatorAddress, req.Amount)
|
2019-02-04 16:45:18 -08:00
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
2019-02-04 07:48:26 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
2018-12-11 06:02:26 -08:00
|
|
|
return
|
2018-05-14 16:43:51 -07:00
|
|
|
}
|
|
|
|
|
2019-02-19 10:15:39 -08:00
|
|
|
fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
|
2018-12-11 06:02:26 -08:00
|
|
|
if err != nil {
|
2019-02-04 07:48:26 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
2018-12-11 06:02:26 -08:00
|
|
|
return
|
2018-05-14 16:43:51 -07:00
|
|
|
}
|
|
|
|
|
2019-02-25 07:16:52 -08:00
|
|
|
if !bytes.Equal(fromAddr, req.DelegatorAddress) {
|
2019-02-04 07:48:26 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusUnauthorized, "must use own delegator address")
|
2019-01-29 11:22:47 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-15 05:34:11 -07:00
|
|
|
utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg})
|
2018-05-14 16:43:51 -07:00
|
|
|
}
|
|
|
|
}
|