forked from ccxt/go-binance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_ws_partial_book_depth.go
57 lines (47 loc) · 1.25 KB
/
json_ws_partial_book_depth.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
package binance
import (
"github.com/buger/jsonparser"
"github.com/shopspring/decimal"
)
var pathInWsPartialBookDepth = [][]string{
{"lastUpdateId"},
{"bids"},
{"asks"},
}
func parseWsPartialBookDepthEvent(data []byte, levels int) *WsPartialBookDepthEvent {
bookDepth := &WsPartialBookDepthEvent{}
jsonparser.EachKey(data, func(idx int, value []byte, vt jsonparser.ValueType, err error){
switch idx {
case 0: // lastUpdateId
bookDepth.LastUpdateId, _ = jsonparser.GetInt(value)
case 1: // bids
bookDepth.Bids = parseBidsAsks(value, levels)
case 2: // asks
bookDepth.Asks = parseBidsAsks(value, levels)
}
}, pathInWsPartialBookDepth...)
return bookDepth
}
func parseBidsAsks(data []byte, levels int) [][2]decimal.Decimal {
asksBids := make([][2]decimal.Decimal, levels, levels)
i := 0
var arr [2]decimal.Decimal
var b int
jsonparser.ArrayEach(data, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
if i > levels - 1 {
return
}
arr = [2]decimal.Decimal{}
b = 0
jsonparser.ArrayEach(value, func(d []byte, dataType jsonparser.ValueType, offset int, err error) {
if b > 1 {
return
}
arr[b], _ = decimal.NewFromString(string(d))
b++
});
asksBids[i] = arr
i++
});
return asksBids
}