-
Notifications
You must be signed in to change notification settings - Fork 1
/
tx.go
executable file
·99 lines (79 loc) · 2.45 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
package rest
import (
"fmt"
"net/http"
"github.com/Timwood0x10/sei-chain/x/oracle/types"
"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"
)
func registerTxHandlers(cliCtx client.Context, rtr *mux.Router) {
rtr.HandleFunc(fmt.Sprintf("/oracle/voters/{%s}/feeder", RestVoter), newDelegateHandlerFunction(cliCtx)).Methods("POST")
rtr.HandleFunc(fmt.Sprintf("/oracle/voters/{%s}/aggregate_vote", RestVoter), newAggregateVoteHandlerFunction(cliCtx)).Methods("POST")
}
type (
delegateReq struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
Feeder sdk.AccAddress `json:"feeder" yaml:"feeder"`
}
aggregateVoteReq struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
ExchangeRates string `json:"exchange_rates" yaml:"exchange_rates"`
}
)
func newDelegateHandlerFunction(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req delegateReq
if !rest.ReadRESTReq(w, r, clientCtx.LegacyAmino, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
voterAddr, ok := checkVoterAddressVar(w, r)
if !ok {
return
}
// create the message
msg := types.NewMsgDelegateFeedConsent(voterAddr, req.Feeder)
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}
func newAggregateVoteHandlerFunction(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req aggregateVoteReq
if !rest.ReadRESTReq(w, r, clientCtx.LegacyAmino, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
feederAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
if rest.CheckBadRequestError(w, err) {
return
}
voterAddr, ok := checkVoterAddressVar(w, r)
if !ok {
return
}
// Check validation of tuples
_, err = types.ParseExchangeRateTuples(req.ExchangeRates)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
// create the message
msg := types.NewMsgAggregateExchangeRateVote(req.ExchangeRates, feederAddr, voterAddr)
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}