github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/order/client/rest/rest_v2.go (about) 1 package rest 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "net/http" 7 "strconv" 8 9 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context" 10 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 11 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/rest" 12 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth" 13 "github.com/fibonacci-chain/fbc/x/common" 14 "github.com/fibonacci-chain/fbc/x/order/keeper" 15 ordertype "github.com/fibonacci-chain/fbc/x/order/types" 16 "github.com/gorilla/mux" 17 ) 18 19 // nolint 20 func RegisterRoutesV2(cliCtx context.CLIContext, r *mux.Router) { 21 r.HandleFunc("/instruments/{instrument_id}/book", depthBookHandlerV2(cliCtx)).Methods("GET") 22 r.HandleFunc("/order/placeorder", broadcastPlaceOrderRequest(cliCtx)).Methods("POST") 23 r.HandleFunc("/order/cancelorder", broadcastCancelOrderRequest(cliCtx)).Methods("POST") 24 } 25 26 func depthBookHandlerV2(cliCtx context.CLIContext) http.HandlerFunc { 27 return func(w http.ResponseWriter, r *http.Request) { 28 vars := mux.Vars(r) 29 product := vars["instrument_id"] 30 sizeStr := r.URL.Query().Get("size") 31 32 // validate request 33 if product == "" { 34 common.HandleErrorResponseV2(w, http.StatusBadRequest, common.ErrorMissingRequiredParam) 35 return 36 } 37 var size int 38 var err error 39 if sizeStr != "" { 40 size, err = strconv.Atoi(sizeStr) 41 if err != nil { 42 common.HandleErrorResponseV2(w, http.StatusBadRequest, common.ErrorInvalidParam) 43 return 44 } 45 } 46 if size < 0 { 47 common.HandleErrorResponseV2(w, http.StatusBadRequest, common.ErrorInvalidParam) 48 return 49 } 50 51 params := keeper.NewQueryDepthBookParams(product, uint(size)) 52 req, err := cliCtx.Codec.MarshalJSON(params) 53 if err != nil { 54 common.HandleErrorResponseV2(w, http.StatusBadRequest, common.ErrorInvalidParam) 55 return 56 } 57 res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/order/%s", ordertype.QueryDepthBookV2), req) 58 common.HandleResponseV2(w, res, err) 59 } 60 } 61 62 // BroadcastReq defines a tx broadcasting request. 63 type BroadcastReq struct { 64 Tx auth.StdTx `json:"tx"` 65 Mode string `json:"mode"` 66 } 67 68 type placeCancelOrderResponse struct { 69 types.TxResponse 70 OrderID string `json:"order_id"` 71 ClientOid string `json:"client_oid"` 72 Result bool `json:"result"` 73 ErrorCode string `json:"error_code"` 74 ErrorMessage string `json:"error_message"` 75 } 76 77 // BroadcastTxRequest implements a tx broadcasting handler that is responsible 78 // for broadcasting a valid and signed tx to a full node. The tx can be 79 // broadcasted via a sync|async|block mechanism. 80 func broadcastPlaceOrderRequest(cliCtx context.CLIContext) http.HandlerFunc { 81 return func(w http.ResponseWriter, r *http.Request) { 82 var req BroadcastReq 83 84 body, err := ioutil.ReadAll(r.Body) 85 if err != nil { 86 rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) 87 return 88 } 89 90 err = cliCtx.Codec.UnmarshalJSON(body, &req) 91 if err != nil { 92 rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) 93 return 94 } 95 96 txBytes, err := cliCtx.Codec.MarshalBinaryLengthPrefixed(req.Tx) 97 if err != nil { 98 rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) 99 return 100 } 101 102 cliCtx = cliCtx.WithBroadcastMode(req.Mode) 103 104 res, err := cliCtx.BroadcastTx(txBytes) 105 if err != nil { 106 rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) 107 return 108 } 109 //TODO: OrderID needs to be obtained when the new version of the interface is developed 110 orderID := "" 111 112 res2 := placeCancelOrderResponse{ 113 res, 114 orderID, 115 "", 116 true, 117 "", 118 "", 119 } 120 if res.Code != 0 { 121 res2.Result = false 122 res2.ErrorCode = strconv.Itoa(int(res.Code)) 123 res2.ErrorMessage = res.Logs[0].Log 124 125 } 126 127 rest.PostProcessResponse(w, cliCtx, res2) 128 } 129 } 130 131 func broadcastCancelOrderRequest(cliCtx context.CLIContext) http.HandlerFunc { 132 return func(w http.ResponseWriter, r *http.Request) { 133 var req BroadcastReq 134 135 body, err := ioutil.ReadAll(r.Body) 136 if err != nil { 137 rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) 138 return 139 } 140 141 err = cliCtx.Codec.UnmarshalJSON(body, &req) 142 if err != nil { 143 rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) 144 return 145 } 146 147 txBytes, err := cliCtx.Codec.MarshalBinaryLengthPrefixed(req.Tx) 148 if err != nil { 149 rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) 150 return 151 } 152 153 cliCtx = cliCtx.WithBroadcastMode(req.Mode) 154 155 res, err := cliCtx.BroadcastTx(txBytes) 156 if err != nil { 157 rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) 158 return 159 } 160 161 res2 := placeCancelOrderResponse{ 162 res, 163 req.Tx.Msgs[0].(*ordertype.MsgCancelOrders).OrderIDs[0], 164 "", 165 true, 166 "", 167 "", 168 } 169 if res.Code != 0 { 170 res2.Result = false 171 res2.ErrorCode = strconv.Itoa(int(res.Code)) 172 res2.ErrorMessage = res.Logs[0].Log 173 174 } 175 176 rest.PostProcessResponse(w, cliCtx, res2) 177 } 178 }