-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathprivate_types.go
353 lines (279 loc) · 7.01 KB
/
private_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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package bitstamp
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"time"
)
var ErrNoSide = errors.New("order side isn't specified")
type BalanceResult map[string]float64
type transactionBody struct {
ID int64 `json:"id"`
OrderID int64 `json:"order_id"`
DateTime string `json:"datetime"`
Type int `json:"type,string"`
Fee float64 `json:"fee,string"`
}
type TransactionResult struct {
transactionBody
Amounts map[string]float64
}
type OpenOrderResult struct {
ID int64 `json:"id,string"`
DateTime string `json:"datetime"`
Type int `json:"type,string"`
Price float64 `json:"price,string"`
Amount float64 `json:"amount,string"`
CurrencyPair string `json:"currency_pair"`
}
func (br *BalanceResult) UnmarshalJSON(data []byte) error {
t := make(map[string]interface{})
*br = make(map[string]float64)
err := json.Unmarshal(data, &t)
if err != nil {
return err
}
for key, value := range t {
key = strings.ToLower(key)
if !strings.HasSuffix(key, "_balance") {
continue
}
key = strings.Replace(key, "_balance", "", 1)
var parsedValue float64
switch pp := value.(type) {
case string:
tmpFloat, err := strconv.ParseFloat(pp, 64)
if err != nil {
return err
}
parsedValue = tmpFloat
case float64:
parsedValue = pp
}
(*br)[key] = parsedValue
}
return nil
}
func (u *TransactionResult) UnmarshalJSON(data []byte) error {
var results transactionBody
var tmp map[string]interface{}
knownFields := map[string]struct{}{
"id": {},
"order_id": {},
"datetime": {},
"type": {},
"fee": {},
}
amounts := make(map[string]float64)
err := json.Unmarshal(data, &results)
if err != nil {
return err
}
(*u).transactionBody = results
err = json.Unmarshal(data, &tmp)
if err != nil {
return err
}
for key, value := range tmp {
if _, ok := knownFields[key]; ok {
continue
}
// TODO: replace with toFloat func
var parsedValue float64
switch vv := value.(type) {
case string:
xx, err := strconv.ParseFloat(vv, 64)
if err != nil {
return err
}
parsedValue = xx
case float64:
parsedValue = vv
case int:
parsedValue = float64(vv)
}
amounts[key] = parsedValue
}
(*u).Amounts = amounts
return nil
}
// CancelAllOrdersResult asd
// TODO: fill Canceled field
type CancelAllOrdersResult struct {
Success bool `json:"success"`
Canceled []interface{} `json:"canceled"`
}
type OrderStatus struct {
Fee float64 `json:"fee"`
Price float64 `json:"price"`
Datetime time.Time `json:"datetime"`
Tid int64 `json:"tid"`
Type int `json:"type"`
Currencies map[string]float64 `json:"currencies"`
}
type OrderStatusResult struct {
Status string `json:"status"`
ID int64 `json:"id"`
AmountRemaining float64 `json:"amount_remaining,string"`
Transactions []OrderStatus `json:"transactions"`
}
type orderStatusResult struct {
Status string `json:"status"`
ID int64 `json:"id"`
AmountRemaining float64 `json:"amount_remaining,string"`
Transactions []map[string]interface{} `json:"transactions"`
}
func interfaceToFloat(data interface{}) (float64, error) {
var parsedValue float64
switch vv := data.(type) {
case string:
xx, err := strconv.ParseFloat(vv, 64)
if err != nil {
return 0, err
}
parsedValue = xx
case float64:
parsedValue = vv
case int:
parsedValue = float64(vv)
default:
return 0, fmt.Errorf("wrong type")
}
return parsedValue, nil
}
// UnmarshalJSON unmarshaller
// {"status": "Finished", "id": 1373320601649153, "amount_remaining": "0.00000000", "transactions": [{"fee": "0.16277", "price": "36171.43000000", "datetime": "2021-06-19 15:58:44.669000", "usd": "32.55428700", "btc": "0.00090000", "tid": 183814449, "type": 2}]}
func (os *OrderStatusResult) UnmarshalJSON(data []byte) error {
var osr orderStatusResult
if err := json.Unmarshal(data, &osr); err != nil {
return err
}
(*os).ID = osr.ID
(*os).Status = osr.Status
(*os).AmountRemaining = osr.AmountRemaining
transactions := make([]OrderStatus, 0)
for _, transaction := range osr.Transactions {
currencies := make(map[string]float64)
var orderStatus OrderStatus
for k, v := range transaction {
// known fields
switch k {
case "fee":
fee, err := interfaceToFloat(v)
if err != nil {
continue
}
orderStatus.Fee = fee
case "price":
price, err := interfaceToFloat(v)
if err != nil {
continue
}
orderStatus.Price = price
case "datetime":
parsedTime, err := time.Parse("2006-01-02 15:04:05.99", v.(string))
if err != nil {
continue
}
orderStatus.Datetime = parsedTime
case "tid":
tid, err := interfaceToFloat(v)
if err != nil {
continue
}
orderStatus.Tid = int64(tid)
case "type":
tp, err := interfaceToFloat(v)
if err != nil {
continue
}
orderStatus.Type = int(tp)
// the rest fields are supposed to be currency assets affected by trade
default:
parsedValue, err := interfaceToFloat(v)
if err != nil {
continue
}
currencies[k] = parsedValue
}
}
orderStatus.Currencies = currencies
transactions = append(transactions, orderStatus)
}
(*os).Transactions = transactions
return nil
}
type OrderCancelResult struct {
ID string `json:"id"`
Amount float64 `json:"amount,string"`
Price float64 `json:"price,string"`
Type int `json:"type,string"`
}
type PlaceOrderResult struct {
ID int64 `json:"id,string"`
DateTime string `json:"datetime"`
Type int `json:"type,string"`
Price float64 `json:"price,string"`
Amount float64 `json:"amount,string"`
}
type OrderType string
const (
Market OrderType = "market"
Limit OrderType = "limit"
)
type OrderSide string
const (
Buy OrderSide = "buy"
Sell OrderSide = "sell"
)
const (
OrderSideBuy = 0
OrderSideSell = 1
ExecDefault = ""
ExecDaily = "daily"
ExecFOK = "fok"
ExecIOC = "ioc"
OrderStatusFinished = "Finished"
OrderStatusOpen = "Open"
OrderStatusCanceled = "Canceled"
TransactionDeposit = 0
TransactionWithdrawal = 1
TransactionTrade = 2
)
type PlaceOrderRequest struct {
Price float64
Amount float64
Symbol string
Side OrderSide
Type OrderType
ExecType string
}
type CommonResult interface {
GetID() int64
GetDateTime() string
GetType() int
GetPrice() float64
GetAmount() float64
}
func (p PlaceOrderResult) GetID() int64 {
return p.ID
}
func (p PlaceOrderResult) GetDateTime() string {
return p.DateTime
}
func (p PlaceOrderResult) GetType() int {
return p.Type
}
func (p PlaceOrderResult) GetPrice() float64 {
return p.Price
}
func (p PlaceOrderResult) GetAmount() float64 {
return p.Amount
}
type GenerateWSTokenResult struct {
Token string `json:"token"`
ValidSec int `json:"valid_sec"` // Validity of token in seconds.
UserID int `json:"user_id"`
}