forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.go
160 lines (132 loc) · 4.97 KB
/
tx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package rest
import (
"bytes"
"net/http"
"github.com/gorilla/mux"
"github.com/cosmos/cosmos-sdk/client/context"
clientrest "github.com/cosmos/cosmos-sdk/client/rest"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/staking"
)
func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec, kb keys.Keybase) {
r.HandleFunc(
"/staking/delegators/{delegatorAddr}/delegations",
postDelegationsHandlerFn(cdc, kb, cliCtx),
).Methods("POST")
r.HandleFunc(
"/staking/delegators/{delegatorAddr}/unbonding_delegations",
postUnbondingDelegationsHandlerFn(cdc, kb, cliCtx),
).Methods("POST")
r.HandleFunc(
"/staking/delegators/{delegatorAddr}/redelegations",
postRedelegationsHandlerFn(cdc, kb, cliCtx),
).Methods("POST")
}
type (
// MsgBeginRedelegateInput defines the properties of a delegation request's body.
MsgDelegationsInput struct {
BaseReq rest.BaseReq `json:"base_req"`
DelegatorAddress sdk.AccAddress `json:"delegator_address"` // in bech32
ValidatorAddress sdk.ValAddress `json:"validator_address"` // in bech32
Delegation sdk.Coin `json:"delegation"`
}
// MsgBeginRedelegateInput defines the properties of a redelegate request's body.
MsgBeginRedelegateInput struct {
BaseReq rest.BaseReq `json:"base_req"`
DelegatorAddress sdk.AccAddress `json:"delegator_address"` // in bech32
ValidatorSrcAddress sdk.ValAddress `json:"validator_src_address"` // in bech32
ValidatorDstAddress sdk.ValAddress `json:"validator_dst_address"` // in bech32
SharesAmount sdk.Dec `json:"shares"`
}
// MsgUndelegateInput defines the properties of a undelegate request's body.
MsgUndelegateInput struct {
BaseReq rest.BaseReq `json:"base_req"`
DelegatorAddress sdk.AccAddress `json:"delegator_address"` // in bech32
ValidatorAddress sdk.ValAddress `json:"validator_address"` // in bech32
SharesAmount sdk.Dec `json:"shares"`
}
)
func postDelegationsHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req MsgDelegationsInput
if !rest.ReadRESTReq(w, r, cdc, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
msg := staking.NewMsgDelegate(req.DelegatorAddress, req.ValidatorAddress, req.Delegation)
if err := msg.ValidateBasic(); err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
if !bytes.Equal(fromAddr, req.DelegatorAddress) {
rest.WriteErrorResponse(w, http.StatusUnauthorized, "must use own delegator address")
return
}
clientrest.WriteGenerateStdTxResponse(w, cdc, cliCtx, req.BaseReq, []sdk.Msg{msg})
}
}
func postRedelegationsHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req MsgBeginRedelegateInput
if !rest.ReadRESTReq(w, r, cdc, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
msg := staking.NewMsgBeginRedelegate(req.DelegatorAddress, req.ValidatorSrcAddress, req.ValidatorDstAddress, req.SharesAmount)
if err := msg.ValidateBasic(); err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
if !bytes.Equal(fromAddr, req.DelegatorAddress) {
rest.WriteErrorResponse(w, http.StatusUnauthorized, "must use own delegator address")
return
}
clientrest.WriteGenerateStdTxResponse(w, cdc, cliCtx, req.BaseReq, []sdk.Msg{msg})
}
}
func postUnbondingDelegationsHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req MsgUndelegateInput
if !rest.ReadRESTReq(w, r, cdc, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
msg := staking.NewMsgUndelegate(req.DelegatorAddress, req.ValidatorAddress, req.SharesAmount)
if err := msg.ValidateBasic(); err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
if !bytes.Equal(fromAddr, req.DelegatorAddress) {
rest.WriteErrorResponse(w, http.StatusUnauthorized, "must use own delegator address")
return
}
clientrest.WriteGenerateStdTxResponse(w, cdc, cliCtx, req.BaseReq, []sdk.Msg{msg})
}
}