-
Notifications
You must be signed in to change notification settings - Fork 40
/
getdepth.go
84 lines (68 loc) · 1.91 KB
/
getdepth.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
package rest
import (
"errors"
"net/http"
"strconv"
"github.com/cosmos/cosmos-sdk/client/context"
rutils "github.com/bnb-chain/node/plugins/dex/client/rest/utils"
"github.com/bnb-chain/node/plugins/dex/store"
"github.com/bnb-chain/node/wire"
)
var allowedLimits = [7]int{5, 10, 20, 50, 100, 500, 1000}
// DepthReqHandler creates an http request handler to show market depth data
func DepthReqHandler(cdc *wire.Codec, ctx context.CLIContext) http.HandlerFunc {
type params struct {
symbol string
limit int
}
responseType := "application/json"
throw := func(w http.ResponseWriter, status int, err error) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(status)
_, _ = w.Write([]byte(err.Error()))
}
return func(w http.ResponseWriter, r *http.Request) {
limitStr := r.FormValue("limit")
limit, err := strconv.Atoi(limitStr)
if err != nil {
throw(w, http.StatusExpectationFailed, errors.New("invalid limit, supported limits: [5,10,20,50,100,500,1000]"))
return
}
// validate limit param
limitOk := -1
for _, lmt := range allowedLimits {
if lmt == limit {
limitOk = lmt
break
}
}
if limitOk == -1 {
throw(w, http.StatusExpectationFailed, errors.New("invalid limit, supported limits: [5,10,20,50,100,500,1000]"))
return
}
// collect params
params := params{
symbol: r.FormValue("symbol"),
limit: limit,
}
// validate pair
err = store.ValidatePairSymbol(params.symbol)
if err != nil {
throw(w, http.StatusNotFound, err)
return
}
// query order book (includes block height)
ob, err := store.GetOrderBook(cdc, ctx, params.symbol, params.limit)
if err != nil {
throw(w, http.StatusInternalServerError, err)
return
}
w.Header().Set("Content-Type", responseType)
w.WriteHeader(http.StatusOK)
err = rutils.StreamDepthResponse(w, ob, limit)
if err != nil {
throw(w, http.StatusInternalServerError, err)
return
}
}
}