-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.go
231 lines (193 loc) · 8.31 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package rest
import (
"net/http"
"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"
"github.com/gorilla/mux"
didexported "github.com/furya-official/fury-blockchain/lib/legacydid"
iidtypes "github.com/furya-official/fury-blockchain/x/iid/types"
"github.com/furya-official/fury-blockchain/x/payments/types"
)
func registerTxHandlers(cliCtx client.Context, r *mux.Router) {
r.HandleFunc("/payments/create_payment_template", newCreatePaymentTemplateRequestHandlerFn(cliCtx)).Methods("POST")
r.HandleFunc("/payments/create_payment_contract", newCreatePaymentContractRequestHandlerFn(cliCtx)).Methods("POST")
r.HandleFunc("/payments/create_subscription", newCreateSubscriptionRequestHandlerFn(cliCtx)).Methods("POST")
r.HandleFunc("/payments/set_payment_contract_authorisation", newSetPaymentContractAuthorisationRequestHandlerFn(cliCtx)).Methods("POST")
r.HandleFunc("/payments/grant_discount", newGrantDiscountRequestHandlerFn(cliCtx)).Methods("POST")
r.HandleFunc("/payments/revoke_discount", newRevokeDiscountRequestHandlerFn(cliCtx)).Methods("POST")
r.HandleFunc("/payments/effect_payment", newEffectPaymentRequestHandlerFn(cliCtx)).Methods("POST")
}
type createPaymentTemplateReq struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
CreatorDid didexported.Did `json:"creator_did" yaml:"creator_did"`
PaymentTemplate types.PaymentTemplate `json:"payment_template" yaml:"payment_template"`
}
func newCreatePaymentTemplateRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req createPaymentTemplateReq
if !rest.ReadRESTReq(w, r, clientCtx.LegacyAmino, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
msg := types.NewMsgCreatePaymentTemplate(req.PaymentTemplate, iidtypes.DIDFragment(req.CreatorDid), "")
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}
type createPaymentContractReq struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
CreatorDid didexported.Did `json:"creator_did" yaml:"creator_did"`
PaymentTemplateId string `json:"payment_template_id" yaml:"payment_template_id"`
PaymentContractId string `json:"payment_contract_id" yaml:"payment_contract_id"`
Payer sdk.AccAddress `json:"payer" yaml:"payer"`
Recipients types.Distribution `json:"recipients" yaml:"recipients"`
CanDeauthorise bool `json:"can_deauthorise" yaml:"can_deauthorise"`
DiscountId sdk.Uint `json:"discount_id" yaml:"discount_id"`
}
func newCreatePaymentContractRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req createPaymentContractReq
if !rest.ReadRESTReq(w, r, clientCtx.LegacyAmino, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
msg := types.NewMsgCreatePaymentContract(req.PaymentTemplateId,
req.PaymentContractId, req.Payer, req.Recipients,
req.CanDeauthorise, req.DiscountId, iidtypes.DIDFragment(req.CreatorDid), "")
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}
type createSubscriptionReq struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
CreatorDid didexported.Did `json:"creator_did" yaml:"creator_did"`
SubscriptionId string `json:"subscription_id" yaml:"subscription_id"`
PaymentContractId string `json:"payment_contract_id" yaml:"payment_contract_id"`
MaxPeriods sdk.Uint `json:"max_periods" yaml:"max_periods"`
Period types.Period `json:"period" yaml:"period"`
}
func newCreateSubscriptionRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req createSubscriptionReq
if !rest.ReadRESTReq(w, r, clientCtx.LegacyAmino, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
msg := types.NewMsgCreateSubscription(req.SubscriptionId, req.PaymentContractId,
req.MaxPeriods, req.Period, iidtypes.DIDFragment(req.CreatorDid), "")
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}
type setPaymentContractAuthorisationReq struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
PayerDid didexported.Did `json:"payer_did" yaml:"payer_did"`
PaymentContractId string `json:"payment_contract_id" yaml:"payment_contract_id"`
Authorised bool `json:"authorised" yaml:"authorised"`
}
func newSetPaymentContractAuthorisationRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req setPaymentContractAuthorisationReq
if !rest.ReadRESTReq(w, r, clientCtx.LegacyAmino, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
msg := types.NewMsgSetPaymentContractAuthorisation(req.PaymentContractId,
req.Authorised, iidtypes.DIDFragment(req.PayerDid), "")
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}
type grantDiscountReq struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
SenderDid didexported.Did `json:"sender_did" yaml:"sender_did"`
PaymentContractId string `json:"payment_contract_id" yaml:"payment_contract_id"`
DiscountId sdk.Uint `json:"discount_id" yaml:"discount_id"`
Recipient sdk.AccAddress `json:"recipient" yaml:"recipient"`
}
func newGrantDiscountRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req grantDiscountReq
if !rest.ReadRESTReq(w, r, clientCtx.LegacyAmino, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
msg := types.NewMsgGrantDiscount(req.PaymentContractId, req.DiscountId,
req.Recipient, iidtypes.DIDFragment(req.SenderDid), "")
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}
type revokeDiscountReq struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
SenderDid didexported.Did `json:"sender_did" yaml:"sender_did"`
PaymentContractId string `json:"payment_contract_id" yaml:"payment_contract_id"`
Holder sdk.AccAddress `json:"holder" yaml:"holder"`
}
func newRevokeDiscountRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req revokeDiscountReq
if !rest.ReadRESTReq(w, r, clientCtx.LegacyAmino, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
msg := types.NewMsgRevokeDiscount(req.PaymentContractId, req.Holder, iidtypes.DIDFragment(req.SenderDid), "")
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
//utils.WriteGenerateStdTxResponse(w, ctx, req.BaseReq, []sdk.Msg{msg})
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}
type effectPaymentReq struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
SenderDid didexported.Did `json:"sender_did" yaml:"sender_did"`
PaymentContractId string `json:"payment_contract_id" yaml:"payment_contract_id"`
}
func newEffectPaymentRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req effectPaymentReq
if !rest.ReadRESTReq(w, r, clientCtx.LegacyAmino, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
msg := types.NewMsgEffectPayment(req.PaymentContractId, iidtypes.DIDFragment(req.SenderDid), "")
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}