-
Notifications
You must be signed in to change notification settings - Fork 7
/
query.go
83 lines (74 loc) · 3.13 KB
/
query.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
package rest
import (
"fmt"
"net/http"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/gorilla/mux"
"github.com/coinexchain/cet-sdk/modules/market/internal/keepers"
"github.com/coinexchain/cet-sdk/modules/market/internal/types"
dex "github.com/coinexchain/cet-sdk/types"
"github.com/coinexchain/cosmos-utils/client/restutil"
)
func queryMarketHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
query := fmt.Sprintf("custom/%s/%s", types.StoreKey, keepers.QueryMarket)
if !types.IsValidTradingPair([]string{vars["stock"], vars["money"]}) {
rest.WriteErrorResponse(w, http.StatusBadRequest, "Invalid Trading pair")
return
}
param := keepers.NewQueryMarketParam(dex.GetSymbol(vars["stock"], vars["money"]))
restutil.RestQuery(cdc, cliCtx, w, r, query, param, nil)
}
}
func queryOrdersInMarketHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
query := fmt.Sprintf("custom/%s/%s", types.StoreKey, keepers.QueryOrdersInMarket)
if !types.IsValidTradingPair([]string{vars["stock"], vars["money"]}) {
rest.WriteErrorResponse(w, http.StatusBadRequest, "Invalid Trading pair")
return
}
param := keepers.NewQueryMarketParam(dex.GetSymbol(vars["stock"], vars["money"]))
restutil.RestQuery(cdc, cliCtx, w, r, query, param, nil)
}
}
func queryMarketsHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
query := fmt.Sprintf("custom/%s/%s", types.StoreKey, keepers.QueryMarkets)
restutil.RestQuery(cdc, cliCtx, w, r, query, nil, nil)
}
}
func queryOrderInfoHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
if err := types.ValidateOrderID(vars["order-id"]); err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, "Invalid Order ID")
return
}
param := keepers.NewQueryOrderParam(vars["order-id"])
route := fmt.Sprintf("custom/%s/%s", types.StoreKey, keepers.QueryOrder)
restutil.RestQuery(cdc, cliCtx, w, r, route, param, nil)
}
}
func queryUserOrderListHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
if _, err := sdk.AccAddressFromBech32(vars["address"]); err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
param := keepers.QueryUserOrderList{User: vars["address"]}
route := fmt.Sprintf("custom/%s/%s", types.StoreKey, keepers.QueryUserOrders)
restutil.RestQuery(cdc, cliCtx, w, r, route, param, nil)
}
}
func queryParamsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
route := fmt.Sprintf("custom/%s/%s", types.StoreKey, keepers.QueryParameters)
restutil.RestQuery(nil, cliCtx, w, r, route, nil, nil)
}
}