-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.go
149 lines (124 loc) · 4.67 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
package rest
import (
"bytes"
"net/http"
"github.com/gorilla/mux"
"github.com/creatachain/creata-sdk/client"
"github.com/creatachain/creata-sdk/client/tx"
sdk "github.com/creatachain/creata-sdk/types"
"github.com/creatachain/creata-sdk/types/rest"
"github.com/creatachain/creata-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.LegacyAmino, &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.LegacyAmino, &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.LegacyAmino, &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)
}
}