forked from b2broker/bitstamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket_types.go
68 lines (58 loc) · 1.49 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
package bitstamp
import (
"encoding/json"
"fmt"
"strings"
"time"
)
const (
eventTrade = "trade"
eventSubscription = "bts:subscription_succeede"
)
type bitstampFill struct {
Channel string `json:"channel"`
Data struct {
ID int64 `json:"id"`
Amount float64 `json:"amount"`
Price float64 `json:"price"`
BuyOrderID int64 `json:"buy_order_id"`
SellOrderID int64 `json:"sell_order_id"`
Type int `json:"type"`
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("not compatible event-type")
}
symbol := strings.Replace(fill.Channel, "live_trades_", "", 1)
createdAt := time.Unix(fill.Data.Timestamp/1000000, fill.Data.Timestamp%1000*1000000)
var side OrderSide
if fill.Data.Type == OrderSideBuy {
side = Buy
} else {
side = Sell
}
return Fill{
TradeID: fill.Data.ID,
OrderID: 0,
BuyOrderID: fill.Data.BuyOrderID,
SellOrderID: fill.Data.SellOrderID,
Symbol: symbol,
Price: fill.Data.Price,
Size: fill.Data.Amount,
Side: string(side),
FilledAt: createdAt,
}, nil
}
type websocketMessage struct {
Event string `json:"event"`
Data struct {
Channel string `json:"channel"`
} `json:"data"`
}