cosmos-sdk/x/staking/client/rest/tx.go

159 lines
4.9 KiB
Go
Raw Normal View History

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"
"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"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
"github.com/cosmos/cosmos-sdk/x/staking/types"
2018-05-14 16:43:51 -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",
postDelegationsHandlerFn(cliCtx),
).Methods("POST")
r.HandleFunc(
2019-01-11 12:08:01 -08:00
"/staking/delegators/{delegatorAddr}/unbonding_delegations",
postUnbondingDelegationsHandlerFn(cliCtx),
).Methods("POST")
r.HandleFunc(
2019-01-11 12:08:01 -08:00
"/staking/delegators/{delegatorAddr}/redelegations",
postRedelegationsHandlerFn(cliCtx),
2018-05-24 08:41:17 -07:00
).Methods("POST")
2018-05-14 16:43:51 -07:00
}
type (
// 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"`
}
// 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"`
}
// 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-05-14 16:43:51 -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) {
var req DelegateRequest
2018-08-06 11:11:30 -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
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
msg := types.NewMsgDelegate(req.DelegatorAddress, req.ValidatorAddress, req.Amount)
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
}
fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
Merge PR #1119: Unbonding, Redelegation * stake/fees spec updates * staking overview.md revisions, moving files * docs reorganization * staking spec state revisions * transaction stake updates * complete staking spec update * WIP adding unbonding/redelegation commands * added msg types for unbonding, redelegation * stake sub-package reorg * working stake reorg * modify lcd tests to not use hardcoded json strings * add description update * index keys * key managment for unbonding redelegation complete * update stake errors * completed handleMsgCompleteUnbonding fn * updated to use begin/complete unbonding/redelegation * fix token shares bug * develop docs into unbonding * got non-tests compiling after merge develop * working fixing tests * PrivlegedKeeper -> PrivilegedKeeper * tests compile * fix some tests * fixing tests * remove PrivilegedKeeper * get unbonding bug * only rpc sig verification failed tests now * move percent unbonding/redelegation to the CLI and out of handler logic * remove min unbonding height * add lcd txs * add pool sanity checks, fix a buncha tests * fix ante. set lcd log to debug (#1322) * redelegation tests, adding query functionality for bonds * add self-delegations at genesis ref #1165 * PR comments (mostly) addressed * cleanup, added Query LCD functionality * test cleanup/fixes * fix governance test * SlashValidatorSet -> ValidatorSet * changelog * stake lcd fix * x/auth: fix chainID in ante * fix lcd test * fix lint, update lint make command for spelling * lowercase error string * don't expose coinkeeper in staking * remove a few duplicate lines in changelog * chain_id in stake lcd tests * added transient redelegation * 'transient' => 'transitive' * Re-add nolint instruction * Fix tiny linter error
2018-06-26 19:00:12 -07: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")
return
}
utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg})
}
}
2018-08-06 11:11:30 -07:00
func postRedelegationsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req RedelegateRequest
2018-08-06 11:11:30 -07:00
if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) {
return
}
2018-08-06 11:11:30 -07:00
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
Merge PR #1119: Unbonding, Redelegation * stake/fees spec updates * staking overview.md revisions, moving files * docs reorganization * staking spec state revisions * transaction stake updates * complete staking spec update * WIP adding unbonding/redelegation commands * added msg types for unbonding, redelegation * stake sub-package reorg * working stake reorg * modify lcd tests to not use hardcoded json strings * add description update * index keys * key managment for unbonding redelegation complete * update stake errors * completed handleMsgCompleteUnbonding fn * updated to use begin/complete unbonding/redelegation * fix token shares bug * develop docs into unbonding * got non-tests compiling after merge develop * working fixing tests * PrivlegedKeeper -> PrivilegedKeeper * tests compile * fix some tests * fixing tests * remove PrivilegedKeeper * get unbonding bug * only rpc sig verification failed tests now * move percent unbonding/redelegation to the CLI and out of handler logic * remove min unbonding height * add lcd txs * add pool sanity checks, fix a buncha tests * fix ante. set lcd log to debug (#1322) * redelegation tests, adding query functionality for bonds * add self-delegations at genesis ref #1165 * PR comments (mostly) addressed * cleanup, added Query LCD functionality * test cleanup/fixes * fix governance test * SlashValidatorSet -> ValidatorSet * changelog * stake lcd fix * x/auth: fix chainID in ante * fix lcd test * fix lint, update lint make command for spelling * lowercase error string * don't expose coinkeeper in staking * remove a few duplicate lines in changelog * chain_id in stake lcd tests * added transient redelegation * 'transient' => 'transitive' * Re-add nolint instruction * Fix tiny linter error
2018-06-26 19:00:12 -07:00
}
msg := types.NewMsgBeginRedelegate(req.DelegatorAddress, req.ValidatorSrcAddress, req.ValidatorDstAddress, req.Amount)
if err := msg.ValidateBasic(); err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
2018-08-06 11:11:30 -07: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")
return
}
utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg})
}
}
func postUnbondingDelegationsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req UndelegateRequest
if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
2018-05-14 16:43:51 -07:00
msg := types.NewMsgUndelegate(req.DelegatorAddress, req.ValidatorAddress, req.Amount)
if err := msg.ValidateBasic(); err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
2018-05-14 16:43:51 -07:00
}
fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
2018-05-14 16:43:51 -07: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")
return
}
utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg})
2018-05-14 16:43:51 -07:00
}
}