forked from baibaratsky/go-poloniex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wamp.go
201 lines (159 loc) · 4.68 KB
/
wamp.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package poloniex
import (
"crypto/tls"
"fmt"
"strconv"
"github.com/mitchellh/mapstructure"
"github.com/shopspring/decimal"
"gopkg.in/jcelliott/turnpike.v2"
)
const (
wampEndpoint = "wss://api.poloniex.com"
wampRealm = "realm1"
messageTypeOrderBookRemove = "orderBookRemove"
messageTypeOrderBookModify = "orderBookModify"
messageTypeNewTrade = "newTrade"
OrderUpdateTypeAsk = "ask"
OrderUpdateTypeBid = "bid"
)
type OrderModification struct {
Type string
Sequence uint
Order
}
type NewTrade struct {
Sequence uint
Trade
}
type WampClient struct {
client *turnpike.Client
}
func NewWampClient(tlsConfig *tls.Config, dial turnpike.DialFunc) (*WampClient, error) {
client, err := turnpike.NewWebsocketClient(turnpike.JSON, wampEndpoint, nil, tlsConfig, dial)
if err != nil {
return nil, err
}
_, err = client.JoinRealm(wampRealm, nil)
if err != nil {
return nil, err
}
return &WampClient{client: client}, nil
}
func (wampClient *WampClient) Close() error {
return wampClient.client.Close()
}
func (wampClient *WampClient) UnsubscribeFromPair(pair string) error {
if err := wampClient.client.Unsubscribe(pair); err != nil {
return err
}
return nil
}
func (wampClient *WampClient) SubscribeToPair(pair string, messageChan chan interface{}, errChan chan error) error {
if err := wampClient.client.Subscribe(pair, nil, marketMessageHandler(messageChan, errChan, pair)); err != nil {
return err
}
return nil
}
func marketMessageHandler(messageChan chan interface{}, errChan chan error, pair string) turnpike.EventHandler {
return func(args []interface{}, kwargs map[string]interface{}) {
sequence, err := parseSequence(kwargs)
if err != nil {
errChan <- err
return
}
for _, messageArg := range args {
message := &marketMessage{Sequence: sequence, Pair: pair}
if err := mapstructure.Decode(messageArg, &message); err != nil {
errChan <- err
continue
}
if message.Type == messageTypeNewTrade {
newTrade, err := message.newTrade()
if err != nil {
errChan <- err
continue
}
messageChan <- newTrade
continue
}
orderModification, err := message.orderModification()
if err != nil {
errChan <- err
continue
}
messageChan <- orderModification
}
}
}
type marketMessage struct {
Type string
Sequence uint
Pair string
Data marketMessageData
}
type marketMessageData struct {
TradeId string
Type string
Rate string
Amount string
Total string
Date string
}
func (message marketMessage) orderModification() (orderModification OrderModification, err error) {
if message.Type != messageTypeOrderBookModify && message.Type != messageTypeOrderBookRemove {
return orderModification, fmt.Errorf("can't convert marketMessage with type %s to OrderModification", message.Type)
}
orderModification.Type = message.Data.Type
orderModification.Sequence = message.Sequence
orderModification.Rate, err = decimal.NewFromString(message.Data.Rate)
if err != nil {
return orderModification, fmt.Errorf("marketMessage.orderModification(), rate: %s", err)
}
if message.Data.Amount != "" {
orderModification.Amount, err = decimal.NewFromString(message.Data.Amount)
if err != nil {
return orderModification, fmt.Errorf("marketMessage.orderModification(), amount: %s", err)
}
orderModification.CalculateTotal()
}
return orderModification, nil
}
func (message marketMessage) newTrade() (newTrade NewTrade, err error) {
newTrade.Sequence = message.Sequence
newTrade.Trade = Trade{
Date: message.Data.Date,
Type: message.Data.Type,
CurrencyPair: message.Pair,
}
newTrade.Rate, err = decimal.NewFromString(message.Data.Rate)
if err != nil {
return newTrade, fmt.Errorf("marketMessage.newTrade(), rate: %s", err)
}
newTrade.Amount, err = decimal.NewFromString(message.Data.Amount)
if err != nil {
return newTrade, fmt.Errorf("marketMessage.newTrade(), amount: %s", err)
}
newTrade.Total, err = decimal.NewFromString(message.Data.Total)
if err != nil {
return newTrade, fmt.Errorf("marketMessage.newTrade(), total: %s", err)
}
id, err := strconv.Atoi(message.Data.TradeId)
if err != nil {
return newTrade, fmt.Errorf("marketMessage.newTrade(), id: %s", err)
}
newTrade.Id = convertibleUint(id)
return newTrade, nil
}
func parseSequence(kwargs map[string]interface{}) (uint, error) {
var sequence uint
sequenceArg, ok := kwargs["seq"]
if !ok {
return sequence, fmt.Errorf("key 'seq' was not found in kwargs: %v", kwargs)
}
sequenceFloat, ok := sequenceArg.(float64)
if !ok {
return sequence, fmt.Errorf("sequence value (%#v) type is %T, expected float64", sequenceArg, sequenceArg)
}
sequence = uint(sequenceFloat)
return sequence, nil
}