-
Notifications
You must be signed in to change notification settings - Fork 8
/
websocket_types.go
82 lines (69 loc) · 1.96 KB
/
websocket_types.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
package bitstamp
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
)
const (
eventTrade = "trade"
eventSubscription = "bts:subscription_succeede"
)
type bitstampFill struct {
Channel string `json:"channel"`
Data struct {
ID int64 `json:"id"`
OrderID int64 `json:"buy_order_id"`
ClientOrderID int64 `json:"client_order_id"`
Amount string `json:"amount"`
Price string `json:"price"`
Fee string `json:"fee"`
Side string `json:"Side"`
Timestamp int64 `json:"microtimestamp,string"`
} `json:"data"`
Event string `json:"event"`
}
func convertMessage(data []byte) (Fill, error) {
var fill bitstampFill
if err := json.Unmarshal(data, &fill); err != nil {
return Fill{}, err
}
if fill.Event != eventTrade && fill.Event != eventSubscription {
return Fill{}, fmt.Errorf("incompatible event-type:%+v", fill)
}
symbol := strings.Replace(fill.Channel, "private-my_trades_", "", 1)
createdAt := time.Unix(fill.Data.Timestamp/1000000, fill.Data.Timestamp%1000*1000000)
if fill.Data.Side != string(Buy) && fill.Data.Side != string(Sell) {
return Fill{}, fmt.Errorf("not valid side: %s", fill.Data.Side)
}
amount, err := strconv.ParseFloat(fill.Data.Amount, 64)
if err != nil {
return Fill{}, fmt.Errorf("amount convertation error: %w", err)
}
price, err := strconv.ParseFloat(fill.Data.Price, 64)
if err != nil {
return Fill{}, fmt.Errorf("price convertation error: %w", err)
}
fee, err := strconv.ParseFloat(fill.Data.Fee, 64)
if err != nil {
return Fill{}, fmt.Errorf("fee convertation error: %w", err)
}
return Fill{
TradeID: fill.Data.ID,
OrderID: fill.Data.OrderID,
Symbol: symbol,
Price: price,
Size: amount,
Fee: fee,
Side: fill.Data.Side,
FilledAt: createdAt,
}, nil
}
type websocketMessage struct {
Event string `json:"event"`
Data struct {
Channel string `json:"channel"`
Auth string `json:"auth"`
} `json:"data"`
}