/
full_information.go
159 lines (147 loc) · 4.82 KB
/
full_information.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
package core
import (
"encoding/json"
"fmt"
"strings"
"github.com/pkg/errors"
)
// When a client subscribes something, we get the full information from rocksdb and push them to client.
// After that, we begin to push delta/incremental information to clients
func pushFullInformation(topic string, params []string, count int, c Subscriber, hub *Hub) error {
var (
err error
depthLevel = "all"
)
count = getCount(count)
if len(params) == 2 && topic == DepthKey {
depthLevel = params[1]
}
switch topic {
case SlashKey:
err = querySlashAndPush(hub, c, count)
case KlineKey:
err = queryKlineAndpush(hub, c, params, count)
case DepthKey:
err = queryDepthAndPush(hub, c, params[0], depthLevel, count)
case OrderKey:
err = queryOrderAndPush(hub, c, params[0], count)
case TickerKey:
market := params[0]
if len(params) == 2 {
market = strings.Join(params, SeparateArgu)
}
err = queryTickerAndPush(hub, c, market)
case TxKey:
err = queryAndPushFunc(hub, c, TxKey, params[0], count, hub.QueryTx)
case LockedKey:
err = queryAndPushFunc(hub, c, LockedKey, params[0], count, hub.QueryLocked)
case UnlockKey:
err = queryAndPushFunc(hub, c, UnlockKey, params[0], count, hub.QueryUnlock)
case IncomeKey:
err = queryAndPushFunc(hub, c, IncomeKey, params[0], count, hub.QueryIncome)
case DealKey:
err = queryAndPushFunc(hub, c, DealKey, params[0], count, hub.QueryDeal)
case BancorKey:
err = queryAndPushFunc(hub, c, BancorKey, params[0], count, hub.QueryBancorInfo)
case BancorTradeKey:
err = queryAndPushFunc(hub, c, BancorTradeKey, params[0], count, hub.QueryBancorTrade)
case BancorDealKey:
err = queryAndPushFunc(hub, c, BancorDealKey, "B:"+params[0], count, hub.QueryBancorDeal)
case RedelegationKey:
err = queryAndPushFunc(hub, c, RedelegationKey, params[0], count, hub.QueryRedelegation)
case UnbondingKey:
err = queryAndPushFunc(hub, c, UnbondingKey, params[0], count, hub.QueryUnbonding)
case CommentKey:
err = queryAndPushFunc(hub, c, CommentKey, params[0], count, hub.QueryComment)
}
return err
}
func getCount(count int) int {
count = limitCount(count)
if count == 0 {
count = 10
}
return count
}
func querySlashAndPush(hub *Hub, c Subscriber, count int) error {
data, _ := hub.QuerySlash(hub.currBlockTime.Unix(), hub.sid, count)
bz := groupOfDataPacket(SlashKey, data)
err := c.WriteMsg(bz)
if err != nil {
return err
}
return nil
}
func queryKlineAndpush(hub *Hub, c Subscriber, params []string, count int) error {
tradingPair := params[0]
if len(params) == 3 {
tradingPair = strings.Join(params[:2], SeparateArgu)
}
candleBz := hub.QueryCandleStick(tradingPair, GetSpanFromSpanStr(params[1]), hub.currBlockTime.Unix(), hub.sid, count)
bz := groupOfDataPacket(KlineKey, candleBz)
err := c.WriteMsg(bz)
if err != nil {
return err
}
return nil
}
func queryDepthAndPush(hub *Hub, c Subscriber, market string, level string, count int) error {
bz, err := hub.getDepthFullData(market, level, count)
if err != nil {
return err
}
msg := []byte(fmt.Sprintf("{\"type\":\"%s\", \"payload\":%s}", DepthFull, string(bz)))
if err := c.WriteMsg(msg); err != nil {
return err
}
return hub.AddLevel(market, level)
}
func queryOrderAndPush(hub *Hub, c Subscriber, account string, count int) error {
data, tags, _ := hub.QueryOrder(account, hub.currBlockTime.Unix(), hub.sid, count)
if len(data) != len(tags) {
return errors.Errorf("The number of orders and tags is not equal")
}
createData := make([]json.RawMessage, 0, len(data)/2)
fillData := make([]json.RawMessage, 0, len(data)/2)
cancelData := make([]json.RawMessage, 0, len(data)/2)
for i := len(data) - 1; i >= 0; i-- {
if tags[i] == CreateOrderEndByte {
createData = append(createData, data[i])
} else if tags[i] == FillOrderEndByte {
fillData = append(fillData, data[i])
} else if tags[i] == CancelOrderEndByte {
cancelData = append(cancelData, data[i])
}
}
bz := groupOfDataPacket(CreateOrderKey, createData)
if err := c.WriteMsg(bz); err != nil {
return err
}
bz = groupOfDataPacket(FillOrderKey, fillData)
if err := c.WriteMsg(bz); err != nil {
return err
}
bz = groupOfDataPacket(CancelOrderKey, cancelData)
err := c.WriteMsg(bz)
return err
}
func queryTickerAndPush(hub *Hub, c Subscriber, market string) error {
tickers := hub.QueryTickers([]string{market})
baseData, err := json.Marshal(tickers)
if err != nil {
return err
}
err = c.WriteMsg([]byte(fmt.Sprintf("{\"type\":\"%s\","+
" \"payload\":%s}", TickerKey, string(baseData))))
return err
}
type queryFunc func(string, int64, int64, int) ([]json.RawMessage, []int64)
func queryAndPushFunc(hub *Hub, c Subscriber, typeKey string, param string, count int, qf queryFunc) error {
data, _ := qf(param, hub.currBlockTime.Unix(), hub.sid, count)
bz := groupOfDataPacket(typeKey, data)
err := c.WriteMsg(bz)
if err != nil {
return err
}
return nil
}