-
Notifications
You must be signed in to change notification settings - Fork 286
/
tx.go
60 lines (49 loc) · 1.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
package rest
import (
"net/http"
"github.com/terra-project/core/x/market/internal/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/gorilla/mux"
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
)
func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router) {
r.HandleFunc("/market/swap", submitSwapHandlerFn(cliCtx)).Methods("POST")
}
// SwapReq defines request body for swap operation
type SwapReq struct {
BaseReq rest.BaseReq `json:"base_req"`
OfferCoin sdk.Coin `json:"offer_coin"`
AskDenom string `json:"ask_denom"`
}
// submitSwapHandlerFn handles a POST vote request
func submitSwapHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req SwapReq
if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) {
err := sdk.ErrUnknownRequest("malformed request")
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
err := sdk.ErrUnknownRequest("malformed request")
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
fromAddress, err := sdk.AccAddressFromBech32(req.BaseReq.From)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
// create the message
msg := types.NewMsgSwap(fromAddress, req.OfferCoin, req.AskDenom)
err = msg.ValidateBasic()
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg})
}
}