-
Notifications
You must be signed in to change notification settings - Fork 179
/
rest_v2.go
179 lines (152 loc) · 4.65 KB
/
rest_v2.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
package rest
import (
"fmt"
"io/ioutil"
"net/http"
"strconv"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/gorilla/mux"
"github.com/okex/okexchain/x/common"
"github.com/okex/okexchain/x/order/keeper"
ordertype "github.com/okex/okexchain/x/order/types"
)
// nolint
func RegisterRoutesV2(cliCtx context.CLIContext, r *mux.Router) {
r.HandleFunc("/instruments/{instrument_id}/book", depthBookHandlerV2(cliCtx)).Methods("GET")
r.HandleFunc("/order/placeorder", broadcastPlaceOrderRequest(cliCtx)).Methods("POST")
r.HandleFunc("/order/cancelorder", broadcastCancelOrderRequest(cliCtx)).Methods("POST")
}
func depthBookHandlerV2(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
product := vars["instrument_id"]
sizeStr := r.URL.Query().Get("size")
// validate request
if product == "" {
common.HandleErrorResponseV2(w, http.StatusBadRequest, common.ErrorMissingRequiredParam)
return
}
var size int
var err error
if sizeStr != "" {
size, err = strconv.Atoi(sizeStr)
if err != nil {
common.HandleErrorResponseV2(w, http.StatusBadRequest, common.ErrorInvalidParam)
return
}
}
if size < 0 {
common.HandleErrorResponseV2(w, http.StatusBadRequest, common.ErrorInvalidParam)
return
}
params := keeper.NewQueryDepthBookParams(product, uint(size))
req, err := cliCtx.Codec.MarshalJSON(params)
if err != nil {
common.HandleErrorResponseV2(w, http.StatusBadRequest, common.ErrorInvalidParam)
return
}
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/order/%s", ordertype.QueryDepthBookV2), req)
common.HandleResponseV2(w, res, err)
}
}
// BroadcastReq defines a tx broadcasting request.
type BroadcastReq struct {
Tx auth.StdTx `json:"tx"`
Mode string `json:"mode"`
}
type placeCancelOrderResponse struct {
types.TxResponse
OrderID string `json:"order_id"`
ClientOid string `json:"client_oid"`
Result bool `json:"result"`
ErrorCode string `json:"error_code"`
ErrorMessage string `json:"error_message"`
}
// BroadcastTxRequest implements a tx broadcasting handler that is responsible
// for broadcasting a valid and signed tx to a full node. The tx can be
// broadcasted via a sync|async|block mechanism.
func broadcastPlaceOrderRequest(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req BroadcastReq
body, err := ioutil.ReadAll(r.Body)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
err = cliCtx.Codec.UnmarshalJSON(body, &req)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
txBytes, err := cliCtx.Codec.MarshalBinaryLengthPrefixed(req.Tx)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
cliCtx = cliCtx.WithBroadcastMode(req.Mode)
res, err := cliCtx.BroadcastTx(txBytes)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
//TODO: OrderID needs to be obtained when the new version of the interface is developed
orderID := ""
res2 := placeCancelOrderResponse{
res,
orderID,
"",
true,
"",
"",
}
if res.Code != 0 {
res2.Result = false
res2.ErrorCode = strconv.Itoa(int(res.Code))
res2.ErrorMessage = res.Logs[0].Log
}
rest.PostProcessResponse(w, cliCtx, res2)
}
}
func broadcastCancelOrderRequest(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req BroadcastReq
body, err := ioutil.ReadAll(r.Body)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
err = cliCtx.Codec.UnmarshalJSON(body, &req)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
txBytes, err := cliCtx.Codec.MarshalBinaryLengthPrefixed(req.Tx)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
cliCtx = cliCtx.WithBroadcastMode(req.Mode)
res, err := cliCtx.BroadcastTx(txBytes)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
res2 := placeCancelOrderResponse{
res,
req.Tx.Msgs[0].(*ordertype.MsgCancelOrders).OrderIDs[0],
"",
true,
"",
"",
}
if res.Code != 0 {
res2.Result = false
res2.ErrorCode = strconv.Itoa(int(res.Code))
res2.ErrorMessage = res.Logs[0].Log
}
rest.PostProcessResponse(w, cliCtx, res2)
}
}